simple_stack 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ module SimpleStack
2
+ module Cacheable
3
+ def reload
4
+ @cached_attributes = {}
5
+ self
6
+ end
7
+
8
+ def cacheable?
9
+ false
10
+ end
11
+
12
+ def cached_attributes
13
+ if cacheable?
14
+ @cached_attributes ||= {}
15
+ else
16
+ {}
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,16 +1,19 @@
1
1
  module SimpleStack
2
2
  class Collection
3
- attr_accessor :hypervisor, :url, :clazz
3
+ include SimpleStack::Cacheable
4
4
 
5
- def initialize(hypervisor, url, clazz)
5
+ attr_accessor :hypervisor, :parent, :url, :clazz
6
+
7
+ def initialize(hypervisor, parent, url, clazz)
6
8
  self.hypervisor = hypervisor
9
+ self.parent = parent
7
10
  self.url = url.to_s
8
11
  self.clazz = clazz
9
12
  end
10
13
 
11
14
  def to_a
12
- @items = hypervisor.get(url).map do |item|
13
- clazz.new hypervisor, "#{url}/#{item["id"]}"
15
+ cached_attributes[:items] ||= hypervisor.get(url).map do |item|
16
+ clazz.new hypervisor, parent, "#{url}/#{item["id"]}"
14
17
  end
15
18
  end
16
19
 
@@ -22,7 +25,12 @@ module SimpleStack
22
25
  response = hypervisor.post(url, options)
23
26
  entity_path = response.headers["location"].sub(/^\//, "").sub(/\/$/, "")
24
27
  entity_url = "#{connection.url}/#{entity_path}"
25
- clazz.new hypervisor, entity_url
28
+ new_item = clazz.new hypervisor, parent, entity_url
29
+ if cacheable?
30
+ cached_attributes[:items] ||= []
31
+ cached_attributes[:items] << self
32
+ end
33
+ new_item
26
34
  end
27
35
 
28
36
  def method_missing(method, *args, &block)
@@ -36,6 +44,5 @@ module SimpleStack
36
44
  def connection
37
45
  hypervisor.connection
38
46
  end
39
-
40
47
  end
41
- end
48
+ end
@@ -1,14 +1,17 @@
1
1
  module SimpleStack
2
2
  class Entity
3
- attr_accessor :hypervisor, :url
3
+ include SimpleStack::Cacheable
4
4
 
5
- def initialize(hypervisor, url)
5
+ attr_accessor :hypervisor, :parent, :url
6
+
7
+ def initialize(hypervisor, parent, url)
6
8
  self.hypervisor = hypervisor
9
+ self.parent = parent
7
10
  self.url = url.to_s
8
11
  end
9
12
 
10
13
  def info
11
- hypervisor.get url
14
+ cached_attributes[:info] ||= hypervisor.get url
12
15
  end
13
16
 
14
17
  def update(attributes = {})
@@ -16,7 +19,12 @@ module SimpleStack
16
19
  end
17
20
 
18
21
  def delete
19
- hypervisor.delete url
22
+ response = hypervisor.delete url
23
+ if cacheable?
24
+ parent.cached_attributes[:items] ||= []
25
+ parent.cached_attributes[:items].delete self
26
+ end
27
+ response
20
28
  end
21
29
 
22
30
  def inspect
@@ -1,13 +1,29 @@
1
1
  module SimpleStack
2
2
  class Guest < Entity
3
3
  def snapshots
4
- SimpleStack::Collection.new hypervisor, "#{url}/snapshots", SimpleStack::Snapshot
4
+ cached_attributes[:snapshots] ||= SimpleStack::Collection.new hypervisor, self, "#{url}/snapshots", SimpleStack::Snapshot
5
+ end
6
+
7
+ def tags
8
+ cached_attributes[:tags] ||= hypervisor.get("#{url}/tags").parsed_response
9
+ end
10
+
11
+ def add_tag(tag)
12
+ hypervisor.post "#{url}/tags", :name => tag
13
+ end
14
+
15
+ def remove_tag(tag)
16
+ hypervisor.delete "#{url}/tags/#{tag}"
5
17
  end
6
18
 
7
19
  def reboot(opts={:force => false})
8
20
  hypervisor.put "#{url}/reboot", :force => opts[:force]
9
21
  end
10
22
 
23
+ def revert_to(snapshot)
24
+ snapshot.revert
25
+ end
26
+
11
27
  def power_state=(state)
12
28
  hypervisor.put "#{url}/power", :state => state
13
29
  end
@@ -1,11 +1,15 @@
1
1
  module SimpleStack
2
2
  class Hypervisor
3
- attr_accessor :connection, :type, :host
3
+ include SimpleStack::Cacheable
4
+
5
+ attr_accessor :connection, :type, :host, :username, :password
4
6
 
5
7
  def initialize(connection, type, options)
6
8
  self.connection = connection
7
9
  self.type = type
8
10
  self.host = options[:host]
11
+ self.username = options[:username]
12
+ self.password = options[:password]
9
13
  end
10
14
 
11
15
  def url
@@ -22,15 +26,15 @@ module SimpleStack
22
26
  end
23
27
 
24
28
  def token
25
- "TODO"
29
+ Base64.encode64("#{username}:#{password}").split("\n").join("")
26
30
  end
27
31
 
28
32
  def info
29
- self.get url
33
+ cached_attributes[:info] ||= self.get url
30
34
  end
31
35
 
32
36
  def guests
33
- SimpleStack::Collection.new self, "#{url}/guests", SimpleStack::Guest
37
+ cached_attributes[:guests] ||= SimpleStack::Collection.new self, self, "#{url}/guests", SimpleStack::Guest
34
38
  end
35
39
 
36
40
  def get(url)
@@ -1,7 +1,11 @@
1
1
  module SimpleStack
2
2
  class Snapshot < Entity
3
3
  def use
4
- raise "Not Implemented"
4
+ revert
5
+ end
6
+
7
+ def revert
8
+ hypervisor.put "#{url}/revert", {}
5
9
  end
6
10
  end
7
11
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleStack
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/simple_stack.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  require "json"
2
+ require "base64"
2
3
  require "httparty"
3
4
  require "simple_stack/version"
4
5
  require "simple_stack/connection"
6
+ require "simple_stack/cacheable"
5
7
  require "simple_stack/collection"
6
8
  require "simple_stack/hypervisor"
7
9
  require "simple_stack/entity"
@@ -16,4 +16,10 @@ describe SimpleStack::Hypervisor do
16
16
  subject.headers.should have_key("x-simplestack-hypervisor-token")
17
17
  subject.headers["Content-Type"].should == "application/json"
18
18
  end
19
+
20
+ it "should have a base64 encoded username:password token for now" do
21
+ subject.username = "admin"
22
+ subject.password = "secret"
23
+ subject.token.should == "YWRtaW46c2VjcmV0"
24
+ end
19
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_stack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-21 00:00:00.000000000 Z
12
+ date: 2012-06-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: !ruby/object:Gem::Requirement
16
+ requirement: &2161072020 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,15 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
24
+ version_requirements: *2161072020
30
25
  - !ruby/object:Gem::Dependency
31
26
  name: httparty
32
- requirement: !ruby/object:Gem::Requirement
27
+ requirement: &2161071580 !ruby/object:Gem::Requirement
33
28
  none: false
34
29
  requirements:
35
30
  - - ! '>='
@@ -37,12 +32,7 @@ dependencies:
37
32
  version: '0'
38
33
  type: :runtime
39
34
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
35
+ version_requirements: *2161071580
46
36
  description: A Simple Stack gem
47
37
  email:
48
38
  - thiago.morello@locaweb.com.br
@@ -55,6 +45,7 @@ files:
55
45
  - README.markdown
56
46
  - Rakefile
57
47
  - lib/simple_stack.rb
48
+ - lib/simple_stack/cacheable.rb
58
49
  - lib/simple_stack/collection.rb
59
50
  - lib/simple_stack/connection.rb
60
51
  - lib/simple_stack/entity.rb
@@ -86,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
77
  version: '0'
87
78
  requirements: []
88
79
  rubyforge_project: simple_stack
89
- rubygems_version: 1.8.23
80
+ rubygems_version: 1.8.10
90
81
  signing_key:
91
82
  specification_version: 3
92
83
  summary: A Simple Stack gem