Module: Conjur::HasAttributes

Included in:
ActsAsAsset, Deputy, HasIdentifier, HasOwner, HostFactoryToken, Resource
Defined in:
lib/conjur/has_attributes.rb

Overview

Many Conjur assets have key-value attributes. Although these should generally be accessed via methods on specific asset classes (for example, Resource#owner), the are available as a Hash on all types supporting attributes.

Instance Method Summary collapse

Instance Method Details

#attributesHash

Note:

this method will use a cached copy of the objects attributes instead of fetching them

Get the attributes for this asset.

Although the Hash returned by this method is mutable, you should treat as immutable unless you know exactly what you're doing. Each asset's attributes are constrained by a server side schema, which means that you will get an error if you violate the schema. and then try to save the asset.

with each call. To ensure that the attributes are fresh, you can use the #refresh method

Returns:

  • (Hash)

    the asset's attributes.



52
53
54
55
# File 'lib/conjur/has_attributes.rb', line 52

def attributes
  return @attributes if @attributes
  fetch
end

#invalidate(&block)

Note:

this is mainly used internally, but included in the public api for completeness.

This method returns an undefined value.

Call a block that will perform actions that might change the asset's attributes. No matter what happens in the block, this method ensures that the cached attributes will be invalidated.



103
104
105
106
107
# File 'lib/conjur/has_attributes.rb', line 103

def invalidate(&block)
  yield
ensure
  @attributes = nil
end

#refreshHash

Note:

any changes to #attributes without a call to #save will be overwritten by this method.

Reload this asset's attributes. This method can be used to guarantee a current view of the entity in the case that it has been modified by an update method or by an external party.

Examples:

res = api.resources.firs
res.attributes # => {  ... }
res.attributes['hello'] = 'blah'
res.refresh
res.attributes['hello'] # => nil

Returns:

  • (Hash)

    the asset's attributes.



92
93
94
# File 'lib/conjur/has_attributes.rb', line 92

def refresh
  fetch
end

#save

Note:

If the objects attributes haven't been fetched (for example, by calling #attributes), this method is a no-op.

This method returns an undefined value.

Update this asset's attributes on the server.

Although you can manipulate an assets attributes and then call #save, the attributes are constrained by a server side schema, and attempting to set an attribute that doesn't exist will result in a 422 Unprocessable Entity error.

If you want to set arbitrary metadata on an asset, you might consider using the Resource#tags method instead.



72
73
74
75
76
# File 'lib/conjur/has_attributes.rb', line 72

def save
  if @attributes
    self.put(attributes.to_json)
  end
end

#to_json(options = {}) ⇒ Object

Returns this objects #attributes. This is primarily to support simple JSON serialization of Conjur assets.

Parameters:

  • options (Hash, nil) (defaults to: {})

    unused, kept for compatibility reasons

See Also:



31
32
33
# File 'lib/conjur/has_attributes.rb', line 31

def to_json(options = {})
  attributes
end