summaryrefslogtreecommitdiff
path: root/lib/chef/api_client.rb
diff options
context:
space:
mode:
authortylercloke <tylercloke@gmail.com>2015-07-02 11:24:24 -0700
committertylercloke <tylercloke@gmail.com>2015-07-06 14:36:03 -0700
commitbd9febad0be3c396e5373158615d93ef1e2033a1 (patch)
tree4d5704f3c11e8f0a74c0c29d57d5969fca1a7cdc /lib/chef/api_client.rb
parentdb6ee7b3fedb5763e9108a0254c4201b84e84fef (diff)
downloadchef-bd9febad0be3c396e5373158615d93ef1e2033a1.tar.gz
Move ApiClient V1 supported code to Chef::ApiClientV1 and restore Chef::ApiClient.
For backwards compatibility. ApiClientV1 will replace ApiClient when Chef 13 is released. Updated client_*.rb knife commands to use ApiClientV1.
Diffstat (limited to 'lib/chef/api_client.rb')
-rw-r--r--lib/chef/api_client.rb161
1 files changed, 31 insertions, 130 deletions
diff --git a/lib/chef/api_client.rb b/lib/chef/api_client.rb
index ad31fb7d7b..6fee78225f 100644
--- a/lib/chef/api_client.rb
+++ b/lib/chef/api_client.rb
@@ -1,7 +1,7 @@
#
-# Author:: Adam Jacob (<adam@chef.io>)
-# Author:: Nuo Yan (<nuo@chef.io>)
-# Copyright:: Copyright (c) 2008, 2015 Chef Software, Inc.
+# Author:: Adam Jacob (<adam@opscode.com>)
+# Author:: Nuo Yan (<nuo@opscode.com>)
+# Copyright:: Copyright (c) 2008 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -23,18 +23,17 @@ require 'chef/mixin/from_file'
require 'chef/mash'
require 'chef/json_compat'
require 'chef/search/query'
-require 'chef/exceptions'
-require 'chef/mixin/api_version_request_handling'
-require 'chef/server_api'
+# DEPRECATION NOTE
+#
+# This code will be removed in Chef 13 in favor of the code in Chef::ApiClientV1,
+# which will be moved to this namespace. New development should occur in
+# Chef::ApiClientV1 until the time before Chef 13.
class Chef
class ApiClient
include Chef::Mixin::FromFile
include Chef::Mixin::ParamsValidate
- include Chef::Mixin::ApiVersionRequestHandling
-
- SUPPORTED_API_VERSIONS = [0,1]
# Create a new Chef::ApiClient object.
def initialize
@@ -43,25 +42,6 @@ class Chef
@private_key = nil
@admin = false
@validator = false
- @create_key = nil
- end
-
- def chef_rest_v0
- @chef_rest_v0 ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url], {:api_version => "0"})
- end
-
- def chef_rest_v1
- @chef_rest_v1 ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url], {:api_version => "1"})
- end
-
- # will default to the current version (Chef::Authenticator::DEFAULT_SERVER_API_VERSION)
- def http_api
- @http_api ||= Chef::REST.new(Chef::Config[:chef_server_url])
- end
-
- # will default to the current version (Chef::Authenticator::DEFAULT_SERVER_API_VERSION)
- def self.http_api
- Chef::REST.new(Chef::Config[:chef_server_url])
end
# Gets or sets the client name.
@@ -113,8 +93,7 @@ class Chef
)
end
- # Private key. The server will return it as a string.
- # Set to true under API V0 to have the server regenerate the default key.
+ # Gets or sets the private key.
#
# @params [Optional String] The string representation of the private key.
# @return [String] The current value.
@@ -122,19 +101,7 @@ class Chef
set_or_return(
:private_key,
arg,
- :kind_of => [String, TrueClass, FalseClass]
- )
- end
-
- # Used to ask server to generate key pair under api V1
- #
- # @params [Optional True/False] Should be true or false - default is false.
- # @return [True/False] The current value
- def create_key(arg=nil)
- set_or_return(
- :create_key,
- arg,
- :kind_of => [ TrueClass, FalseClass ]
+ :kind_of => [String, FalseClass]
)
end
@@ -145,14 +112,13 @@ class Chef
def to_hash
result = {
"name" => @name,
+ "public_key" => @public_key,
"validator" => @validator,
"admin" => @admin,
'json_class' => self.class.name,
"chef_type" => "client"
}
- result["private_key"] = @private_key unless @private_key.nil?
- result["public_key"] = @public_key unless @public_key.nil?
- result["create_key"] = @create_key unless @create_key.nil?
+ result["private_key"] = @private_key if @private_key
result
end
@@ -166,11 +132,10 @@ class Chef
def self.from_hash(o)
client = Chef::ApiClient.new
client.name(o["name"] || o["clientname"])
+ client.private_key(o["private_key"]) if o.key?("private_key")
+ client.public_key(o["public_key"])
client.admin(o["admin"])
client.validator(o["validator"])
- client.private_key(o["private_key"]) if o.key?("private_key")
- client.public_key(o["public_key"]) if o.key?("public_key")
- client.create_key(o["create_key"]) if o.key?("create_key")
client
end
@@ -182,6 +147,10 @@ class Chef
from_hash(Chef::JSONCompat.parse(j))
end
+ def self.http_api
+ Chef::REST.new(Chef::Config[:chef_server_url])
+ end
+
def self.reregister(name)
api_client = load(name)
api_client.reregister
@@ -218,11 +187,11 @@ class Chef
# Save this client via the REST API, returns a hash including the private key
def save
begin
- update
+ http_api.put("clients/#{name}", { :name => self.name, :admin => self.admin, :validator => self.validator})
rescue Net::HTTPServerException => e
# If that fails, go ahead and try and update it
if e.response.code == "404"
- create
+ http_api.post("clients", {:name => self.name, :admin => self.admin, :validator => self.validator })
else
raise e
end
@@ -230,95 +199,18 @@ class Chef
end
def reregister
- # Try API V0 and if it fails due to V0 not being supported, raise the proper error message.
- # reregister only supported in API V0 or lesser.
- reregistered_self = chef_rest_v0.put("clients/#{name}", { :name => name, :admin => admin, :validator => validator, :private_key => true })
+ reregistered_self = http_api.put("clients/#{name}", { :name => name, :admin => admin, :validator => validator, :private_key => true })
if reregistered_self.respond_to?(:[])
private_key(reregistered_self["private_key"])
else
private_key(reregistered_self.private_key)
end
self
- rescue Net::HTTPServerException => e
- # if there was a 406 related to versioning, give error explaining that
- # only API version 0 is supported for reregister command
- if e.response.code == "406" && e.response["x-ops-server-api-version"]
- version_header = Chef::JSONCompat.from_json(e.response["x-ops-server-api-version"])
- min_version = version_header["min_version"]
- max_version = version_header["max_version"]
- error_msg = reregister_only_v0_supported_error_msg(max_version, min_version)
- raise Chef::Exceptions::OnlyApiVersion0SupportedForAction.new(error_msg)
- else
- raise e
- end
- end
-
- # Updates the client via the REST API
- def update
- # NOTE: API V1 dropped support for updating client keys via update (aka PUT),
- # but this code never supported key updating in the first place. Since
- # it was never implemented, we will simply ignore that functionality
- # as it is being deprecated.
- # Delete this comment after V0 support is dropped.
- payload = { :name => name }
- payload[:validator] = validator unless validator.nil?
-
- # DEPRECATION
- # This field is ignored in API V1, but left for backwards-compat,
- # can remove after API V0 is no longer supported.
- payload[:admin] = admin unless admin.nil?
-
- begin
- new_client = chef_rest_v1.put("clients/#{name}", payload)
- rescue Net::HTTPServerException => e
- # rescue API V0 if 406 and the server supports V0
- supported_versions = server_client_api_version_intersection(e, SUPPORTED_API_VERSIONS)
- raise e unless supported_versions && supported_versions.include?(0)
- new_client = chef_rest_v0.put("clients/#{name}", payload)
- end
-
- new_client
end
# Create the client via the REST API
def create
- payload = {
- :name => name,
- :validator => validator,
- # this field is ignored in API V1, but left for backwards-compat,
- # can remove after OSC 11 support is finished?
- :admin => admin
- }
- begin
- # try API V1
- raise Chef::Exceptions::InvalidClientAttribute, "You cannot set both public_key and create_key for create." if !create_key.nil? && !public_key.nil?
-
- payload[:public_key] = public_key unless public_key.nil?
- payload[:create_key] = create_key unless create_key.nil?
-
- new_client = chef_rest_v1.post("clients", payload)
-
- # get the private_key out of the chef_key hash if it exists
- if new_client['chef_key']
- if new_client['chef_key']['private_key']
- new_client['private_key'] = new_client['chef_key']['private_key']
- end
- new_client['public_key'] = new_client['chef_key']['public_key']
- new_client.delete('chef_key')
- end
-
- rescue Net::HTTPServerException => e
- # rescue API V0 if 406 and the server supports V0
- supported_versions = server_client_api_version_intersection(e, SUPPORTED_API_VERSIONS)
- raise e unless supported_versions && supported_versions.include?(0)
-
- # under API V0, a key pair will always be created unless public_key is
- # passed on initial POST
- payload[:public_key] = public_key unless public_key.nil?
-
- new_client = chef_rest_v0.post("clients", payload)
- end
- Chef::ApiClient.from_hash(self.to_hash.merge(new_client))
+ http_api.post("clients", self)
end
# As a string
@@ -326,5 +218,14 @@ class Chef
"client[#{@name}]"
end
+ def inspect
+ "Chef::ApiClient name:'#{name}' admin:'#{admin.inspect}' validator:'#{validator}' " +
+ "public_key:'#{public_key}' private_key:'#{private_key}'"
+ end
+
+ def http_api
+ @http_api ||= Chef::REST.new(Chef::Config[:chef_server_url])
+ end
+
end
end