rest_connection 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. data/README.rdoc +1 -1
  2. data/Rakefile +2 -3
  3. data/VERSION +1 -1
  4. data/git_hooks/post-commit +43 -0
  5. data/git_hooks/pre-commit +98 -14
  6. data/lib/rest_connection.rb +150 -22
  7. data/lib/rest_connection/rightscale/account.rb +2 -0
  8. data/lib/rest_connection/rightscale/alert_spec.rb +1 -5
  9. data/lib/rest_connection/rightscale/alert_spec_subject.rb +21 -0
  10. data/lib/rest_connection/rightscale/audit_entry.rb +2 -0
  11. data/lib/rest_connection/rightscale/child_account.rb +2 -0
  12. data/lib/rest_connection/rightscale/cloud.rb +2 -0
  13. data/lib/rest_connection/rightscale/cloud_account.rb +2 -0
  14. data/lib/rest_connection/rightscale/ec2_ebs_volume.rb +5 -1
  15. data/lib/rest_connection/rightscale/ec2_elastic_ip.rb +2 -0
  16. data/lib/rest_connection/rightscale/ec2_security_group.rb +24 -0
  17. data/lib/rest_connection/rightscale/ec2_ssh_key.rb +2 -0
  18. data/lib/rest_connection/rightscale/executable.rb +2 -0
  19. data/lib/rest_connection/rightscale/instance_type.rb +6 -0
  20. data/lib/rest_connection/rightscale/mc_datacenter.rb +2 -0
  21. data/lib/rest_connection/rightscale/mc_image.rb +2 -0
  22. data/lib/rest_connection/rightscale/mc_instance.rb +2 -0
  23. data/lib/rest_connection/rightscale/mc_instance_type.rb +2 -0
  24. data/lib/rest_connection/rightscale/mc_multi_cloud_image.rb +2 -0
  25. data/lib/rest_connection/rightscale/mc_multi_cloud_image_setting.rb +2 -0
  26. data/lib/rest_connection/rightscale/mc_security_group.rb +62 -0
  27. data/lib/rest_connection/rightscale/mc_server_template_multi_cloud_image.rb +2 -0
  28. data/lib/rest_connection/rightscale/mc_ssh_key.rb +2 -0
  29. data/lib/rest_connection/rightscale/mc_tag.rb +2 -0
  30. data/lib/rest_connection/rightscale/mc_volume.rb +2 -0
  31. data/lib/rest_connection/rightscale/mc_volume_attachment.rb +2 -0
  32. data/lib/rest_connection/rightscale/mc_volume_snapshot.rb +2 -0
  33. data/lib/rest_connection/rightscale/mc_volume_type.rb +2 -0
  34. data/lib/rest_connection/rightscale/monitoring_metric.rb +2 -0
  35. data/lib/rest_connection/rightscale/multi_cloud_image.rb +2 -0
  36. data/lib/rest_connection/rightscale/permission.rb +2 -0
  37. data/lib/rest_connection/rightscale/right_script.rb +3 -0
  38. data/lib/rest_connection/rightscale/rightscale_api_base.rb +51 -4
  39. data/lib/rest_connection/rightscale/rightscale_api_gateway.rb +60 -33
  40. data/lib/rest_connection/rightscale/rightscale_api_resources.rb +5 -0
  41. data/lib/rest_connection/rightscale/s3_bucket.rb +2 -1
  42. data/lib/rest_connection/rightscale/security_group_rule.rb +31 -0
  43. data/lib/rest_connection/rightscale/server_ec2_ebs_volume.rb +40 -0
  44. data/lib/rest_connection/rightscale/session.rb +61 -0
  45. data/lib/rest_connection/rightscale/sqs_queue.rb +22 -0
  46. data/lib/rest_connection/rightscale/tag.rb +2 -0
  47. data/lib/rest_connection/rightscale/task.rb +4 -2
  48. data/lib/rest_connection/rightscale/user.rb +2 -0
  49. metadata +12 -6
@@ -24,4 +24,6 @@
24
24
  class Account
25
25
  include RightScale::Api::Gateway
26
26
  extend RightScale::Api::GatewayExtend
27
+
28
+ deny_methods :index, :create, :destroy, :update
27
29
  end
@@ -17,11 +17,7 @@ class AlertSpec
17
17
  include RightScale::Api::Base
18
18
  extend RightScale::Api::BaseExtend
19
19
 
20
-
21
-
22
20
  def attach(params)
23
- connection.post('alert_spec_subjects.js' , :alert_spec_subject => params)
21
+ AlertSpecSubject.create(params)
24
22
  end
25
-
26
-
27
23
  end
@@ -0,0 +1,21 @@
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
+ class AlertSpecSubject
17
+ include RightScale::Api::Base
18
+ extend RightScale::Api::BaseExtend
19
+
20
+ deny_methods :index, :destroy, :update, :show
21
+ end
@@ -28,6 +28,8 @@ class AuditEntry
28
28
  include RightScale::Api::Base
29
29
  extend RightScale::Api::BaseExtend
30
30
 
31
+ deny_methods :index, :create, :destroy, :update
32
+
31
33
  def wait_for_state(state, timeout=900)
32
34
  while(timeout > 0)
33
35
  reload
@@ -25,6 +25,8 @@ class ChildAccount
25
25
  include RightScale::Api::Gateway
26
26
  extend RightScale::Api::GatewayExtend
27
27
 
28
+ deny_methods :destroy, :update, :show
29
+
28
30
  def self.filters
29
31
  [:name]
30
32
  end
@@ -20,6 +20,8 @@ class Cloud
20
20
  include RightScale::Api::Gateway
21
21
  extend RightScale::Api::GatewayExtend
22
22
 
23
+ deny_methods :create, :destroy, :update
24
+
23
25
  def self.filters
24
26
  [:description, :name]
25
27
  end
@@ -20,6 +20,8 @@ class CloudAccount
20
20
  include RightScale::Api::Gateway
21
21
  extend RightScale::Api::GatewayExtend
22
22
 
23
+ deny_methods :update
24
+
23
25
  def cloud_id
24
26
  self.cloud.split("/").last
25
27
  end
@@ -21,6 +21,10 @@ class Ec2EbsVolume
21
21
  extend RightScale::Api::TaggableExtend
22
22
 
23
23
  def attach(params)
24
- connection.post('component_ec2_ebs_volumes.js' , :component_ec2_ebs_volume => params)
24
+ @link = ServerEc2EbsVolume.create(params)
25
+ end
26
+
27
+ def detach
28
+ @link.destroy if @link
25
29
  end
26
30
  end
@@ -17,4 +17,6 @@
17
17
  class Ec2ElasticIp
18
18
  include RightScale::Api::Base
19
19
  extend RightScale::Api::BaseExtend
20
+
21
+ deny_methods :update
20
22
  end
@@ -16,4 +16,28 @@
16
16
  class Ec2SecurityGroup
17
17
  include RightScale::Api::Base
18
18
  extend RightScale::Api::BaseExtend
19
+
20
+ # NOTE - Create, Destroy, and Update require "security_manager" permissions
21
+ # NOTE - Can't remove rules, can only add
22
+ def add_rule(opts={})
23
+ opts.each { |k,v| opts["#{k}".to_sym] = v }
24
+ update_types = [
25
+ :name => [:owner, :group],
26
+ :cidr_ips => [:cidr_ip, :protocol, :from_port, :to_port],
27
+ :group => [:owner, :group, :protocol, :from_port, :to_port],
28
+ ]
29
+ type = (opts[:protocol] ? (opts[:cidr_ip] ? :cidr_ips : :group) : :name)
30
+ unless update_types[type].reduce(true) { |b,field| b && opts[field] }
31
+ arg_expectation = update_types.values.pretty_inspect
32
+ raise ArgumentError.new("add_rule requires one of these groupings: #{arg_expectation}")
33
+ end
34
+
35
+ params = {}
36
+ update_types[type].each { |field| params[field] = opts[field] }
37
+
38
+ uri = URI.parse(self.href)
39
+ connection.put(uri.path, params)
40
+
41
+ self.reload
42
+ end
19
43
  end
@@ -17,6 +17,8 @@ class Ec2SshKey
17
17
  include RightScale::Api::Base
18
18
  extend RightScale::Api::BaseExtend
19
19
 
20
+ deny_methods :index, :update
21
+
20
22
  def self.create(opts)
21
23
  create_opts = { self.resource_singular_name.to_sym => opts }
22
24
  create_opts['cloud_id'] = opts['cloud_id'] if opts['cloud_id']
@@ -2,6 +2,8 @@ class Executable
2
2
  include RightScale::Api::Base
3
3
  extend RightScale::Api::BaseExtend
4
4
 
5
+ deny_methods :index, :create, :destroy, :update
6
+
5
7
  # executable can be EITHER a right_script or recipe
6
8
  # executable example params format:
7
9
  # can have recipes AND right_scripts
@@ -20,10 +20,16 @@ class InstanceType
20
20
  include RightScale::Api::Gateway
21
21
  extend RightScale::Api::GatewayExtend
22
22
 
23
+ deny_methods :create, :destroy, :update
24
+
23
25
  def self.parse_args(cloud_id)
24
26
  "clouds/#{cloud_id}/"
25
27
  end
26
28
 
29
+ def self.filters
30
+ [:cpu_architecture, :description, :name, :resource_uid]
31
+ end
32
+
27
33
  def show
28
34
  inst_href = URI.parse(self.href)
29
35
  @params.merge! connection.get(inst_href.path, 'view' => "default")
@@ -20,6 +20,8 @@ class McDatacenter
20
20
  include RightScale::Api::Gateway
21
21
  extend RightScale::Api::GatewayExtend
22
22
 
23
+ deny_methods :create, :destroy, :update
24
+
23
25
  def resource_plural_name
24
26
  "datacenters"
25
27
  end
@@ -20,6 +20,8 @@ class McImage
20
20
  include RightScale::Api::Gateway
21
21
  extend RightScale::Api::GatewayExtend
22
22
 
23
+ deny_methods :create, :destroy, :update
24
+
23
25
  def resource_plural_name
24
26
  "images"
25
27
  end
@@ -24,6 +24,8 @@ class McInstance
24
24
  include RightScale::Api::McInput
25
25
  attr_accessor :monitoring_metrics
26
26
 
27
+ deny_methods :create, :destroy
28
+
27
29
  def resource_plural_name
28
30
  "instances"
29
31
  end
@@ -20,6 +20,8 @@ class McInstanceType
20
20
  include RightScale::Api::Gateway
21
21
  extend RightScale::Api::GatewayExtend
22
22
 
23
+ deny_methods :create, :destroy, :update
24
+
23
25
  def resource_plural_name
24
26
  "instance_types"
25
27
  end
@@ -23,6 +23,8 @@ class McMultiCloudImage
23
23
  extend RightScale::Api::McTaggableExtend
24
24
  attr_reader :settings
25
25
 
26
+ deny_methods :create, :destroy, :update
27
+
26
28
  def resource_plural_name
27
29
  "multi_cloud_images"
28
30
  end
@@ -20,6 +20,8 @@ class McMultiCloudImageSetting
20
20
  include RightScale::Api::Gateway
21
21
  extend RightScale::Api::GatewayExtend
22
22
 
23
+ deny_methods :create, :destroy, :update
24
+
23
25
  def resource_plural_name
24
26
  "settings"
25
27
  end
@@ -20,6 +20,8 @@ class McSecurityGroup
20
20
  include RightScale::Api::Gateway
21
21
  extend RightScale::Api::GatewayExtend
22
22
 
23
+ deny_methods :update
24
+
23
25
  def resource_plural_name
24
26
  "security_groups"
25
27
  end
@@ -43,4 +45,64 @@ class McSecurityGroup
43
45
  def self.filters
44
46
  [:name, :resource_uid]
45
47
  end
48
+
49
+ # NOTE: Create & Destroy require "security_manager" permissions
50
+ def self.create(cloud_id, opts={})
51
+ url = "#{parse_args(cloud_id)}#{self.resource_plural_name}"
52
+ location = connection.post(url, self.resource_singular_name.to_sym => opts)
53
+ newrecord = self.new('links' => [ {'rel' => 'self', 'href' => location } ])
54
+
55
+ rules = opts[:rules] || opts["rules"]
56
+ [rules].flatten.each { |rule_hash| newrecord.add_rule(rule_hash) } if rules
57
+
58
+ newrecord.reload
59
+ newrecord
60
+ end
61
+
62
+ def rules
63
+ self.load(SecurityGroupRule)
64
+ end
65
+
66
+ def add_rule(opts={})
67
+ opts.each { |k,v| opts["#{k}".to_sym] = v }
68
+ fields = [
69
+ {"1.0" => :owner, "1.5" => :group_owner}, # optional
70
+ {"1.0" => :group, "1.5" => :group_name}, # optional
71
+ {"1.0" => :cidr_ip, "1.5" => :cidr_ips}, # optional
72
+ {"1.0" => :protocol, "1.5" => :protocol}, # "tcp" || "udp" || "icmp"
73
+ {"1.0" => :from_port, "1.5" => :start_port}, # optional
74
+ {"1.0" => :to_port, "1.5" => :end_port}, # optional
75
+ { "1.5" => :source_type}, # "cidr_ips" || "group"
76
+ { "1.5" => :icmp_code}, # optional
77
+ { "1.5" => :icmp_type}, # optional
78
+ { "1.5" => :security_group_href}, # optional
79
+ ]
80
+ unless opts[:protocol]
81
+ raise ArgumentError.new("add_rule requires the 'protocol' option")
82
+ end
83
+ params = {
84
+ :source_type => ((opts[:cidr_ip] || opts[:cidr_ips]) ? "cidr_ips" : "group"),
85
+ :security_group_href => self.href,
86
+ :protocol_details => {}
87
+ }
88
+
89
+ fields.each { |ver|
90
+ next unless val = opts[ver["1.0"]] || opts[ver["1.5"]]
91
+ if ver["1.5"].to_s =~ /port|icmp/
92
+ params[:protocol_details][ver["1.5"]] = val
93
+ else
94
+ params[ver["1.5"]] = val
95
+ end
96
+ }
97
+
98
+ SecurityGroupRule.create(params)
99
+ end
100
+
101
+ def remove_rules_by_filters(filters={})
102
+ rules_to_delete = rules
103
+ filters.each do |filter,regex|
104
+ @rules.reject! { |rule| rule[filter] =~ Regexp.new(regex) }
105
+ end
106
+ @rules.each { |rule| rule.destroy }
107
+ end
46
108
  end
@@ -21,6 +21,8 @@ class McServerTemplateMultiCloudImage
21
21
  include RightScale::Api::Gateway
22
22
  extend RightScale::Api::GatewayExtend
23
23
 
24
+ deny_methods :update
25
+
24
26
  def resource_plural_name
25
27
  "server_template_multi_cloud_images"
26
28
  end
@@ -20,6 +20,8 @@ class McSshKey
20
20
  include RightScale::Api::Gateway
21
21
  extend RightScale::Api::GatewayExtend
22
22
 
23
+ deny_methods :update
24
+
23
25
  def resource_plural_name
24
26
  "ssh_keys"
25
27
  end
@@ -20,6 +20,8 @@ class McTag
20
20
  include RightScale::Api::Gateway
21
21
  extend RightScale::Api::GatewayExtend
22
22
 
23
+ deny_methods :index, :show, :create, :destroy, :update
24
+
23
25
  def resource_plural_name
24
26
  "tags"
25
27
  end
@@ -22,6 +22,8 @@ class McVolume
22
22
  include RightScale::Api::McTaggable
23
23
  extend RightScale::Api::McTaggableExtend
24
24
 
25
+ deny_methods :update
26
+
25
27
  def resource_plural_name
26
28
  "volumes"
27
29
  end
@@ -20,6 +20,8 @@ class McVolumeAttachment
20
20
  include RightScale::Api::Gateway
21
21
  extend RightScale::Api::GatewayExtend
22
22
 
23
+ deny_methods :update
24
+
23
25
  def resource_plural_name
24
26
  "volume_attachments"
25
27
  end
@@ -22,6 +22,8 @@ class McVolumeSnapshot
22
22
  include RightScale::Api::McTaggable
23
23
  extend RightScale::Api::McTaggableExtend
24
24
 
25
+ deny_methods :update
26
+
25
27
  def resource_plural_name
26
28
  "volume_snapshots"
27
29
  end
@@ -20,6 +20,8 @@ class McVolumeType
20
20
  include RightScale::Api::Gateway
21
21
  extend RightScale::Api::GatewayExtend
22
22
 
23
+ deny_methods :create, :destroy, :update
24
+
23
25
  def resource_plural_name
24
26
  "volume_types"
25
27
  end
@@ -20,6 +20,8 @@ class MonitoringMetric
20
20
  include RightScale::Api::Gateway
21
21
  extend RightScale::Api::GatewayExtend
22
22
 
23
+ deny_methods :create, :destroy, :update
24
+
23
25
  def self.parse_args(cloud_id, instance_id)
24
26
  "clouds/#{cloud_id}/instances/#{instance_id}/"
25
27
  end
@@ -17,6 +17,8 @@ class MultiCloudImage
17
17
  include RightScale::Api::Base
18
18
  extend RightScale::Api::BaseExtend
19
19
 
20
+ deny_methods :create, :destroy, :update
21
+
20
22
  def supported_cloud_ids
21
23
  @params["multi_cloud_image_cloud_settings"].map { |mcics| mcics.cloud_id }
22
24
  end
@@ -25,6 +25,8 @@ class Permission
25
25
  include RightScale::Api::Gateway
26
26
  extend RightScale::Api::GatewayExtend
27
27
 
28
+ deny_methods :update
29
+
28
30
  def self.filters
29
31
  [:user_href]
30
32
  end
@@ -17,6 +17,9 @@
17
17
  class RightScript
18
18
  include RightScale::Api::Base
19
19
  extend RightScale::Api::BaseExtend
20
+
21
+ deny_methods :create, :destroy, :update
22
+
20
23
  def self.from_yaml(yaml)
21
24
  scripts = []
22
25
  x = YAML.load(yaml)
@@ -17,6 +17,19 @@ require 'active_support/inflector'
17
17
 
18
18
  module RightScale
19
19
  module Api
20
+ BASE_COOKIE_REFRESH = proc do
21
+ def refresh_cookie
22
+ # login
23
+ @cookie = nil
24
+ resp = get("login")
25
+ unless resp.code == "302" || resp.code == "204"
26
+ raise "ERROR: Login failed. #{resp.message}. Code:#{resp.code}"
27
+ end
28
+ @cookie = resp.response['set-cookie']
29
+ true
30
+ end
31
+ end
32
+
20
33
  module BaseExtend
21
34
  def connection()
22
35
  @@connection ||= RestConnection::Connection.new
@@ -24,6 +37,12 @@ module RightScale
24
37
  settings[:common_headers]["X_API_VERSION"] = "1.0"
25
38
  settings[:api_href] = settings[:api_url]
26
39
  settings[:extension] = ".js"
40
+
41
+ unless @@connection.respond_to?(:refresh_cookie)
42
+ @@connection.instance_exec(&(RightScale::Api::BASE_COOKIE_REFRESH))
43
+ end
44
+
45
+ @@connection.refresh_cookie unless @@connection.cookie
27
46
  @@connection
28
47
  end
29
48
 
@@ -59,7 +78,7 @@ module RightScale
59
78
  end
60
79
 
61
80
  def find_by_nickname(nickname)
62
- connection.logger("DEPRICATION WARNING: use of find_by_nickname is depricated, please use find_by(:nickname) { |n| n == '#{nickname}' } ")
81
+ connection.logger("DEPRECATION WARNING: use of find_by_nickname is deprecated, please use find_by(:nickname) { |n| n == '#{nickname}' } ")
63
82
  self.find_by(:nickname) { |n| n == nickname }
64
83
  end
65
84
 
@@ -89,7 +108,7 @@ module RightScale
89
108
  end
90
109
 
91
110
  def find_by_id(id)
92
- connection.logger("DEPRICATION WARNING: use of find_by_id is depricated, please use find(id) ")
111
+ connection.logger("DEPRECATION WARNING: use of find_by_id is deprecated, please use find(id) ")
93
112
  self.find(id)
94
113
  end
95
114
 
@@ -100,12 +119,12 @@ module RightScale
100
119
  newrecord
101
120
  end
102
121
 
103
- # filter is only implemented on some api endpoints
122
+ # filter is only implemented on some api endpoints
104
123
  def find_by_nickname_speed(nickname)
105
124
  self.find_with_filter('nickname' => nickname)
106
125
  end
107
126
 
108
- # filter is only implemented on some api endpoints
127
+ # filter is only implemented on some api endpoints
109
128
  def find_with_filter(filter = {})
110
129
  filter_params = []
111
130
  filter.each { |key,val|
@@ -160,6 +179,28 @@ module RightScale
160
179
  }
161
180
  return (args.empty? ? find_all : ret.flatten.uniq)
162
181
  end
182
+
183
+ def deny_methods(*symbols)
184
+ symbols.map! { |sym| sym.to_sym }
185
+ if symbols.delete(:index)
186
+ symbols |= [:find_all, :find_by, :find_by_cloud_id, :find_by_nickname, :find_by_nickname_speed, :find_with_filter]
187
+ end
188
+ if symbols.delete(:show)
189
+ symbols |= [:show, :reload, :find, :find_by_id]
190
+ end
191
+ if symbols.delete(:update)
192
+ symbols |= [:save, :update]
193
+ end
194
+ symbols.each do |sym|
195
+ sym = sym.to_sym
196
+ eval_str = "undef #{sym.inspect}"
197
+ if self.respond_to?(sym)
198
+ instance_eval(eval_str)
199
+ elsif self.new.respond_to?(sym)
200
+ class_eval(eval_str)
201
+ end
202
+ end
203
+ end
163
204
  end
164
205
 
165
206
  module Base
@@ -175,6 +216,12 @@ module RightScale
175
216
  settings[:common_headers]["X_API_VERSION"] = "1.0"
176
217
  settings[:api_href] = settings[:api_url]
177
218
  settings[:extension] = ".js"
219
+
220
+ unless @@connection.respond_to?(:refresh_cookie)
221
+ @@connection.instance_exec(&(RightScale::Api::BASE_COOKIE_REFRESH))
222
+ end
223
+
224
+ @@connection.refresh_cookie unless @@connection.cookie
178
225
  @@connection
179
226
  end
180
227