Class: Chef::Knife::ClcServerList

Inherits:
Chef::Knife show all
Includes:
ClcBase
Defined in:
lib/chef/knife/clc_server_list.rb

Instance Method Summary collapse

Methods included from ClcBase

included

Instance Method Details

#cloud_serversObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/chef/knife/clc_server_list.rb', line 56

def cloud_servers
  if config[:clc_datacenter]
    connection.list_servers(config[:clc_datacenter])
  elsif config[:clc_all]
    datacenters = connection.list_datacenters

    datacenters.map do |dc|
      connection.list_servers(dc['id'])
    end.flatten
  end
end

#executeObject



29
30
31
32
33
34
35
36
37
# File 'lib/chef/knife/clc_server_list.rb', line 29

def execute
  servers = cloud_servers
  merge_public_ips!(servers)
  merge_chef_nodes!(servers) if config[:clc_chef_nodes]

  context[:servers] = servers

  render
end

#fieldsObject



81
82
83
84
85
86
# File 'lib/chef/knife/clc_server_list.rb', line 81

def fields
  # TODO AS: Displaying shortened list of fields for now
  # default_fields = %w(id name publicIP groupId locationId osType status)
  default_fields = %w(name publicIP status)
  config[:clc_chef_nodes] ? default_fields.insert(3, 'chefNode') : default_fields
end

#filtersObject



88
89
90
91
92
93
# File 'lib/chef/knife/clc_server_list.rb', line 88

def filters
  {
    'publicIP' => ->(ip) { ip || '-' },
    'chefNode' => ->(name) { name || '-' }
  }
end

#headersObject



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/chef/knife/clc_server_list.rb', line 68

def headers
  {
    'id' => 'ID',
    'name' => 'Name',
    'publicIP' => 'Public IP',
    'chefNode' => 'Chef Node',
    'groupId' => 'Group',
    'osType' => 'OS Type',
    'status' => 'Status',
    'locationId' => 'DC'
  }
end

#merge_chef_nodes!(servers) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/chef/knife/clc_server_list.rb', line 47

def merge_chef_nodes!(servers)
  nodes = Chef::Node.list(true).values
  servers.map! do |server|
    existing_node = nodes.find { |node| node['machinename'] == server['name'] }
    server['chefNode'] = existing_node.name if existing_node
    server
  end
end

#merge_public_ips!(servers) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/chef/knife/clc_server_list.rb', line 39

def merge_public_ips!(servers)
  servers.map! do |server|
    ip_link = server['links'].find { |link| link['rel'] == 'publicIPAddress' }
    server['publicIP'] = ip_link['id'] if ip_link
    server
  end
end

#parse_and_validate_parametersObject



112
113
114
115
116
# File 'lib/chef/knife/clc_server_list.rb', line 112

def parse_and_validate_parameters
  if config[:clc_datacenter].nil? && !config[:clc_all]
    errors << 'Datacenter ID is required'
  end
end

#renderObject



102
103
104
105
106
107
108
109
110
# File 'lib/chef/knife/clc_server_list.rb', line 102

def render
  ui.info Hirb::Helpers::AutoTable.render(context[:servers],
    :headers => headers,
    :fields => fields,
    :filters => filters,
    :max_fields => width_limits,
    :resize => false,
    :description => false)
end

#width_limitsObject



95
96
97
98
99
100
# File 'lib/chef/knife/clc_server_list.rb', line 95

def width_limits
  {
    'chefNode' => 21,
    'status' => 15
  }
end