Created
June 20, 2011 04:51
-
-
Save fairchild/1035145 to your computer and use it in GitHub Desktop.
Basic Synaptic compute API class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
# require 'restclient' | |
require 'httparty' | |
require 'yajl' | |
class Synaptic | |
include HTTParty | |
# format :json | |
base_uri 'https://compute.synaptic.att.com/CirrusServices/resources' | |
headers('Accept' => 'application/vnd.com.sun.cloud.Login+json', | |
'Content-Type' => 'application/vnd.com.sun.cloud.Login+json', | |
'X-Cloud-Specification-Version' => '0.1') | |
attr_accessor :user_id, :password, :account_id | |
def initialize(user_id, password) | |
@user_id = user_id | |
@password = password | |
token | |
end | |
def token(u=user_id, pass=password) | |
return @token if @token | |
response = post('/login', 'user_id' => username, 'password' => password) | |
self.headers = headers.merge('authentication' => response['authentication']) | |
@account_id = response['account_uri'].split('/').last if response['account_uri'] | |
@token = response['authentication'] | |
end | |
def locations | |
get('/locations') | |
end | |
def clouds(cloud_id=nil) | |
get("/accounts/#{account_id}/clouds/#{cloud_id}") | |
end | |
def vm_templates(id=nil, opts={}) | |
if opts.empty? | |
get("/vmtemplates/#{id}") | |
elsif id | |
put("/vmtemplates/#{id}", opts) | |
else | |
post("/vmtemplates", opts) | |
end | |
end | |
def run_instance(opts={}) | |
raise "missing required parameter" unless(opts[:account_id] && opts[:cloud_id] && opts[:vdc_id] && opts[:cluster_id]) | |
end_point = "/accounts/#{opts.delete(:account_id)}/clouds/#{opts.delete(:cloud_id)}/vdcs/#{opts.delete(:vdc_id)}/clusters/#{opts.delete(:cluster_id)}/vms" | |
post(end_point, :body=>opts) | |
end | |
def terminate_instance(opts={}) | |
raise "missing required parameter" unless(opts[:account_id] && opts[:cloud_id] && opts[:vdc_id] && opts[:cluster_id] && opts[:vm_id]) | |
delete("/accounts/#{opts[:account_id]}/clouds/#{opts[:cloud_id]}/vdcs/#{opts[:vdc_id]}/clusters/#{opts[:cluster_id]}/vms/#{opts[:vm_id]}") | |
#NOTE: Really, vm_id's should be globally unique, allowing for simpler, delete("/vms/#{opts[:vm_id]}") | |
end | |
def describe_instances | |
raise "missing required parameter" unless(opts[:account_id] && opts[:cloud_id] && opts[:vdc_id] && opts[:cluster_id]) | |
get("/accounts/#{opts[:account_id]}/clouds/#{opts[:cloud_id]}/vdcs/#{opts[:vdc_id]}/clusters/#{opts[:cluster_id]}/vms/") | |
end | |
end | |
res=Synaptic.post("login", :body => { "user_id" =>ENV['SYNAPTIC_USER'], "password" => ENV['SYNAPTIC_PASSWORD'] }.to_json ) | |
caas = Synaptic.new('name', 'secret') | |
puts caas.locations | |
puts caas.clouds | |
puts "==========" | |
machine_image = caas.vm_templates.first | |
puts caas.run_instance(machine_image) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment