Class: Splunk::Collection

Inherits:
ReadOnlyCollection show all
Defined in:
lib/splunk-sdk-ruby/collection.rb

Overview

Class representing a collection in Splunk.

A Collection is a group of items, usually of class Entity or one of its subclasses, but occasionally another Collection. Usually you obtain a Collection by calling one of the convenience methods on Service.

A Collection is enumerable, and implements many of the methods found on Hash, so methods like each, select, and delete_if all work, as does fetching a member of the Collection with [].

Instance Attribute Summary

Attributes inherited from ReadOnlyCollection

#entity_class, #resource, #service

Instance Method Summary collapse

Methods inherited from ReadOnlyCollection

#assoc, #atom_entry_to_entity, #each, #each_key, #each_pair, #each_value, #empty?, #fetch, #has_key?, #initialize, #keys, #length, #values

Constructor Details

This class inherits a constructor from Splunk::ReadOnlyCollection

Instance Method Details

#create(name, args = {}) ⇒ Object

Creates an item in this collection.

The name argument is required. All other arguments are passed as a hash, though they vary from collection to collection.

Returns: the created entity.

Example:

service = Splunk::connect(:username => 'admin', :password => 'foo')
service.users.create('jack',
  :password => 'mypassword',
  :realname => 'Jack_be_nimble',
  :roles => ['user'])

Raises:

  • (StandardError)


342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/splunk-sdk-ruby/collection.rb', line 342

def create(name, args={})
  body_args = args.clone()
  body_args["name"] = name

  request_args = {
      :method => :POST,
      :resource => @resource,
      :body => body_args
  }
  if args.has_key?(:namespace)
    request_args[:namespace] = body_args.delete(:namespace)
  end

  response = @service.request(request_args)

  if @always_fetch
    fetch_args = {:method => :GET,
                  :resource => @resource + [name]}
    if args.has_key?(:namespace)
      fetch_args[:namespace] = args[:namespace]
    end
    response = @service.request(fetch_args)
  end
  feed = AtomFeed.new(response.body)
  raise StandardError.new("No entities returned") if feed.entries.empty?
  entity = atom_entry_to_entity(feed.entries[0])
  raise StandardError.new("Found nil entity.") if entity.nil?
  return entity
end

#delete(name, namespace = nil) ⇒ Object

Deletes an item from the collection.

Entities from different namespaces may have the same name, so if you are connected to Splunk using a namespace with wildcards in it, there may be multiple entities in the collection with the same name. In this case you must specify a namespace as well, or the delete method will raise an AmbiguousEntityReference error.

Example:

service = Splunk::connect(:username => 'admin', :password => 'foo')
props = service.confs['props']
props.delete('sdk-tests')


386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/splunk-sdk-ruby/collection.rb', line 386

def delete(name, namespace=nil)
  if namespace.nil?
    namespace = @service.namespace
  end

  # We don't want to handle any cases about deleting ambiguously named
  # entities.
  if !namespace.is_exact?
    raise StandardError.new("Must provide an exact namespace to delete an entity.")
  end

  @service.request(:method => :DELETE,
                   :namespace => namespace,
                   :resource => @resource + [name])
  return self
end

#delete_if(&block) ⇒ Object

Deletes all entities on this collection for which the block returns true.

If block is omitted, returns an enumerator over all members of the collection.



409
410
411
412
413
414
415
416
417
418
# File 'lib/splunk-sdk-ruby/collection.rb', line 409

def delete_if(&block)
  # Without a block, just return an enumerator
  return each() if !block_given?

  values.each() do |entity|
    if block.call(entity)
      delete(entity.name, entity.namespace)
    end
  end
end