Class: Chef::Knife::ClcServerShow

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

Instance Method Summary collapse

Methods included from ClcBase

included

Instance Method Details

#executeObject



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

def execute
  ui.info 'Requesting server info...'

  context[:server] = get_server
  context[:creds] = get_creds if config[:clc_creds]
  context[:ip_addresses] = get_ip_addresses if config[:clc_ports]

  render
end

#format_permission(permission) ⇒ Object



154
155
156
157
158
159
160
161
162
163
# File 'lib/chef/knife/clc_server_show.rb', line 154

def format_permission(permission)
  protocol = permission['protocol']

  if %w(tcp udp).include? protocol.downcase
    ports = permission.values_at('port', 'portTo').compact
    [protocol, ports.join('-')].join(':')
  else
    protocol
  end
end

#get_credsObject



61
62
63
64
# File 'lib/chef/knife/clc_server_show.rb', line 61

def get_creds
  creds_link = context[:server]['links'].find { |link| link['rel'] == 'credentials' }
  connection.follow(creds_link) if creds_link
end

#get_ip_addressesObject



66
67
68
# File 'lib/chef/knife/clc_server_show.rb', line 66

def get_ip_addresses
  context[:ip_addresses] = connection.list_ip_addresses(context[:server]['id'])
end

#get_serverObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/chef/knife/clc_server_show.rb', line 47

def get_server
  server = connection.show_server(name_args[0], config[:clc_uuid])

  server['details'] ||= {}
  server['details'].tap do |details|
    details['ipAddresses'] ||= []
    public_ips = server['details']['ipAddresses'].map { |addr| addr['public'] }.compact
    private_ips = server['details']['ipAddresses'].map { |addr| addr['internal'] }.compact
    details.merge!('publicIps' => public_ips, 'privateIps' => private_ips)
  end

  server
end

#ip_fieldsObject



134
135
136
# File 'lib/chef/knife/clc_server_show.rb', line 134

def ip_fields
  %w(id internalIPAddress ports sourceRestrictions)
end

#ip_filtersObject



147
148
149
150
151
152
# File 'lib/chef/knife/clc_server_show.rb', line 147

def ip_filters
  {
    'sourceRestrictions' => ->(sources) { sources.empty? ? '-' : sources.map { |source| source['cidr'] }.join(', ') },
    'ports' => ->(ports) { ports.map { |permission| format_permission(permission) }.join(', ') }
  }
end

#ip_headersObject



138
139
140
141
142
143
144
145
# File 'lib/chef/knife/clc_server_show.rb', line 138

def ip_headers
  {
    'id' => 'Public IP',
    'internalIPAddress' => 'Internal IP',
    'ports' => 'Ports',
    'sourceRestrictions' => 'Sources'
  }
end

#parse_and_validate_parametersObject



31
32
33
34
35
# File 'lib/chef/knife/clc_server_show.rb', line 31

def parse_and_validate_parameters
  unless name_args[0]
    errors << 'Server ID is required'
  end
end

#property_filtersObject



104
105
106
107
108
109
110
111
112
# File 'lib/chef/knife/clc_server_show.rb', line 104

def property_filters
  {
    'description' => ->(description) { description.empty? ? '-' : description },
    'memoryMB' => ->(memory) { "#{memory} MB" },
    'storageGB' => ->(storage) { "#{storage} GB" },
    'publicIps' => ->(ips) { ips.empty? ? '-' : ips.join(', ') },
    'privateIps' => ->(ips) { ips.empty? ? '-' : ips.join(', ') }
  }
end

#property_labelsObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/chef/knife/clc_server_show.rb', line 84

def property_labels
  {
    'id' => 'ID',
    'name' => 'Name',
    'description' => 'Description',
    'groupId' => 'Group',
    'locationId' => 'Location',
    'osType' => 'OS Type',
    'cpu' => 'CPUs',
    'memoryMB' => 'Memory',
    'storageGB' => 'Storage',
    'status' => 'Status',
    'powerState' => 'Power State',
    'publicIps' => 'Public IPs',
    'privateIps' => 'Private IPs',
    'userName' => 'Username',
    'password' => 'Password'
  }
end

#renderObject



70
71
72
73
74
# File 'lib/chef/knife/clc_server_show.rb', line 70

def render
  render_properties
  render_creds if config[:clc_creds]
  render_addresses if config[:clc_ports]
end

#render_addressesObject



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/chef/knife/clc_server_show.rb', line 121

def render_addresses
  if context[:ip_addresses].empty?
    ui.info 'No additional networking info available'
  else
    ui.info Hirb::Helpers::AutoTable.render(context[:ip_addresses],
      :headers => ip_headers,
      :fields => ip_fields,
      :filters => ip_filters,
      :resize => false,
      :description => false)
  end
end

#render_credsObject



165
166
167
# File 'lib/chef/knife/clc_server_show.rb', line 165

def render_creds
  render_fields(fields: %w(userName password), container: context[:creds] || {})
end

#render_fields(fields: [], labels: property_labels, filters: property_filters, container: {}) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/chef/knife/clc_server_show.rb', line 170

def render_fields(fields: [], labels: property_labels, filters: property_filters, container: {})
  fields.each do |field|
    value = container[field]
    filter = filters[field]

    formatted_value = if value
      filter ? filter.call(value) : value
    else
      '-'
    end

    label = labels[field] || field

    ui.info "#{ui.color(label + ':', :bold)} #{formatted_value}"
  end
end

#render_propertiesObject



114
115
116
117
118
119
# File 'lib/chef/knife/clc_server_show.rb', line 114

def render_properties
  server = context[:server]

  render_fields(:fields => server_properties, :container => server)
  render_fields(:fields => server_detail_properties, :container => server['details'])
end

#server_detail_propertiesObject



80
81
82
# File 'lib/chef/knife/clc_server_show.rb', line 80

def server_detail_properties
  %w(powerState cpu memoryMB storageGB publicIps privateIps)
end

#server_propertiesObject



76
77
78
# File 'lib/chef/knife/clc_server_show.rb', line 76

def server_properties
  %w(id name description groupId locationId osType status)
end