class Chef::Knife::SceAddressCreate

Public Instance Methods

run() click to toggle source
# 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