Class: Chef::Knife::BaremetalcloudListServers

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/baremetalcloud_list_servers.rb

Instance Method Summary collapse

Instance Method Details

#errorHandling(response) ⇒ Object

Method to handle baremetalcloud errors



25
26
27
28
29
30
31
32
# File 'lib/chef/knife/baremetalcloud_list_servers.rb', line 25

def errorHandling(response)
  # Error handling
  unless response[:error].nil?
    puts "#{ui.color("ERROR:", :bold)} #{response[:error][:name]}"
    puts "Description: #{response[:error][:description]}"
    exit 1 
  end
end

#locateConfigValue(key) ⇒ Object



19
20
21
22
# File 'lib/chef/knife/baremetalcloud_list_servers.rb', line 19

def locateConfigValue(key)
  key = key.to_sym
  config[key] || Chef::Config[:knife][key]
end

#runObject

Plugin method called by Knife



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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/chef/knife/baremetalcloud_list_servers.rb', line 51

def run
  
  # Verify mandatory arguments
  verifyArguments
  
  # Configure the API abstraction @bmc
  @bmc = Fog::Compute.new({
    :bare_metal_cloud_username => locateConfigValue(:baremetalcloud_username),
    :bare_metal_cloud_password => locateConfigValue(:baremetalcloud_password),
    :provider => 'BareMetalCloud'
  })
  
  # Get the API response
  response = @bmc.list_servers.body
  
  # Error handling
  errorHandling(response)
  
  # only one server
  if ( response[:server].class == Hash ) 
    puts "#{response[:server][:id]}\t#{response[:server][:state]}\t#{response[:server][:name]}\t#{response[:server][:location]}\t#{response[:server][:ip][:address]}\t#{response[:server][:login][:username]}\t#{response[:server][:login][:password]}"
  
  # more than one server
  elsif ( response[:server].class == Array ) 
    response[:server].each do |resp|
      
      # 1855 and 1955 have only "system" credentials
      if ( resp[:login].class == Hash )
        puts "#{resp[:id]}\t#{resp[:state]}\t#{resp[:name]}\t#{resp[:location]}\t#{resp[:ip][:address]}\t#{resp[:login][:username]}\t#{resp[:login][:password]}"
     
      # M600 and M610 have iDRAC credentials
      else
        # Username and password variables
        username = password = nil
        
        # Get only system login. Skip iDRAC
        resp[:login].each do |r|
          if ( r[:name] == "system")
              username = r[:username]
              password = r[:password]
          end
        end
        puts "#{resp[:id]}\t#{resp[:state]}\t#{resp[:name]}\t#{resp[:location]}\t#{resp[:ip][:address]}\t#{username}\t#{password}"
        
      end
      
    end
    
  end
  
end

#verifyArgumentsObject

Method to verify mandatory arguments



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chef/knife/baremetalcloud_list_servers.rb', line 36

def verifyArguments
  # Parameters :baremetalcloud_username and :baremetalcloud_password are mandatory
  unless config[:baremetalcloud_username]
    ui.error("--username is a mandatory parameter")
    exit 1
  end
  
  unless config[:baremetalcloud_password]
    ui.error("--password is a mandatory parameter")
    exit 1
  end

end