Class: Kontena::Plugin::Cloud::Node::ListCommand

Inherits:
Command
  • Object
show all
Includes:
Cli::Common, Cli::TableGenerator::Helper, Common, Organization::Common, Platform::Common
Defined in:
lib/kontena/plugin/cloud/node/list_command.rb

Constant Summary

Constants included from CloudCommand

CloudCommand::PLATFORM_NOT_SELECTED_ERROR

Instance Method Summary collapse

Methods included from Organization::Common

#fetch_organizations, #prompt_organization

Methods included from Platform::Common

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

Methods included from Common

#cached_platforms_by_id, #compute_client, #compute_url, #config, #get_platform

Methods included from CloudCommand

#verify_current_grid, #verify_current_master, #verify_current_master_token

Instance Method Details

#default_organizationObject



50
51
52
53
54
55
56
# File 'lib/kontena/plugin/cloud/node/list_command.rb', line 50

def default_organization
  unless current_master
    exit_with_error "Organization is required"
  end
  org, _ = parse_platform_name(current_master.name)
  org
end

#executeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kontena/plugin/cloud/node/list_command.rb', line 17

def execute
  nodes = compute_client.get("/organizations/#{self.organization}/nodes")['data']
  nodes.delete_if { |n| n.dig('attributes', 'state') == 'terminated' }
  if quiet?
    puts nodes.map { |n| n.dig('attributes', 'name')}
    return
  end
  platforms = fetch_platforms(nodes)
  print_table(nodes) do |n|
    node = Kontena::Cli::Models::Node.new(n)
    n['name'] = "#{state_icon(node.state)} #{node.name}"
    n['type'] = node.type
    n['region'] = node.region
    if platform = platforms[node.platform_id]
      n['platform'] = platform.to_path
    else
      n['platform'] = pastel.red('<orphan>')
    end
  end
end

#fetch_platforms(nodes) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/kontena/plugin/cloud/node/list_command.rb', line 38

def fetch_platforms(nodes)
  platform_ids = nodes.map { |n| Kontena::Cli::Models::Node.new(n).platform_id }.compact.uniq
  platforms = {}
  platform_ids.each { |id|
    platform_data = cloud_client.get("/organizations/#{self.organization}/platforms/#{id}")['data'] rescue nil
    if platform_data
      platforms[id] = Kontena::Cli::Models::Platform.new(platform_data)
    end
  }
  platforms
end

#fieldsObject



58
59
60
61
62
63
64
65
# File 'lib/kontena/plugin/cloud/node/list_command.rb', line 58

def fields
  {
    'name' => 'name',
    'type' => 'type',
    'platform' => 'platform',
    'region' => 'region'
  }
end

#state_icon(state) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/kontena/plugin/cloud/node/list_command.rb', line 67

def state_icon(state)
  case state
  when nil
    " ".freeze
  when 'running'.freeze
    pastel.green(''.freeze)
  else
    pastel.dark(''.freeze)
  end
end