Class: Kontena::Plugin::Cloud::Platform::User::RemoveCommand

Inherits:
Command
  • Object
show all
Includes:
Cli::Common, Common
Defined in:
lib/kontena/plugin/cloud/platform/user/remove_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

#executeObject



14
15
16
17
18
19
20
# File 'lib/kontena/plugin/cloud/platform/user/remove_command.rb', line 14

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
  confirm_command(self.username_list.join(',')) unless forced?
  remove_users(platform, username_list)
end

#prompt_users(platform) ⇒ Object



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

def prompt_users(platform)
  platform_members = []
  spinner "Resolving organization #{pastel.cyan(name)} current users" do
     platform_members = cloud_client.get("/organizations/#{current_organization}/platforms/#{platform.id}/relationships/users")['data']
  end
  users = prompt.multi_select("Choose users:") do |menu|
    platform_members.each do |u|
      menu.choice u.dig('attributes', 'username'), u['id']
    end
  end
  if platform_members.size - users.size < 1
    exit_with_error "Cannot remove the last user of the platform"
  end
  users
end

#remove_users(platform, user_ids) ⇒ Object



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

def remove_users(platform, user_ids)
  users = []
  user_ids.each do |u|
    users << {
      type: 'users',
      id: u
    }
  end
  spinner "Removing users from platform #{pastel.cyan(name)}" do
    data = {data: users}
    cloud_client.delete("/organizations/#{current_organization}/platforms/#{platform.id}/relationships/users", data)
  end
end