Class: Chef::ApiClient

Inherits:
Object show all
Includes:
Mixin::FromFile, Mixin::ParamsValidate
Defined in:
lib/chef/api_client.rb,
lib/chef/api_client/registration.rb

Defined Under Namespace

Classes: Registration

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::ParamsValidate

#set_or_return, #validate

Methods included from Mixin::FromFile

#class_from_file, #from_file

Constructor Details

#initializeApiClient

Create a new Chef::ApiClient object.



34
35
36
37
38
39
# File 'lib/chef/api_client.rb', line 34

def initialize
  @name = ''
  @public_key = nil
  @private_key = nil
  @admin = false
end

Class Method Details

.http_apiObject



121
122
123
# File 'lib/chef/api_client.rb', line 121

def self.http_api
  Chef::REST.new(Chef::Config[:chef_server_url])
end

.json_create(o) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/chef/api_client.rb', line 112

def self.json_create(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
end

.list(inflate = false) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/chef/api_client.rb', line 130

def self.list(inflate=false)
  if inflate
    response = Hash.new
    Chef::Search::Query.new.search(:client) do |n|
      n = self.json_create(n) if n.instance_of?(Hash)
      response[n.name] = n
    end
    response
  else
    http_api.get("clients")
  end
end

.load(name) ⇒ Object

Load a client by name via the API



144
145
146
147
148
149
150
151
152
153
# File 'lib/chef/api_client.rb', line 144

def self.load(name)
  response = http_api.get("clients/#{name}")
  if response.kind_of?(Chef::ApiClient)
    response
  else
    client = Chef::ApiClient.new
    client.name(response['clientname'])
    client
  end
end

.reregister(name) ⇒ Object



125
126
127
128
# File 'lib/chef/api_client.rb', line 125

def self.reregister(name)
  api_client = load(name)
  api_client.reregister
end

Instance Method Details

#admin(arg = nil) ⇒ True/False

Gets or sets whether this client is an admin.

Returns:

  • (True/False)

    The current value



57
58
59
60
61
62
63
# File 'lib/chef/api_client.rb', line 57

def admin(arg=nil)
  set_or_return(
    :admin,
    arg,
    :kind_of => [ TrueClass, FalseClass ]
  )
end

#createObject

Create the client via the REST API



185
186
187
# File 'lib/chef/api_client.rb', line 185

def create
  http_api.post("clients", self)
end

#destroyObject

Remove this client via the REST API



156
157
158
# File 'lib/chef/api_client.rb', line 156

def destroy
  http_api.delete("clients/#{@name}")
end

#http_apiObject



199
200
201
# File 'lib/chef/api_client.rb', line 199

def http_api
  @http_api ||= Chef::REST.new(Chef::Config[:chef_server_url])
end

#inspectObject



194
195
196
197
# File 'lib/chef/api_client.rb', line 194

def inspect
  "Chef::ApiClient name:'#{name}' admin:'#{admin.inspect}' " +
  "public_key:'#{public_key}' private_key:'#{private_key}'"
end

#name(arg = nil) ⇒ String

Gets or sets the client name.

Returns:

  • (String)

    The current value of the name.



45
46
47
48
49
50
51
# File 'lib/chef/api_client.rb', line 45

def name(arg=nil)
  set_or_return(
    :name,
    arg,
    :regex => /^[\-[:alnum:]_\.]+$/
  )
end

#private_key(arg = nil) ⇒ String

Gets or sets the private key.

Returns:

  • (String)

    The current value.



81
82
83
84
85
86
87
# File 'lib/chef/api_client.rb', line 81

def private_key(arg=nil)
  set_or_return(
    :private_key,
    arg,
    :kind_of => String
  )
end

#public_key(arg = nil) ⇒ String

Gets or sets the public key.

Returns:

  • (String)

    The current value.



69
70
71
72
73
74
75
# File 'lib/chef/api_client.rb', line 69

def public_key(arg=nil)
  set_or_return(
    :public_key,
    arg,
    :kind_of => String
  )
end

#reregisterObject



174
175
176
177
178
179
180
181
182
# File 'lib/chef/api_client.rb', line 174

def reregister
  reregistered_self = http_api.put("clients/#{name}", { :name => name, :admin => admin, :private_key => true })
  if reregistered_self.respond_to?(:[])
    private_key(reregistered_self["private_key"])
  else
    private_key(reregistered_self.private_key)
  end
  self
end

#saveObject

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



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/chef/api_client.rb', line 161

def save
  begin
    http_api.put("clients/#{name}", { :name => self.name, :admin => self.admin})
  rescue Net::HTTPServerException => e
    # If that fails, go ahead and try and update it
    if e.response.code == "404"
      http_api.post("clients", {:name => self.name, :admin => self.admin })
    else
      raise e
    end
  end
end

#to_hashHash

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

Returns:

  • (Hash)


93
94
95
96
97
98
99
100
101
102
103
# File 'lib/chef/api_client.rb', line 93

def to_hash
  result = {
    "name" => @name,
    "public_key" => @public_key,
    "admin" => @admin,
    'json_class' => self.class.name,
    "chef_type" => "client"
  }
  result["private_key"] = @private_key if @private_key
  result
end

#to_json(*a) ⇒ String

The JSON representation of the object.

Returns:

  • (String)

    the JSON string.



108
109
110
# File 'lib/chef/api_client.rb', line 108

def to_json(*a)
  to_hash.to_json(*a)
end

#to_sObject

As a string



190
191
192
# File 'lib/chef/api_client.rb', line 190

def to_s
  "client[#{@name}]"
end