chef-api 0.9.0 → 0.10.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/chef-api.rb +19 -20
- data/lib/chef-api/aclable.rb +35 -0
- data/lib/chef-api/authentication.rb +23 -25
- data/lib/chef-api/configurable.rb +14 -14
- data/lib/chef-api/connection.rb +69 -68
- data/lib/chef-api/defaults.rb +25 -24
- data/lib/chef-api/error_collection.rb +1 -1
- data/lib/chef-api/errors.rb +3 -3
- data/lib/chef-api/log.rb +7 -0
- data/lib/chef-api/multipart.rb +17 -17
- data/lib/chef-api/resource.rb +17 -15
- data/lib/chef-api/resources/base.rb +25 -23
- data/lib/chef-api/resources/client.rb +5 -3
- data/lib/chef-api/resources/collection_proxy.rb +4 -3
- data/lib/chef-api/resources/cookbook.rb +2 -2
- data/lib/chef-api/resources/cookbook_version.rb +1 -1
- data/lib/chef-api/resources/data_bag.rb +4 -4
- data/lib/chef-api/resources/data_bag_item.rb +2 -3
- data/lib/chef-api/resources/environment.rb +1 -1
- data/lib/chef-api/resources/group.rb +15 -0
- data/lib/chef-api/resources/node.rb +11 -8
- data/lib/chef-api/resources/organization.rb +2 -2
- data/lib/chef-api/resources/partial_search.rb +4 -4
- data/lib/chef-api/resources/principal.rb +1 -1
- data/lib/chef-api/resources/role.rb +2 -1
- data/lib/chef-api/resources/search.rb +6 -6
- data/lib/chef-api/resources/user.rb +5 -3
- data/lib/chef-api/util.rb +8 -8
- data/lib/chef-api/validator.rb +3 -3
- data/lib/chef-api/validators/base.rb +3 -3
- data/lib/chef-api/validators/required.rb +1 -1
- data/lib/chef-api/validators/type.rb +1 -1
- data/lib/chef-api/version.rb +1 -1
- data/templates/errors/abstract_method.erb +5 -0
- data/templates/errors/cannot_regenerate_key.erb +1 -0
- data/templates/errors/chef_api_error.erb +1 -0
- data/templates/errors/file_not_found.erb +1 -0
- data/templates/errors/http_bad_request.erb +3 -0
- data/templates/errors/http_forbidden_request.erb +3 -0
- data/templates/errors/http_gateway_timeout.erb +3 -0
- data/templates/errors/http_method_not_allowed.erb +3 -0
- data/templates/errors/http_not_acceptable.erb +3 -0
- data/templates/errors/http_not_found.erb +3 -0
- data/templates/errors/http_server_unavailable.erb +1 -0
- data/templates/errors/http_unauthorized_request.erb +3 -0
- data/templates/errors/insufficient_file_permissions.erb +1 -0
- data/templates/errors/invalid_resource.erb +1 -0
- data/templates/errors/invalid_validator.erb +1 -0
- data/templates/errors/missing_url_parameter.erb +1 -0
- data/templates/errors/not_a_directory.erb +1 -0
- data/templates/errors/resource_already_exists.erb +1 -0
- data/templates/errors/resource_not_found.erb +1 -0
- data/templates/errors/resource_not_mutable.erb +1 -0
- data/templates/errors/unknown_attribute.erb +1 -0
- metadata +44 -13
data/lib/chef-api/defaults.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require_relative "version"
|
2
|
+
require "pathname" unless defined?(Pathname)
|
3
|
+
require "json" unless defined?(JSON)
|
4
4
|
|
5
5
|
module ChefAPI
|
6
6
|
module Defaults
|
7
7
|
# Default API endpoint
|
8
|
-
ENDPOINT =
|
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[
|
50
|
-
|
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[
|
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[
|
89
|
-
ENV[
|
90
|
-
elsif config[
|
91
|
-
config[
|
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?(
|
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[
|
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[
|
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
|
-
|
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[
|
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[
|
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[
|
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[
|
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[
|
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[
|
179
|
+
if ENV["CHEF_API_SSL_VERIFY"].nil? && config["CHEF_API_SSL_VERIFY"].nil?
|
179
180
|
true
|
180
181
|
else
|
181
|
-
%w
|
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[
|
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
|
data/lib/chef-api/errors.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "erb" unless defined?(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(
|
34
|
+
class_name = self.class.to_s.split("::").last
|
35
35
|
filename = @filename || Util.underscore(class_name)
|
36
|
-
ChefAPI.root.join(
|
36
|
+
ChefAPI.root.join("templates", "errors", "#{filename}.erb")
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
data/lib/chef-api/log.rb
ADDED
data/lib/chef-api/multipart.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "cgi" unless defined?(CGI)
|
2
|
+
require "mime/types" unless defined?(MIME::Types)
|
3
3
|
|
4
4
|
module ChefAPI
|
5
5
|
module Multipart
|
6
|
-
BOUNDARY =
|
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(
|
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
|
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 =
|
103
|
-
part <<
|
104
|
-
part <<
|
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[
|
136
|
-
|
137
|
-
part =
|
138
|
-
part <<
|
139
|
-
part <<
|
140
|
-
part <<
|
141
|
-
part <<
|
142
|
-
part <<
|
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
|
data/lib/chef-api/resource.rb
CHANGED
@@ -1,19 +1,21 @@
|
|
1
|
+
require_relative "aclable"
|
1
2
|
module ChefAPI
|
2
3
|
module Resource
|
3
|
-
autoload :Base,
|
4
|
-
autoload :Client,
|
5
|
-
autoload :CollectionProxy,
|
6
|
-
autoload :Cookbook,
|
7
|
-
autoload :CookbookVersion,
|
8
|
-
autoload :DataBag,
|
9
|
-
autoload :DataBagItem,
|
10
|
-
autoload :Environment,
|
11
|
-
autoload :
|
12
|
-
autoload :
|
13
|
-
autoload :
|
14
|
-
autoload :
|
15
|
-
autoload :
|
16
|
-
autoload :
|
17
|
-
autoload :
|
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
|
@@ -1,6 +1,8 @@
|
|
1
1
|
module ChefAPI
|
2
2
|
class Resource::Base
|
3
3
|
class << self
|
4
|
+
require "cgi" unless defined?(CGI)
|
5
|
+
|
4
6
|
# Including the Enumberable module gives us magic
|
5
7
|
include Enumerable
|
6
8
|
|
@@ -13,7 +15,7 @@ module ChefAPI
|
|
13
15
|
# the path to the file on disk
|
14
16
|
#
|
15
17
|
def from_file(path)
|
16
|
-
raise Error::AbstractMethod.new(method:
|
18
|
+
raise Error::AbstractMethod.new(method: "Resource::Base#from_file")
|
17
19
|
end
|
18
20
|
|
19
21
|
#
|
@@ -78,7 +80,7 @@ module ChefAPI
|
|
78
80
|
# has_many :environments, class_name: 'Environment'
|
79
81
|
#
|
80
82
|
def has_many(method, options = {})
|
81
|
-
class_name = options[:class_name] || "Resource::#{Util.camelize(method).sub(/s$/,
|
83
|
+
class_name = options[:class_name] || "Resource::#{Util.camelize(method).sub(/s$/, "")}"
|
82
84
|
rest_endpoint = options[:rest_endpoint] || method
|
83
85
|
|
84
86
|
class_eval <<-EOH, __FILE__, __LINE__ + 1
|
@@ -227,7 +229,7 @@ module ChefAPI
|
|
227
229
|
# an array containing the list of resources that were deleted
|
228
230
|
#
|
229
231
|
def destroy_all(prefix = {})
|
230
|
-
map
|
232
|
+
map(&:destroy)
|
231
233
|
end
|
232
234
|
|
233
235
|
#
|
@@ -404,8 +406,8 @@ module ChefAPI
|
|
404
406
|
# an instance of the resource represented by this JSON
|
405
407
|
#
|
406
408
|
def from_json(response, prefix = {})
|
407
|
-
response.delete(
|
408
|
-
response.delete(
|
409
|
+
response.delete("json_class")
|
410
|
+
response.delete("chef_type")
|
409
411
|
|
410
412
|
new(response, prefix)
|
411
413
|
end
|
@@ -432,7 +434,7 @@ module ChefAPI
|
|
432
434
|
# @return [String]
|
433
435
|
#
|
434
436
|
def inspect
|
435
|
-
"#{classname}(#{schema.attributes.keys.join(
|
437
|
+
"#{classname}(#{schema.attributes.keys.join(", ")})"
|
436
438
|
end
|
437
439
|
|
438
440
|
#
|
@@ -444,7 +446,7 @@ module ChefAPI
|
|
444
446
|
# @return [String]
|
445
447
|
#
|
446
448
|
def classname
|
447
|
-
name.split(
|
449
|
+
name.split("::")[1..-1].join("::")
|
448
450
|
end
|
449
451
|
|
450
452
|
#
|
@@ -456,7 +458,7 @@ module ChefAPI
|
|
456
458
|
# @return [String]
|
457
459
|
#
|
458
460
|
def type
|
459
|
-
Util.underscore(name.split(
|
461
|
+
Util.underscore(name.split("::").last).gsub("_", " ")
|
460
462
|
end
|
461
463
|
|
462
464
|
#
|
@@ -482,7 +484,7 @@ module ChefAPI
|
|
482
484
|
# the path to the resource
|
483
485
|
#
|
484
486
|
def resource_path(id, prefix = {})
|
485
|
-
[expanded_collection_path(prefix), id].join(
|
487
|
+
[expanded_collection_path(prefix), id].join("/")
|
486
488
|
end
|
487
489
|
|
488
490
|
#
|
@@ -505,15 +507,15 @@ module ChefAPI
|
|
505
507
|
#
|
506
508
|
def expanded_collection_path(prefix = {})
|
507
509
|
collection_path.gsub(/:\w+/) do |param|
|
508
|
-
key = param.delete(
|
510
|
+
key = param.delete(":")
|
509
511
|
value = prefix[key] || prefix[key.to_sym]
|
510
512
|
|
511
513
|
if value.nil?
|
512
514
|
raise Error::MissingURLParameter.new(param: key)
|
513
515
|
end
|
514
516
|
|
515
|
-
|
516
|
-
end.sub(
|
517
|
+
CGI.escape(value)
|
518
|
+
end.sub(%r{^/}, "") # Remove leading slash
|
517
519
|
end
|
518
520
|
|
519
521
|
#
|
@@ -522,7 +524,7 @@ module ChefAPI
|
|
522
524
|
# @return [ChefAPI::Connection]
|
523
525
|
#
|
524
526
|
def connection
|
525
|
-
Thread.current[
|
527
|
+
Thread.current["chefapi.connection"] || ChefAPI.connection
|
526
528
|
end
|
527
529
|
end
|
528
530
|
|
@@ -620,7 +622,7 @@ module ChefAPI
|
|
620
622
|
# true if the attribute exists, false otherwise
|
621
623
|
#
|
622
624
|
def attribute?(key)
|
623
|
-
_attributes.
|
625
|
+
_attributes.key?(key.to_sym)
|
624
626
|
end
|
625
627
|
|
626
628
|
#
|
@@ -633,12 +635,12 @@ module ChefAPI
|
|
633
635
|
#
|
634
636
|
def protected?
|
635
637
|
@protected ||= self.class.protected_resources.any? do |thing|
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
638
|
+
if thing.is_a?(Proc)
|
639
|
+
thing.call(self)
|
640
|
+
else
|
641
|
+
id == thing
|
642
|
+
end
|
643
|
+
end
|
642
644
|
end
|
643
645
|
|
644
646
|
#
|
@@ -775,7 +777,7 @@ module ChefAPI
|
|
775
777
|
#
|
776
778
|
def validate!
|
777
779
|
unless valid?
|
778
|
-
sentence = errors.full_messages.join(
|
780
|
+
sentence = errors.full_messages.join(", ")
|
779
781
|
raise Error::InvalidResource.new(errors: sentence)
|
780
782
|
end
|
781
783
|
|
@@ -896,7 +898,7 @@ module ChefAPI
|
|
896
898
|
# @return [Boolean]
|
897
899
|
#
|
898
900
|
def ignore_attribute?(key)
|
899
|
-
@schema.ignored_attributes.
|
901
|
+
@schema.ignored_attributes.key?(key.to_sym)
|
900
902
|
end
|
901
903
|
|
902
904
|
#
|
@@ -954,7 +956,7 @@ module ChefAPI
|
|
954
956
|
end
|
955
957
|
end
|
956
958
|
|
957
|
-
"#<#{self.class.classname} #{attrs.join(
|
959
|
+
"#<#{self.class.classname} #{attrs.join(", ")}>"
|
958
960
|
end
|
959
961
|
end
|
960
962
|
end
|