Class: Chef::Knife::GandiServerList

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/gandi_server_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



46
47
48
# File 'lib/chef/knife/gandi_server_list.rb', line 46

def api_key
  @api_key
end

#connectionObject (readonly)

Returns the value of attribute connection.



46
47
48
# File 'lib/chef/knife/gandi_server_list.rb', line 46

def connection
  @connection
end

Instance Method Details

#runObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/chef/knife/gandi_server_list.rb', line 48

def run
  # Unsual to extend here but enables 'plugin_helper' to be require'd lazily
  extend KnifeGandi::PluginHelper
  $stdout.sync = true
  
  # Necessary changes to xmlrpc's defaults, prevent warnings from showing up in prompt
  suppress_warnings do
    XMLRPC::Config.const_set(:ENABLE_NIL_PARSER, true)
  end
  
  @connection = XMLRPC::Client.new2(KnifeGandi::API_ENDPOINT_URL)
  @api_key    = Chef::Config[:knife][:gandi_api_key] || config[:gandi_api_key] || raise("Please provide an API key")
  
  server_list = [
    ui.color('ID', :bold),
    ui.color('Hostname', :bold),
    ui.color('Date Created', :bold),
    ui.color('Cores', :bold),
    ui.color('Memory', :bold),
    ui.color('Image', :bold),
    ui.color('State', :bold),
    ui.color('Public IPs', :bold)
  ]
  
  connection.call('vm.list', api_key).each do |server|
    # Adds a some overhead but returns much more vm attributes including IPs and Image
    server = connection.call('vm.info', api_key, server['id'])
    server_list << server['id'].to_s
    server_list << server['hostname']
    server_list << server['date_created'].to_date.to_s
    server_list << server['cores'].to_s
    server_list << server['memory'].to_s + 'mb'
    server_list << server['disks'].find{ |disk| disk['is_boot_disk'] }['label']
    server_list << server['state']
    server_list << public_ips_of(server, :version => locate_config_value(:ip_version)).join(', ')
  end

  puts ui.list(server_list, :columns_across, 8)
end