summaryrefslogtreecommitdiff
path: root/lib/chef/mixin
diff options
context:
space:
mode:
authortylercloke <tylercloke@gmail.com>2015-05-27 14:32:24 -0700
committertylercloke <tylercloke@gmail.com>2015-06-05 10:38:48 -0700
commit9b9d376f2e86cd2738c2c2928f77bf48f1dd20ee (patch)
tree226227e7e32f421a0dde0f1ad2046059eff449c3 /lib/chef/mixin
parent20f7e1c78c55d2a16d5033bc2bbe5904d225adb0 (diff)
downloadchef-9b9d376f2e86cd2738c2c2928f77bf48f1dd20ee.tar.gz
Added versioned Chef Object API support code and repaired Chef::User.create.
Also added Chef::User.create V1 API support. Chef::User.create will attempt to use V1 of the server API, and if it fails, it will fall back to V0.
Diffstat (limited to 'lib/chef/mixin')
-rw-r--r--lib/chef/mixin/api_version_request_handling.rb53
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/chef/mixin/api_version_request_handling.rb b/lib/chef/mixin/api_version_request_handling.rb
new file mode 100644
index 0000000000..ea68afc6af
--- /dev/null
+++ b/lib/chef/mixin/api_version_request_handling.rb
@@ -0,0 +1,53 @@
+#
+# Author:: Tyler Cloke (tyler@chef.io)
+# Copyright:: Copyright 2015 Chef Software, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+class Chef
+ module ApiVersionRequestHandling
+ # takes in an http exception, and a min and max supported API version and
+ # handles all the versioning cases
+ #
+ # it will raise an exception if there was a non-versioning related error
+ # or the server and the client are not compatible
+ #
+ # if the server does not support versioning, then it will not raise, and you
+ # can assume API v0 is safe to send
+ def handle_version_http_exception(exception, min_client_supported_version, max_client_supported_version)
+
+ # only rescue 406 Unacceptable
+ return false unless exception.response.code == "406" || !exception.response["x-ops-server-api-version"]
+
+ exception.response["x-ops-server-api-version"]
+
+ # if the version header doesn't exist, just assume API v0
+ if exception.response["x-ops-server-api-version"]
+ header = Chef::JSONCompat.from_json(exception.response["x-ops-server-api-version"])
+ min_server_version = Integer(header["min_version"])
+ max_server_version = Integer(header["max_version"])
+
+ # if the min API version the server supports is greater than the min version the client supports
+ # and the max API version the server supports is less than the max version the client supports
+ if min_server_version > min_client_supported_version || max_server_version < max_client_supported_version
+ # if it had x-ops-server-api-version header, it will error out properly, just raise it
+ return false
+ end
+ end
+ true
+ end
+
+ end
+end