chef-api 0.8.0 → 0.10.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/lib/chef-api.rb +19 -20
  3. data/lib/chef-api/aclable.rb +35 -0
  4. data/lib/chef-api/authentication.rb +23 -25
  5. data/lib/chef-api/configurable.rb +14 -14
  6. data/lib/chef-api/connection.rb +68 -67
  7. data/lib/chef-api/defaults.rb +25 -24
  8. data/lib/chef-api/error_collection.rb +1 -1
  9. data/lib/chef-api/errors.rb +3 -3
  10. data/lib/chef-api/log.rb +7 -0
  11. data/lib/chef-api/multipart.rb +17 -17
  12. data/lib/chef-api/resource.rb +17 -15
  13. data/lib/chef-api/resources/base.rb +22 -22
  14. data/lib/chef-api/resources/client.rb +5 -3
  15. data/lib/chef-api/resources/collection_proxy.rb +4 -3
  16. data/lib/chef-api/resources/cookbook.rb +2 -2
  17. data/lib/chef-api/resources/cookbook_version.rb +1 -1
  18. data/lib/chef-api/resources/data_bag.rb +4 -4
  19. data/lib/chef-api/resources/data_bag_item.rb +2 -3
  20. data/lib/chef-api/resources/environment.rb +1 -1
  21. data/lib/chef-api/resources/group.rb +15 -0
  22. data/lib/chef-api/resources/node.rb +11 -8
  23. data/lib/chef-api/resources/organization.rb +2 -2
  24. data/lib/chef-api/resources/partial_search.rb +4 -4
  25. data/lib/chef-api/resources/principal.rb +1 -1
  26. data/lib/chef-api/resources/role.rb +2 -1
  27. data/lib/chef-api/resources/search.rb +6 -6
  28. data/lib/chef-api/resources/user.rb +3 -3
  29. data/lib/chef-api/util.rb +8 -8
  30. data/lib/chef-api/validator.rb +3 -3
  31. data/lib/chef-api/validators/base.rb +3 -3
  32. data/lib/chef-api/validators/required.rb +1 -1
  33. data/lib/chef-api/validators/type.rb +1 -1
  34. data/lib/chef-api/version.rb +1 -1
  35. metadata +25 -58
  36. data/.gitignore +0 -21
  37. data/.travis.yml +0 -18
  38. data/CHANGELOG.md +0 -63
  39. data/Gemfile +0 -12
  40. data/README.md +0 -352
  41. data/Rakefile +0 -11
  42. data/chef-api.gemspec +0 -25
  43. data/spec/integration/resources/client_spec.rb +0 -62
  44. data/spec/integration/resources/environment_spec.rb +0 -8
  45. data/spec/integration/resources/node_spec.rb +0 -8
  46. data/spec/integration/resources/partial_search_spec.rb +0 -23
  47. data/spec/integration/resources/role_spec.rb +0 -8
  48. data/spec/integration/resources/search_spec.rb +0 -21
  49. data/spec/integration/resources/user_spec.rb +0 -8
  50. data/spec/spec_helper.rb +0 -32
  51. data/spec/support/chef_server.rb +0 -198
  52. data/spec/support/cookbook.tar.gz +0 -0
  53. data/spec/support/shared/chef_api_resource.rb +0 -91
  54. data/spec/support/user.pem +0 -27
  55. data/spec/unit/authentication_spec.rb +0 -70
  56. data/spec/unit/defaults_spec.rb +0 -60
  57. data/spec/unit/errors_spec.rb +0 -294
  58. data/spec/unit/resources/base_spec.rb +0 -49
  59. data/spec/unit/resources/client_spec.rb +0 -53
  60. data/spec/unit/resources/connection_spec.rb +0 -53
@@ -1,11 +1,11 @@
1
- require 'chef-api/version'
2
- require 'pathname'
3
- require 'json'
1
+ require_relative "version"
2
+ require "pathname"
3
+ require "json"
4
4
 
5
5
  module ChefAPI
6
6
  module Defaults
7
7
  # Default API endpoint
8
- ENDPOINT = 'https://api.opscode.com/'.freeze
8
+ ENDPOINT = "https://api.opscode.com/".freeze
9
9
 
10
10
  # Default User Agent header string
11
11
  USER_AGENT = "ChefAPI Ruby Gem #{ChefAPI::VERSION}".freeze
@@ -37,7 +37,7 @@ module ChefAPI
37
37
  if result = chef_api_config_path
38
38
  Pathname(result).expand_path
39
39
  else
40
- Pathname('')
40
+ Pathname("")
41
41
  end
42
42
  end
43
43
 
@@ -46,8 +46,8 @@ module ChefAPI
46
46
  #
47
47
  # @return [String, nil] Path to config file, or nil
48
48
  def chef_api_config_path
49
- ENV['CHEF_API_CONFIG'] || if ENV.key?('HOME')
50
- '~/.chef-api'
49
+ ENV["CHEF_API_CONFIG"] || if ENV.key?("HOME")
50
+ "~/.chef-api"
51
51
  else
52
52
  nil
53
53
  end
@@ -70,7 +70,7 @@ module ChefAPI
70
70
  # @return [String] (default: +https://api.opscode.com/+)
71
71
  #
72
72
  def endpoint
73
- ENV['CHEF_API_ENDPOINT'] || config['CHEF_API_ENDPOINT'] || ENDPOINT
73
+ ENV["CHEF_API_ENDPOINT"] || config["CHEF_API_ENDPOINT"] || ENDPOINT
74
74
  end
75
75
 
76
76
  #
@@ -85,12 +85,12 @@ module ChefAPI
85
85
  # @return [true, false]
86
86
  #
87
87
  def flavor
88
- if ENV['CHEF_API_FLAVOR']
89
- ENV['CHEF_API_FLAVOR'].to_sym
90
- elsif config['CHEF_API_FLAVOR']
91
- config['CHEF_API_FLAVOR']
88
+ if ENV["CHEF_API_FLAVOR"]
89
+ ENV["CHEF_API_FLAVOR"].to_sym
90
+ elsif config["CHEF_API_FLAVOR"]
91
+ config["CHEF_API_FLAVOR"]
92
92
  else
93
- endpoint.include?('/organizations') ? :enterprise : :open_source
93
+ endpoint.include?("/organizations") ? :enterprise : :open_source
94
94
  end
95
95
  end
96
96
 
@@ -100,7 +100,7 @@ module ChefAPI
100
100
  # @return [String]
101
101
  #
102
102
  def user_agent
103
- ENV['CHEF_API_USER_AGENT'] || config['CHEF_API_USER_AGENT'] || USER_AGENT
103
+ ENV["CHEF_API_USER_AGENT"] || config["CHEF_API_USER_AGENT"] || USER_AGENT
104
104
  end
105
105
 
106
106
  #
@@ -111,7 +111,7 @@ module ChefAPI
111
111
  # @return [String, nil]
112
112
  #
113
113
  def client
114
- ENV['CHEF_API_CLIENT'] || config['CHEF_API_CLIENT']
114
+ ENV["CHEF_API_CLIENT"] || config["CHEF_API_CLIENT"]
115
115
  end
116
116
 
117
117
  #
@@ -122,15 +122,16 @@ module ChefAPI
122
122
  # @return [String, nil]
123
123
  #
124
124
  def key
125
- ENV['CHEF_API_KEY'] || config['CHEF_API_KEY']
125
+ ENV["CHEF_API_KEY"] || config["CHEF_API_KEY"]
126
126
  end
127
+
127
128
  #
128
129
  # The HTTP Proxy server address as a string
129
130
  #
130
131
  # @return [String, nil]
131
132
  #
132
133
  def proxy_address
133
- ENV['CHEF_API_PROXY_ADDRESS'] || config['CHEF_API_PROXY_ADDRESS']
134
+ ENV["CHEF_API_PROXY_ADDRESS"] || config["CHEF_API_PROXY_ADDRESS"]
134
135
  end
135
136
 
136
137
  #
@@ -139,7 +140,7 @@ module ChefAPI
139
140
  # @return [String, nil]
140
141
  #
141
142
  def proxy_password
142
- ENV['CHEF_API_PROXY_PASSWORD'] || config['CHEF_API_PROXY_PASSWORD']
143
+ ENV["CHEF_API_PROXY_PASSWORD"] || config["CHEF_API_PROXY_PASSWORD"]
143
144
  end
144
145
 
145
146
  #
@@ -148,7 +149,7 @@ module ChefAPI
148
149
  # @return [String, nil]
149
150
  #
150
151
  def proxy_port
151
- ENV['CHEF_API_PROXY_PORT'] || config['CHEF_API_PROXY_PORT']
152
+ ENV["CHEF_API_PROXY_PORT"] || config["CHEF_API_PROXY_PORT"]
152
153
  end
153
154
 
154
155
  #
@@ -157,7 +158,7 @@ module ChefAPI
157
158
  # @return [String, nil]
158
159
  #
159
160
  def proxy_username
160
- ENV['CHEF_API_PROXY_USERNAME'] || config['CHEF_API_PROXY_USERNAME']
161
+ ENV["CHEF_API_PROXY_USERNAME"] || config["CHEF_API_PROXY_USERNAME"]
161
162
  end
162
163
 
163
164
  #
@@ -166,7 +167,7 @@ module ChefAPI
166
167
  # @return [String, nil]
167
168
  #
168
169
  def ssl_pem_file
169
- ENV['CHEF_API_SSL_PEM_FILE'] || config['CHEF_API_SSL_PEM_FILE']
170
+ ENV["CHEF_API_SSL_PEM_FILE"] || config["CHEF_API_SSL_PEM_FILE"]
170
171
  end
171
172
 
172
173
  #
@@ -175,10 +176,10 @@ module ChefAPI
175
176
  # @return [true, false]
176
177
  #
177
178
  def ssl_verify
178
- if ENV['CHEF_API_SSL_VERIFY'].nil? && config['CHEF_API_SSL_VERIFY'].nil?
179
+ if ENV["CHEF_API_SSL_VERIFY"].nil? && config["CHEF_API_SSL_VERIFY"].nil?
179
180
  true
180
181
  else
181
- %w[t y].include?(ENV['CHEF_API_SSL_VERIFY'].downcase[0]) || config['CHEF_API_SSL_VERIFY']
182
+ %w{t y}.include?(ENV["CHEF_API_SSL_VERIFY"].downcase[0]) || config["CHEF_API_SSL_VERIFY"]
182
183
  end
183
184
  end
184
185
 
@@ -188,7 +189,7 @@ module ChefAPI
188
189
  # @return [Integer, nil]
189
190
  #
190
191
  def read_timeout
191
- timeout_from_env = ENV['CHEF_API_READ_TIMEOUT'] || config['CHEF_API_READ_TIMEOUT']
192
+ timeout_from_env = ENV["CHEF_API_READ_TIMEOUT"] || config["CHEF_API_READ_TIMEOUT"]
192
193
 
193
194
  Integer(timeout_from_env) unless timeout_from_env.nil?
194
195
  end
@@ -34,7 +34,7 @@ module ChefAPI
34
34
  # @return [Array<String>]
35
35
  #
36
36
  def full_messages
37
- self.map do |key, errors|
37
+ map do |key, errors|
38
38
  errors.map do |error|
39
39
  "`#{key}' #{error}"
40
40
  end
@@ -1,4 +1,4 @@
1
- require 'erb'
1
+ require "erb"
2
2
 
3
3
  module ChefAPI
4
4
  module Error
@@ -31,9 +31,9 @@ module ChefAPI
31
31
  private
32
32
 
33
33
  def template
34
- class_name = self.class.to_s.split('::').last
34
+ class_name = self.class.to_s.split("::").last
35
35
  filename = @filename || Util.underscore(class_name)
36
- ChefAPI.root.join('templates', 'errors', "#{filename}.erb")
36
+ ChefAPI.root.join("templates", "errors", "#{filename}.erb")
37
37
  end
38
38
  end
39
39
 
@@ -0,0 +1,7 @@
1
+ require "mixlib/log"
2
+
3
+ module ChefAPI
4
+ class Log
5
+ extend Mixlib::Log
6
+ end
7
+ end
@@ -1,9 +1,9 @@
1
- require 'cgi'
2
- require 'mime/types'
1
+ require "cgi"
2
+ require "mime/types"
3
3
 
4
4
  module ChefAPI
5
5
  module Multipart
6
- BOUNDARY = '------ChefAPIMultipartBoundary'.freeze
6
+ BOUNDARY = "------ChefAPIMultipartBoundary".freeze
7
7
 
8
8
  class Body
9
9
  def initialize(params = {})
@@ -48,12 +48,12 @@ module ChefAPI
48
48
  # Read from IOs in order until `length` bytes have been received.
49
49
  def read(length = nil, outbuf = nil)
50
50
  got_result = false
51
- outbuf = outbuf ? outbuf.replace('') : ''
51
+ outbuf = outbuf ? outbuf.replace("") : ""
52
52
 
53
53
  while io = current_io
54
54
  if result = io.read(length)
55
55
  got_result ||= !result.nil?
56
- result.force_encoding('BINARY') if result.respond_to?(:force_encoding)
56
+ result.force_encoding("BINARY") if result.respond_to?(:force_encoding)
57
57
  outbuf << result
58
58
  length -= result.length if length
59
59
  break if length == 0
@@ -65,7 +65,7 @@ module ChefAPI
65
65
  end
66
66
 
67
67
  def rewind
68
- @ios.each { |io| io.rewind }
68
+ @ios.each(&:rewind)
69
69
  @index = 0
70
70
  end
71
71
 
@@ -99,9 +99,9 @@ module ChefAPI
99
99
  private
100
100
 
101
101
  def build(name, value)
102
- part = %|--#{BOUNDARY}\r\n|
103
- part << %|Content-Disposition: form-data; name="#{CGI.escape(name)}"\r\n\r\n|
104
- part << %|#{value}\r\n|
102
+ part = %{--#{BOUNDARY}\r\n}
103
+ part << %{Content-Disposition: form-data; name="#{CGI.escape(name)}"\r\n\r\n}
104
+ part << %{#{value}\r\n}
105
105
  part
106
106
  end
107
107
  end
@@ -132,14 +132,14 @@ module ChefAPI
132
132
 
133
133
  def build(name, file)
134
134
  filename = File.basename(file.path)
135
- mime_type = MIME::Types.type_for(filename)[0] || MIME::Types['application/octet-stream'][0]
136
-
137
- part = %|--#{BOUNDARY}\r\n|
138
- part << %|Content-Disposition: form-data; name="#{CGI.escape(name)}"; filename="#{filename}"\r\n|
139
- part << %|Content-Length: #{file.size}\r\n|
140
- part << %|Content-Type: #{mime_type.simplified}\r\n|
141
- part << %|Content-Transfer-Encoding: binary\r\n|
142
- part << %|\r\n|
135
+ mime_type = MIME::Types.type_for(filename)[0] || MIME::Types["application/octet-stream"][0]
136
+
137
+ part = %{--#{BOUNDARY}\r\n}
138
+ part << %{Content-Disposition: form-data; name="#{CGI.escape(name)}"; filename="#{filename}"\r\n}
139
+ part << %{Content-Length: #{file.size}\r\n}
140
+ part << %{Content-Type: #{mime_type.simplified}\r\n}
141
+ part << %{Content-Transfer-Encoding: binary\r\n}
142
+ part << %{\r\n}
143
143
  part
144
144
  end
145
145
  end
@@ -1,19 +1,21 @@
1
+ require_relative "aclable"
1
2
  module ChefAPI
2
3
  module Resource
3
- autoload :Base, 'chef-api/resources/base'
4
- autoload :Client, 'chef-api/resources/client'
5
- autoload :CollectionProxy, 'chef-api/resources/collection_proxy'
6
- autoload :Cookbook, 'chef-api/resources/cookbook'
7
- autoload :CookbookVersion, 'chef-api/resources/cookbook_version'
8
- autoload :DataBag, 'chef-api/resources/data_bag'
9
- autoload :DataBagItem, 'chef-api/resources/data_bag_item'
10
- autoload :Environment, 'chef-api/resources/environment'
11
- autoload :Node, 'chef-api/resources/node'
12
- autoload :Organization, 'chef-api/resources/organization'
13
- autoload :PartialSearch, 'chef-api/resources/partial_search'
14
- autoload :Principal, 'chef-api/resources/principal'
15
- autoload :Role, 'chef-api/resources/role'
16
- autoload :Search, 'chef-api/resources/search'
17
- autoload :User, 'chef-api/resources/user'
4
+ autoload :Base, "chef-api/resources/base"
5
+ autoload :Client, "chef-api/resources/client"
6
+ autoload :CollectionProxy, "chef-api/resources/collection_proxy"
7
+ autoload :Cookbook, "chef-api/resources/cookbook"
8
+ autoload :CookbookVersion, "chef-api/resources/cookbook_version"
9
+ autoload :DataBag, "chef-api/resources/data_bag"
10
+ autoload :DataBagItem, "chef-api/resources/data_bag_item"
11
+ autoload :Environment, "chef-api/resources/environment"
12
+ autoload :Group, "chef-api/resources/group"
13
+ autoload :Node, "chef-api/resources/node"
14
+ autoload :Organization, "chef-api/resources/organization"
15
+ autoload :PartialSearch, "chef-api/resources/partial_search"
16
+ autoload :Principal, "chef-api/resources/principal"
17
+ autoload :Role, "chef-api/resources/role"
18
+ autoload :Search, "chef-api/resources/search"
19
+ autoload :User, "chef-api/resources/user"
18
20
  end
19
21
  end
@@ -13,7 +13,7 @@ module ChefAPI
13
13
  # the path to the file on disk
14
14
  #
15
15
  def from_file(path)
16
- raise Error::AbstractMethod.new(method: 'Resource::Base#from_file')
16
+ raise Error::AbstractMethod.new(method: "Resource::Base#from_file")
17
17
  end
18
18
 
19
19
  #
@@ -78,7 +78,7 @@ module ChefAPI
78
78
  # has_many :environments, class_name: 'Environment'
79
79
  #
80
80
  def has_many(method, options = {})
81
- class_name = options[:class_name] || "Resource::#{Util.camelize(method).sub(/s$/, '')}"
81
+ class_name = options[:class_name] || "Resource::#{Util.camelize(method).sub(/s$/, "")}"
82
82
  rest_endpoint = options[:rest_endpoint] || method
83
83
 
84
84
  class_eval <<-EOH, __FILE__, __LINE__ + 1
@@ -227,7 +227,7 @@ module ChefAPI
227
227
  # an array containing the list of resources that were deleted
228
228
  #
229
229
  def destroy_all(prefix = {})
230
- map { |resource| resource.destroy }
230
+ map(&:destroy)
231
231
  end
232
232
 
233
233
  #
@@ -404,8 +404,8 @@ module ChefAPI
404
404
  # an instance of the resource represented by this JSON
405
405
  #
406
406
  def from_json(response, prefix = {})
407
- response.delete('json_class')
408
- response.delete('chef_type')
407
+ response.delete("json_class")
408
+ response.delete("chef_type")
409
409
 
410
410
  new(response, prefix)
411
411
  end
@@ -432,7 +432,7 @@ module ChefAPI
432
432
  # @return [String]
433
433
  #
434
434
  def inspect
435
- "#{classname}(#{schema.attributes.keys.join(', ')})"
435
+ "#{classname}(#{schema.attributes.keys.join(", ")})"
436
436
  end
437
437
 
438
438
  #
@@ -444,7 +444,7 @@ module ChefAPI
444
444
  # @return [String]
445
445
  #
446
446
  def classname
447
- name.split('::')[1..-1].join('::')
447
+ name.split("::")[1..-1].join("::")
448
448
  end
449
449
 
450
450
  #
@@ -456,7 +456,7 @@ module ChefAPI
456
456
  # @return [String]
457
457
  #
458
458
  def type
459
- Util.underscore(name.split('::').last).gsub('_', ' ')
459
+ Util.underscore(name.split("::").last).gsub("_", " ")
460
460
  end
461
461
 
462
462
  #
@@ -482,7 +482,7 @@ module ChefAPI
482
482
  # the path to the resource
483
483
  #
484
484
  def resource_path(id, prefix = {})
485
- [expanded_collection_path(prefix), id].join('/')
485
+ [expanded_collection_path(prefix), id].join("/")
486
486
  end
487
487
 
488
488
  #
@@ -505,7 +505,7 @@ module ChefAPI
505
505
  #
506
506
  def expanded_collection_path(prefix = {})
507
507
  collection_path.gsub(/:\w+/) do |param|
508
- key = param.delete(':')
508
+ key = param.delete(":")
509
509
  value = prefix[key] || prefix[key.to_sym]
510
510
 
511
511
  if value.nil?
@@ -513,7 +513,7 @@ module ChefAPI
513
513
  end
514
514
 
515
515
  URI.escape(value)
516
- end.sub(/^\//, '') # Remove leading slash
516
+ end.sub(%r{^/}, "") # Remove leading slash
517
517
  end
518
518
 
519
519
  #
@@ -522,7 +522,7 @@ module ChefAPI
522
522
  # @return [ChefAPI::Connection]
523
523
  #
524
524
  def connection
525
- Thread.current['chefapi.connection'] || ChefAPI.connection
525
+ Thread.current["chefapi.connection"] || ChefAPI.connection
526
526
  end
527
527
  end
528
528
 
@@ -620,7 +620,7 @@ module ChefAPI
620
620
  # true if the attribute exists, false otherwise
621
621
  #
622
622
  def attribute?(key)
623
- _attributes.has_key?(key.to_sym)
623
+ _attributes.key?(key.to_sym)
624
624
  end
625
625
 
626
626
  #
@@ -633,12 +633,12 @@ module ChefAPI
633
633
  #
634
634
  def protected?
635
635
  @protected ||= self.class.protected_resources.any? do |thing|
636
- if thing.is_a?(Proc)
637
- thing.call(self)
638
- else
639
- id == thing
640
- end
641
- end
636
+ if thing.is_a?(Proc)
637
+ thing.call(self)
638
+ else
639
+ id == thing
640
+ end
641
+ end
642
642
  end
643
643
 
644
644
  #
@@ -775,7 +775,7 @@ module ChefAPI
775
775
  #
776
776
  def validate!
777
777
  unless valid?
778
- sentence = errors.full_messages.join(', ')
778
+ sentence = errors.full_messages.join(", ")
779
779
  raise Error::InvalidResource.new(errors: sentence)
780
780
  end
781
781
 
@@ -896,7 +896,7 @@ module ChefAPI
896
896
  # @return [Boolean]
897
897
  #
898
898
  def ignore_attribute?(key)
899
- @schema.ignored_attributes.has_key?(key.to_sym)
899
+ @schema.ignored_attributes.key?(key.to_sym)
900
900
  end
901
901
 
902
902
  #
@@ -954,7 +954,7 @@ module ChefAPI
954
954
  end
955
955
  end
956
956
 
957
- "#<#{self.class.classname} #{attrs.join(', ')}>"
957
+ "#<#{self.class.classname} #{attrs.join(", ")}>"
958
958
  end
959
959
  end
960
960
  end
@@ -1,6 +1,7 @@
1
1
  module ChefAPI
2
2
  class Resource::Client < Resource::Base
3
- collection_path '/clients'
3
+ include ChefAPI::AclAble
4
+ collection_path "/clients"
4
5
 
5
6
  schema do
6
7
  attribute :name, type: String, primary: true, required: true
@@ -13,7 +14,7 @@ module ChefAPI
13
14
  end
14
15
 
15
16
  # @todo implement
16
- protect 'chef-webui', 'chef-validator'
17
+ protect "chef-webui", "chef-validator"
17
18
 
18
19
  class << self
19
20
  #
@@ -48,7 +49,7 @@ module ChefAPI
48
49
  #
49
50
  def initialize(attributes = {}, prefix = {})
50
51
  if certificate = attributes.delete(:certificate) ||
51
- attributes.delete('certificate')
52
+ attributes.delete("certificate")
52
53
  x509 = OpenSSL::X509::Certificate.new(certificate)
53
54
  attributes[:public_key] = x509.public_key.to_pem
54
55
  end
@@ -76,6 +77,7 @@ module ChefAPI
76
77
  #
77
78
  def regenerate_keys
78
79
  raise Error::CannotRegenerateKey if new_resource?
80
+
79
81
  update(private_key: true).save!
80
82
  self
81
83
  end