Skip to content

Instantly share code, notes, and snippets.

@khash
Last active December 30, 2015 01:49
Show Gist options
  • Save khash/7758645 to your computer and use it in GitHub Desktop.
Save khash/7758645 to your computer and use it in GitHub Desktop.
A sample ruby script to force the IP address of a server within the stack
require 'rubygems'
require 'oauth2'
require 'json'
base = 'https://www.cloud66.com'
api_url = 'https://www.cloud66.com/api/2'
if File.exists? '/tmp/cloud66_oauth_test.json'
config = JSON.parse(File.read('/tmp/cloud66_oauth_test.json'))
client = OAuth2::Client.new(config['app_id'], config['app_secret'], :site => base)
token = OAuth2::AccessToken.new(client, config['token'])
else
client = OAuth2::Client.new(ENV['APP_ID'], ENV['APP_SECRET'], :site => base)
puts client.auth_code.authorize_url(:redirect_uri => 'urn:ietf:wg:oauth:2.0:oob', :scope => 'public admin redeploy')
puts "Enter the code:"
code = gets.strip
token = client.auth_code.get_token(code, :redirect_uri => "urn:ietf:wg:oauth:2.0:oob")
# save it
File.write('/tmp/cloud66_oauth_test.json', { :app_id => ENV['APP_ID'], :app_secret => ENV['APP_SECRET'], :token => token.token }.to_json)
end
# Now you can use the toekn to call API methods, like:
# List all the stacks
response = token.get("#{api_url}/stacks.json")
# list all the servers in the stack
stack_uid = 'ENTER_STACK_UID'
response = token.get("#{api_url}/stacks/#{stack_uid}/servers.json")
server_uid = 'ENTER_SERVER_UID'
# or force an IP change notification for the server
response = token.post("#{api_url}/servers/#{server_uid}/force_ip.json", :body => { :ext_ipv4 => 'NEW_IP_ADDRESS_HERE' })
# show the response (no error handling)
puts JSON.parse(response.body)['response']
@ollym
Copy link

ollym commented Dec 2, 2013

Many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment