Class: Chef::Knife::BaremetalcloudListImages

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/baremetalcloud_list_images.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_images.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_images.rb', line 19

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

#runObject

Plugin method called by Knife



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
# File 'lib/chef/knife/baremetalcloud_list_images.rb', line 50

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_images.body
  
  # Error handling
  errorHandling(response)
  
  if response[:image].length > 1
    response[:image].each do |resp|
      puts "#{resp[:size]}\t#{resp[:name]}"
    end
  end
  
end

#verifyArgumentsObject

Method to verify mandatory arguments



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

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