Class: Kontena::Plugin::Cloud::Platform::User::AddCommand

Inherits:
Command
  • Object
show all
Includes:
Cli::Common, Common
Defined in:
lib/kontena/plugin/cloud/platform/user/add_command.rb

Constant Summary

Constants included from CloudCommand

CloudCommand::PLATFORM_NOT_SELECTED_ERROR

Instance Method Summary collapse

Methods included from Common

#cached_platforms, #current_organization, #current_platform, #fetch_platforms, #fetch_platforms_for_org, #find_platform_by_name, #login_to_platform, #parse_platform_name, #platform_config_exists?, #prompt_platform, #require_platform

Methods included from CloudCommand

#verify_current_grid, #verify_current_master, #verify_current_master_token

Instance Method Details

#add_users(platform, usernames) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kontena/plugin/cloud/platform/user/add_command.rb', line 37

def add_users(platform, usernames)
  users = []
  usernames.each do |u|
    user = {
      type: 'users',
      id: u
    }
    user[:meta] = { role: role } if role
    users << user
  end
  spinner "Adding users to platform #{pastel.cyan(name)}" do
    data = {data: users}
    cloud_client.post("/organizations/#{current_organization}/platforms/#{platform.id}/relationships/users", data)
  end
end

#executeObject



13
14
15
16
17
18
# File 'lib/kontena/plugin/cloud/platform/user/add_command.rb', line 13

def execute
  require_platform(name)
  platform = find_platform_by_name(current_grid, current_organization)
  self.username_list = prompt_users(platform) if self.username_list.count == 0
  add_users(platform, username_list)
end

#prompt_users(platform) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kontena/plugin/cloud/platform/user/add_command.rb', line 20

def prompt_users(platform)
  organization_members = cloud_client.get("/organizations/#{current_organization}/members")['data']
  platform_members = cloud_client.get("/organizations/#{current_organization}/platforms/#{platform.id}/relationships/users")['data']
  users = organization_members.map do |u|
    username = u.dig('attributes', 'username')
    if !platform_members.any?{|m| m['id'] == username }
      username
    end
  end.compact
  exit_with_error("All organization members are already added to platform") if users.size == 0
  prompt.multi_select("Choose users:") do |menu|
    users.each do |username|
      menu.choice username, username
    end
  end
end