class Chef::ApiClientV1

Constants

SUPPORTED_API_VERSIONS

Public Class Methods

from_hash(o) click to toggle source
# File lib/chef/api_client_v1.rb, line 171
def self.from_hash(o)
  client = Chef::ApiClientV1.new
  client.name(o["name"] || o["clientname"])
  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
from_json(j) click to toggle source
# File lib/chef/api_client_v1.rb, line 182
def self.from_json(j)
  Chef::ApiClientV1.from_hash(Chef::JSONCompat.from_json(j))
end
http_api() click to toggle source
# File lib/chef/api_client_v1.rb, line 67
def self.http_api
  Chef::ServerAPI.new(Chef::Config[:chef_server_url], { api_version: "1", inflate_json_class: false })
end
list(inflate = false) click to toggle source
# File lib/chef/api_client_v1.rb, line 191
def self.list(inflate = false)
  if inflate
    response = Hash.new
    Chef::Search::Query.new.search(:client) do |n|
      n = from_hash(n) if n.instance_of?(Hash)
      response[n.name] = n
    end
    response
  else
    http_api.get("clients")
  end
end
load(name) click to toggle source

Load a client by name via the API @param name [String] the client name

# File lib/chef/api_client_v1.rb, line 206
def self.load(name)
  response = http_api.get("clients/#{name}")
  Chef::ApiClientV1.from_hash(response)
end
new() click to toggle source

Create a new Chef::ApiClientV1 object.

# File lib/chef/api_client_v1.rb, line 50
def initialize
  @name = ""
  @public_key = nil
  @private_key = nil
  @admin = false
  @validator = false
  @create_key = nil
end
reregister(name) click to toggle source
# File lib/chef/api_client_v1.rb, line 186
def self.reregister(name)
  api_client = Chef::ApiClientV1.load(name)
  api_client.reregister
end

Public Instance Methods

admin(arg = nil) click to toggle source

Gets or sets whether this client is an admin.

@param arg [Optional True/False] Should be true or false - default is false. @return [True/False] The current value

# File lib/chef/api_client_v1.rb, line 87
def admin(arg = nil)
  set_or_return(
    :admin,
    arg,
    kind_of: [ TrueClass, FalseClass ]
  )
end
chef_rest_v0() click to toggle source
# File lib/chef/api_client_v1.rb, line 59
def chef_rest_v0
  @chef_rest_v0 ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url], { api_version: "0", inflate_json_class: false })
end
chef_rest_v1() click to toggle source
# File lib/chef/api_client_v1.rb, line 63
def chef_rest_v1
  @chef_rest_v1 ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url], { api_version: "1", inflate_json_class: false })
end
create() click to toggle source

Create the client via the REST API

# File lib/chef/api_client_v1.rb, line 280
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::HTTPClientException => 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::ApiClientV1.from_hash(to_h.merge(new_client))
end
create_key(arg = nil) click to toggle source

Used to ask server to generate key pair under api V1

@param arg [Optional True/False] Should be true or false - default is false. @return [True/False] The current value

# File lib/chef/api_client_v1.rb, line 137
def create_key(arg = nil)
  set_or_return(
    :create_key,
    arg,
    kind_of: [ TrueClass, FalseClass ]
  )
end
destroy() click to toggle source

Remove this client via the REST API

# File lib/chef/api_client_v1.rb, line 212
def destroy
  chef_rest_v1.delete("clients/#{@name}")
end
name(arg = nil) click to toggle source

Gets or sets the client name.

@param arg [Optional String] The name must be alpha-numeric plus - and _. @return [String] The current value of the name.

# File lib/chef/api_client_v1.rb, line 75
def name(arg = nil)
  set_or_return(
    :name,
    arg,
    regex: /^[\-[:alnum:]_\.]+$/
  )
end
private_key(arg = nil) click to toggle source

Private key. The server will return it as a string. Set to true under API V0 to have the server regenerate the default key.

@param arg [Optional String] The string representation of the private key. @return [String] The current value.

# File lib/chef/api_client_v1.rb, line 125
def private_key(arg = nil)
  set_or_return(
    :private_key,
    arg,
    kind_of: [String, TrueClass, FalseClass]
  )
end
public_key(arg = nil) click to toggle source

Gets or sets the public key.

@param arg [Optional String] The string representation of the public key. @return [String] The current value.

# File lib/chef/api_client_v1.rb, line 99
def public_key(arg = nil)
  set_or_return(
    :public_key,
    arg,
    kind_of: String
  )
end
reregister() click to toggle source
# File lib/chef/api_client_v1.rb, line 228
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 })
  if reregistered_self.respond_to?(:[])
    private_key(reregistered_self["private_key"])
  else
    private_key(reregistered_self.private_key)
  end
  self
rescue Net::HTTPClientException => 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
save() click to toggle source

Save this client via the REST API, returns a hash including the private key

# File lib/chef/api_client_v1.rb, line 217
def save
  update
rescue Net::HTTPClientException => e
  # If that fails, go ahead and try and update it
  if e.response.code == "404"
    create
  else
    raise e
  end
end
to_h() click to toggle source

The hash representation of the object. Includes the name and public_key. Private key is included if available.

@return [Hash]

# File lib/chef/api_client_v1.rb, line 149
def to_h
  result = {
    "name" => @name,
    "validator" => @validator,
    "admin" => @admin,
    "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
end
Also aliased as: to_hash
to_hash()
Alias for: to_h
to_json(*a) click to toggle source

The JSON representation of the object.

@return [String] the JSON string.

# File lib/chef/api_client_v1.rb, line 167
def to_json(*a)
  Chef::JSONCompat.to_json(to_h, *a)
end
to_s() click to toggle source

As a string

# File lib/chef/api_client_v1.rb, line 321
def to_s
  "client[#{@name}]"
end
update() click to toggle source

Updates the client via the REST API

# File lib/chef/api_client_v1.rb, line 253
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::HTTPClientException => 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

  Chef::ApiClientV1.from_hash(new_client)
end
validator(arg = nil) click to toggle source

Gets or sets whether this client is a validator.

@param arg [Boolean] whether or not the client is a validator. If

`nil`, retrieves the already-set value.

@return [Boolean] The current value

# File lib/chef/api_client_v1.rb, line 112
def validator(arg = nil)
  set_or_return(
    :validator,
    arg,
    kind_of: [TrueClass, FalseClass]
  )
end