Module: Conjur::Exists

Included in:
ActsAsAsset, Deputy, Resource, Role
Defined in:
lib/conjur/exists.rb

Overview

Provides an exists? method for things that may or may not exist.

Most conjur assets returned by api.asset_name methods (e.g., API#group, API#user) may or may not exist. The #exists? method lets you determine whether or not such assets do in fact exist.

Instance Method Summary collapse

Instance Method Details

#exists?(options = {}) ⇒ Boolean

Check whether this asset exists by performing a HEAD request to its URL.

This method will return false if the asset doesn't exist.

Examples:

does_not_exist = api.user 'does-not-exist' # This returns without error.

# this is wrong!
owner = does_not_exist.ownerid # raises RestClient::ResourceNotFound

# this is right!
owner = if does_not_exist.exists?
  does_not_exist.ownerid
 else
    nil # or some sensible default
 end

Parameters:

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

    included for compatibility: don't use this argument!

Returns:

  • (Boolean)

    does it exist?



49
50
51
52
53
54
55
56
57
58
# File 'lib/conjur/exists.rb', line 49

def exists?(options = {})
  begin
    self.head(options)
    true
  rescue RestClient::Forbidden
    true
  rescue RestClient::ResourceNotFound
    false
  end
end