Module: ChefZero::RSpec::WhenTheChefServerInstanceMethods

Defined in:
lib/chef_zero/rspec.rb

Instance Method Summary collapse

Instance Method Details

#acl(data) ⇒ Object



201
202
203
# File 'lib/chef_zero/rspec.rb', line 201

def acl(data)
  acl_for(@current_object_path, data)
end

#acl_for(path, data) ⇒ Object



197
198
199
# File 'lib/chef_zero/rspec.rb', line 197

def acl_for(path, data)
  ChefZero::RSpec.server.load_data({ "acls" => { path => data } }, current_org)
end

#client(name, data, &block) ⇒ Object



205
206
207
208
209
210
# File 'lib/chef_zero/rspec.rb', line 205

def client(name, data, &block)
  with_object_path("clients/#{name}") do
    ChefZero::RSpec.server.load_data({ "clients" => { name => data } }, current_org)
    instance_eval(&block) if block_given?
  end
end

#container(name, data, &block) ⇒ Object



212
213
214
215
216
217
# File 'lib/chef_zero/rspec.rb', line 212

def container(name, data, &block)
  with_object_path("containers/#{name}") do
    ChefZero::RSpec.server.load_data({ "containers" => { name => data } }, current_org)
    instance_eval(&block) if block_given?
  end
end

#cookbook(name, version, data = {}, options = {}, &block) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/chef_zero/rspec.rb', line 219

def cookbook(name, version, data = {}, options = {}, &block)
  with_object_path("cookbooks/#{name}") do
    # If you didn't specify metadata.rb, we generate it for you. If you
    # explicitly set it to nil, that means you don't want it at all.
    if data.key?("metadata.rb")
      if data["metadata.rb"].nil?
        data.delete("metadata.rb")
      end
    else
      data["metadata.rb"] = "name #{name.inspect}; version #{version.inspect}"
    end
    ChefZero::RSpec.server.load_data({ "cookbooks" => { "#{name}-#{version}" => data.merge(options) } }, current_org)
    instance_eval(&block) if block_given?
  end
end

#cookbook_artifact(name, identifier, data = {}, &block) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/chef_zero/rspec.rb', line 235

def cookbook_artifact(name, identifier, data = {}, &block)
  with_object_path("cookbook_artifacts/#{name}") do
    # If you didn't specify metadata.rb, we generate it for you. If you
    # explicitly set it to nil, that means you don't want it at all.
    if data.key?("metadata.rb")
      if data["metadata.rb"].nil?
        data.delete("metadata.rb")
      end
    else
      data["metadata.rb"] = "name #{name.inspect}"
    end
    ChefZero::RSpec.server.load_data({ "cookbook_artifacts" => { "#{name}-#{identifier}" => data } }, current_org)
    instance_eval(&block) if block_given?
  end
end

#current_orgObject



341
342
343
# File 'lib/chef_zero/rspec.rb', line 341

def current_org
  @current_org || ChefZero::RSpec.server.options[:single_org] || nil
end

#data_bag(name, data, &block) ⇒ Object



251
252
253
254
255
256
# File 'lib/chef_zero/rspec.rb', line 251

def data_bag(name, data, &block)
  with_object_path("data/#{name}") do
    ChefZero::RSpec.server.load_data({ "data" => { name => data } }, current_org)
    instance_eval(&block) if block_given?
  end
end

#dejsonize(data) ⇒ Object



333
334
335
336
337
338
339
# File 'lib/chef_zero/rspec.rb', line 333

def dejsonize(data)
  if data.is_a?(String)
    data
  else
    FFI_Yajl::Encoder.encode(data, pretty: true)
  end
end

#environment(name, data, &block) ⇒ Object



258
259
260
261
262
263
# File 'lib/chef_zero/rspec.rb', line 258

def environment(name, data, &block)
  with_object_path("environments/#{name}") do
    ChefZero::RSpec.server.load_data({ "environments" => { name => data } }, current_org)
    instance_eval(&block) if block_given?
  end
end

#group(name, data, &block) ⇒ Object



265
266
267
268
269
270
# File 'lib/chef_zero/rspec.rb', line 265

def group(name, data, &block)
  with_object_path("groups/#{name}") do
    ChefZero::RSpec.server.load_data({ "groups" => { name => data } }, current_org)
    instance_eval(&block) if block_given?
  end
end

#node(name, data, &block) ⇒ Object



272
273
274
275
276
277
# File 'lib/chef_zero/rspec.rb', line 272

def node(name, data, &block)
  with_object_path("nodes/#{name}") do
    ChefZero::RSpec.server.load_data({ "nodes" => { name => data } }, current_org)
    instance_eval(&block) if block_given?
  end
end

#org_invite(*usernames) ⇒ Object



279
280
281
# File 'lib/chef_zero/rspec.rb', line 279

def org_invite(*usernames)
  ChefZero::RSpec.server.load_data({ "invites" => usernames }, current_org)
end

#org_member(*usernames) ⇒ Object



283
284
285
# File 'lib/chef_zero/rspec.rb', line 283

def org_member(*usernames)
  ChefZero::RSpec.server.load_data({ "members" => usernames }, current_org)
end

#organization(name, org = "{}", &block) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/chef_zero/rspec.rb', line 181

def organization(name, org = "{}", &block)
  ChefZero::RSpec.server.data_store.set([ "organizations", name, "org" ], dejsonize(org), :create_dir, :create)
  prev_org_name = @current_org
  @current_org = name
  prev_object_path = @current_object_path
  @current_object_path = "organizations/#{name}"
  if block_given?
    begin
      instance_eval(&block)
    ensure
      @current_org = prev_org_name
      @current_object_path = prev_object_path
    end
  end
end

#policy(name, version, data, &block) ⇒ Object



287
288
289
290
291
292
# File 'lib/chef_zero/rspec.rb', line 287

def policy(name, version, data, &block)
  with_object_path("policies/#{name}") do
    ChefZero::RSpec.server.load_data({ "policies" => { name => { version => data } } }, current_org)
    instance_eval(&block) if block_given?
  end
end

#policy_group(name, data, &block) ⇒ Object



294
295
296
297
298
299
# File 'lib/chef_zero/rspec.rb', line 294

def policy_group(name, data, &block)
  with_object_path("policy_groups/#{name}") do
    ChefZero::RSpec.server.load_data({ "policy_groups" => { name => data } }, current_org)
    instance_eval(&block) if block_given?
  end
end

#role(name, data, &block) ⇒ Object



301
302
303
304
305
306
# File 'lib/chef_zero/rspec.rb', line 301

def role(name, data, &block)
  with_object_path("roles/#{name}") do
    ChefZero::RSpec.server.load_data({ "roles" => { name => data } }, current_org)
    instance_eval(&block) if block_given?
  end
end

#sandbox(name, data, &block) ⇒ Object



308
309
310
311
312
313
# File 'lib/chef_zero/rspec.rb', line 308

def sandbox(name, data, &block)
  with_object_path("sandboxes/#{name}") do
    ChefZero::RSpec.server.load_data({ "sandboxes" => { name => data } }, current_org)
    instance_eval(&block) if block_given?
  end
end

#user(name, data, &block) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/chef_zero/rspec.rb', line 315

def user(name, data, &block)
  if ChefZero::RSpec.server.options[:osc_compat]
    with_object_path("users/#{name}") do
      ChefZero::RSpec.server.load_data({ "users" => { name => data } }, current_org)
      instance_eval(&block) if block_given?
    end
  else
    old_object_path = @current_object_path
    @current_object_path = "users/#{name}"
    begin
      ChefZero::RSpec.server.load_data({ "users" => { name => data } }, current_org)
      instance_eval(&block) if block_given?
    ensure
      @current_object_path = old_object_path
    end
  end
end

#with_object_path(object_path) ⇒ Object



345
346
347
348
349
350
351
352
# File 'lib/chef_zero/rspec.rb', line 345

def with_object_path(object_path)
  old_object_path = @current_object_path
  @current_object_path = object_path
  begin
    yield if block_given?
  end
  @current_object_path = old_object_path
end