Skip to content

Instantly share code, notes, and snippets.

@pstengel
Created April 19, 2011 13:20
Show Gist options
  • Save pstengel/927638 to your computer and use it in GitHub Desktop.
Save pstengel/927638 to your computer and use it in GitHub Desktop.
Adds an IP range to an OnApp VM
#!/usr/bin/env ruby
I_KNOW_I_AM_USING_AN_OLD_AND_BUGGY_VERSION_OF_LIBXML2=true
require 'rubygems'
require 'mechanize'
require 'pp'
if ARGV[0].to_i == 0 || ARGV[1].to_i == 0
puts "Usage: add-iprange.rb <VM ID> <number of IPs>"
exit 1
end
agent = Mechanize.new
agent.get('https://onapp-portal.accountservergroup.com/session/new') do |page|
page.form_with(:action => '/session') do |login|
login['session[login]'] = 'HEARTS'
login['session[password]'] = 'PONIES'
end.submit
checked = false
ARGV[1].to_i.times do |t|
agent.get("https://onapp-portal.accountservergroup.com/virtual_machines/#{ARGV[0]}/ip_addresses/new") do |ippage|
if ippage.body.match(/Resource not found/)
puts "Invalid VM ID"
exit 1
end
ippage.form_with(:action => "/virtual_machines/#{ARGV[0]}/ip_addresses") do |ipadd|
if ipadd.field('ip_address[ip_address_id]').options.count - 1 < ARGV[1].to_i && !checked
puts "Not enough IPs"
exit 1
else
checked = true
end
ipadd.field('ip_address[network_interface_id]').option_with(:text => 'eth0').select
ipadd.field('ip_address[ip_address_id]').option_with(:value => /[0-9]+/).select
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment