Module: ChefZero::RSpec::WhenTheChefServerInstanceMethods

Defined in:
lib/chef_zero/rspec.rb

Instance Method Summary collapse

Instance Method Details

#acl(data) ⇒ Object


186
187
188
# File 'lib/chef_zero/rspec.rb', line 186

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

#acl_for(path, data) ⇒ Object


182
183
184
# File 'lib/chef_zero/rspec.rb', line 182

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

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


190
191
192
193
194
195
# File 'lib/chef_zero/rspec.rb', line 190

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


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

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


204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/chef_zero/rspec.rb', line 204

def cookbook(name, version, data = {}, options = {}, &block)
  with_object_path("cookbooks/#{name}") do
    if data.has_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

#current_orgObject


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

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

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


218
219
220
221
222
223
# File 'lib/chef_zero/rspec.rb', line 218

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


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

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

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


225
226
227
228
229
230
# File 'lib/chef_zero/rspec.rb', line 225

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


232
233
234
235
236
237
# File 'lib/chef_zero/rspec.rb', line 232

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


239
240
241
242
243
244
# File 'lib/chef_zero/rspec.rb', line 239

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


246
247
248
# File 'lib/chef_zero/rspec.rb', line 246

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

#org_member(*usernames) ⇒ Object


250
251
252
# File 'lib/chef_zero/rspec.rb', line 250

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

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


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/chef_zero/rspec.rb', line 166

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

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


254
255
256
257
258
259
# File 'lib/chef_zero/rspec.rb', line 254

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


261
262
263
264
265
266
# File 'lib/chef_zero/rspec.rb', line 261

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


268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/chef_zero/rspec.rb', line 268

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


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

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