rest_connection 0.0.23 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. data/README.rdoc +3 -3
  2. data/Rakefile +5 -4
  3. data/VERSION +1 -1
  4. data/git_hooks/post-commit.disabled +4 -0
  5. data/git_hooks/post-merge.disabled +4 -0
  6. data/git_hooks/pre-commit +53 -0
  7. data/lib/rest_connection.rb +75 -12
  8. data/lib/rest_connection/patches.rb +106 -0
  9. data/lib/rest_connection/rightscale/account.rb +27 -0
  10. data/lib/rest_connection/rightscale/alert_spec.rb +2 -2
  11. data/lib/rest_connection/rightscale/audit_entry.rb +5 -5
  12. data/lib/rest_connection/rightscale/child_account.rb +27 -0
  13. data/lib/rest_connection/rightscale/cloud.rb +26 -0
  14. data/lib/rest_connection/rightscale/credential.rb +1 -1
  15. data/lib/rest_connection/rightscale/deployment.rb +33 -7
  16. data/lib/rest_connection/rightscale/ec2_ebs_snapshot.rb +3 -1
  17. data/lib/rest_connection/rightscale/ec2_ebs_volume.rb +3 -1
  18. data/lib/rest_connection/rightscale/ec2_elastic_ip.rb +1 -1
  19. data/lib/rest_connection/rightscale/ec2_security_group.rb +2 -2
  20. data/lib/rest_connection/rightscale/ec2_server_array.rb +7 -5
  21. data/lib/rest_connection/rightscale/ec2_ssh_key.rb +11 -2
  22. data/lib/rest_connection/rightscale/ec2_ssh_key_internal.rb +2 -2
  23. data/lib/rest_connection/rightscale/executable.rb +4 -4
  24. data/lib/rest_connection/rightscale/instance.rb +5 -3
  25. data/lib/rest_connection/rightscale/instance_type.rb +31 -0
  26. data/lib/rest_connection/rightscale/macro.rb +19 -0
  27. data/lib/rest_connection/rightscale/mc_datacenter.rb +53 -0
  28. data/lib/rest_connection/rightscale/mc_deployment.rb +60 -0
  29. data/lib/rest_connection/rightscale/mc_image.rb +42 -0
  30. data/lib/rest_connection/rightscale/mc_instance.rb +170 -0
  31. data/lib/rest_connection/rightscale/mc_instance_type.rb +47 -0
  32. data/lib/rest_connection/rightscale/mc_multi_cloud_image.rb +58 -0
  33. data/lib/rest_connection/rightscale/mc_multi_cloud_image_setting.rb +46 -0
  34. data/lib/rest_connection/rightscale/mc_security_group.rb +42 -0
  35. data/lib/rest_connection/rightscale/mc_server.rb +250 -6
  36. data/lib/rest_connection/rightscale/mc_server_array.rb +44 -0
  37. data/lib/rest_connection/rightscale/mc_server_template.rb +53 -0
  38. data/lib/rest_connection/rightscale/mc_ssh_key.rb +50 -0
  39. data/lib/rest_connection/rightscale/mc_tag.rb +69 -0
  40. data/lib/rest_connection/rightscale/mc_volume.rb +53 -0
  41. data/lib/rest_connection/rightscale/mc_volume_attachment.rb +48 -0
  42. data/lib/rest_connection/rightscale/mc_volume_snapshot.rb +55 -0
  43. data/lib/rest_connection/rightscale/mc_volume_type.rb +47 -0
  44. data/lib/rest_connection/rightscale/monitoring_metric.rb +35 -0
  45. data/lib/rest_connection/rightscale/multi_cloud_image.rb +16 -2
  46. data/lib/rest_connection/rightscale/multi_cloud_image_cloud_setting_internal.rb +2 -2
  47. data/lib/rest_connection/rightscale/multi_cloud_image_internal.rb +25 -1
  48. data/lib/rest_connection/rightscale/permission.rb +27 -0
  49. data/lib/rest_connection/rightscale/right_script.rb +4 -4
  50. data/lib/rest_connection/rightscale/right_script_internal.rb +4 -4
  51. data/lib/rest_connection/rightscale/rightscale_api_base.rb +69 -20
  52. data/lib/rest_connection/rightscale/rightscale_api_gateway.rb +198 -13
  53. data/lib/rest_connection/rightscale/rightscale_api_internal.rb +3 -3
  54. data/lib/rest_connection/rightscale/rightscale_api_mc_input.rb +32 -0
  55. data/lib/rest_connection/rightscale/rightscale_api_mc_taggable.rb +57 -0
  56. data/lib/rest_connection/rightscale/rightscale_api_resources.rb +32 -2
  57. data/lib/rest_connection/rightscale/rightscale_api_taggable.rb +116 -0
  58. data/lib/rest_connection/rightscale/rs_internal.rb +6 -6
  59. data/lib/rest_connection/rightscale/s3_bucket.rb +36 -0
  60. data/lib/rest_connection/rightscale/server.rb +186 -37
  61. data/lib/rest_connection/rightscale/server_interface.rb +264 -0
  62. data/lib/rest_connection/rightscale/server_internal.rb +3 -3
  63. data/lib/rest_connection/rightscale/server_template.rb +15 -4
  64. data/lib/rest_connection/rightscale/server_template_internal.rb +4 -4
  65. data/lib/rest_connection/rightscale/status.rb +2 -2
  66. data/lib/rest_connection/rightscale/tag.rb +2 -2
  67. data/lib/rest_connection/rightscale/task.rb +47 -0
  68. data/lib/rest_connection/rightscale/user.rb +27 -0
  69. data/lib/rest_connection/ssh_hax.rb +42 -40
  70. data/rest_connection.gemspec +5 -5
  71. data/spec/mcserver_spec.rb +17 -0
  72. data/spec/multi.rb +16 -0
  73. metadata +65 -12
@@ -1,4 +1,4 @@
1
- # This file is part of RestConnection
1
+ # This file is part of RestConnection
2
2
  #
3
3
  # RestConnection is free software: you can redistribute it and/or modify
4
4
  # it under the terms of the GNU General Public License as published by
@@ -13,7 +13,21 @@
13
13
  # You should have received a copy of the GNU General Public License
14
14
  # along with RestConnection. If not, see <http://www.gnu.org/licenses/>.
15
15
 
16
- class MultiCloudImage
16
+ class MultiCloudImage
17
17
  include RightScale::Api::Base
18
18
  extend RightScale::Api::BaseExtend
19
+
20
+ def supported_cloud_ids
21
+ @params["multi_cloud_image_cloud_settings"].map { |mcics| mcics.cloud_id }
22
+ end
23
+
24
+ # You must have access to multiple APIs for this (0.1, and 1.5)
25
+ def find_and_flatten_settings()
26
+ some_settings = McMultiCloudImage.find(rs_id.to_i).get_settings
27
+ internal = MultiCloudImageInternal.new("href" => self.href)
28
+ internal.reload
29
+ more_settings = internal.settings
30
+ @params["multi_cloud_image_cloud_settings"] = some_settings + more_settings
31
+ end
32
+
19
33
  end
@@ -1,4 +1,4 @@
1
- # This file is part of RestConnection
1
+ # This file is part of RestConnection
2
2
  #
3
3
  # RestConnection is free software: you can redistribute it and/or modify
4
4
  # it under the terms of the GNU General Public License as published by
@@ -40,5 +40,5 @@ class MultiCloudImageCloudSettingInternal
40
40
  location = connection.post(self.resource_plural_name, self.resource_singular_name.to_sym => opts)
41
41
  newrecord = self.new('href' => location)
42
42
  end
43
-
43
+
44
44
  end
@@ -1,4 +1,4 @@
1
- # This file is part of RestConnection
1
+ # This file is part of RestConnection
2
2
  #
3
3
  # RestConnection is free software: you can redistribute it and/or modify
4
4
  # it under the terms of the GNU General Public License as published by
@@ -45,4 +45,28 @@ class MultiCloudImageInternal
45
45
  MultiCloudImage.new(:href => connection.post(t.path + "/clone"))
46
46
  end
47
47
 
48
+ def transform_settings
49
+ if @params["multi_cloud_image_cloud_settings"] && @params["multi_cloud_image_cloud_settings"].first.is_a?(Hash)
50
+ @params["multi_cloud_image_cloud_settings"].map! { |setting|
51
+ # Have to reject because API0.1 returns all clouds
52
+ next if setting["fingerprint"] || setting["cloud_id"] > 10
53
+ MultiCloudImageCloudSettingInternal.new(setting)
54
+ }
55
+ @params["multi_cloud_image_cloud_settings"].compact!
56
+ end
57
+ end
58
+
59
+ def initialize(params={})
60
+ @params = params
61
+ transform_settings
62
+ end
63
+
64
+ def settings
65
+ transform_settings
66
+ @params["multi_cloud_image_cloud_settings"]
67
+ end
68
+
69
+ def supported_cloud_ids
70
+ @params["multi_cloud_image_cloud_settings"].map { |mcics| mcics.cloud_id }
71
+ end
48
72
  end
@@ -0,0 +1,27 @@
1
+ # This file is part of RestConnection
2
+ #
3
+ # RestConnection is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # RestConnection is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with RestConnection. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ #
17
+ # You must have Beta v1.5 API access to use these internal API calls.
18
+ #
19
+
20
+ #
21
+ # Permission Resource requires "admin" role
22
+ #
23
+
24
+ class Permission
25
+ include RightScale::Api::Gateway
26
+ extend RightScale::Api::GatewayExtend
27
+ end
@@ -1,4 +1,4 @@
1
- # This file is part of RestConnection
1
+ # This file is part of RestConnection
2
2
  #
3
3
  # RestConnection is free software: you can redistribute it and/or modify
4
4
  # it under the terms of the GNU General Public License as published by
@@ -14,7 +14,7 @@
14
14
  # along with RestConnection. If not, see <http://www.gnu.org/licenses/>.
15
15
 
16
16
 
17
- class RightScript
17
+ class RightScript
18
18
  include RightScale::Api::Base
19
19
  extend RightScale::Api::BaseExtend
20
20
  def self.from_yaml(yaml)
@@ -23,7 +23,7 @@ class RightScript
23
23
  x.keys.each do |script|
24
24
  scripts << self.new('href' => "right_scripts/#{script}", 'name' => x[script].ivars['name'])
25
25
  end
26
- scripts
26
+ scripts
27
27
  end
28
28
 
29
29
  def self.from_instance_info(file = "/var/spool/ec2/rs_cache/info.yml")
@@ -38,7 +38,7 @@ class RightScript
38
38
  x.keys.each do |script|
39
39
  scripts << self.new('href' => "right_scripts/#{script}", 'name' => x[script].ivars['name'])
40
40
  end
41
- scripts
41
+ scripts
42
42
  end
43
43
 
44
44
  end
@@ -1,4 +1,4 @@
1
- # This file is part of RestConnection
1
+ # This file is part of RestConnection
2
2
  #
3
3
  # RestConnection is free software: you can redistribute it and/or modify
4
4
  # it under the terms of the GNU General Public License as published by
@@ -14,7 +14,7 @@
14
14
  # along with RestConnection. If not, see <http://www.gnu.org/licenses/>.
15
15
 
16
16
 
17
- class RightScriptInternal
17
+ class RightScriptInternal
18
18
  include RightScale::Api::Base
19
19
  extend RightScale::Api::BaseExtend
20
20
  include RightScale::Api::Internal
@@ -41,11 +41,11 @@ class RightScriptInternal
41
41
  t = URI.parse(self.href)
42
42
  RightScript.new(:href => connection.post(t.path + "/commit", :commit_message => message))
43
43
  end
44
-
44
+
45
45
  # clones a RightScript and returns the new RightScript resource that's been created.
46
46
  def clone
47
47
  t = URI.parse(self.href)
48
48
  RightScript.new(:href => connection.post(t.path + "/clone"))
49
49
  end
50
-
50
+
51
51
  end
@@ -1,4 +1,4 @@
1
- # This file is part of RestConnection
1
+ # This file is part of RestConnection
2
2
  #
3
3
  # RestConnection is free software: you can redistribute it and/or modify
4
4
  # it under the terms of the GNU General Public License as published by
@@ -21,7 +21,7 @@ module RightScale
21
21
  def connection()
22
22
  @@connection ||= RestConnection::Connection.new
23
23
  settings = @@connection.settings
24
- settings[:common_headers]["X_API_VERSION"] = "1.0"
24
+ settings[:common_headers]["X_API_VERSION"] = "1.0"
25
25
  settings[:api_href] = settings[:api_url]
26
26
  settings[:extension] = ".js"
27
27
  @@connection
@@ -29,7 +29,7 @@ module RightScale
29
29
 
30
30
  def resource_plural_name
31
31
  self.to_s.underscore.pluralize
32
- end
32
+ end
33
33
 
34
34
  def resource_singular_name
35
35
  self.to_s.underscore
@@ -37,7 +37,7 @@ module RightScale
37
37
  # matches using result of block match expression
38
38
  # ex: Server.find_by(:nickname) { |n| n =~ /production/ }
39
39
  def find_by(attrib, &block)
40
- self.find_all.select do |s|
40
+ self.find_all.select do |s|
41
41
  yield(s[attrib.to_s])
42
42
  end
43
43
  end
@@ -63,8 +63,8 @@ module RightScale
63
63
  self.find_by(:nickname) { |n| n == nickname }
64
64
  end
65
65
 
66
- # the argument can be
67
- # 1) takes href (URI),
66
+ # the argument can be
67
+ # 1) takes href (URI),
68
68
  # 2) or id (Integer)
69
69
  # 3) or symbol :all, :first, :last
70
70
  def find(href, additional_params={}, &block)
@@ -79,7 +79,7 @@ module RightScale
79
79
  return results
80
80
  elsif href == :first
81
81
  return results.first
82
- elsif href == :last
82
+ elsif href == :last
83
83
  return results.last
84
84
  end
85
85
  elsif uri = URI.parse(href)
@@ -107,8 +107,8 @@ module RightScale
107
107
 
108
108
  # filter is only implemented on some api endpoints
109
109
  def find_with_filter(filter = {})
110
- filter_params = []
111
- filter.each { |key,val|
110
+ filter_params = []
111
+ filter.each { |key,val|
112
112
  filter_params << "#{key}=#{val}"
113
113
  }
114
114
  a = Array.new
@@ -117,6 +117,42 @@ module RightScale
117
117
  end
118
118
  return a
119
119
  end
120
+
121
+ def [](*args)
122
+ ret = []
123
+ args.each { |arg|
124
+ temp = []
125
+ begin
126
+ if arg.is_a?(Hash)
127
+ if arg.keys.first.to_s == "cloud_id"
128
+ temp << find_by_cloud_id(arg.values.first.to_i)
129
+ else
130
+ temp << find_with_filter(arg)
131
+ end
132
+ elsif arg.is_a?(Regexp)
133
+ temp << find_by(:nickname) { |n| n =~ arg }
134
+ else
135
+ temp << find(arg)
136
+ end
137
+ rescue
138
+ end
139
+ temp.flatten!
140
+ if temp.empty?
141
+ all = find_all
142
+ if arg.is_a?(Hash)
143
+ temp << all.select { |v| v.__send__(arg.keys.first.to_sym) =~ /#{arg.values.first}/ }
144
+ elsif arg.is_a?(Regexp)
145
+ temp += all.select { |n| n.name =~ arg }
146
+ temp += all.select { |n| n.nickname =~ arg } if temp.empty?
147
+ else
148
+ temp += all.select { |n| n.name =~ /#{arg}/ }
149
+ temp += all.select { |n| n.nickname =~ /#{arg}/ } if temp.empty?
150
+ end
151
+ end
152
+ ret += temp
153
+ }
154
+ return (args.empty? ? find_all : ret.flatten.uniq)
155
+ end
120
156
  end
121
157
 
122
158
  module Base
@@ -129,12 +165,12 @@ module RightScale
129
165
  def connection()
130
166
  @@connection ||= RestConnection::Connection.new
131
167
  settings = @@connection.settings
132
- settings[:common_headers]["X_API_VERSION"] = "1.0"
168
+ settings[:common_headers]["X_API_VERSION"] = "1.0"
133
169
  settings[:api_href] = settings[:api_url]
134
170
  settings[:extension] = ".js"
135
171
  @@connection
136
172
  end
137
-
173
+
138
174
  def resource_plural_name
139
175
  self.class.to_s.underscore.pluralize
140
176
  end
@@ -168,34 +204,47 @@ module RightScale
168
204
  @params[mn] = args[0]
169
205
  @params[mn_dash] = args[0]
170
206
  end
171
- return @params[mn]
207
+ return @params[mn]
172
208
  elsif @params[mn_dash]
173
209
  if assignment
174
- @params[mn_dash] = args[0]
210
+ @params[mn_dash] = args[0]
175
211
  @params[mn] = args[0]
176
212
  end
177
- return @params[mn_dash]
213
+ return @params[mn_dash]
178
214
  elsif @params[mn.to_sym]
179
215
  return @params[mn.to_sym]
180
216
  elsif assignment
181
217
  @params[mn] = args[0]
182
218
  @params[mn_dash] = args[0]
183
- return @params[mn]
184
- else
219
+ return @params[mn]
220
+ else
185
221
  return nil
186
222
  #raise "called unknown method #{method_name} with #{args.inspect}"
187
223
  end
188
224
  end
189
225
 
190
226
  def [](name)
191
- try_these = [name, name.to_s.gsub(/_/,'-'), name.to_sym]
227
+ try_these = [name.to_s, name.to_s.gsub(/_/,'-'), name.to_sym]
228
+ try_these.each do |t|
229
+ if @params[t]
230
+ return @params[t]
231
+ end
232
+ end
233
+ nil
234
+ end
235
+
236
+ def []=(name,val)
237
+ try_these = [name.to_s, name.to_s.gsub(/_/,'-'), name.to_sym]
192
238
  try_these.each do |t|
193
239
  if @params[t]
194
- return @params[name]
195
- else
196
- return nil
240
+ @params[t] = val
197
241
  end
198
242
  end
243
+ val
244
+ end
245
+
246
+ def rs_id
247
+ self.href.split(/\//).last
199
248
  end
200
249
 
201
250
  end
@@ -1,48 +1,233 @@
1
1
  module RightScale
2
2
  module Api
3
3
  module Gateway
4
- def connection
4
+ include RightScale::Api::Base
5
+
6
+ def initialize(params = {})
7
+ @params = parse_params(params)
8
+ end
9
+
10
+ def parse_params(params = {})
11
+ params
12
+ end
13
+
14
+ def connection
5
15
  @@gateway_connection ||= RestConnection::Connection.new
6
16
  settings = @@gateway_connection.settings
7
- settings[:common_headers]["X_API_VERSION"] = "1.5"
17
+ settings[:common_headers]["X_API_VERSION"] = "1.5"
8
18
  settings[:api_href], account = settings[:api_url].split(/\/acct\//) if settings[:api_url].include?("acct")
9
- settings[:extension] = ""
19
+ settings[:extension] = ".json"
10
20
  unless @@gateway_connection.cookie
11
21
  # login
12
22
  params = { "email" => settings[:user], "password" => settings[:pass], "account_href" => "/api/accounts/#{account}" }
13
23
  resp = @@gateway_connection.post("session", params)
14
24
  raise "ERROR: Login failed. #{resp.message}. Code:#{resp.code}" unless resp.code == "302" || resp.code == "204"
15
25
  @@gateway_connection.cookie = resp.response['set-cookie']
16
-
26
+
17
27
  # test session
18
- resp, data = @@gateway_connection.get("session")
19
- raise "ERROR: Invalid session. #{resp.message}. Code:#{resp.code}" unless resp.code == "200"
28
+ resp = @@gateway_connection.get("session")
29
+ raise "ERROR: Invalid session. #{resp["message"]}." unless resp.is_a?(Hash)
20
30
  end
21
31
  @@gateway_connection
22
32
  end
33
+
34
+ def hash_of_links
35
+ ret = {}
36
+ unless @params['links']# and not (@params['nickname'] or @params['name'])
37
+ @params = Kernel.const_get(self.class.to_s).find_by(:name) { |n| n == self.nickname }.first.params
38
+ connection.logger("in hash_of_links: @params = #{@params.inspect}") if ENV['REST_CONNECT_DEBUG']
39
+ end
40
+ @params['links'].each { |link| ret[link['rel']] = link['href'] } if @params['links']
41
+ ret
42
+ end
43
+
44
+ def href
45
+ return @params['href'] if @params['href']
46
+ ret = nil
47
+ unless @params['links']
48
+ raise "Cannot find attribute 'nickname' or 'name' in #{self.inspect}. Aborting." unless self.nickname
49
+ @params = Kernel.const_get(self.class.to_s).find_by(:name) { |n| n == self.nickname }.first.params
50
+ connection.logger("in href: @params = #{@params.inspect}") if ENV['REST_CONNECT_DEBUG']
51
+ end
52
+ @params['links'].each { |link| ret = link['href'] if link['rel'] == 'self' }
53
+ ret
54
+ end
55
+
56
+ def actions
57
+ ret = []
58
+ unless @params['actions']
59
+ raise "Cannot find attribute 'nickname' or 'name' in #{self.inspect}. Aborting." unless self.nickname
60
+ @params = Kernel.const_get(self.class.to_s).find_by(:name) { |n| n == self.nickname }.first.params
61
+ connection.logger("in actions: @params = #{@params.inspect}") if ENV['REST_CONNECT_DEBUG']
62
+ end
63
+ @params['actions'].each { |action| ret << action['rel'] }
64
+ ret
65
+ end
66
+
67
+ def save
68
+ update
69
+ end
70
+
71
+ def method_missing(method_name, *args)
72
+ puts "DEBUG: method_missing in #{self.class.to_s}: #{method_name}" if ENV['REST_CONNECT_DEBUG']
73
+ mn = method_name.to_s
74
+ assignment = mn.gsub!(/=/,"")
75
+ mn_dash = mn.gsub(/_/,"-")
76
+ if self[mn]
77
+ if assignment
78
+ self[mn] = args[0]
79
+ self[mn_dash] = args[0]
80
+ end
81
+ return self[mn]
82
+ elsif self[mn_dash]
83
+ if assignment
84
+ self[mn_dash] = args[0]
85
+ self[mn] = args[0]
86
+ end
87
+ return self[mn_dash]
88
+ elsif self[mn.to_sym]
89
+ return self[mn.to_sym]
90
+ elsif assignment
91
+ self[mn] = args[0]
92
+ self[mn_dash] = args[0]
93
+ return self[mn]
94
+ else
95
+ return nil
96
+ #raise "called unknown method #{method_name} with #{args.inspect}"
97
+ end
98
+ end
99
+
100
+ def [](name)
101
+ try_these = [name.to_s, name.to_s.gsub(/_/,'-'), name.to_sym]
102
+ if try_these.include?(:nickname)
103
+ try_these += ["name", :name]
104
+ end
105
+ try_these.each do |t|
106
+ if @params[t]
107
+ return @params[t]
108
+ elsif hash_of_links[t]
109
+ return hash_of_links[t]
110
+ end
111
+ end
112
+ return nil
113
+ end
114
+
115
+ def []=(name,val)
116
+ try_these = [name.to_s, name.to_s.gsub(/_/,'-'), name.to_sym]
117
+ if try_these.include?(:nickname)
118
+ try_these += ["name", :name]
119
+ end
120
+ try_these.each do |t|
121
+ if @params[t]
122
+ @params[t] = val
123
+ elsif hash_of_links[t]
124
+ @params['links'].each { |link|
125
+ link['href'] = val if link['rel'] == t
126
+ }
127
+ end
128
+ end
129
+ val
130
+ end
131
+
132
+ def load(resource)
133
+ if resource.is_a?(Class)
134
+ param_string = resource.resource_singular_name
135
+ class_name = resource
136
+ elsif resource.is_a?(String) or resource.is_a?(Symbol)
137
+ param_string = resource
138
+ begin
139
+ class_name = Kernel.const_get(resource.singularize.camelize)
140
+ rescue
141
+ class_name = Kernel.const_get("Mc#{resource.singularize.camelize}")
142
+ end
143
+ end
144
+ if self[param_string].nil?
145
+ return class_name.load_all(self[param_string.pluralize])
146
+ elsif param_string.pluralize == param_string
147
+ return class_name.load_all(self[param_string])
148
+ else
149
+ return class_name.load(self[param_string])
150
+ end
151
+ end
23
152
  end
24
153
 
25
154
  module GatewayExtend
26
- def connection
155
+ include RightScale::Api::BaseExtend
156
+ def connection
27
157
  @@gateway_connection ||= RestConnection::Connection.new
28
158
  settings = @@gateway_connection.settings
29
- settings[:common_headers]["X_API_VERSION"] = "1.5"
159
+ settings[:common_headers]["X_API_VERSION"] = "1.5"
30
160
  settings[:api_href], account = settings[:api_url].split(/\/acct\//) if settings[:api_url].include?("acct")
31
- settings[:extension] = ""
161
+ settings[:extension] = ".json"
32
162
  unless @@gateway_connection.cookie
33
163
  # login
34
164
  params = { "email" => settings[:user], "password" => settings[:pass], "account_href" => "/api/accounts/#{account}" }
35
165
  resp = @@gateway_connection.post("session", params)
36
166
  raise "ERROR: Login failed. #{resp.message}. Code:#{resp.code}" unless resp.code == "302" || resp.code == "204"
37
167
  @@gateway_connection.cookie = resp.response['set-cookie']
38
-
168
+
39
169
  # test session
40
- resp, data = @@gateway_connection.get("session")
41
- raise "ERROR: Invalid session. #{resp.message}. Code:#{resp.code}" unless resp.code == "200"
170
+ resp = @@gateway_connection.get("session")
171
+ raise "ERROR: Invalid session. #{resp["message"]}." unless resp.is_a?(Hash)
42
172
  end
43
173
  @@gateway_connection
44
174
  end
175
+
176
+ def find_by(attrib, *args, &block)
177
+ attrib = :name if attrib == :nickname
178
+ self.find_all(*args).select do |s|
179
+ yield(s[attrib.to_s])
180
+ end
181
+ end
182
+
183
+ def find_all(*args)
184
+ # self.find_with_filter(*args, {})
185
+ a = Array.new
186
+ url = "#{parse_args(*args)}#{self.resource_plural_name}"
187
+ connection.get(url).each do |object|
188
+ a << self.new(object)
189
+ end
190
+ return a
191
+ end
192
+
193
+ def find_with_filter(*args)
194
+ filter_params = []
195
+ filter = {}
196
+ filter = args.pop if args.last.is_a?(Hash)
197
+ filter.each { |key,val|
198
+ filter_params << "#{key}==#{val}"
199
+ }
200
+ a = Array.new
201
+ url = "#{parse_args(*args)}#{self.resource_plural_name}"
202
+ connection.get(url, :filter => filter_params).each do |object|
203
+ a << self.new(object)
204
+ end
205
+ return a
206
+ end
207
+
208
+ def load(url)
209
+ return self.new(connection.get(url))
210
+ end
211
+
212
+ def load_all(url)
213
+ a = Array.new
214
+ connection.get(url).each do |object|
215
+ a << self.new(object)
216
+ end
217
+ return a
218
+ end
219
+
220
+ def parse_args()
221
+ nil
222
+ end
223
+
224
+ def create(opts)
225
+ location = connection.post(self.resource_plural_name, self.resource_singular_name.to_sym => opts)
226
+ newrecord = self.new('links' => [ {'rel' => 'self', 'href' => location } ])
227
+ newrecord.reload
228
+ newrecord
229
+ end
45
230
  end
46
231
  end
47
232
  end
48
-
233
+