chef-api 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +2 -2
  3. data/CHANGELOG.md +17 -0
  4. data/README.md +42 -0
  5. data/chef-api.gemspec +1 -1
  6. data/lib/chef-api/configurable.rb +3 -2
  7. data/lib/chef-api/connection.rb +77 -47
  8. data/lib/chef-api/defaults.rb +28 -8
  9. data/lib/chef-api/errors.rb +32 -3
  10. data/lib/chef-api/resource.rb +1 -0
  11. data/lib/chef-api/resources/base.rb +30 -21
  12. data/lib/chef-api/resources/client.rb +6 -8
  13. data/lib/chef-api/resources/collection_proxy.rb +19 -2
  14. data/lib/chef-api/resources/data_bag.rb +1 -1
  15. data/lib/chef-api/resources/organization.rb +22 -0
  16. data/lib/chef-api/resources/user.rb +72 -1
  17. data/lib/chef-api/schema.rb +59 -21
  18. data/lib/chef-api/version.rb +1 -1
  19. data/lib/chef-api.rb +26 -5
  20. data/spec/integration/resources/client_spec.rb +54 -0
  21. data/spec/integration/resources/user_spec.rb +8 -0
  22. data/spec/spec_helper.rb +1 -1
  23. data/spec/support/chef_server.rb +1 -0
  24. data/spec/unit/errors_spec.rb +294 -0
  25. data/spec/unit/resources/client_spec.rb +0 -16
  26. data/spec/unit/resources/connection_spec.rb +51 -0
  27. data/templates/errors/abstract_method.erb +5 -0
  28. data/templates/errors/cannot_regenerate_key.erb +1 -0
  29. data/templates/errors/chef_api_error.erb +1 -0
  30. data/templates/errors/file_not_found.erb +1 -0
  31. data/templates/errors/http_bad_request.erb +3 -0
  32. data/templates/errors/http_forbidden_request.erb +3 -0
  33. data/templates/errors/http_gateway_timeout.erb +3 -0
  34. data/templates/errors/http_method_not_allowed.erb +3 -0
  35. data/templates/errors/http_not_acceptable.erb +3 -0
  36. data/templates/errors/http_not_found.erb +3 -0
  37. data/templates/errors/http_server_unavailable.erb +1 -0
  38. data/templates/errors/http_unauthorized_request.erb +3 -0
  39. data/templates/errors/insufficient_file_permissions.erb +1 -0
  40. data/templates/errors/invalid_resource.erb +1 -0
  41. data/templates/errors/invalid_validator.erb +1 -0
  42. data/templates/errors/missing_url_parameter.erb +1 -0
  43. data/templates/errors/not_a_directory.erb +1 -0
  44. data/templates/errors/resource_already_exists.erb +1 -0
  45. data/templates/errors/resource_not_found.erb +1 -0
  46. data/templates/errors/resource_not_mutable.erb +1 -0
  47. data/templates/errors/unknown_attribute.erb +1 -0
  48. metadata +43 -17
  49. data/lib/chef-api/logger.rb +0 -160
  50. data/lib/chef-api/proxy.rb +0 -72
  51. data/locales/en.yml +0 -89
@@ -1,160 +0,0 @@
1
- require 'logger'
2
-
3
- module ChefAPI
4
- module Logger
5
- class << self
6
- # @private
7
- def included(base)
8
- base.send(:extend, ClassMethods)
9
- base.send(:include, InstanceMethods)
10
- end
11
-
12
- #
13
- # Get the logger class for the given name. If a logger does not exist
14
- # for the given name, a new one is created.
15
- #
16
- # @param [String] name
17
- # the name of the logger to find
18
- #
19
- # @return [DefaultLogger]
20
- # the logger for the given name
21
- #
22
- def logger_for(name)
23
- loggers[name] ||= DefaultLogger.new(name, level)
24
- end
25
-
26
- #
27
- # The global log level.
28
- #
29
- # @return [Symbol]
30
- #
31
- def level
32
- @level ||= :warn
33
- end
34
-
35
- #
36
- # Set the global log level.
37
- #
38
- # @param [String, Symbol] level
39
- # the log level
40
- #
41
- # @return [Symbol]
42
- #
43
- def level=(level)
44
- @level = level.to_s.downcase.to_sym
45
-
46
- loggers.each do |_, logger|
47
- logger.level = @level
48
- end
49
-
50
- @level
51
- end
52
-
53
- private
54
-
55
- def loggers
56
- @loggers ||= {}
57
- end
58
- end
59
-
60
- module ClassMethods
61
- #
62
- # Write a message to the logger for this class.
63
- #
64
- # @return [DefaultLogger]
65
- #
66
- def log
67
- @log ||= Logger.logger_for(name)
68
- end
69
- end
70
-
71
- module InstanceMethods
72
- #
73
- # Write a message to the logger for this instance's class.
74
- #
75
- # @return [DefaultLogger]
76
- #
77
- def log
78
- @log ||= Logger.logger_for(self.class.name)
79
- end
80
- end
81
-
82
- #
83
- # The default logger for everything logged through the Chef API.
84
- #
85
- class DefaultLogger < ::Logger
86
- class << self
87
- private
88
-
89
- #
90
- # @macro attr_questioner
91
- #
92
- # @method $1?
93
- # Determine if the current logger's level is +:$1+.
94
- #
95
- # @return [Boolean]
96
- # true if the current log level is +:$1+ or lower, false otherwise
97
- #
98
- def attr_questioner(name)
99
- class_eval <<-EOH, __FILE__, __LINE__ + 1
100
- def #{name}?
101
- level == ::Logger::Severity.const_get('#{name}'.upcase)
102
- end
103
- EOH
104
- end
105
- end
106
-
107
- attr_questioner :fatal
108
- attr_questioner :error
109
- attr_questioner :warn
110
- attr_questioner :info
111
- attr_questioner :debug
112
-
113
- #
114
- # Create a new logger with the given name. In debug mode, the +name+ is
115
- # used to identify the caller of the log message. In other modes, it is
116
- # ignored entirely.
117
- #
118
- # @param [String] name
119
- # the name of the class calling the logger
120
- #
121
- def initialize(name, level)
122
- super($stdout)
123
-
124
- @formatter = formatter
125
- @progname = name
126
- self.level = level
127
- end
128
-
129
- #
130
- # Set this logger's level to the given key.
131
- #
132
- # @example Set the log level to +info+
133
- # logger.level = :info
134
- #
135
- # @param [String, Symbol] value
136
- # the value to set for the logger
137
- #
138
- def level=(value)
139
- @level = ::Logger::Severity.const_get(value.to_s.upcase)
140
- end
141
-
142
- private
143
-
144
- def formatter
145
- Proc.new do |severity, timestamp, progname, message|
146
- if debug?
147
- "[#{progname}] #{message}\n"
148
- else
149
- message + "\n"
150
- end
151
- end
152
- end
153
- end
154
-
155
- class NullLogger < DefaultLogger
156
- def initialize(*args); end
157
- def add(*args, &block); end
158
- end
159
- end
160
- end
@@ -1,72 +0,0 @@
1
- module ChefAPI
2
- #
3
- # Create a proxy object, which delegates all methods to the class given in
4
- # the initializer. This is used by the client object to filter the first
5
- # parameter of any singleton methods to pass in the given client as the first
6
- # argument. It's dirty, but it allows developers to write pretty code, which
7
- # is more what I care about.
8
- #
9
- # @example Without a proxy object
10
- # client = ChefAPI::Client.new('...')
11
- # repo = ChefAPI::Resource::Repository.new(client, name: '...')
12
- #
13
- # @example With a proxy object
14
- # client = ChefAPI::Client.new('...')
15
- # repo = client.repositories.new(name: '...')
16
- #
17
- class Proxy < BasicObject
18
- #
19
- # This is quite possibly the worst thing I've ever done in my life.
20
- # Dynamically remove all methods from this class, just we we can delegate
21
- # *everything* to the instance class.
22
- #
23
- # It is presumed there is no hope...
24
- #
25
- instance_methods.each do |name|
26
- unless name =~ /__|object_id|instance_eval/
27
- undef_method name
28
- end
29
- end
30
-
31
- #
32
- # Create a new proxy object.
33
- #
34
- # @param [ChefAPI::Client] client
35
- # the client object to use for the proxy
36
- # @param [Class] klass
37
- # the class to proxy to
38
- #
39
- def initialize(client, klass)
40
- @client = client
41
- @klass = klass
42
-
43
- klass.singleton_methods.each do |name|
44
- instance_eval <<-EOH, __FILE__, __LINE__ + 1
45
- def #{name}(*args)
46
- if args.last.is_a?(::Hash)
47
- args.last[:client] = @client
48
- else
49
- args << { client: @client }
50
- end
51
-
52
- @klass.send(:#{name}, *args)
53
- end
54
- EOH
55
- end
56
- end
57
-
58
- # @private
59
- def method_missing(m, *args, &block)
60
- if @klass.respond_to?(m)
61
- @klass.send(m, *args, &block)
62
- else
63
- super
64
- end
65
- end
66
-
67
- # @private
68
- def respond_to_missing?(m, include_private = false)
69
- @klass.respond_to?(m) || super
70
- end
71
- end
72
- end
data/locales/en.yml DELETED
@@ -1,89 +0,0 @@
1
- en:
2
- chef_api:
3
- errors:
4
- abstract_method: >
5
- `%{method}` is an abstract method. You must override this method in
6
- your subclass with the proper implementation and logic. For more
7
- information, please see the inline documentation for %{method}. If you
8
- are not a developer, this is most likely a bug in the ChefAPI gem.
9
- Please file a bug report at
10
- https://github.com/sethvargo/chef-api/issues/new and include the
11
- command(s) you ran to arrive at this error.
12
- cannot_regenerate_key: >
13
- You called `regenerate_key` on a client that does not exist on the
14
- remote Chef Server. You can only regenerate a private key for a client
15
- that is persisted. Try saving this record this client before
16
- regenerating the private key.
17
- file_not_found: >
18
- I could not find a file at `%{path}`. Please make sure you have typed
19
- the path correctly and that the resource at `%{path}` does actually
20
- exist.
21
- http_bad_request: >
22
- The Chef Server did not understand the request because it was malformed.
23
- The Chef Server returned this message:
24
-
25
- %{message}
26
- http_forbidden_request: >
27
- The Chef Server actively refused to fulfill the request. The Chef Server
28
- returned this message:
29
-
30
- %{message}
31
- http_method_not_allowed: >
32
- That HTTP method is not allowed on this URL. The Chef Server returned
33
- this message:
34
-
35
- %{message}
36
- http_not_acceptable: >
37
- The Chef Server identified this request as unacceptable. This usually
38
- means you have not specified the correct Accept or Content-Type headers
39
- on the request object. The Chef Server returned this message:
40
-
41
- %{message}
42
- http_not_found: >
43
- The requested URL does not exist on the Chef Server. The Chef Server
44
- returned this message:
45
-
46
- %{message}
47
- http_server_unavailable: >
48
- The Chef Server is currently unavailable or is not currently accepting
49
- client connections. Please ensure the server is accessible via ping
50
- or telnet on your local network. If this error persists, please contact
51
- your network administrator.
52
- http_unauthorized_request: >
53
- The Chef Server requires authorization. Please ensure you have specified
54
- the correct client name and private key. If this error continues, please
55
- verify the given client has the proper permissions on the Chef Server.
56
- The Chef Server returned this message:
57
-
58
- %{message}
59
- insufficient_file_permissions: >
60
- I cannot read the file at `%{path}` because the permissions on the file
61
- do not permit it. Please ensure the file has the correct permissions and
62
- that this Ruby process is running as a user with access to `%{path}`.
63
- invalid_resource: >
64
- There were errors saving your resource: %{errors}
65
- invalid_validator: >
66
- `%{key}` is not a valid validator. Please make sure it is spelled
67
- correctly and that the constant is properly defined. If you are using
68
- a custom validator, please ensure the validator extends
69
- ChefAPI::Validator::Base and is a subclass of ChefAPI::Validator.
70
- missing_url_parameter: >
71
- The required URL parameter `%{param}' was not present. Please specify
72
- `%{param}' as an option, like Resource.new(id, %{param}: 'value').
73
- not_a_directory: >
74
- The given path `%{path}' is not a directory. Please make sure you have
75
- passed the path to a directory on disk.
76
- resource_already_exists: >
77
- The %{type} `%{id}` already exists on the Chef Server. Each
78
- %{type} must have a unique identifier and the Chef Server indicated
79
- this %{type} already exists. If you are trying to update the %{type},
80
- consider using the `update` method instead.
81
- resource_not_found: >
82
- There is no %{type} with an id of `%{id}` on the Chef Server. If you
83
- are updating the %{type}, please make sure the %{type} exists and has
84
- the correct Chef identifier (primary key).
85
- resource_not_mutable: >
86
- The %{type} `%{id}` is not mutable. It may be locked by the remote
87
- Chef Server, or the Chef Server may not permit modifying the resource.
88
- unknown_attribute: >
89
- `%{attribute}` is not a valid attribute