diff options
author | Thom May <thom@chef.io> | 2015-12-02 12:19:33 +0000 |
---|---|---|
committer | Thom May <thom@chef.io> | 2016-01-11 15:40:42 +0000 |
commit | d99e306a41b1402209d320cb7119b12a3bbb962f (patch) | |
tree | f65940702826deb991e6198967d3e9e96cb2857a /lib | |
parent | 1b71aeb423b009f6d1a44215c89e9976957b47e9 (diff) | |
download | chef-d99e306a41b1402209d320cb7119b12a3bbb962f.tar.gz |
Convert all uses of Chef::REST to Chef::ServerAPItm/no_more_rest
In the process, stop auto-expanding JSON in the HTTP client, and let
individual classes control that themselves.
Fixes #2737, Fixes #3518
Diffstat (limited to 'lib')
49 files changed, 313 insertions, 206 deletions
diff --git a/lib/chef/api_client/registration.rb b/lib/chef/api_client/registration.rb index bc941d5bfa..99667acc0a 100644 --- a/lib/chef/api_client/registration.rb +++ b/lib/chef/api_client/registration.rb @@ -17,7 +17,7 @@ # require 'chef/config' -require 'chef/rest' +require 'chef/server_api' require 'chef/exceptions' class Chef @@ -45,7 +45,7 @@ class Chef #-- # If client creation fails with a 5xx, it is retried up to 5 times. These # retries are on top of the retries with randomized exponential backoff - # built in to Chef::REST. The retries here are a workaround for failures + # built in to Chef::ServerAPI. The retries here are a workaround for failures # caused by resource contention in Hosted Chef when creating a very large # number of clients simultaneously, (e.g., spinning up 100s of ec2 nodes # at once). Future improvements to the affected component should make diff --git a/lib/chef/application/windows_service.rb b/lib/chef/application/windows_service.rb index 2f938059ca..9becf4b33f 100644 --- a/lib/chef/application/windows_service.rb +++ b/lib/chef/application/windows_service.rb @@ -23,7 +23,7 @@ require 'chef/client' require 'chef/config' require 'chef/handler/error_report' require 'chef/log' -require 'chef/rest' +require 'chef/http' require 'mixlib/cli' require 'socket' require 'uri' @@ -308,7 +308,7 @@ class Chef begin case config[:config_file] when /^(http|https):\/\// - Chef::REST.new("", nil, nil).fetch(config[:config_file]) { |f| apply_config(f.path) } + Chef::HTTP.new("").streaming_request(config[:config_file]) { |f| apply_config(f.path) } else ::File::open(config[:config_file]) { |f| apply_config(f.path) } end diff --git a/lib/chef/audit/audit_reporter.rb b/lib/chef/audit/audit_reporter.rb index d952d8a249..face24f1f5 100644 --- a/lib/chef/audit/audit_reporter.rb +++ b/lib/chef/audit/audit_reporter.rb @@ -127,10 +127,8 @@ class Chef end Chef::Log.debug "Audit Report:\n#{Chef::JSONCompat.to_json_pretty(run_data)}" - # Since we're posting compressed data we can not directly call post_rest which expects JSON begin - audit_url = rest_client.create_url(audit_history_url) - rest_client.post(audit_url, run_data, headers) + rest_client.post(audit_history_url, run_data, headers) rescue StandardError => e if e.respond_to? :response # 404 error code is OK. This means the version of server we're running against doesn't support diff --git a/lib/chef/chef_fs/file_system/chef_server_root_dir.rb b/lib/chef/chef_fs/file_system/chef_server_root_dir.rb index 09181ac4b4..badb70ce50 100644 --- a/lib/chef/chef_fs/file_system/chef_server_root_dir.rb +++ b/lib/chef/chef_fs/file_system/chef_server_root_dir.rb @@ -104,7 +104,7 @@ class Chef end def chef_rest - Chef::REST.new(chef_server_url, chef_username, chef_private_key) + Chef::ServerAPI.new(chef_server_url, :client_name => chef_username, :signing_key_filename => chef_private_key) end def api_path diff --git a/lib/chef/client.rb b/lib/chef/client.rb index 6ac5cecbdf..ead804879f 100644 --- a/lib/chef/client.rb +++ b/lib/chef/client.rb @@ -22,7 +22,7 @@ require 'chef/config' require 'chef/mixin/params_validate' require 'chef/mixin/path_sanity' require 'chef/log' -require 'chef/rest' +require 'chef/server_api' require 'chef/api_client' require 'chef/api_client/registration' require 'chef/audit/runner' @@ -92,7 +92,7 @@ class Chef # # The rest object used to communicate with the Chef server. # - # @return [Chef::REST] + # @return [Chef::ServerAPI] # attr_reader :rest @@ -575,7 +575,7 @@ class Chef # If Chef::Config.client_key does not exist, we register the client with the # Chef server and fire the registration_start and registration_completed events. # - # @return [Chef::REST] The server connection object. + # @return [Chef::ServerAPI] The server connection object. # # @see Chef::Config#chef_server_url # @see Chef::Config#client_key @@ -601,7 +601,8 @@ class Chef events.registration_completed end # We now have the client key, and should use it from now on. - @rest = Chef::REST.new(config[:chef_server_url], client_name, config[:client_key]) + @rest = Chef::ServerAPI.new(config[:chef_server_url], client_name: client_name, + signing_key_filename: config[:client_key]) register_reporters rescue Exception => e # TODO this should probably only ever fire if we *started* registration. diff --git a/lib/chef/cookbook/remote_file_vendor.rb b/lib/chef/cookbook/remote_file_vendor.rb index 7868430227..b118c75f9e 100644 --- a/lib/chef/cookbook/remote_file_vendor.rb +++ b/lib/chef/cookbook/remote_file_vendor.rb @@ -63,7 +63,7 @@ class Chef # (remote, per manifest), do the update. This will also execute if there # is no current checksum. if current_checksum != found_manifest_record['checksum'] - raw_file = @rest.get_rest(found_manifest_record[:url], true) + raw_file = @rest.get(found_manifest_record[:url], true) Chef::Log.debug("Storing updated #{cache_filename} in the cache.") Chef::FileCache.move_to(raw_file.path, cache_filename) diff --git a/lib/chef/cookbook/synchronizer.rb b/lib/chef/cookbook/synchronizer.rb index fc8e739d73..b499963653 100644 --- a/lib/chef/cookbook/synchronizer.rb +++ b/lib/chef/cookbook/synchronizer.rb @@ -1,5 +1,6 @@ require 'chef/client' require 'chef/util/threaded_job_queue' +require 'chef/server_api' require 'singleton' class Chef @@ -274,7 +275,7 @@ class Chef # downloaded to the path +destination+ which is relative to the Chef file # cache root. def download_file(url, destination) - raw_file = server_api.get_rest(url, true) + raw_file = server_api.streaming_request(url) Chef::Log.info("Storing updated #{destination} in the cache.") cache.move_to(raw_file.path, destination) @@ -286,7 +287,7 @@ class Chef end def server_api - Chef::REST.new(Chef::Config[:chef_server_url]) + Chef::ServerAPI.new(Chef::Config[:chef_server_url]) end end diff --git a/lib/chef/cookbook_uploader.rb b/lib/chef/cookbook_uploader.rb index f24ce2cd56..64a8a4e168 100644 --- a/lib/chef/cookbook_uploader.rb +++ b/lib/chef/cookbook_uploader.rb @@ -9,6 +9,7 @@ require 'chef/cookbook/syntax_check' require 'chef/cookbook/file_system_file_vendor' require 'chef/util/threaded_job_queue' require 'chef/sandbox' +require 'chef/server_api' class Chef class CookbookUploader @@ -31,7 +32,7 @@ class Chef # uploading the cookbook. This allows frozen CookbookVersion # documents on the server to be overwritten (otherwise a 409 is # returned by the server) - # * :rest A Chef::REST object that you have configured the way you like it. + # * :rest A Chef::ServerAPI object that you have configured the way you like it. # If you don't provide this, one will be created using the values # in Chef::Config. # * :concurrency An integer that decided how many threads will be used to @@ -39,7 +40,7 @@ class Chef def initialize(cookbooks, opts={}) @opts = opts @cookbooks = Array(cookbooks) - @rest = opts[:rest] || Chef::REST.new(Chef::Config[:chef_server_url]) + @rest = opts[:rest] || Chef::ServerAPI.new(Chef::Config[:chef_server_url]) @concurrency = opts[:concurrency] || 10 @policy_mode = opts[:policy_mode] || false end diff --git a/lib/chef/cookbook_version.rb b/lib/chef/cookbook_version.rb index 0e9617f98c..4eb118d3bc 100644 --- a/lib/chef/cookbook_version.rb +++ b/lib/chef/cookbook_version.rb @@ -25,6 +25,7 @@ require 'chef/cookbook/metadata' require 'chef/version_class' require 'chef/digester' require 'chef/cookbook_manifest' +require 'chef/server_api' class Chef @@ -459,11 +460,13 @@ class Chef end private :preferences_for_path - def self.json_create(o) - cookbook_version = new(o["cookbook_name"]) + def self.from_hash(o) + cookbook_version = new(o["cookbook_name"] || o["name"]) + # We want the Chef::Cookbook::Metadata object to always be inflated cookbook_version.metadata = Chef::Cookbook::Metadata.from_hash(o["metadata"]) cookbook_version.manifest = o + cookbook_version.identifier = o["identifier"] if o.key?("identifier") # We don't need the following step when we decide to stop supporting deprecated operators in the metadata (e.g. <<, >>) cookbook_version.manifest["metadata"] = Chef::JSONCompat.from_json(Chef::JSONCompat.to_json(cookbook_version.metadata)) @@ -472,13 +475,12 @@ class Chef cookbook_version end + def self.json_create(o) + from_hash(o) + end + def self.from_cb_artifact_data(o) - cookbook_version = new(o["name"]) - # We want the Chef::Cookbook::Metadata object to always be inflated - cookbook_version.metadata = Chef::Cookbook::Metadata.from_hash(o["metadata"]) - cookbook_version.manifest = o - cookbook_version.identifier = o["identifier"] - cookbook_version + from_hash(o) end # @deprecated This method was used by the Ruby Chef Server and is no longer @@ -543,22 +545,22 @@ class Chef end def self.chef_server_rest - Chef::REST.new(Chef::Config[:chef_server_url]) + Chef::ServerAPI.new(Chef::Config[:chef_server_url]) end def destroy - chef_server_rest.delete_rest("cookbooks/#{name}/#{version}") + chef_server_rest.delete("cookbooks/#{name}/#{version}") self end def self.load(name, version="_latest") version = "_latest" if version == "latest" - chef_server_rest.get_rest("cookbooks/#{name}/#{version}") + from_hash(chef_server_rest.get("cookbooks/#{name}/#{version}")) end # The API returns only a single version of each cookbook in the result from the cookbooks method def self.list - chef_server_rest.get_rest('cookbooks') + chef_server_rest.get('cookbooks') end # Alias latest_cookbooks as list @@ -567,7 +569,7 @@ class Chef end def self.list_all_versions - chef_server_rest.get_rest('cookbooks?num_versions=all') + chef_server_rest.get('cookbooks?num_versions=all') end ## @@ -577,7 +579,7 @@ class Chef # [String]:: Array of cookbook versions, which are strings like 'x.y.z' # nil:: if the cookbook doesn't exist. an error will also be logged. def self.available_versions(cookbook_name) - chef_server_rest.get_rest("cookbooks/#{cookbook_name}")[cookbook_name]["versions"].map do |cb| + chef_server_rest.get("cookbooks/#{cookbook_name}")[cookbook_name]["versions"].map do |cb| cb["version"] end rescue Net::HTTPServerException => e diff --git a/lib/chef/data_bag.rb b/lib/chef/data_bag.rb index 401ba6f63f..9d0dc53da5 100644 --- a/lib/chef/data_bag.rb +++ b/lib/chef/data_bag.rb @@ -24,6 +24,7 @@ require 'chef/mixin/from_file' require 'chef/data_bag_item' require 'chef/mash' require 'chef/json_compat' +require 'chef/server_api' class Chef class DataBag @@ -70,15 +71,19 @@ class Chef end def chef_server_rest - @chef_server_rest ||= Chef::REST.new(Chef::Config[:chef_server_url]) + @chef_server_rest ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url]) end def self.chef_server_rest - Chef::REST.new(Chef::Config[:chef_server_url]) + Chef::ServerAPI.new(Chef::Config[:chef_server_url]) end # Create a Chef::Role from JSON def self.json_create(o) + from_hash(o) + end + + def self.from_hash(o) bag = new bag.name(o["name"]) bag @@ -104,7 +109,7 @@ class Chef response end else - Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("data") + Chef::ServerAPI.new(Chef::Config[:chef_server_url]).get("data") end end end @@ -120,7 +125,7 @@ class Chef end Dir.glob(File.join(Chef::Util::PathHelper.escape_glob(path, name.to_s), "*.json")).inject({}) do |bag, f| - item = Chef::JSONCompat.from_json(IO.read(f)) + item = Chef::JSONCompat.parse(IO.read(f)) # Check if we have multiple items with similar names (ids) and raise if their content differs if data_bag.has_key?(item["id"]) && data_bag[item["id"]] != item @@ -132,12 +137,12 @@ class Chef end return data_bag else - Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("data/#{name}") + Chef::ServerAPI.new(Chef::Config[:chef_server_url]).get("data/#{name}") end end def destroy - chef_server_rest.delete_rest("data/#{@name}") + chef_server_rest.delete("data/#{@name}") end # Save the Data Bag via RESTful API @@ -156,7 +161,7 @@ class Chef #create a data bag via RESTful API def create - chef_server_rest.post_rest("data", self) + chef_server_rest.post("data", self) self end diff --git a/lib/chef/data_bag_item.rb b/lib/chef/data_bag_item.rb index 31c9b69330..7ef9fffe07 100644 --- a/lib/chef/data_bag_item.rb +++ b/lib/chef/data_bag_item.rb @@ -25,6 +25,7 @@ require 'chef/mixin/params_validate' require 'chef/mixin/from_file' require 'chef/data_bag' require 'chef/mash' +require 'chef/server_api' require 'chef/json_compat' class Chef @@ -58,11 +59,11 @@ class Chef end def chef_server_rest - @chef_server_rest ||= Chef::REST.new(Chef::Config[:chef_server_url]) + @chef_server_rest ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url]) end def self.chef_server_rest - Chef::REST.new(Chef::Config[:chef_server_url]) + Chef::ServerAPI.new(Chef::Config[:chef_server_url]) end def raw_data @@ -125,22 +126,23 @@ class Chef end def self.from_hash(h) + h.delete("chef_type") + h.delete("json_class") + h.delete("name") + item = new - item.raw_data = h + item.data_bag(h.delete("data_bag")) if h.key?("data_bag") + if h.key?("raw_data") + item.raw_data = Mash.new(h["raw_data"]) + else + item.raw_data = h + end item end # Create a Chef::DataBagItem from JSON def self.json_create(o) - bag_item = new - bag_item.data_bag(o["data_bag"]) - o.delete("data_bag") - o.delete("chef_type") - o.delete("json_class") - o.delete("name") - - bag_item.raw_data = Mash.new(o["raw_data"]) - bag_item + from_hash(o) end # Load a Data Bag Item by name via either the RESTful API or local data_bag_path if run in solo mode @@ -149,7 +151,7 @@ class Chef bag = Chef::DataBag.load(data_bag) item = bag[name] else - item = Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("data/#{data_bag}/#{name}") + item = Chef::ServerAPI.new(Chef::Config[:chef_server_url]).get("data/#{data_bag}/#{name}") end if item.kind_of?(DataBagItem) @@ -162,7 +164,7 @@ class Chef end def destroy(data_bag=data_bag(), databag_item=name) - chef_server_rest.delete_rest("data/#{data_bag}/#{databag_item}") + chef_server_rest.delete("data/#{data_bag}/#{databag_item}") end # Save this Data Bag Item via RESTful API @@ -172,18 +174,18 @@ class Chef if Chef::Config[:why_run] Chef::Log.warn("In why-run mode, so NOT performing data bag item save.") else - r.put_rest("data/#{data_bag}/#{item_id}", self) + r.put("data/#{data_bag}/#{item_id}", self) end rescue Net::HTTPServerException => e raise e unless e.response.code == "404" - r.post_rest("data/#{data_bag}", self) + r.post("data/#{data_bag}", self) end self end # Create this Data Bag Item via RESTful API def create - chef_server_rest.post_rest("data/#{data_bag}", self) + chef_server_rest.post("data/#{data_bag}", self) self end diff --git a/lib/chef/environment.rb b/lib/chef/environment.rb index 7d4b410639..5612978a08 100644 --- a/lib/chef/environment.rb +++ b/lib/chef/environment.rb @@ -24,6 +24,7 @@ require 'chef/mash' require 'chef/mixin/params_validate' require 'chef/mixin/from_file' require 'chef/version_constraint' +require 'chef/server_api' class Chef class Environment @@ -47,11 +48,11 @@ class Chef end def chef_server_rest - @chef_server_rest ||= Chef::REST.new(Chef::Config[:chef_server_url]) + @chef_server_rest ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url]) end def self.chef_server_rest - Chef::REST.new(Chef::Config[:chef_server_url]) + Chef::ServerAPI.new(Chef::Config[:chef_server_url]) end def name(arg=nil) @@ -216,6 +217,10 @@ class Chef end def self.json_create(o) + from_hash(o) + end + + def self.from_hash(o) environment = new environment.name(o["name"]) environment.description(o["description"]) @@ -233,7 +238,7 @@ class Chef end response else - chef_server_rest.get_rest("environments") + chef_server_rest.get("environments") end end @@ -241,7 +246,7 @@ class Chef if Chef::Config[:solo] load_from_file(name) else - chef_server_rest.get_rest("environments/#{name}") + chef_server_rest.get("environments/#{name}") end end @@ -267,26 +272,26 @@ class Chef end def destroy - chef_server_rest.delete_rest("environments/#{@name}") + chef_server_rest.delete("environments/#{@name}") end def save begin - chef_server_rest.put_rest("environments/#{@name}", self) + chef_server_rest.put("environments/#{@name}", self) rescue Net::HTTPServerException => e raise e unless e.response.code == "404" - chef_server_rest.post_rest("environments", self) + chef_server_rest.post("environments", self) end self end def create - chef_server_rest.post_rest("environments", self) + chef_server_rest.post("environments", self) self end def self.load_filtered_recipe_list(environment) - chef_server_rest.get_rest("environments/#{environment}/recipes") + chef_server_rest.get("environments/#{environment}/recipes") end def to_s diff --git a/lib/chef/http/simple.rb b/lib/chef/http/simple.rb index 8519554f2b..f59fcaa08b 100644 --- a/lib/chef/http/simple.rb +++ b/lib/chef/http/simple.rb @@ -1,3 +1,21 @@ +# +# Author:: Daniel DeLeo (<dan@chef.io>) +# Copyright:: Copyright (c) 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. +# + require 'chef/http' require 'chef/http/authenticator' require 'chef/http/decompressor' diff --git a/lib/chef/http/simple_json.rb b/lib/chef/http/simple_json.rb new file mode 100644 index 0000000000..5dfdfbb680 --- /dev/null +++ b/lib/chef/http/simple_json.rb @@ -0,0 +1,43 @@ +# +# Author:: Thom May (<thom@chef.io>) +# Copyright:: Copyright (c) 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. +# + +require 'chef/http' +require 'chef/http/authenticator' +require 'chef/http/decompressor' +require 'chef/http/cookie_manager' +require 'chef/http/validate_content_length' + +class Chef + class HTTP + + class SimpleJSON < HTTP + + use JSONInput + use JSONOutput + use CookieManager + use Decompressor + use RemoteRequestID + + # ValidateContentLength should come after Decompressor + # because the order of middlewares is reversed when handling + # responses. + use ValidateContentLength + + end + end +end diff --git a/lib/chef/key.rb b/lib/chef/key.rb index be4be7f230..47bfe1fcee 100644 --- a/lib/chef/key.rb +++ b/lib/chef/key.rb @@ -19,6 +19,7 @@ require 'chef/json_compat' require 'chef/mixin/params_validate' require 'chef/exceptions' +require 'chef/server_api' class Chef # Class for interacting with a chef key object. Can be used to create new keys, @@ -31,7 +32,7 @@ class Chef # @attr [String] public_key the RSA string of this key # @attr [String] private_key the RSA string of the private key if returned via a POST or PUT # @attr [String] expiration_date the ISO formatted string YYYY-MM-DDTHH:MM:SSZ, i.e. 2020-12-24T21:00:00Z - # @attr [String] rest Chef::REST object, initialized and cached via chef_rest method + # @attr [String] rest Chef::ServerAPI object, initialized and cached via chef_rest method # @attr [string] api_base either "users" or "clients", initialized and cached via api_base method # # @attr_reader [String] actor_field_name must be either 'client' or 'user' @@ -60,9 +61,9 @@ class Chef def chef_rest @rest ||= if @actor_field_name == "user" - Chef::REST.new(Chef::Config[:chef_server_root]) + Chef::ServerAPI.new(Chef::Config[:chef_server_root]) else - Chef::REST.new(Chef::Config[:chef_server_url]) + Chef::ServerAPI.new(Chef::Config[:chef_server_url]) end end @@ -151,7 +152,7 @@ class Chef payload['public_key'] = @public_key unless @public_key.nil? payload['create_key'] = @create_key if @create_key payload['expiration_date'] = @expiration_date unless @expiration_date.nil? - result = chef_rest.post_rest("#{api_base}/#{@actor}/keys", payload) + result = chef_rest.post("#{api_base}/#{@actor}/keys", payload) # append the private key to the current key if the server returned one, # since the POST endpoint just returns uri and private_key if needed. new_key = self.to_hash @@ -174,7 +175,7 @@ class Chef # to @name. put_name = @name if put_name.nil? - new_key = chef_rest.put_rest("#{api_base}/#{@actor}/keys/#{put_name}", to_hash) + new_key = chef_rest.put("#{api_base}/#{@actor}/keys/#{put_name}", to_hash) # if the server returned a public_key, remove the create_key field, as we now have a key if new_key["public_key"] self.delete_create_key @@ -197,7 +198,7 @@ class Chef raise Chef::Exceptions::MissingKeyAttribute, "the name field must be populated when delete is called" end - chef_rest.delete_rest("#{api_base}/#{@actor}/keys/#{@name}") + chef_rest.delete("#{api_base}/#{@actor}/keys/#{@name}") end # Class methods @@ -226,22 +227,22 @@ class Chef end def self.list_by_user(actor, inflate=false) - keys = Chef::REST.new(Chef::Config[:chef_server_root]).get_rest("users/#{actor}/keys") + keys = Chef::ServerAPI.new(Chef::Config[:chef_server_root]).get("users/#{actor}/keys") self.list(keys, actor, :load_by_user, inflate) end def self.list_by_client(actor, inflate=false) - keys = Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("clients/#{actor}/keys") + keys = Chef::ServerAPI.new(Chef::Config[:chef_server_url]).get("clients/#{actor}/keys") self.list(keys, actor, :load_by_client, inflate) end def self.load_by_user(actor, key_name) - response = Chef::REST.new(Chef::Config[:chef_server_root]).get_rest("users/#{actor}/keys/#{key_name}") + response = Chef::ServerAPI.new(Chef::Config[:chef_server_root]).get("users/#{actor}/keys/#{key_name}") Chef::Key.from_hash(response.merge({"user" => actor})) end def self.load_by_client(actor, key_name) - response = Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("clients/#{actor}/keys/#{key_name}") + response = Chef::ServerAPI.new(Chef::Config[:chef_server_url]).get("clients/#{actor}/keys/#{key_name}") Chef::Key.from_hash(response.merge({"client" => actor})) end diff --git a/lib/chef/knife.rb b/lib/chef/knife.rb index 2820f58e85..34e437c82f 100644 --- a/lib/chef/knife.rb +++ b/lib/chef/knife.rb @@ -26,14 +26,16 @@ require 'chef/mixin/path_sanity' require 'chef/knife/core/subcommand_loader' require 'chef/knife/core/ui' require 'chef/local_mode' -require 'chef/rest' +require 'chef/server_api' require 'chef/http/authenticator' +require 'chef/http/http_request' +require 'chef/http' require 'pp' class Chef class Knife - Chef::REST::RESTRequest.user_agent = "Chef Knife#{Chef::REST::RESTRequest::UA_COMMON}" + Chef::HTTP::HTTPRequest.user_agent = "Chef Knife#{Chef::HTTP::HTTPRequest::UA_COMMON}" include Mixlib::CLI include Chef::Mixin::PathSanity @@ -551,15 +553,15 @@ class Chef def rest @rest ||= begin - require 'chef/rest' - Chef::REST.new(Chef::Config[:chef_server_url]) + require 'chef/server_api' + Chef::ServerAPI.new(Chef::Config[:chef_server_url]) end end def noauth_rest @rest ||= begin - require 'chef/rest' - Chef::REST.new(Chef::Config[:chef_server_url], false, false) + require 'chef/http/simple_json' + Chef::HTTP::SimpleJSON.new(Chef::Config[:chef_server_url]) end end diff --git a/lib/chef/knife/bootstrap/client_builder.rb b/lib/chef/knife/bootstrap/client_builder.rb index 6414ac5c72..f5a2ff2bb1 100644 --- a/lib/chef/knife/bootstrap/client_builder.rb +++ b/lib/chef/knife/bootstrap/client_builder.rb @@ -17,7 +17,7 @@ # require 'chef/node' -require 'chef/rest' +require 'chef/server_api' require 'chef/api_client/registration' require 'chef/api_client' require 'chef/knife/bootstrap' @@ -185,22 +185,22 @@ class Chef # @param relative_path [String] URI path relative to the chef organization # @return [Boolean] if the relative path exists or returns a 404 def resource_exists?(relative_path) - rest.get_rest(relative_path) + rest.get(relative_path) true rescue Net::HTTPServerException => e raise unless e.response.code == "404" false end - # @return [Chef::REST] REST client using the client credentials + # @return [Chef::ServerAPI] REST client using the client credentials def client_rest - @client_rest ||= Chef::REST.new(chef_server_url, node_name, client_path) + @client_rest ||= Chef::ServerAPI.new(chef_server_url, :client_name => node_name, :signing_key_filename => client_path) end - # @return [Chef::REST] REST client using the cli user's knife credentials + # @return [Chef::ServerAPI] REST client using the cli user's knife credentials # this uses the users's credentials def rest - @rest ||= Chef::REST.new(chef_server_url) + @rest ||= Chef::ServerAPI.new(chef_server_url) end end end diff --git a/lib/chef/knife/cookbook_bulk_delete.rb b/lib/chef/knife/cookbook_bulk_delete.rb index 65fa888486..ec0d06937f 100644 --- a/lib/chef/knife/cookbook_bulk_delete.rb +++ b/lib/chef/knife/cookbook_bulk_delete.rb @@ -60,9 +60,9 @@ class Chef cookbooks_names.each do |cookbook_name| - versions = rest.get_rest("cookbooks/#{cookbook_name}")[cookbook_name]["versions"].map {|v| v["version"]}.flatten + versions = rest.get("cookbooks/#{cookbook_name}")[cookbook_name]["versions"].map {|v| v["version"]}.flatten versions.each do |version| - object = rest.delete_rest("cookbooks/#{cookbook_name}/#{version}#{config[:purge] ? "?purge=true" : ""}") + object = rest.delete("cookbooks/#{cookbook_name}/#{version}#{config[:purge] ? "?purge=true" : ""}") ui.info("Deleted cookbook #{cookbook_name.ljust(25)} [#{version}]") end end diff --git a/lib/chef/knife/cookbook_delete.rb b/lib/chef/knife/cookbook_delete.rb index f436d270bd..5fe0e9664d 100644 --- a/lib/chef/knife/cookbook_delete.rb +++ b/lib/chef/knife/cookbook_delete.rb @@ -85,7 +85,7 @@ class Chef end def available_versions - @available_versions ||= rest.get_rest("cookbooks/#{@cookbook_name}").map do |name, url_and_version| + @available_versions ||= rest.get("cookbooks/#{@cookbook_name}").map do |name, url_and_version| url_and_version["versions"].map {|url_by_version| url_by_version["version"]} end.flatten rescue Net::HTTPServerException => e @@ -143,7 +143,7 @@ class Chef def delete_request(path) path += "?purge=true" if config[:purge] - rest.delete_rest(path) + rest.delete(path) end end diff --git a/lib/chef/knife/cookbook_download.rb b/lib/chef/knife/cookbook_download.rb index cb8eeb8edf..6ba5fc7d6c 100644 --- a/lib/chef/knife/cookbook_download.rb +++ b/lib/chef/knife/cookbook_download.rb @@ -69,7 +69,7 @@ class Chef ui.info("Downloading #{@cookbook_name} cookbook version #{@version}") - cookbook = rest.get_rest("cookbooks/#{@cookbook_name}/#{@version}") + cookbook = Chef::CookbookVersion.load(@cookbook_name, @version) manifest = cookbook.manifest basedir = File.join(config[:download_directory], "#{@cookbook_name}-#{cookbook.version}") @@ -90,8 +90,7 @@ class Chef dest = File.join(basedir, segment_file['path'].gsub('/', File::SEPARATOR)) Chef::Log.debug("Downloading #{segment_file['path']} to #{dest}") FileUtils.mkdir_p(File.dirname(dest)) - rest.sign_on_redirect = false - tempfile = rest.get_rest(segment_file['url'], true) + tempfile = rest.streaming_request(segment_file['url']) FileUtils.mv(tempfile.path, dest) end end diff --git a/lib/chef/knife/cookbook_list.rb b/lib/chef/knife/cookbook_list.rb index 75f18a154b..dd78e854da 100644 --- a/lib/chef/knife/cookbook_list.rb +++ b/lib/chef/knife/cookbook_list.rb @@ -39,7 +39,7 @@ class Chef env = config[:environment] num_versions = config[:all_versions] ? "num_versions=all" : "num_versions=1" api_endpoint = env ? "/environments/#{env}/cookbooks?#{num_versions}" : "/cookbooks?#{num_versions}" - cookbook_versions = rest.get_rest(api_endpoint) + cookbook_versions = rest.get(api_endpoint) ui.output(format_cookbook_list_for_display(cookbook_versions)) end end diff --git a/lib/chef/knife/cookbook_show.rb b/lib/chef/knife/cookbook_show.rb index 7c9cbebdb1..07f7684c27 100644 --- a/lib/chef/knife/cookbook_show.rb +++ b/lib/chef/knife/cookbook_show.rb @@ -67,9 +67,9 @@ class Chef cookbook_name, segment, filename = @name_args[0], @name_args[2], @name_args[3] cookbook_version = @name_args[1] == 'latest' ? '_latest' : @name_args[1] - cookbook = rest.get_rest("cookbooks/#{cookbook_name}/#{cookbook_version}") + cookbook = rest.get("cookbooks/#{cookbook_name}/#{cookbook_version}") manifest_entry = cookbook.preferred_manifest_record(node, segment, filename) - temp_file = rest.get_rest(manifest_entry[:url], true) + temp_file = rest.get(manifest_entry[:url], true) # the temp file is cleaned up elsewhere temp_file.open if temp_file.closed? @@ -77,16 +77,16 @@ class Chef when 3 # We are showing a specific part of the cookbook cookbook_version = @name_args[1] == 'latest' ? '_latest' : @name_args[1] - result = rest.get_rest("cookbooks/#{@name_args[0]}/#{cookbook_version}") + result = rest.get("cookbooks/#{@name_args[0]}/#{cookbook_version}") output(result.manifest[@name_args[2]]) when 2 # We are showing the whole cookbook data cookbook_version = @name_args[1] == 'latest' ? '_latest' : @name_args[1] - output(rest.get_rest("cookbooks/#{@name_args[0]}/#{cookbook_version}")) + output(rest.get("cookbooks/#{@name_args[0]}/#{cookbook_version}")) when 1 # We are showing the cookbook versions (all of them) cookbook_name = @name_args[0] env = config[:environment] api_endpoint = env ? "environments/#{env}/cookbooks/#{cookbook_name}" : "cookbooks/#{cookbook_name}" - output(format_cookbook_list_for_display(rest.get_rest(api_endpoint))) + output(format_cookbook_list_for_display(rest.get(api_endpoint))) when 0 show_usage ui.fatal("You must specify a cookbook name") diff --git a/lib/chef/knife/cookbook_site_download.rb b/lib/chef/knife/cookbook_site_download.rb index 3e586e6542..72608f3a30 100644 --- a/lib/chef/knife/cookbook_site_download.rb +++ b/lib/chef/knife/cookbook_site_download.rb @@ -63,7 +63,7 @@ class Chef def current_cookbook_data @current_cookbook_data ||= begin - noauth_rest.get_rest "#{cookbooks_api_url}/#{@name_args[0]}" + noauth_rest.get "#{cookbooks_api_url}/#{@name_args[0]}" end end @@ -79,14 +79,14 @@ class Chef specific_cookbook_version_url end - noauth_rest.get_rest uri + noauth_rest.get uri end end def download_cookbook ui.info "Downloading #{@name_args[0]} from Supermarket at version #{version} to #{download_location}" noauth_rest.sign_on_redirect = false - tf = noauth_rest.get_rest desired_cookbook_data["file"], true + tf = noauth_rest.get desired_cookbook_data["file"], true ::FileUtils.cp tf.path, download_location ui.info "Cookbook saved: #{download_location}" diff --git a/lib/chef/knife/cookbook_site_list.rb b/lib/chef/knife/cookbook_site_list.rb index 846123c867..b5354ed6e6 100644 --- a/lib/chef/knife/cookbook_site_list.rb +++ b/lib/chef/knife/cookbook_site_list.rb @@ -42,7 +42,7 @@ class Chef def get_cookbook_list(items=10, start=0, cookbook_collection={}) cookbooks_url = "https://supermarket.chef.io/api/v1/cookbooks?items=#{items}&start=#{start}" - cr = noauth_rest.get_rest(cookbooks_url) + cr = noauth_rest.get(cookbooks_url) cr["items"].each do |cookbook| cookbook_collection[cookbook["cookbook_name"]] = cookbook end diff --git a/lib/chef/knife/cookbook_site_search.rb b/lib/chef/knife/cookbook_site_search.rb index 0baaf90f1c..decbf6c2c3 100644 --- a/lib/chef/knife/cookbook_site_search.rb +++ b/lib/chef/knife/cookbook_site_search.rb @@ -30,7 +30,7 @@ class Chef def search_cookbook(query, items=10, start=0, cookbook_collection={}) cookbooks_url = "https://supermarket.chef.io/api/v1/search?q=#{query}&items=#{items}&start=#{start}" - cr = noauth_rest.get_rest(cookbooks_url) + cr = noauth_rest.get(cookbooks_url) cr["items"].each do |cookbook| cookbook_collection[cookbook["cookbook_name"]] = cookbook end diff --git a/lib/chef/knife/cookbook_site_share.rb b/lib/chef/knife/cookbook_site_share.rb index beb98b71b8..043ca84a58 100644 --- a/lib/chef/knife/cookbook_site_share.rb +++ b/lib/chef/knife/cookbook_site_share.rb @@ -108,7 +108,7 @@ class Chef def get_category(cookbook_name) begin - data = noauth_rest.get_rest("https://supermarket.chef.io/api/v1/cookbooks/#{@name_args[0]}") + data = noauth_rest.get("https://supermarket.chef.io/api/v1/cookbooks/#{@name_args[0]}") if !data["category"] && data["error_code"] ui.fatal("Received an error from Supermarket: #{data["error_code"]}. On the first time you upload it, you are required to specify the category you want to share this cookbook to.") exit(1) diff --git a/lib/chef/knife/cookbook_site_show.rb b/lib/chef/knife/cookbook_site_show.rb index 6b65b62570..521a60eb36 100644 --- a/lib/chef/knife/cookbook_site_show.rb +++ b/lib/chef/knife/cookbook_site_show.rb @@ -31,15 +31,15 @@ class Chef def get_cookbook_data case @name_args.length when 1 - noauth_rest.get_rest("https://supermarket.chef.io/api/v1/cookbooks/#{@name_args[0]}") + noauth_rest.get("https://supermarket.chef.io/api/v1/cookbooks/#{@name_args[0]}") when 2 - noauth_rest.get_rest("https://supermarket.chef.io/api/v1/cookbooks/#{@name_args[0]}/versions/#{name_args[1].gsub('.', '_')}") + noauth_rest.get("https://supermarket.chef.io/api/v1/cookbooks/#{@name_args[0]}/versions/#{name_args[1].gsub('.', '_')}") end end def get_cookbook_list(items=10, start=0, cookbook_collection={}) cookbooks_url = "https://supermarket.chef.io/api/v1/cookbooks?items=#{items}&start=#{start}" - cr = noauth_rest.get_rest(cookbooks_url) + cr = noauth_rest.get(cookbooks_url) cr["items"].each do |cookbook| cookbook_collection[cookbook["cookbook_name"]] = cookbook end diff --git a/lib/chef/knife/cookbook_site_unshare.rb b/lib/chef/knife/cookbook_site_unshare.rb index 77bb18322c..0c196c328a 100644 --- a/lib/chef/knife/cookbook_site_unshare.rb +++ b/lib/chef/knife/cookbook_site_unshare.rb @@ -41,7 +41,7 @@ class Chef confirm "Do you really want to unshare all versions of the cookbook #{@cookbook_name}" begin - rest.delete_rest "https://supermarket.chef.io/api/v1/cookbooks/#{@name_args[0]}" + rest.delete "https://supermarket.chef.io/api/v1/cookbooks/#{@name_args[0]}" rescue Net::HTTPServerException => e raise e unless e.message =~ /Forbidden/ ui.error "Forbidden: You must be the maintainer of #{@cookbook_name} to unshare it." diff --git a/lib/chef/knife/data_bag_create.rb b/lib/chef/knife/data_bag_create.rb index f8a7619a8a..1becad88b9 100644 --- a/lib/chef/knife/data_bag_create.rb +++ b/lib/chef/knife/data_bag_create.rb @@ -51,7 +51,7 @@ class Chef # create the data bag begin - rest.post_rest("data", { "name" => @data_bag_name }) + rest.post("data", { "name" => @data_bag_name }) ui.info("Created data_bag[#{@data_bag_name}]") rescue Net::HTTPServerException => e raise unless e.to_s =~ /^409/ @@ -68,7 +68,7 @@ class Chef output end) item.data_bag(@data_bag_name) - rest.post_rest("data/#{@data_bag_name}", item) + rest.post("data/#{@data_bag_name}", item) end end end diff --git a/lib/chef/knife/data_bag_delete.rb b/lib/chef/knife/data_bag_delete.rb index 575e9d604d..a3215d4c54 100644 --- a/lib/chef/knife/data_bag_delete.rb +++ b/lib/chef/knife/data_bag_delete.rb @@ -32,11 +32,11 @@ class Chef def run if @name_args.length == 2 delete_object(Chef::DataBagItem, @name_args[1], "data_bag_item") do - rest.delete_rest("data/#{@name_args[0]}/#{@name_args[1]}") + rest.delete("data/#{@name_args[0]}/#{@name_args[1]}") end elsif @name_args.length == 1 delete_object(Chef::DataBag, @name_args[0], "data_bag") do - rest.delete_rest("data/#{@name_args[0]}") + rest.delete("data/#{@name_args[0]}") end else show_usage diff --git a/lib/chef/knife/data_bag_edit.rb b/lib/chef/knife/data_bag_edit.rb index 6ef4b33f59..88c5669508 100644 --- a/lib/chef/knife/data_bag_edit.rb +++ b/lib/chef/knife/data_bag_edit.rb @@ -65,7 +65,7 @@ class Chef item_to_save = edited_item end - rest.put_rest("data/#{@name_args[0]}/#{@name_args[1]}", item_to_save) + rest.put("data/#{@name_args[0]}/#{@name_args[1]}", item_to_save) stdout.puts("Saved data_bag_item[#{@name_args[1]}]") ui.output(edited_item) if config[:print_after] end diff --git a/lib/chef/knife/environment_compare.rb b/lib/chef/knife/environment_compare.rb index 792ec444ea..54f011f323 100644 --- a/lib/chef/knife/environment_compare.rb +++ b/lib/chef/knife/environment_compare.rb @@ -57,7 +57,7 @@ class Chef end # Get all cookbooks so we can compare them all - cookbooks = rest.get_rest("/cookbooks?num_versions=1") if config[:all] + cookbooks = rest.get("/cookbooks?num_versions=1") if config[:all] # display matrix view of in the requested format. if config[:format] == 'summary' diff --git a/lib/chef/knife/index_rebuild.rb b/lib/chef/knife/index_rebuild.rb index 4b9fcdd159..95b0dcaffb 100644 --- a/lib/chef/knife/index_rebuild.rb +++ b/lib/chef/knife/index_rebuild.rb @@ -38,7 +38,7 @@ class Chef else deprecated_server_message nag - output rest.post_rest("/search/reindex", {}) + output rest.post("/search/reindex", {}) end end @@ -50,7 +50,7 @@ class Chef # for a node we know won't exist; the 404 response that comes # back will give us what we want dummy_node = "knife_index_rebuild_test_#{rand(1000000)}" - rest.get_rest("/nodes/#{dummy_node}") + rest.get("/nodes/#{dummy_node}") rescue Net::HTTPServerException => exception r = exception.response parse_api_info(r) diff --git a/lib/chef/knife/raw.rb b/lib/chef/knife/raw.rb index 601cfcef9b..de8742deb9 100644 --- a/lib/chef/knife/raw.rb +++ b/lib/chef/knife/raw.rb @@ -1,4 +1,5 @@ require 'chef/knife' +require 'chef/http' class Chef class Knife diff --git a/lib/chef/knife/recipe_list.rb b/lib/chef/knife/recipe_list.rb index ed7d2a9509..46ad619f1d 100644 --- a/lib/chef/knife/recipe_list.rb +++ b/lib/chef/knife/recipe_list.rb @@ -22,7 +22,7 @@ class Chef::Knife::RecipeList < Chef::Knife banner "knife recipe list [PATTERN]" def run - recipes = rest.get_rest('cookbooks/_recipes') + recipes = rest.get('cookbooks/_recipes') if pattern = @name_args.first recipes = recipes.grep(Regexp.new(pattern)) end diff --git a/lib/chef/node.rb b/lib/chef/node.rb index 0c13e5474a..759a45e878 100644 --- a/lib/chef/node.rb +++ b/lib/chef/node.rb @@ -27,7 +27,7 @@ require 'chef/mixin/deep_merge' require 'chef/dsl/include_attribute' require 'chef/dsl/platform_introspection' require 'chef/environment' -require 'chef/rest' +require 'chef/server_api' require 'chef/run_list' require 'chef/node/attribute' require 'chef/mash' @@ -99,10 +99,10 @@ class Chef # for saving node data we use validate_utf8: false which will not # raise an exception on bad utf8 data, but will replace the bad # characters and render valid JSON. - @chef_server_rest ||= Chef::REST.new( + @chef_server_rest ||= Chef::ServerAPI.new( Chef::Config[:chef_server_url], - Chef::Config[:node_name], - Chef::Config[:client_key], + client_name: Chef::Config[:node_name], + signing_key_filename: Chef::Config[:client_key], validate_utf8: false, ) end @@ -532,6 +532,11 @@ class Chef # Create a Chef::Node from JSON def self.json_create(o) + from_hash(o) + end + + def self.from_hash(o) + return o if o.kind_of? Chef::Node node = new node.name(o["name"]) node.chef_environment(o["chef_environment"]) @@ -561,7 +566,7 @@ class Chef Chef::Search::Query.new.search(:node, "chef_environment:#{environment}") {|n| response[n.name] = n unless n.nil?} response else - Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("environments/#{environment}/nodes") + Chef::ServerAPI.new(Chef::Config[:chef_server_url]).get("environments/#{environment}/nodes") end end @@ -569,11 +574,12 @@ class Chef if inflate response = Hash.new Chef::Search::Query.new.search(:node) do |n| + n = Chef::Node.from_hash(n) response[n.name] = n unless n.nil? end response else - Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("nodes") + Chef::ServerAPI.new(Chef::Config[:chef_server_url]).get("nodes") end end @@ -594,12 +600,12 @@ class Chef # Load a node by name def self.load(name) - Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("nodes/#{name}") + from_hash(Chef::ServerAPI.new(Chef::Config[:chef_server_url]).get("nodes/#{name}")) end # Remove this node via the REST API def destroy - chef_server_rest.delete_rest("nodes/#{name}") + chef_server_rest.delete("nodes/#{name}") end # Save this node via the REST API @@ -610,11 +616,11 @@ class Chef if Chef::Config[:why_run] Chef::Log.warn("In why-run mode, so NOT performing node save.") else - chef_server_rest.put_rest("nodes/#{name}", data_for_save) + chef_server_rest.put("nodes/#{name}", data_for_save) end rescue Net::HTTPServerException => e if e.response.code == "404" - chef_server_rest.post_rest("nodes", data_for_save) + chef_server_rest.post("nodes", data_for_save) # Chef Server before 12.3 rejects node JSON with 'policy_name' or # 'policy_group' keys, but 'policy_name' will be detected first. # Backcompat can be removed in 13.0 @@ -629,14 +635,14 @@ class Chef # Create the node via the REST API def create - chef_server_rest.post_rest("nodes", data_for_save) + chef_server_rest.post("nodes", data_for_save) self rescue Net::HTTPServerException => e # Chef Server before 12.3 rejects node JSON with 'policy_name' or # 'policy_group' keys, but 'policy_name' will be detected first. # Backcompat can be removed in 13.0 if e.response.code == "400" && e.response.body.include?("Invalid key policy_name") - chef_server_rest.post_rest("nodes", data_for_save_without_policyfile_attrs) + chef_server_rest.post("nodes", data_for_save_without_policyfile_attrs) else raise end @@ -663,10 +669,10 @@ class Chef def save_without_policyfile_attrs trimmed_data = data_for_save_without_policyfile_attrs - chef_server_rest.put_rest("nodes/#{name}", trimmed_data) + chef_server_rest.put("nodes/#{name}", trimmed_data) rescue Net::HTTPServerException => e raise e unless e.response.code == "404" - chef_server_rest.post_rest("nodes", trimmed_data) + chef_server_rest.post("nodes", trimmed_data) end def data_for_save_without_policyfile_attrs diff --git a/lib/chef/org.rb b/lib/chef/org.rb index 41d74b6186..81eca6a991 100644 --- a/lib/chef/org.rb +++ b/lib/chef/org.rb @@ -18,7 +18,7 @@ require 'chef/json_compat' require 'chef/mixin/params_validate' -require 'chef/rest' +require 'chef/server_api' class Chef class Org @@ -35,7 +35,7 @@ class Chef end def chef_rest - @chef_rest ||= Chef::REST.new(Chef::Config[:chef_server_root]) + @chef_rest ||= Chef::ServerAPI.new(Chef::Config[:chef_server_root]) end def name(arg=nil) @@ -74,18 +74,18 @@ class Chef def create payload = {:name => self.name, :full_name => self.full_name} - new_org = chef_rest.post_rest("organizations", payload) + new_org = chef_rest.post("organizations", payload) Chef::Org.from_hash(self.to_hash.merge(new_org)) end def update payload = {:name => self.name, :full_name => self.full_name} - new_org = chef_rest.put_rest("organizations/#{name}", payload) + new_org = chef_rest.put("organizations/#{name}", payload) Chef::Org.from_hash(self.to_hash.merge(new_org)) end def destroy - chef_rest.delete_rest("organizations/#{@name}") + chef_rest.delete("organizations/#{@name}") end def save @@ -102,13 +102,13 @@ class Chef def associate_user(username) request_body = {:user => username} - response = chef_rest.post_rest "organizations/#{@name}/association_requests", request_body + response = chef_rest.post "organizations/#{@name}/association_requests", request_body association_id = response["uri"].split("/").last - chef_rest.put_rest "users/#{username}/association_requests/#{association_id}", { :response => 'accept' } + chef_rest.put "users/#{username}/association_requests/#{association_id}", { :response => 'accept' } end def dissociate_user(username) - chef_rest.delete_rest "organizations/#{name}/users/#{username}" + chef_rest.delete "organizations/#{name}/users/#{username}" end # Class methods @@ -129,12 +129,12 @@ class Chef end def self.load(org_name) - response = Chef::REST.new(Chef::Config[:chef_server_root]).get_rest("organizations/#{org_name}") + response = Chef::ServerAPI.new(Chef::Config[:chef_server_root]).get("organizations/#{org_name}") Chef::Org.from_hash(response) end def self.list(inflate=false) - orgs = Chef::REST.new(Chef::Config[:chef_server_root]).get_rest('organizations') + orgs = Chef::ServerAPI.new(Chef::Config[:chef_server_root]).get('organizations') if inflate orgs.inject({}) do |org_map, (name, _url)| org_map[name] = Chef::Org.load(name) diff --git a/lib/chef/policy_builder/dynamic.rb b/lib/chef/policy_builder/dynamic.rb index c9842ba532..d4b3df748e 100644 --- a/lib/chef/policy_builder/dynamic.rb +++ b/lib/chef/policy_builder/dynamic.rb @@ -19,7 +19,6 @@ require 'forwardable' require 'chef/log' -require 'chef/rest' require 'chef/run_context' require 'chef/config' require 'chef/node' diff --git a/lib/chef/policy_builder/expand_node_object.rb b/lib/chef/policy_builder/expand_node_object.rb index 848dd00684..870351b6fb 100644 --- a/lib/chef/policy_builder/expand_node_object.rb +++ b/lib/chef/policy_builder/expand_node_object.rb @@ -20,7 +20,7 @@ # require 'chef/log' -require 'chef/rest' +require 'chef/server_api' require 'chef/run_context' require 'chef/config' require 'chef/node' @@ -198,7 +198,12 @@ class Chef begin events.cookbook_resolution_start(@expanded_run_list_with_versions) cookbook_hash = api_service.post("environments/#{node.chef_environment}/cookbook_versions", - {:run_list => @expanded_run_list_with_versions}) + {:run_list => @expanded_run_list_with_versions}) + + cookbook_hash = cookbook_hash.inject({}) do |memo, (key, value)| + memo[key] = Chef::CookbookVersion.from_hash(value) + memo + end rescue Exception => e # TODO: wrap/munge exception to provide helpful error output events.cookbook_resolution_failed(@expanded_run_list_with_versions, e) @@ -257,7 +262,7 @@ class Chef end def api_service - @api_service ||= Chef::REST.new(config[:chef_server_url]) + @api_service ||= Chef::ServerAPI.new(config[:chef_server_url]) end def config diff --git a/lib/chef/policy_builder/policyfile.rb b/lib/chef/policy_builder/policyfile.rb index 3633110d6c..249bebbd98 100644 --- a/lib/chef/policy_builder/policyfile.rb +++ b/lib/chef/policy_builder/policyfile.rb @@ -20,10 +20,10 @@ # require 'chef/log' -require 'chef/rest' require 'chef/run_context' require 'chef/config' require 'chef/node' +require 'chef/server_api' class Chef module PolicyBuilder @@ -455,7 +455,7 @@ class Chef # @api private def http_api - @api_service ||= Chef::REST.new(config[:chef_server_url]) + @api_service ||= Chef::ServerAPI.new(config[:chef_server_url]) end # @api private diff --git a/lib/chef/resource_reporter.rb b/lib/chef/resource_reporter.rb index 1175b0afb3..e2c71f7bd5 100644 --- a/lib/chef/resource_reporter.rb +++ b/lib/chef/resource_reporter.rb @@ -121,7 +121,7 @@ class Chef if reporting_enabled? begin resource_history_url = "reports/nodes/#{node_name}/runs" - server_response = @rest_client.post_rest(resource_history_url, {:action => :start, :run_id => run_id, + server_response = @rest_client.post(resource_history_url, {:action => :start, :run_id => run_id, :start_time => start_time.to_s}, headers) rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e handle_error_starting_run(e, resource_history_url) @@ -230,10 +230,9 @@ class Chef Chef::Log.debug run_data.inspect compressed_data = encode_gzip(Chef::JSONCompat.to_json(run_data)) Chef::Log.debug("Sending compressed run data...") - # Since we're posting compressed data we can not directly call post_rest which expects JSON - reporting_url = @rest_client.create_url(resource_history_url) + # Since we're posting compressed data we can not directly call post which expects JSON begin - @rest_client.raw_http_request(:POST, reporting_url, headers({'Content-Encoding' => 'gzip'}), compressed_data) + @rest_client.raw_request(:POST, resource_history_url, headers({'Content-Encoding' => 'gzip'}), compressed_data) rescue StandardError => e if e.respond_to? :response Chef::FileCache.store("failed-reporting-data.json", Chef::JSONCompat.to_json_pretty(run_data), 0640) diff --git a/lib/chef/role.rb b/lib/chef/role.rb index c085d1d714..6984a8a245 100644 --- a/lib/chef/role.rb +++ b/lib/chef/role.rb @@ -24,6 +24,7 @@ require 'chef/mixin/from_file' require 'chef/run_list' require 'chef/mash' require 'chef/json_compat' +require 'chef/server_api' require 'chef/search/query' class Chef @@ -45,11 +46,11 @@ class Chef end def chef_server_rest - @chef_server_rest ||= Chef::REST.new(Chef::Config[:chef_server_url]) + @chef_server_rest ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url]) end def self.chef_server_rest - Chef::REST.new(Chef::Config[:chef_server_url]) + Chef::ServerAPI.new(Chef::Config[:chef_server_url]) end def name(arg=nil) @@ -170,6 +171,10 @@ class Chef # Create a Chef::Role from JSON def self.json_create(o) + from_hash(o) + end + + def self.from_hash(o) role = new role.name(o["name"]) role.description(o["description"]) @@ -199,42 +204,42 @@ class Chef end response else - chef_server_rest.get_rest("roles") + chef_server_rest.get("roles") end end # Load a role by name from the API def self.load(name) - chef_server_rest.get_rest("roles/#{name}") + from_hash(chef_server_rest.get("roles/#{name}")) end def environment(env_name) - chef_server_rest.get_rest("roles/#{@name}/environments/#{env_name}") + chef_server_rest.get("roles/#{@name}/environments/#{env_name}") end def environments - chef_server_rest.get_rest("roles/#{@name}/environments") + chef_server_rest.get("roles/#{@name}/environments") end # Remove this role via the REST API def destroy - chef_server_rest.delete_rest("roles/#{@name}") + chef_server_rest.delete("roles/#{@name}") end # Save this role via the REST API def save begin - chef_server_rest.put_rest("roles/#{@name}", self) + chef_server_rest.put("roles/#{@name}", self) rescue Net::HTTPServerException => e raise e unless e.response.code == "404" - chef_server_rest.post_rest("roles", self) + chef_server_rest.post("roles", self) end self end # Create the role via the REST API def create - chef_server_rest.post_rest("roles", self) + chef_server_rest.post("roles", self) self end @@ -258,7 +263,8 @@ class Chef if js_path && File.exists?(js_path) # from_json returns object.class => json_class in the JSON. - return Chef::JSONCompat.from_json(IO.read(js_path)) + hsh = Chef::JSONCompat.parse(IO.read(js_path)) + return from_hash(hsh) elsif rb_path && File.exists?(rb_path) role = Chef::Role.new role.name(name) diff --git a/lib/chef/run_list/run_list_expansion.rb b/lib/chef/run_list/run_list_expansion.rb index 64e4326fb8..2f2d170446 100644 --- a/lib/chef/run_list/run_list_expansion.rb +++ b/lib/chef/run_list/run_list_expansion.rb @@ -21,7 +21,7 @@ require 'chef/mash' require 'chef/mixin/deep_merge' require 'chef/role' -require 'chef/rest' +require 'chef/server_api' require 'chef/json_compat' class Chef @@ -45,7 +45,7 @@ class Chef attr_reader :missing_roles_with_including_role # The data source passed to the constructor. Not used in this class. - # In subclasses, this is a couchdb or Chef::REST object pre-configured + # In subclasses, this is a Chef::ServerAPI object pre-configured # to fetch roles from their correct location. attr_reader :source @@ -214,11 +214,11 @@ class Chef class RunListExpansionFromAPI < RunListExpansion def rest - @rest ||= (source || Chef::REST.new(Chef::Config[:chef_server_url])) + @rest ||= (source || Chef::ServerAPI.new(Chef::Config[:chef_server_url])) end def fetch_role(name, included_by) - rest.get_rest("roles/#{name}") + Chef::Role.from_hash(rest.get("roles/#{name}")) rescue Net::HTTPServerException => e if e.message == '404 "Not Found"' role_not_found(name, included_by) diff --git a/lib/chef/search/query.rb b/lib/chef/search/query.rb index 658af8779c..c5c6bc6ce0 100644 --- a/lib/chef/search/query.rb +++ b/lib/chef/search/query.rb @@ -18,7 +18,7 @@ require 'chef/config' require 'chef/exceptions' -require 'chef/rest' +require 'chef/server_api' require 'uri' @@ -35,7 +35,7 @@ class Chef end def rest - @rest ||= Chef::REST.new(@url || @config[:chef_server_url]) + @rest ||= Chef::ServerAPI.new(@url || @config[:chef_server_url]) end # Backwards compatability for cookbooks. @@ -150,12 +150,26 @@ WARNDEP query_string = create_query_string(type, query, rows, start, sort) if filter_result - response = rest.post_rest(query_string, filter_result) + response = rest.post(query_string, filter_result) # response returns rows in the format of # { "url" => url_to_node, "data" => filter_result_hash } response['rows'].map! { |row| row['data'] } else - response = rest.get_rest(query_string) + response = rest.get(query_string) + response['rows'].map! do |row| + case type.to_s + when 'node' + Chef::Node.from_hash(row) + when 'role' + Chef::Role.from_hash(row) + when 'environment' + Chef::Environment.from_hash(row) + when 'client' + Chef::ApiClient.from_hash(row) + else + Chef::DataBagItem.from_hash(row) + end + end end response diff --git a/lib/chef/server_api.rb b/lib/chef/server_api.rb index 764296f8c8..6c864d53fb 100644 --- a/lib/chef/server_api.rb +++ b/lib/chef/server_api.rb @@ -23,6 +23,7 @@ require 'chef/http/decompressor' require 'chef/http/json_input' require 'chef/http/json_output' require 'chef/http/remote_request_id' +require 'chef/http/validate_content_length' class Chef class ServerAPI < Chef::HTTP @@ -31,6 +32,7 @@ class Chef options[:client_name] ||= Chef::Config[:node_name] options[:signing_key_filename] ||= Chef::Config[:client_key] options[:signing_key_filename] = nil if chef_zero_uri?(url) + options[:inflate_json_class] = false super(url, options) end @@ -40,6 +42,30 @@ class Chef use Chef::HTTP::Decompressor use Chef::HTTP::Authenticator use Chef::HTTP::RemoteRequestID + + # ValidateContentLength should come after Decompressor + # because the order of middlewares is reversed when handling + # responses. + use Chef::HTTP::ValidateContentLength + + # Makes an HTTP request to +path+ with the given +method+, +headers+, and + # +data+ (if applicable). Does not apply any middleware, besides that + # needed for Authentication. + def raw_request(method, path, headers={}, data=false) + url = create_url(path) + method, url, headers, data = Chef::HTTP::Authenticator.new(options).handle_request(method, url, headers, data) + method, url, headers, data = Chef::HTTP::RemoteRequestID.new(options).handle_request(method, url, headers, data) + response, rest_request, return_value = send_http_request(method, url, headers, data) + response.error! unless success_response?(response) + return_value + rescue Exception => exception + log_failed_request(response, return_value) unless response.nil? + + if exception.respond_to?(:chef_rest_request=) + exception.chef_rest_request = rest_request + end + raise + end end end diff --git a/lib/chef/shell/ext.rb b/lib/chef/shell/ext.rb index d516524765..17525d777c 100644 --- a/lib/chef/shell/ext.rb +++ b/lib/chef/shell/ext.rb @@ -23,7 +23,7 @@ require 'chef/dsl/platform_introspection' require 'chef/version' require 'chef/shell/shell_session' require 'chef/shell/model_wrapper' -require 'chef/shell/shell_rest' +require 'chef/server_api' require 'chef/json_compat' module Shell @@ -536,7 +536,7 @@ E desc "A REST Client configured to authenticate with the API" def api - @rest = Shell::ShellREST.new(Chef::Config[:chef_server_url]) + @rest = Chef::ServerAPI.new(Chef::Config[:chef_server_url]) end end diff --git a/lib/chef/shell/shell_rest.rb b/lib/chef/shell/shell_rest.rb deleted file mode 100644 index a485a0a1a8..0000000000 --- a/lib/chef/shell/shell_rest.rb +++ /dev/null @@ -1,28 +0,0 @@ -#-- -# Author:: Daniel DeLeo (<dan@opscode.com>) -# Copyright:: Copyright (c) 2010 Opscode, 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. -# - -module Shell - class ShellREST < Chef::REST - - alias :get :get_rest - alias :put :put_rest - alias :post :post_rest - alias :delete :delete_rest - - end -end diff --git a/lib/chef/shell/shell_session.rb b/lib/chef/shell/shell_session.rb index 73e6c34ebb..7b3939da69 100644 --- a/lib/chef/shell/shell_session.rb +++ b/lib/chef/shell/shell_session.rb @@ -201,7 +201,7 @@ module Shell def rebuild_context @run_status = Chef::RunStatus.new(@node, @events) - Chef::Cookbook::FileVendor.fetch_from_remote(Chef::REST.new(Chef::Config[:chef_server_url])) + Chef::Cookbook::FileVendor.fetch_from_remote(Chef::ServerAPI.new(Chef::Config[:chef_server_url])) cookbook_hash = @client.sync_cookbooks cookbook_collection = Chef::CookbookCollection.new(cookbook_hash) @run_context = Chef::RunContext.new(node, cookbook_collection, @events) @@ -253,7 +253,8 @@ module Shell end def register - @rest = Chef::REST.new(Chef::Config[:chef_server_url], Chef::Config[:node_name], Chef::Config[:client_key]) + @rest = Chef::ServerAPI.new(Chef::Config[:chef_server_url], :client_name => Chef::Config[:node_name], + :signing_key_filename => Chef::Config[:client_key]) end end diff --git a/lib/chef/user_v1.rb b/lib/chef/user_v1.rb index 31cb0576a2..077fca50b9 100644 --- a/lib/chef/user_v1.rb +++ b/lib/chef/user_v1.rb @@ -140,7 +140,7 @@ class Chef def destroy # will default to the current API version (Chef::Authenticator::DEFAULT_SERVER_API_VERSION) - Chef::REST.new(Chef::Config[:chef_server_url]).delete("users/#{@username}") + Chef::ServerAPI.new(Chef::Config[:chef_server_url]).delete("users/#{@username}") end def create @@ -287,7 +287,7 @@ class Chef end def self.list(inflate=false) - response = Chef::REST.new(Chef::Config[:chef_server_url]).get('users') + response = Chef::ServerAPI.new(Chef::Config[:chef_server_url]).get('users') users = if response.is_a?(Array) # EC 11 / CS 12 V0, V1 # GET /organizations/<org>/users @@ -312,7 +312,7 @@ class Chef def self.load(username) # will default to the current API version (Chef::Authenticator::DEFAULT_SERVER_API_VERSION) - response = Chef::REST.new(Chef::Config[:chef_server_url]).get("users/#{username}") + response = Chef::ServerAPI.new(Chef::Config[:chef_server_url]).get("users/#{username}") Chef::UserV1.from_hash(response) end |