chef-infra-api 0.10.0 → 0.10.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
 
@@ -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,21 +1,21 @@
1
- require "chef-api/aclable"
1
+ require_relative "aclable"
2
2
  module ChefAPI
3
3
  module Resource
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'
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"
20
20
  end
21
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,7 +1,7 @@
1
1
  module ChefAPI
2
2
  class Resource::Client < Resource::Base
3
3
  include ChefAPI::AclAble
4
- collection_path '/clients'
4
+ collection_path "/clients"
5
5
 
6
6
  schema do
7
7
  attribute :name, type: String, primary: true, required: true
@@ -14,7 +14,7 @@ module ChefAPI
14
14
  end
15
15
 
16
16
  # @todo implement
17
- protect 'chef-webui', 'chef-validator'
17
+ protect "chef-webui", "chef-validator"
18
18
 
19
19
  class << self
20
20
  #
@@ -49,7 +49,7 @@ module ChefAPI
49
49
  #
50
50
  def initialize(attributes = {}, prefix = {})
51
51
  if certificate = attributes.delete(:certificate) ||
52
- attributes.delete('certificate')
52
+ attributes.delete("certificate")
53
53
  x509 = OpenSSL::X509::Certificate.new(certificate)
54
54
  attributes[:public_key] = x509.public_key.to_pem
55
55
  end
@@ -77,6 +77,7 @@ module ChefAPI
77
77
  #
78
78
  def regenerate_keys
79
79
  raise Error::CannotRegenerateKey if new_resource?
80
+
80
81
  update(private_key: true).save!
81
82
  self
82
83
  end
@@ -59,6 +59,7 @@ module ChefAPI
59
59
  #
60
60
  def fetch(id)
61
61
  return nil unless exists?(id)
62
+
62
63
  cached(id) { klass.from_url(get(id), prefix) }
63
64
  end
64
65
 
@@ -76,7 +77,7 @@ module ChefAPI
76
77
  # true if the resource exists, false otherwise
77
78
  #
78
79
  def exists?(id)
79
- collection.has_key?(id.to_s)
80
+ collection.key?(id.to_s)
80
81
  end
81
82
 
82
83
  #
@@ -131,9 +132,9 @@ module ChefAPI
131
132
  def inspect
132
133
  objects = collection
133
134
  .map { |id, _| cached(id) || klass.new(klass.schema.primary_key => id) }
134
- .map { |object| object.to_s }
135
+ .map(&:to_s)
135
136
 
136
- "#<#{self.class.name} [#{objects.join(', ')}]>"
137
+ "#<#{self.class.name} [#{objects.join(", ")}]>"
137
138
  end
138
139
 
139
140
  private
@@ -5,7 +5,7 @@ module ChefAPI
5
5
  # of +cookbook_version+ objects that fully detail the layout of a cookbook.
6
6
  #
7
7
  class Resource::Cookbook < Resource::Base
8
- collection_path '/cookbooks'
8
+ collection_path "/cookbooks"
9
9
 
10
10
  schema do
11
11
  attribute :name, type: String, primary: true, required: true
@@ -13,7 +13,7 @@ module ChefAPI
13
13
 
14
14
  has_many :versions,
15
15
  class_name: CookbookVersion,
16
- rest_endpoint: '/?num_versions=all'
16
+ rest_endpoint: "/?num_versions=all"
17
17
 
18
18
  class << self
19
19
  def from_json(response, prefix = {})
@@ -1,6 +1,6 @@
1
1
  module ChefAPI
2
2
  class Resource::CookbookVersion < Resource::Base
3
- collection_path '/cookbooks/:cookbook'
3
+ collection_path "/cookbooks/:cookbook"
4
4
 
5
5
  schema do
6
6
  attribute :name, type: String, primary: true, required: true
@@ -1,6 +1,6 @@
1
1
  module ChefAPI
2
2
  class Resource::DataBag < Resource::Base
3
- collection_path '/data'
3
+ collection_path "/data"
4
4
 
5
5
  schema do
6
6
  attribute :name, type: String, primary: true, required: true
@@ -26,9 +26,9 @@ module ChefAPI
26
26
  # @return [Array<DataBagItem>]
27
27
  #
28
28
  def from_file(path, name = File.basename(path))
29
- path = File.expand_path(path)
29
+ path = File.expand_path(path)
30
30
 
31
- raise Error::FileNotFound.new(path: path) unless File.exists?(path)
31
+ raise Error::FileNotFound.new(path: path) unless File.exist?(path)
32
32
  raise Error::NotADirectory.new(path: path) unless File.directory?(path)
33
33
 
34
34
  raise ArgumentError unless File.directory?(path)
@@ -1,6 +1,6 @@
1
1
  module ChefAPI
2
2
  class Resource::DataBagItem < Resource::Base
3
- collection_path '/data/:bag'
3
+ collection_path "/data/:bag"
4
4
 
5
5
  schema do
6
6
  attribute :id, type: String, primary: true, required: true
@@ -28,11 +28,10 @@ module ChefAPI
28
28
  def initialize(attributes = {}, prefix = {}, bag = nil)
29
29
  @bag = bag || Resource::DataBag.fetch(prefix[:bag])
30
30
 
31
- id = attributes.delete(:id) || attributes.delete('id')
31
+ id = attributes.delete(:id) || attributes.delete("id")
32
32
  super({ id: id, data: attributes }, prefix)
33
33
  end
34
34
 
35
-
36
35
  #
37
36
  # Override the to_hash method to move data to the upper scope.
38
37
  #
@@ -1,6 +1,6 @@
1
1
  module ChefAPI
2
2
  class Resource::Environment < Resource::Base
3
- collection_path '/environments'
3
+ collection_path "/environments"
4
4
 
5
5
  schema do
6
6
  attribute :name, type: String, primary: true, required: true
@@ -1,6 +1,6 @@
1
1
  module ChefAPI
2
2
  class Resource::Group < Resource::Base
3
- collection_path '/groups'
3
+ collection_path "/groups"
4
4
 
5
5
  schema do
6
6
  attribute :groupname, type: String, primary: true, required: true
@@ -13,4 +13,3 @@ module ChefAPI
13
13
  end
14
14
  end
15
15
  end
16
-
@@ -1,7 +1,7 @@
1
1
  module ChefAPI
2
2
  class Resource::Node < Resource::Base
3
3
  include ChefAPI::AclAble
4
- collection_path '/nodes'
4
+ collection_path "/nodes"
5
5
 
6
6
  schema do
7
7
  attribute :name, type: String, primary: true, required: true
@@ -14,7 +14,7 @@ module ChefAPI
14
14
  attribute :policy_group, type: String
15
15
 
16
16
  # Enterprise Chef attributes
17
- attribute :chef_environment, type: String, default: '_default'
17
+ attribute :chef_environment, type: String, default: "_default"
18
18
  end
19
19
  end
20
20
  end