-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRightAPI.rb
executable file
·133 lines (108 loc) · 3.38 KB
/
RightAPI.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/ruby
#
# Copyright 2009 RightScale, Inc.
# http://www.rightscale.com
# <user>
#
# Ruby API Wrapper for RightScale API functions
# Class: RightAPI
#
# Allows easier access to the RightScale API within your own ruby code and
# Has limited debugging & error handling at this point.
#
# Requires rest_client Ruby gem available online.
# 'gem install rest-client'
#
# Example:
# api = RightAPI.new
# api.log = true
# api.login(:username => 'user', :password => 'pass', :account => '1234')
#
# Allows you to send API messages to RightScale in a standard format (see API reference)
# http://support.rightscale.com/15-References/RightScale_API_Reference_Guide
#
# resourceid returns id # of created object
# headers returns hash of returned http headers
# duration returns the time the api call took
#
# api.send(API_STRING,REST_TYPE, PARAMS)
# e.g. API_STRING = "ec2_ssh_keys/1234"
# REST_TYPE = GET | PUT | POST | DELETE
# PARAMS = optional depending on call
#
# api.send("servers") Returns list of your servers
#
# params = { 'deployment[nickname]' => 'my_deployment_name', 'deployment[description]' => 'my_description' }
# api.send("deployments","post",params)
#
require 'rubygems' if VERSION < "1.9.0" # not required if ruby >= 1.9
class RightAPI
require 'rest_client'
@apiobject = Object.new
@apiheader = {}
@resid
@puts_exceptions = true
@reraise_exceptions = false
attr_accessor :api_version, :log, :debug, :api_url, :log_file, :puts_exceptions, :reraise_exceptions
def initialize
@api_version = '1.0' if @api_version.nil? # Change default API version
@log_file_default = "rest.log"
@api_url = "https://my.rightscale.com/api/acct/" if @api_url.nil?
end
def login(opts={})
puts "Debug mode on\n\n" if @debug
@username = opts[:username]
@password = opts[:password]
@account = opts[:account]
@api_call = "#{@api_url}#{@account}"
unless @log.nil?
puts "logging=#{@log}" if @debug
puts "log_file=#{@log_file}" if @debug
@log_file == nil ? RestClient.log = "#{@log_file_default}" : RestClient.log = "#{@log_file}"
end
@apiobject = RestClient::Resource.new("#{@api_call}",@username,@password)
rescue => e
puts "Error: #{e.message}" if @puts_exceptions
raise if @reraise_exceptions
end
def send(apistring,type = "get", params = {})
@responsecode = ""
api_version= { :x_api_version => "#{@api_version}", :api_version => "#{@api_version}" }
raise "No API call given" if apistring.empty?
raise "Invalid Action: get | put | post | delete only" unless type.match(/(get|post|put|delete)/)
@callstart = Time.now
@reply = @apiobject[apistring].send(type.to_sym, api_version.merge(params))
@time = Time.now - @callstart
@apiheader = @reply.headers
@resid = @apiheader[:location].match(/\d+$/) if @apiheader[:status].downcase.match(/201 created/)
# Return results
@reply.body
rescue
@responsecode = $!
puts "Error: #{$!}" if @puts_exceptions
raise if @reraise_exceptions
end
# Returns the resource id of the created object
def resourceid
@resid
end
# Show existing api connection string
def show_connection
puts @apiobject.inspect
end
# Returns length of time api call took
def time
@time.to_f
end
# returns hash of http headers returned
def headers
@apiheader
end
# Return xml of matching names
def search(name="")
self.send("servers?filter=nickname=#{name}")
end
def code
@responsecode
end
end