diff options
-rw-r--r-- | lib/chef/api_client.rb | 7 | ||||
-rw-r--r-- | lib/chef/user.rb | 7 | ||||
-rw-r--r-- | lib/chef/versioned_rest.rb | 27 | ||||
-rw-r--r-- | spec/unit/api_client_spec.rb | 22 |
4 files changed, 22 insertions, 41 deletions
diff --git a/lib/chef/api_client.rb b/lib/chef/api_client.rb index 383095b273..d486cc9808 100644 --- a/lib/chef/api_client.rb +++ b/lib/chef/api_client.rb @@ -24,15 +24,14 @@ require 'chef/mash' require 'chef/json_compat' require 'chef/search/query' require 'chef/exceptions' -require 'chef/versioned_rest' require 'chef/mixin/api_version_request_handling' +require 'chef/server_api' class Chef class ApiClient include Chef::Mixin::FromFile include Chef::Mixin::ParamsValidate - include Chef::VersionedRest include Chef::ApiVersionRequestHandling SUPPORTED_API_VERSIONS = [0,1] @@ -48,11 +47,11 @@ class Chef end def chef_rest_v0 - @chef_rest_v0 ||= get_versioned_rest_object(Chef::Config[:chef_server_url], "0") + @chef_rest_v0 ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url], {:api_version => "0"}) end def chef_rest_v1 - @chef_rest_v1 ||= get_versioned_rest_object(Chef::Config[:chef_server_url], "1") + @chef_rest_v1 ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url], {:api_version => "1"}) end def http_api diff --git a/lib/chef/user.rb b/lib/chef/user.rb index 0ec7e87ed3..2ffba18005 100644 --- a/lib/chef/user.rb +++ b/lib/chef/user.rb @@ -21,16 +21,15 @@ require 'chef/mixin/from_file' require 'chef/mash' require 'chef/json_compat' require 'chef/search/query' -require 'chef/versioned_rest' require 'chef/mixin/api_version_request_handling' require 'chef/exceptions' +require 'chef/server_api' class Chef class User include Chef::Mixin::FromFile include Chef::Mixin::ParamsValidate - include Chef::VersionedRest include Chef::ApiVersionRequestHandling SUPPORTED_API_VERSIONS = [0,1] @@ -50,11 +49,11 @@ class Chef end def chef_root_rest_v0 - @chef_root_rest_v0 ||= get_versioned_rest_object(Chef::Config[:chef_server_root], "0") + @chef_root_rest_v0 ||= Chef::ServerAPI.new(Chef::Config[:chef_server_root], {:api_version => "0"}) end def chef_root_rest_v1 - @chef_root_rest_v1 ||= get_versioned_rest_object(Chef::Config[:chef_server_root], "1") + @chef_root_rest_v1 ||= Chef::ServerAPI.new(Chef::Config[:chef_server_root], {:api_version => "1"}) end def username(arg=nil) diff --git a/lib/chef/versioned_rest.rb b/lib/chef/versioned_rest.rb deleted file mode 100644 index b37a7e2249..0000000000 --- a/lib/chef/versioned_rest.rb +++ /dev/null @@ -1,27 +0,0 @@ -# -# 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 VersionedRest - # Helper for getting a sane interface to passing an API version to Chef::REST - # api_version should be a string of an integer - def get_versioned_rest_object(url, api_version) - Chef::REST.new(url, Chef::Config[:node_name], Chef::Config[:client_key], {:api_version => api_version}) - end - end -end diff --git a/spec/unit/api_client_spec.rb b/spec/unit/api_client_spec.rb index e1ffa9252a..a95597d40e 100644 --- a/spec/unit/api_client_spec.rb +++ b/spec/unit/api_client_spec.rb @@ -332,24 +332,34 @@ describe Chef::ApiClient do end context "and the client exists" do + let(:chef_rest_v0_mock) { double('chef rest root v0 object') } + let(:payload) { + {:name => "lost-my-key", :admin => false, :validator => false, :private_key => true} + } + before do @api_client_without_key = Chef::ApiClient.new @api_client_without_key.name("lost-my-key") - expect(@http_client).to receive(:get).with("clients/lost-my-key").and_return(@api_client_without_key) - end + allow(@api_client_without_key).to receive(:chef_rest_v0).and_return(chef_rest_v0_mock) + #allow(@api_client_with_key).to receive(:http_api).and_return(_api_mock) + allow(chef_rest_v0_mock).to receive(:put).with("clients/lost-my-key", payload).and_return(@api_client_with_key) + allow(chef_rest_v0_mock).to receive(:get).with("clients/lost-my-key").and_return(@api_client_without_key) + allow(@http_client).to receive(:get).with("clients/lost-my-key").and_return(@api_client_without_key) + end context "and the client exists on a Chef 11-like server" do before do @api_client_with_key = Chef::ApiClient.new @api_client_with_key.name("lost-my-key") @api_client_with_key.private_key("the new private key") - expect(@http_client).to receive(:put). - with("clients/lost-my-key", :name => "lost-my-key", :admin => false, :validator => false, :private_key => true). - and_return(@api_client_with_key) + allow(@api_client_with_key).to receive(:chef_rest_v0).and_return(chef_rest_v0_mock) end it "returns an ApiClient with a private key" do + expect(chef_rest_v0_mock).to receive(:put).with("clients/lost-my-key", payload). + and_return(@api_client_with_key) + response = Chef::ApiClient.reregister("lost-my-key") # no sane == method for ApiClient :'( expect(response).to eq(@api_client_without_key) @@ -362,7 +372,7 @@ describe Chef::ApiClient do context "and the client exists on a Chef 10-like server" do before do @api_client_with_key = {"name" => "lost-my-key", "private_key" => "the new private key"} - expect(@http_client).to receive(:put). + expect(chef_rest_v0_mock).to receive(:put). with("clients/lost-my-key", :name => "lost-my-key", :admin => false, :validator => false, :private_key => true). and_return(@api_client_with_key) end |