summaryrefslogtreecommitdiff
path: root/lib/chef/mixin/versioned_api.rb
diff options
context:
space:
mode:
authorThom May <thom@chef.io>2017-01-18 11:31:19 +0000
committerThom May <thom@chef.io>2017-02-14 10:22:23 -0800
commitd4b87e5e0f73d92836b4523907e17b1a88be9317 (patch)
tree2c1cfe83eedec5c2a9d1c9bff40d07b3a8e75a4f /lib/chef/mixin/versioned_api.rb
parentf76e23dd27410505440eb06a174c6ce8c5afb341 (diff)
downloadchef-d4b87e5e0f73d92836b4523907e17b1a88be9317.tar.gz
Beginning of automatic API version detection
When we make a request to a chef server, we capture the minimum and maximum support API versions and allow them to be queried. We then provide some infrastructure for making decisions on which class should be used, in a middleware-ish mechanism. Signed-off-by: Thom May <thom@chef.io>
Diffstat (limited to 'lib/chef/mixin/versioned_api.rb')
-rw-r--r--lib/chef/mixin/versioned_api.rb49
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/chef/mixin/versioned_api.rb b/lib/chef/mixin/versioned_api.rb
new file mode 100644
index 0000000000..b8ec61c5f0
--- /dev/null
+++ b/lib/chef/mixin/versioned_api.rb
@@ -0,0 +1,49 @@
+#--
+# Copyright:: Copyright 2017, 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 Mixin
+ module VersionedAPI
+
+ def self.included(base)
+ # When this file is mixed in, make sure we also add the class methods
+ base.send :extend, ClassMethods
+ end
+
+ module ClassMethods
+ def versioned_interfaces
+ @versioned_interfaces ||= []
+ end
+
+ def add_api_version(klass)
+ versioned_interfaces << klass
+ end
+ end
+
+ def select_api_version
+ self.class.versioned_interfaces.select do |klass|
+ version = klass.send(:supported_api_version)
+ # min and max versions will be nil if we've not made a request to the server yet,
+ # in which case we'll just start with the highest version and see what happens
+ ServerAPIVersions.instance.min_server_version.nil? || (version >= ServerAPIVersions.instance.min_server_version && version <= ServerAPIVersions.instance.max_server_version)
+ end
+ .sort { |a, b| a.send(:supported_api_version) <=> b.send(:supported_api_version) }
+ .last
+ end
+ end
+ end
+end