spice 1.0.0.rc → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. data/.gitignore +2 -0
  2. data/Gemfile +0 -14
  3. data/README.md +1 -1
  4. data/Rakefile +1 -1
  5. data/TODO.md +2 -1
  6. data/lib/spice.rb +47 -72
  7. data/lib/spice/authentication.rb +23 -45
  8. data/lib/spice/base.rb +104 -0
  9. data/lib/spice/client.rb +8 -48
  10. data/lib/spice/config.rb +37 -19
  11. data/lib/spice/connection.rb +33 -117
  12. data/lib/spice/connection/clients.rb +26 -6
  13. data/lib/spice/connection/cookbooks.rb +19 -14
  14. data/lib/spice/connection/data_bags.rb +31 -20
  15. data/lib/spice/connection/environments.rb +6 -7
  16. data/lib/spice/connection/nodes.rb +23 -6
  17. data/lib/spice/connection/roles.rb +5 -5
  18. data/lib/spice/connection/search.rb +12 -12
  19. data/lib/spice/cookbook.rb +3 -29
  20. data/lib/spice/cookbook_version.rb +9 -61
  21. data/lib/spice/core_ext/array.rb +7 -0
  22. data/lib/spice/core_ext/enumerable.rb +11 -0
  23. data/lib/spice/core_ext/hash.rb +49 -0
  24. data/lib/spice/core_ext/mash.rb +219 -0
  25. data/lib/spice/data_bag.rb +3 -37
  26. data/lib/spice/data_bag_item.rb +7 -47
  27. data/lib/spice/environment.rb +12 -27
  28. data/lib/spice/error.rb +22 -10
  29. data/lib/spice/identity_map.rb +8 -0
  30. data/lib/spice/node.rb +13 -34
  31. data/lib/spice/request.rb +73 -11
  32. data/lib/spice/response/parse_json.rb +8 -3
  33. data/lib/spice/role.rb +8 -33
  34. data/lib/spice/version.rb +1 -1
  35. data/spec/fixtures/client.pem +27 -0
  36. data/spec/fixtures/clients/create.json +4 -0
  37. data/spec/fixtures/clients/index.json +5 -0
  38. data/spec/fixtures/clients/reregister.json +5 -0
  39. data/spec/fixtures/clients/show.json +8 -0
  40. data/spec/fixtures/clients/update.json +5 -0
  41. data/spec/fixtures/cookbook_versions/show.json +108 -0
  42. data/spec/fixtures/cookbook_versions/update.json +62 -0
  43. data/spec/fixtures/cookbooks/index-0.10.json +28 -0
  44. data/spec/fixtures/cookbooks/index-0.9.json +4 -0
  45. data/spec/fixtures/cookbooks/show-0.10.json +15 -0
  46. data/spec/fixtures/cookbooks/show-apache2-0.9.json +5 -0
  47. data/spec/fixtures/cookbooks/show-unicorn-0.9.json +5 -0
  48. data/spec/fixtures/data_bag_items/create.json +1 -0
  49. data/spec/fixtures/data_bag_items/show.json +4 -0
  50. data/spec/fixtures/data_bag_items/update.json +4 -0
  51. data/spec/fixtures/data_bags/create.json +3 -0
  52. data/spec/fixtures/data_bags/index.json +3 -0
  53. data/spec/fixtures/data_bags/show.json +3 -0
  54. data/spec/fixtures/environments/cookbook.json +15 -0
  55. data/spec/fixtures/environments/cookbooks.json +28 -0
  56. data/spec/fixtures/environments/create.json +1 -0
  57. data/spec/fixtures/environments/delete.json +8 -0
  58. data/spec/fixtures/environments/index.json +3 -0
  59. data/spec/fixtures/environments/show.json +8 -0
  60. data/spec/fixtures/environments/update.json +8 -0
  61. data/spec/fixtures/nodes/cookbooks.json +41 -0
  62. data/spec/fixtures/nodes/create.json +1 -0
  63. data/spec/fixtures/nodes/delete.json +17 -0
  64. data/spec/fixtures/nodes/index.json +3 -0
  65. data/spec/fixtures/nodes/show.json +17 -0
  66. data/spec/fixtures/nodes/update.json +13 -0
  67. data/spec/fixtures/roles/create.json +1 -0
  68. data/spec/fixtures/roles/delete.json +11 -0
  69. data/spec/fixtures/roles/index.json +3 -0
  70. data/spec/fixtures/roles/show.json +12 -0
  71. data/spec/fixtures/roles/update.json +11 -0
  72. data/spec/fixtures/search/client.json +1 -0
  73. data/spec/fixtures/search/data_bag.json +1 -0
  74. data/spec/fixtures/search/environment.json +1 -0
  75. data/spec/fixtures/search/node.json +1 -0
  76. data/spec/fixtures/search/role.json +1 -0
  77. data/spec/spec_helper.rb +5 -4
  78. data/spec/spice/base_spec.rb +34 -0
  79. data/spec/spice/client_spec.rb +0 -61
  80. data/spec/spice/config_spec.rb +14 -0
  81. data/spec/spice/connection/clients_spec.rb +82 -0
  82. data/spec/spice/connection/cookbooks_spec.rb +86 -0
  83. data/spec/spice/connection/data_bags_spec.rb +126 -0
  84. data/spec/spice/connection_spec.rb +63 -0
  85. data/spec/spice/cookbook_spec.rb +0 -61
  86. data/spec/spice/data_bag_item_spec.rb +7 -0
  87. data/spec/spice/data_bag_spec.rb +0 -160
  88. data/spec/spice/environment_spec.rb +0 -89
  89. data/spec/spice/node_spec.rb +0 -74
  90. data/spec/spice/role_spec.rb +0 -61
  91. data/spec/spice_spec.rb +8 -116
  92. data/spec/support/helpers.rb +58 -0
  93. data/spice.gemspec +13 -15
  94. metadata +227 -87
  95. data/.watchr +0 -22
  96. data/lib/spice/connection/authentication.rb +0 -47
  97. data/lib/spice/mock.rb +0 -46
  98. data/lib/spice/persistence.rb +0 -62
  99. data/spec/spice/chef_spec.rb +0 -52
  100. data/spec/spice/search_spec.rb +0 -2
  101. data/spec/support/chef_requests.rb +0 -0
  102. data/spec/support/client_requests.rb +0 -95
  103. data/spec/support/cookbook_requests.rb +0 -95
  104. data/spec/support/data_bag_item_requests.rb +0 -138
  105. data/spec/support/data_bag_requests.rb +0 -95
  106. data/spec/support/environment_requests.rb +0 -221
  107. data/spec/support/node_requests.rb +0 -138
  108. data/spec/support/respond_with_matcher.rb +0 -53
  109. data/spec/support/role_requests.rb +0 -95
@@ -7,7 +7,8 @@ module Spice
7
7
  # @option options [String] :sort Order by which to sort the results
8
8
  # @option options [Numeric] :start The number by which to offset the results
9
9
  # @option options [Numeric] :rows The maximum number of rows to return
10
- def search(index, options={})
10
+ def search(index, options=Mash.new)
11
+ index = index.to_s
11
12
  options = {:q => options} if options.is_a? String
12
13
  options.symbolize_keys!
13
14
 
@@ -22,28 +23,27 @@ module Spice
22
23
  params = options.collect{ |k, v| "#{k}=#{CGI::escape(v.to_s)}"}.join("&")
23
24
  case index
24
25
  when 'node'
25
- connection.get("/search/#{CGI::escape(index.to_s)}?#{params}").body['rows'].map do |node|
26
- Spice::Node.new(node)
26
+ get("/search/#{CGI::escape(index.to_s)}?#{params}")['rows'].map do |node|
27
+ Spice::Node.get_or_new(node)
27
28
  end
28
29
  when 'role'
29
- connection.get("/search/#{CGI::escape(index.to_s)}?#{params}").body['rows'].map do |role|
30
- Spice::Role.new(role)
30
+ get("/search/#{CGI::escape(index.to_s)}?#{params}")['rows'].map do |role|
31
+ Spice::Role.get_or_new(role)
31
32
  end
32
33
  when 'client'
33
- connection.get("/search/#{CGI::escape(index.to_s)}?#{params}").body['rows'].map do |client|
34
- Spice::Client.new(client)
34
+ get("/search/#{CGI::escape(index.to_s)}?#{params}")['rows'].map do |client|
35
+ Spice::Client.get_or_new(client)
35
36
  end
36
37
  when 'environment'
37
- connection.get("/search/#{CGI::escape(index.to_s)}?#{params}").body['rows'].map do |env|
38
+ get("/search/#{CGI::escape(index.to_s)}?#{params}")['rows'].map do |env|
38
39
  env['attrs'] = env.delete('attributes')
39
- Spice::Environment.new(env)
40
+ Spice::Environment.get_or_new(env)
40
41
  end
41
42
  else
42
43
  # assume it's a data bag
43
- connection.get("/search/#{CGI::escape(index.to_s)}?#{params}").body['rows'].map do |db|
44
+ get("/search/#{CGI::escape(index.to_s)}?#{params}")['rows'].map do |db|
44
45
  data = db['raw_data']
45
- id = data.delete('id')
46
- Spice::DataBagItem.new(:_id => id, :data => data, :name => index)
46
+ Spice::DataBagItem.get_or_new(data)
47
47
  end
48
48
  end
49
49
  end # def search
@@ -1,34 +1,8 @@
1
- require 'spice/persistence'
1
+ require 'spice/base'
2
2
 
3
3
  module Spice
4
- class Cookbook
5
- include Virtus
6
- include Aequitas
7
- include Spice::Persistence
8
- extend Spice::Persistence
4
+ class Cookbook < Base
5
+ attr_reader :name, :versions
9
6
 
10
- endpoint "cookbooks"
11
-
12
- # @macro [attach] attribute
13
- # @attribute [rw]
14
- # @return [$2] the $1 attribute
15
- attribute :name, String
16
- attribute :versions, Array, :default => []
17
-
18
- validates_presence_of :name
19
-
20
- def self.get(name)
21
- connection.cookbook(name)
22
- end
23
-
24
- # Check if the cookbook exists on the Chef server
25
- def new_record?
26
- begin
27
- connection.get("/cookbooks/#{name}")
28
- return false
29
- rescue Spice::Error::NotFound
30
- return true
31
- end
32
- end
33
7
  end
34
8
  end
@@ -1,68 +1,16 @@
1
- require 'spice/persistence'
1
+ require 'spice/base'
2
2
 
3
3
  module Spice
4
- class CookbookVersion
5
- include Virtus
6
- include Aequitas
7
- include Spice::Persistence
8
- extend Spice::Persistence
4
+ class CookbookVersion < Base
5
+ attr_reader :name, :version, :definitions, :files, :providers, :metadata,
6
+ :libraries, :templates, :resources, :attributes, :cookbook_name,
7
+ :recipes, :root_files
9
8
 
10
- endpoint "cookbooks"
11
-
12
- # @macro [attach] attribute
13
- # @attribute [rw]
14
- # @return [$2] the $1 attribute
15
- attribute :name, String
16
- attribute :version, String
17
- attribute :definitions, Array, :default => []
18
- attribute :files, Array, :default => []
19
- attribute :providers, Array, :default => []
20
- attribute :metadata, Hash, :default => {}
21
- attribute :libraries, Array, :default => []
22
- attribute :templates, Array, :default => []
23
- attribute :resources, Array, :default => []
24
- attribute :_attributes, Array, :default => []
25
- attribute :json_class, String, :default => "Chef::CookbookVersion"
26
- attribute :cookbook_name, String
27
- attribute :version, String
28
- attribute :recipes, Array, :default => []
29
- attribute :root_files, Array, :default => []
30
- attribute :chef_type, String, :default => "cookbook_version"
31
-
32
- validates_presence_of :name
33
-
34
- def do_post
35
- duped_attributes = attributes.dup
36
- duped_attributes[:attributes] = attributes[:_attributes]
37
- duped_attributes.delete(:_attributes)
38
-
39
- connection.put("/cookbooks/#{cookbook_name}/#{version}", duped_attributes)
40
- end
41
-
42
- def do_put
43
- duped_attributes = attributes.dup
44
- duped_attributes[:attributes] = attributes[:_attributes]
45
- duped_attributes.delete(:_attributes)
46
-
47
- connection.put("/cookbooks/#{cookbook_name}/#{version}", duped_attributes)
9
+ def initialize(attrs=Mash.new)
10
+ super
11
+ @attrs['json_class'] ||= "Chef::CookbookVersion"
12
+ @attrs['chef_type'] ||= 'cookbook_version'
48
13
  end
49
14
 
50
- def do_delete
51
- connection.delete("/cookbooks/#{cookbook_name}/#{version}")
52
- end
53
-
54
- def self.get(name, version)
55
- connection.cookbook_version(name, version)
56
- end
57
-
58
- # Check if the cookbook version exists on the Chef server
59
- def new_record?
60
- begin
61
- connection.get("/cookbooks/#{name.split('-').first}/#{version}")
62
- return false
63
- rescue Spice::Error::NotFound
64
- return true
65
- end
66
- end
67
15
  end
68
16
  end
@@ -0,0 +1,7 @@
1
+ class Array
2
+
3
+ def extract_options!
4
+ last.is_a?(::Hash) ? pop : {}
5
+ end # def extract_options!
6
+
7
+ end
@@ -0,0 +1,11 @@
1
+ module Enumerable
2
+
3
+ def threaded_map
4
+ threads = []
5
+ each do |object|
6
+ threads << Thread.new{yield object}
7
+ end
8
+ threads.map(&:value)
9
+ end
10
+
11
+ end
@@ -0,0 +1,49 @@
1
+ class Hash
2
+
3
+ # Return a hash that includes everything but the given keys.
4
+ #
5
+ # @param keys [Array, Set]
6
+ # @return [Hash]
7
+ def except(*keys)
8
+ self.dup.except!(*keys)
9
+ end
10
+
11
+ # Replaces the hash without the given keys.
12
+ #
13
+ # @param keys [Array, Set]
14
+ # @return [Hash]
15
+ def except!(*keys)
16
+ keys.each{|key| delete(key)}
17
+ self
18
+ end
19
+
20
+ # Merges self with another hash, recursively
21
+ #
22
+ # @param hash [Hash] The hash to merge
23
+ # @return [Hash]
24
+ def deep_merge(hash)
25
+ target = self.dup
26
+ hash.keys.each do |key|
27
+ if hash[key].is_a?(Hash) && self[key].is_a?(Hash)
28
+ target[key] = target[key].deep_merge(hash[key])
29
+ next
30
+ end
31
+ target[key] = hash[key]
32
+ end
33
+ target
34
+ end
35
+
36
+ def stringify_keys!
37
+ keys.each do |key|
38
+ self[key.to_s] = delete(key)
39
+ end
40
+ self
41
+ end unless defined? stringify_keys
42
+
43
+ def symbolize_keys!
44
+ keys.each do |key|
45
+ self[(key.to_sym rescue key) || key] = delete(key)
46
+ end
47
+ self
48
+ end unless defined? symbolize_keys
49
+ end
@@ -0,0 +1,219 @@
1
+ # Copyright (c) 2009 Dan Kubb
2
+
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ # ---
23
+ # ---
24
+
25
+ # Some portions of blank.rb and mash.rb are verbatim copies of software
26
+ # licensed under the MIT license. That license is included below:
27
+
28
+ # Copyright (c) 2005-2008 David Heinemeier Hansson
29
+
30
+ # Permission is hereby granted, free of charge, to any person obtaining
31
+ # a copy of this software and associated documentation files (the
32
+ # "Software"), to deal in the Software without restriction, including
33
+ # without limitation the rights to use, copy, modify, merge, publish,
34
+ # distribute, sublicense, and/or sell copies of the Software, and to
35
+ # permit persons to whom the Software is furnished to do so, subject to
36
+ # the following conditions:
37
+
38
+ # The above copyright notice and this permission notice shall be
39
+ # included in all copies or substantial portions of the Software.
40
+
41
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
42
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
43
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
44
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
45
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
46
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
47
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
48
+
49
+ # This class has dubious semantics and we only have it so that people can write
50
+ # params[:key] instead of params['key'].
51
+ class Mash < Hash
52
+
53
+ # @param constructor<Object>
54
+ # The default value for the mash. Defaults to an empty hash.
55
+ #
56
+ # [Alternatives]
57
+ # If constructor is a Hash, a new mash will be created based on the keys of
58
+ # the hash and no default value will be set.
59
+ def initialize(constructor = {})
60
+ if constructor.is_a?(Hash)
61
+ super()
62
+ update(constructor)
63
+ else
64
+ super(constructor)
65
+ end
66
+ end
67
+
68
+ # @param orig<Object> Mash being copied
69
+ #
70
+ # @return [Object] A new copied Mash
71
+ def initialize_copy(orig)
72
+ super
73
+ # Handle nested values
74
+ each do |k,v|
75
+ if v.kind_of?(Mash) || v.is_a?(Array)
76
+ self[k] = v.dup
77
+ end
78
+ end
79
+ self
80
+ end
81
+
82
+ # @param key<Object> The default value for the mash. Defaults to nil.
83
+ #
84
+ # [Alternatives]
85
+ # If key is a Symbol and it is a key in the mash, then the default value will
86
+ # be set to the value matching the key.
87
+ def default(key = nil)
88
+ if key.is_a?(Symbol) && include?(key = key.to_s)
89
+ self[key]
90
+ else
91
+ super
92
+ end
93
+ end
94
+
95
+ alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
96
+ alias_method :regular_update, :update unless method_defined?(:regular_update)
97
+
98
+ # @param key<Object> The key to set.
99
+ # @param value<Object>
100
+ # The value to set the key to.
101
+ #
102
+ # @see Mash#convert_key
103
+ # @see Mash#convert_value
104
+ def []=(key, value)
105
+ regular_writer(convert_key(key), convert_value(value))
106
+ end
107
+
108
+ # @param other_hash<Hash>
109
+ # A hash to update values in the mash with. The keys and the values will be
110
+ # converted to Mash format.
111
+ #
112
+ # @return [Mash] The updated mash.
113
+ def update(other_hash)
114
+ other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
115
+ self
116
+ end
117
+
118
+ alias_method :merge!, :update
119
+
120
+ # @param key<Object> The key to check for. This will be run through convert_key.
121
+ #
122
+ # @return [Boolean] True if the key exists in the mash.
123
+ def key?(key)
124
+ super(convert_key(key))
125
+ end
126
+
127
+ # def include? def has_key? def member?
128
+ alias_method :include?, :key?
129
+ alias_method :has_key?, :key?
130
+ alias_method :member?, :key?
131
+
132
+ # @param key<Object> The key to fetch. This will be run through convert_key.
133
+ # @param extras<Array> Default value.
134
+ #
135
+ # @return [Object] The value at key or the default value.
136
+ def fetch(key, *extras)
137
+ super(convert_key(key), *extras)
138
+ end
139
+
140
+ # @param indices<Array>
141
+ # The keys to retrieve values for. These will be run through +convert_key+.
142
+ #
143
+ # @return [Array] The values at each of the provided keys
144
+ def values_at(*indices)
145
+ indices.collect {|key| self[convert_key(key)]}
146
+ end
147
+
148
+ # @param hash<Hash> The hash to merge with the mash.
149
+ #
150
+ # @return [Mash] A new mash with the hash values merged in.
151
+ def merge(hash)
152
+ self.dup.update(hash)
153
+ end
154
+
155
+ # @param key<Object>
156
+ # The key to delete from the mash.\
157
+ def delete(key)
158
+ super(convert_key(key))
159
+ end
160
+
161
+ # @return [Mash] A new mash without the selected keys.
162
+ #
163
+ # @example
164
+ # { :one => 1, :two => 2, :three => 3 }.except(:one)
165
+ # #=> { "two" => 2, "three" => 3 }
166
+ def except(*keys)
167
+ super(*keys.map {|k| convert_key(k)})
168
+ end
169
+
170
+ # Used to provide the same interface as Hash.
171
+ #
172
+ # @return [Mash] This mash unchanged.
173
+ def stringify_keys!; self end
174
+
175
+ # @return [Hash] The mash as a Hash with symbolized keys.
176
+ def symbolize_keys
177
+ h = Hash.new(default)
178
+ each { |key, val| h[key.to_sym] = val }
179
+ h
180
+ end
181
+
182
+ # @return [Hash] The mash as a Hash with string keys.
183
+ def to_hash
184
+ Hash.new(default).merge(self)
185
+ end
186
+
187
+ # @return [Mash] Convert a Hash into a Mash
188
+ # The input Hash's default value is maintained
189
+ def self.from_hash(hash)
190
+ mash = Mash.new(hash)
191
+ mash.default = hash.default
192
+ mash
193
+ end
194
+
195
+ protected
196
+ # @param key<Object> The key to convert.
197
+ #
198
+ # @api private
199
+ def convert_key(key)
200
+ key.kind_of?(Symbol) ? key.to_s : key
201
+ end
202
+
203
+ # @param value<Object> The value to convert.
204
+ #
205
+ # @return [Object]
206
+ # The converted value. A Hash or an Array of hashes, will be converted to
207
+ # their Mash equivalents.
208
+ #
209
+ # @api private
210
+ def convert_value(value)
211
+ if value.class == Hash
212
+ Mash.from_hash(value)
213
+ elsif value.is_a?(Array)
214
+ value.collect { |e| convert_value(e) }
215
+ else
216
+ value
217
+ end
218
+ end
219
+ end