Module: Chef::Mixin::ApiVersionRequestHandling

Included in:
ApiClientV1, UserV1
Defined in:
lib/chef/mixin/api_version_request_handling.rb

Instance Method Summary collapse

Instance Method Details

#reregister_only_v0_supported_error_msg(max_version, min_version) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/chef/mixin/api_version_request_handling.rb', line 54

def reregister_only_v0_supported_error_msg(max_version, min_version)
  <<-EOH
The reregister command only supports server API version 0.
The server that received the request supports a min version of #{min_version} and a max version of #{max_version}.
User keys are now managed via the key rotation commmands.
Please refer to the documentation on how to manage your keys via the key rotation commands:
https://docs.chef.io/server_security.html#key-rotation
EOH
end

#server_client_api_version_intersection(exception, supported_client_versions) ⇒ Object

Input: exeception:

Net::HTTPServerException that may or may not contain the x-ops-server-api-version header

supported_client_versions:

An array of Integers that represent the API versions the client supports.

Output: nil:

If the execption was not a 406 or the server does not support versioning

Array of length zero:

If there was no intersection between supported client versions and supported server versions

Arrary of Integers:

If there was an intersection of supported versions, the array returns will contain that intersection


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/chef/mixin/api_version_request_handling.rb', line 35

def server_client_api_version_intersection(exception, supported_client_versions)
  # return empty array unless 406 Unacceptable with proper header
  return nil if exception.response.code != "406" || exception.response["x-ops-server-api-version"].nil?

  # intersection of versions the server and client support, will be of length zero if no intersection
  server_supported_client_versions = Array.new

  header = Chef::JSONCompat.from_json(exception.response["x-ops-server-api-version"])
  min_server_version = Integer(header["min_version"])
  max_server_version = Integer(header["max_version"])

  supported_client_versions.each do |version|
    if version >= min_server_version && version <= max_server_version
      server_supported_client_versions.push(version)
    end
  end
  server_supported_client_versions
end