Class: Etna::Clients::Magma::CreateProjectWorkflow

Inherits:
Struct
  • Object
show all
Defined in:
lib/etna/clients/magma/workflows/create_project_workflow.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#janus_clientObject

Returns the value of attribute janus_client

Returns:

  • (Object)

    the current value of janus_client



18
19
20
# File 'lib/etna/clients/magma/workflows/create_project_workflow.rb', line 18

def janus_client
  @janus_client
end

#magma_clientObject

Returns the value of attribute magma_client

Returns:

  • (Object)

    the current value of magma_client



18
19
20
# File 'lib/etna/clients/magma/workflows/create_project_workflow.rb', line 18

def magma_client
  @magma_client
end

#project_nameObject

Returns the value of attribute project_name

Returns:

  • (Object)

    the current value of project_name



18
19
20
# File 'lib/etna/clients/magma/workflows/create_project_workflow.rb', line 18

def project_name
  @project_name
end

#project_name_fullObject

Returns the value of attribute project_name_full

Returns:

  • (Object)

    the current value of project_name_full



18
19
20
# File 'lib/etna/clients/magma/workflows/create_project_workflow.rb', line 18

def project_name_full
  @project_name_full
end

Instance Method Details

#add_janus_user(email, name, role) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/etna/clients/magma/workflows/create_project_workflow.rb', line 26

def add_janus_user(email, name, role)
  janus_client.add_user(Etna::Clients::Janus::AddUserRequest.new(
    project_name: project_name,
    email: email,
    name: name,
    role: role,
  ))
end

#create!Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/etna/clients/magma/workflows/create_project_workflow.rb', line 87

def create!
  setup_janus_project!
  setup_magma_project!

  while true
    puts "Add more users? Y/n"
    break unless STDIN.gets.chomp == 'Y'
    puts "User name?"
    name = STDIN.gets.chomp
    puts "Email?"
    email = STDIN.gets.chomp
    puts "Role? (editor/viewer/administrator)"
    role = STDIN.gets.chomp
    puts "Adding #{name} (#{email}) as a #{role}."
    puts "Confirm? Y/n"
    break unless STDIN.gets.chomp == 'Y'

    if role == 'administrator'
      add_janus_user(email, name, 'editor')
      promote_to_administrator(email)
    else
      add_janus_user(email, name, role)
    end
  end

  puts "All complete!"
  puts "You need to visit Janus to refresh your token."
  puts "You can now log into any app to manage your data."
end

#create_janus_project!Object



19
20
21
22
23
24
# File 'lib/etna/clients/magma/workflows/create_project_workflow.rb', line 19

def create_janus_project!
  janus_client.add_project(Etna::Clients::Janus::AddProjectRequest.new(
    project_name: project_name,
    project_name_full: project_name_full
  ))
end

#create_magma_project!Object



55
56
57
58
59
# File 'lib/etna/clients/magma/workflows/create_project_workflow.rb', line 55

def create_magma_project!
  magma_client.update_model(Etna::Clients::Magma::UpdateModelRequest.new(
    project_name: project_name,
    actions: [Etna::Clients::Magma::AddProjectAction.new]))
end

#create_magma_project_record!Object



61
62
63
64
65
66
67
# File 'lib/etna/clients/magma/workflows/create_project_workflow.rb', line 61

def create_magma_project_record!
  magma_client.update_json(Etna::Clients::Magma::UpdateRequest.new(
    project_name: project_name,
    revisions: {
        'project' => { project_name => { name: project_name } },
    }))
end

#promote_to_administrator(email) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/etna/clients/magma/workflows/create_project_workflow.rb', line 35

def promote_to_administrator(email)
  janus_client.update_permission(Etna::Clients::Janus::UpdatePermissionRequest.new(
    project_name: project_name,
    # email: user['email'],
    email: email,
    role: 'administrator',
  ))
end

#refreshed_tokenObject



44
45
46
# File 'lib/etna/clients/magma/workflows/create_project_workflow.rb', line 44

def refreshed_token
  janus_client.refresh_token(Etna::Clients::Janus::RefreshTokenRequest.new).token
end

#setup_janus_project!Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/etna/clients/magma/workflows/create_project_workflow.rb', line 69

def setup_janus_project!
  puts "Creating Janus project."
  create_janus_project!
  puts "Done! Adding you as an administrator on the project."
  add_janus_user(user['email'], "#{user['name']}", 'editor')
  promote_to_administrator(user['email'])
  update_magma_client_token!

  puts "Done with setting up the project in Janus!"
end

#setup_magma_project!Object



80
81
82
83
84
85
# File 'lib/etna/clients/magma/workflows/create_project_workflow.rb', line 80

def setup_magma_project!
  puts "Creating the project in Magma."
  create_magma_project!
  puts "Done! Adding your new project record."
  create_magma_project_record!
end

#update_magma_client_token!Object



48
49
50
51
52
53
# File 'lib/etna/clients/magma/workflows/create_project_workflow.rb', line 48

def update_magma_client_token!
  self.magma_client = Etna::Clients::Magma.new(
    host: self.magma_client.host,
    token: refreshed_token,
    ignore_ssl: self.magma_client.ignore_ssl)
end

#userObject



117
118
119
# File 'lib/etna/clients/magma/workflows/create_project_workflow.rb', line 117

def user
  @user ||= JSON.parse(Base64.urlsafe_decode64(magma_client.token.split('.')[1]))
end