Class: Chef::Knife::SceAddressCreate

Inherits:
Chef::Knife show all
Includes:
SceBase
Defined in:
lib/chef/knife/sce_address_create.rb

Instance Method Summary collapse

Methods included from SceBase

#connection, #connection_storage, #datacenter_id, included, #locate_config_value, #msg_pair, #validate!

Instance Method Details

#runObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
# File 'lib/chef/knife/sce_address_create.rb', line 28

def run
  
  $stdout.sync = true

  validate!
  
  location = nil
  begin
    location = connection.locations.get(@name_args[0])
  rescue
    ui.error "Location #{@name_args[0]} is not a valid location ID."
    exit 1
  end
  
  if location.nil?
    ui.error "Location #{@name_args[0]} is not a valid location ID."
    exit 1
  end
  
  offering_id = nil
  address_offerings = connection.list_address_offerings[:body]["addresses"]
  address_offerings.each do |offer|
    if offer["location"] == location.id
      offering_id = offer["id"]
    end
  end
  
  if offering_id.nil?
    ui.error "Could not fetch offer ID for location #{location.name}"
    exit 1
  end
  
  puts "Creating an IP address at #{location.name} for offer ID #{offering_id}"
  
  response = connection.create_address(@name_args[0], offering_id)
  if response.is_a?(Excon::Response)
    puts "\n"
    puts "New IP address allocated, IP value will be available for you shortly."
    puts "Please use 'knife sce address list' and check the 'State' of allocation '#{response[:body]["id"]}'."
    puts "Your IP is available for use when it is in state 'Free'."
    puts "\n"
  else
    ui.error response[:body].to_s
    exit 1
  end
  
end