rightresource 0.3.2 → 0.3.3

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.
@@ -87,6 +87,7 @@ module RightResource
87
87
 
88
88
  # RestFul Method
89
89
  def action(method, path, params={})
90
+ connection.clear
90
91
  case method
91
92
  when :get
92
93
  connection.get(path, params)
@@ -96,6 +97,8 @@ module RightResource
96
97
  connection.put(path, params)
97
98
  when :delete
98
99
  connection.delete(path, params)
100
+ else
101
+ raise ArgumentError, "Unsupported HTTP method!!"
99
102
  end
100
103
  rescue => e
101
104
  logger.error("#{e.class}: #{e.pretty_inspect}")
@@ -138,7 +141,6 @@ module RightResource
138
141
  connection.clear
139
142
  result = format.decode(connection.get(path || [])).map do |resource|
140
143
  correct_attributes(resource)
141
- resource
142
144
  end
143
145
  instantiate_collection(result)
144
146
  rescue => e
@@ -166,8 +168,6 @@ module RightResource
166
168
  connection.clear
167
169
  result = format.decode(connection.get(path)).tap do |resource|
168
170
  correct_attributes(resource)
169
- resource[:id] = resource[:href].match(/[0-9]+$/).to_s.to_i
170
- resource
171
171
  end
172
172
  instantiate_record(result)
173
173
  rescue => e
@@ -210,11 +210,13 @@ module RightResource
210
210
  end
211
211
 
212
212
  # Update resource
213
- # def update(id, params={})
214
- # #TODO: refactor
215
- # path = element_path(id)
216
- # connection.put(path, params)
217
- # end
213
+ def update(id, params={})
214
+ resource = self.show(id)
215
+ return nil if resource.nil?
216
+
217
+ resource.update_attributes(params)
218
+ resource.save
219
+ end
218
220
 
219
221
  # Delete resource
220
222
  # Example:
@@ -224,6 +226,7 @@ module RightResource
224
226
  # Server.destory(server.id)
225
227
  def destory(id)
226
228
  path = element_path(id)
229
+ connection.clear
227
230
  connection.delete(path)
228
231
  end
229
232
 
@@ -254,27 +257,28 @@ module RightResource
254
257
  # '-'(dash) is included but not '_'(under score) in AWS Parameter keys.
255
258
  # On the other hand '_'(under score) is included in RightScale Parameter keys
256
259
  # e.g. Hash["ip-address"] -> Hash[:ip_address], Hash["deployment_href"] -> Hash[:deployment_href]
257
- def correct_attributes(attributes)
258
- return unless attributes.is_a?(Hash)
260
+ def correct_attributes(attrs)
261
+ return unless attrs.is_a?(Hash)
259
262
 
260
- attributes.alter_keys
261
- attributes.each do |key,value| # recursive
262
- attributes[key] =
263
+ attrs.alter_keys
264
+ attrs.each do |key,value| # recursive
265
+ attrs[key] =
263
266
  case value
264
267
  when Array
265
- value.map do |attrs|
266
- if attrs.is_a?(Hash)
267
- correct_attributes(attrs)
268
+ value.map do |a|
269
+ if a.is_a?(Hash)
270
+ a.dup.alter_keys rescue a.alter_keys
268
271
  else
269
- attrs.dup rescue attrs
272
+ a.dup rescue a
270
273
  end
271
274
  end
272
275
  when Hash
273
- correct_attributes(value)
276
+ value.dup.alter_keys rescue value.alter_keys
274
277
  else
275
278
  value.dup rescue value
276
279
  end
277
280
  end
281
+ attrs
278
282
  end
279
283
 
280
284
  protected
@@ -330,52 +334,68 @@ module RightResource
330
334
  # self.class.known_attributes + self.attributes.keys.map(&:to_s)
331
335
  end
332
336
 
333
- # Duplicate resource
337
+ # Duplicate resource without save
334
338
  def dup
335
339
  self.class.new.tap do |resource|
336
340
  resource.attributes = @attributes.reject {|key,value| key == :href}
337
- resource.load_accessor(resource.attributes)
338
341
  end
339
342
  end
340
343
 
341
- def initialize(attributes={})
342
- # sub-resource4json's key name contains '-'
343
- @attributes = {}
344
- load_attributes(attributes)
345
- if @attributes
346
- if self.class.resource_id && self.class.status_code == 201
347
- @id = self.class.resource_id
348
- else
349
- @id = @attributes[:href].match(/\d+$/).to_s if @attributes[:href]
350
- end
351
- load_accessor(@attributes)
344
+ # Duplicate resource with save
345
+ def clone
346
+ resource = dup
347
+ resource.save
348
+ end
349
+
350
+ def initialize(attrs={})
351
+ if self.class.resource_id && self.class.status_code == 201
352
+ @id = self.class.resource_id
353
+ else
354
+ @id = attrs[:href].match(/\d+$/).to_s if attrs[:href]
352
355
  end
356
+
357
+ @attributes = {}
358
+ # sub-resource4json's key name contains '-'
359
+ loads(attrs)
353
360
  yield self if block_given?
354
361
  end
355
362
 
356
- def load_attributes(attributes)
357
- raise ArgumentError, "expected an attributes Hash, got #{attributes.pretty_inspect}" unless attributes.is_a?(Hash)
358
- @attributes = self.class.correct_attributes(attributes)
363
+ def loads(attrs)
364
+ raise ArgumentError, "expected an attributes Hash, got #{attrs.pretty_inspect}" unless attrs.is_a?(Hash)
365
+
366
+ attrs.each do |key,value| # recursive
367
+ @attributes[key] =
368
+ case value
369
+ when Array
370
+ value.map do |a|
371
+ if a.is_a?(Hash)
372
+ a.dup.alter_keys rescue a.alter_keys
373
+ else
374
+ a.dup rescue a
375
+ end
376
+ end
377
+ when Hash
378
+ value.dup.alter_keys rescue value.alter_keys
379
+ else
380
+ value.dup rescue value
381
+ end
382
+ end
359
383
  self
360
384
  end
361
385
 
362
- def update_attributes(attributes)
363
- load_attributes(attributes) && load_accessor(attributes) && save
386
+ # reload attributes of resource from the rightscale web api(discard local modified)
387
+ def reload
388
+ self.loads(self.class.show(self.id).attributes)
364
389
  end
365
390
 
366
- def load_accessor(attributes)
367
- raise ArgumentError, "expected an attributes Hash, got #{attributes.pretty_inspect}" unless attributes.is_a?(Hash)
368
- attributes.each_pair do |key, value|
369
- instance_variable_set("@#{key}", value) # Initialize instance variables
370
- self.class.class_eval do
371
- define_method("#{key}=") do |new_value| # ex. obj.key = new_value
372
- instance_variable_set("@#{key}", new_value)
373
- end
374
- define_method(key) do
375
- instance_variable_get("@#{key}")
376
- end
377
- end
378
- end
391
+ # Updates a single attribute and then saves the object.
392
+ def update_attribute(name, value)
393
+ self.send("#{name}=".to_sym, value)
394
+ self.save
395
+ end
396
+
397
+ def update_attributes(attrs)
398
+ loads(attrs) && save
379
399
  end
380
400
 
381
401
  def new?
@@ -388,15 +408,13 @@ module RightResource
388
408
  end
389
409
 
390
410
  def destory
411
+ connection.clear
391
412
  connection.delete(element_path)
392
413
  end
393
414
 
394
415
  # For checking <tt>respond_to?</tt> without searching the attributes (which is faster).
395
416
  alias_method :respond_to_without_attributes?, :respond_to?
396
417
 
397
- # A method to determine if an object responds to a message (e.g., a method call). In Active Resource, a Person object with a
398
- # +name+ attribute can answer <tt>true</tt> to <tt>my_person.respond_to?(:name)</tt>, <tt>my_person.respond_to?(:name=)</tt>, and
399
- # <tt>my_person.respond_to?(:name?)</tt>.
400
418
  def respond_to?(method, include_priv = false)
401
419
  method_name = method.to_s
402
420
  if attributes.nil?
@@ -422,7 +440,9 @@ module RightResource
422
440
  pair = URI.decode({resource_name.to_sym => attrs}.to_params).split('&').map {|l| l.split('=')}
423
441
  h = Hash[*pair.flatten]
424
442
  @@non_rs_params.each {|key| h[key.to_s] = self.attributes[key] if self.attributes.has_key?(key) && self.attributes[key]}
443
+ connection.clear
425
444
  connection.put(element_path, h)
445
+ self
426
446
  end
427
447
 
428
448
  def create
@@ -430,9 +450,11 @@ module RightResource
430
450
  pair = URI.decode({resource_name.to_sym => attrs}.to_params).split('&').map {|l| l.split('=')}
431
451
  h= Hash[*pair.flatten]
432
452
  @@non_rs_params.each {|key| h[key.to_s] = self.attributes[key] if self.attributes.has_key?(key) && self.attributes[key]}
453
+ connection.clear
433
454
  connection.post(collection_path, h)
434
455
  self.id = self.class.resource_id
435
456
  self.href = self.class.headers[:location]
457
+ self
436
458
  end
437
459
 
438
460
  def resource_name
@@ -454,12 +476,12 @@ module RightResource
454
476
  if method_name =~ /(=|\?)$/
455
477
  case $1
456
478
  when "="
457
- attributes[$`.to_sym] = arguments.first
479
+ self.attributes[$`.to_sym] = arguments.first
458
480
  when "?"
459
- attributes[$`.to_sym]
481
+ self.attributes[$`.to_sym]
460
482
  end
461
483
  else
462
- return attributes[method_symbol] if attributes.include?(method_symbol)
484
+ return self.attributes[method_symbol] if self.attributes.include?(method_symbol)
463
485
  # not set right now but we know about it
464
486
  # return nil if known_attributes.include?(method_name)
465
487
  super
@@ -1,3 +1,6 @@
1
1
  class Ec2ElasticIp < RightResource::Base
2
+ class << self
3
+ undef :update
4
+ end
2
5
  undef :update
3
6
  end
@@ -1,6 +1,6 @@
1
1
  class MultiCloudImage < RightResource::Base
2
2
  class << self
3
- undef :create, :destory
3
+ undef :create, :update, :destory
4
4
  end
5
5
  undef :create, :update, :destory
6
6
  end
@@ -1,6 +1,6 @@
1
1
  class RightScript < RightResource::Base
2
2
  class << self
3
- undef :create, :destory
3
+ undef :create, :update, :destory
4
4
  end
5
5
  undef :create, :update, :destory
6
6
  end
@@ -1,3 +1,6 @@
1
1
  class S3Bucket < RightResource::Base
2
+ class << self
3
+ undef :update
4
+ end
2
5
  undef :update
3
6
  end
@@ -1,5 +1,6 @@
1
1
  class ServerTemplate < RightResource::Base
2
2
  class << self
3
+ # Get scripts added to the ServerTemplate every phases
3
4
  # === Parameters
4
5
  # * _id_ - ServerTemplate id
5
6
  # * _params_ - Hash (keys = [:phase]) ex. 'boot', 'operational', 'decommission' if not defined, all phases
@@ -1,6 +1,6 @@
1
1
  class Status < RightResource::Base
2
2
  class << self
3
- undef :index, :create, :destory
3
+ undef :index, :create, :update, :destory
4
4
  end
5
5
  undef :create, :update, :destory
6
6
  end
@@ -14,7 +14,7 @@
14
14
  # p Tag.search(:resource_href => ec2_href)
15
15
  class Tag < RightResource::Base
16
16
  class << self
17
- undef :index, :show, :create, :destory
17
+ undef :index, :show, :create, :update, :destory
18
18
 
19
19
  [:set, :unset].each do |act_method|
20
20
  define_method(act_method) do |params|
@@ -54,10 +54,10 @@ class Tag < RightResource::Base
54
54
  if params[:resource_type] == "ec2_instance"
55
55
  resource_name = "Server"
56
56
  else
57
- resource_name = params[:resource_type].split(/-/).map {|r| r.capitalize}.join
57
+ resource_name = params[:resource_type].split(/-/).map {|r| r.capitalize}.join # underscore2pascal
58
58
  end
59
- klass = const_get(resource_name)
60
- klass.instantiate_collection(result)
59
+ klass = const_get(resource_name) # Get class name
60
+ klass.instantiate_collection(result) # Get every class instance
61
61
  end
62
62
  rescue => e
63
63
  logger.error("#{e.class}: #{e.pretty_inspect}")
@@ -67,6 +67,7 @@ class Tag < RightResource::Base
67
67
  logger.debug {"#{__FILE__} #{__LINE__}: #{self.class}\n#{self.pretty_inspect}\n"}
68
68
  end
69
69
 
70
+ # Get all taggable resources name (server, server_array, server_template, etc.)
70
71
  # === Examples
71
72
  # Tag.taggable_resources
72
73
  def taggable_resources
@@ -38,6 +38,6 @@ require 'right_resource/macro'
38
38
  require 'right_resource/credential'
39
39
 
40
40
  module RightResource
41
- VERSION = '0.3.2'
41
+ VERSION = '0.3.3'
42
42
  API_VERSION = "1.0"
43
43
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rightresource
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 2
10
- version: 0.3.2
9
+ - 3
10
+ version: 0.3.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Satoshi Ohki
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-24 00:00:00 +09:00
18
+ date: 2010-11-29 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency