Class: Splunk::ReadOnlyEntity

Inherits:
Object
  • Object
show all
Extended by:
Synonyms
Defined in:
lib/splunk-sdk-ruby/entity.rb

Overview

ReadOnlyEntity represents entities that can be read, but not created or updated, via the REST API. The canonical example is a modular input kind.

Direct Known Subclasses

Entity, ModularInputKind

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, namespace, resource, name, state = nil) ⇒ ReadOnlyEntity

:nodoc:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/splunk-sdk-ruby/entity.rb', line 32

def initialize(service, namespace, resource, name, state=nil) # :nodoc:
  @service = service
  @namespace = namespace
  if !@namespace.is_exact?
    raise StandardError.new("Must provide an exact namespace to " +
                                "Entity (found: #{@namespace}")
  end
  @resource = resource
  @name = name
  @state = state
  if !state # If the state was not provided, we need to fetch it.
    refresh()
  end
end

Instance Attribute Details

#nameObject (readonly)

The name of this Entity.

Returns: a String.



52
53
54
# File 'lib/splunk-sdk-ruby/entity.rb', line 52

def name
  @name
end

#namespaceObject (readonly)

The namespace of this Entity.

Returns: a Namespace.



59
60
61
# File 'lib/splunk-sdk-ruby/entity.rb', line 59

def namespace
  @namespace
end

#resourceObject (readonly)

The path of the collection this entity lives in.

For example, on an app this will be [“apps”, “local”].

Returns: an Array of Strings.



68
69
70
# File 'lib/splunk-sdk-ruby/entity.rb', line 68

def resource
  @resource
end

#serviceObject (readonly)

The service this entity refers to.

Returns: a Service object.



75
76
77
# File 'lib/splunk-sdk-ruby/entity.rb', line 75

def service
  @service
end

Instance Method Details

#[]Object

Fetch a field on this entity.

Returns: a String.



94
# File 'lib/splunk-sdk-ruby/entity.rb', line 94

synonym "[]", "fetch"

#fetch(key, default = nil) ⇒ Object

Fetches the field key on this entity.

You may provide a default value. All values are returned as strings.

Returns: a String.



85
86
87
# File 'lib/splunk-sdk-ruby/entity.rb', line 85

def fetch(key, default=nil)
  @state["content"].fetch(key, default)
end

Returns a Hash of the links associated with this entity.

The links typically include keys such as “list”, “edit”, or “disable”.

Returns: a Hash of Strings to URL objects.



104
105
106
# File 'lib/splunk-sdk-ruby/entity.rb', line 104

def links()
  return @state["links"]
end

#read(*field_list) ⇒ Object

DEPRECATED. Use fetch and [] instead (since entities now cache their state).

Returns all or a specified subset of key/value pairs on this Entity

In the absence of arguments, returns a Hash of all the fields on this Entity. If you specify one or more Strings or Arrays of Strings, all the keys specified in the arguments will be returned in the Hash.

Returns: a Hash with Strings as keys, and Strings or Hashes or Arrays as values.



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/splunk-sdk-ruby/entity.rb', line 121

def read(*field_list)
  warn "[DEPRECATION] Entity#read is deprecated. Use [] and fetch instead."
  if field_list.empty?
    return @state["content"].clone()
  else
    field_list = field_list.flatten()
    result = {}
    field_list.each() do |key|
      result[key] = fetch(key).clone()
    end
    return result
  end
end

#readmetaObject

Returns the metadata for this Entity.

This method is identical to

entity.read('eai:acl', 'eai:attributes')

Returns: a Hash with the keys “eai:acl” and “eai:attributes”.



144
145
146
# File 'lib/splunk-sdk-ruby/entity.rb', line 144

def readmeta
  read('eai:acl', 'eai:attributes')
end

#refreshObject

Refreshes the cached state of this Entity.

Returns: the Entity.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/splunk-sdk-ruby/entity.rb', line 153

def refresh()
  response = @service.request(:resource => @resource + [name],
                              :namespace => @namespace)
  if response.code == 204 or response.body.nil?
    # This code is here primarily to handle the case of a job not yet being
    # ready, in which case you get back empty bodies.
    raise EntityNotReady.new((@resource + [name]).join("/"))
  end
  # We are guaranteed a unique entity, since entities must have
  # exact namespaces.
  feed = AtomFeed.new(response.body)
  @state = feed.entries[0]
  self
end