class Soar::Registry::Staff::Directory::DynamoDb

Constants

INDEXED_ATTRIBUTES
LIMIT

Attributes

client[R]
configuration[R]
credentials[R]

Public Instance Methods

authenticate(credentials) click to toggle source
# File lib/soar/registry/staff/directory/dynamo_db.rb, line 40
def authenticate(credentials)
  raise Soar::Registry::Staff::Directory::Error::AuthenticationError, 'missing username' if not credentials.key?('username')
  raise Soar::Registry::Staff::Directory::Error::AuthenticationError, 'missing password' if not credentials.key?('password')
  @credentials = {
    "access_key_id" => credentials['username'],
    "secret_access_key" => credentials['password']
  }
end
bootstrap(configuration) click to toggle source

@param [Hash] configuration for the directory provider @return [Nil] @raise [Soar::Registry::Staff::Directory::Error::BootstrapError] if required configuration is missing

# File lib/soar/registry/staff/directory/dynamo_db.rb, line 21
def bootstrap(configuration)
  @configuration = Hashie.stringify_keys(configuration)
  raise Soar::Registry::Staff::Directory::Error::BootstrapError, 'Missing region' if not @configuration.has_key?('region')
  raise Soar::Registry::Staff::Directory::Error::BootstrapError, 'Missing endpoint' if not @configuration.has_key?('endpoint')
  @table_name = @configuration.delete('table')
  raise Soar::Registry::Staff::Directory::Error::BootstrapError, 'Missing table name' if not @table_name
end
bootstrapped?() click to toggle source

@return [Bool] whether directory provider received minimum required configuration

# File lib/soar/registry/staff/directory/dynamo_db.rb, line 32
def bootstrapped?
  @configuration.has_key?('region') and @configuration.has_key?('endpoint') 
end
connect() click to toggle source
# File lib/soar/registry/staff/directory/dynamo_db.rb, line 49
def connect
  begin
    options = @configuration
    options['credentials'] = Aws::Credentials.new(@credentials['access_key_id'], @credentials['secret_access_key'])
    @client = Aws::DynamoDB::Client.new(Hashie.symbolize_keys(options))
    true
  rescue ArgumentError => e
    raise Soar::Registry::Staff::Directory::Error::ConnectionError, e.message 
  rescue 
    raise Soar::Registry::Staff::Directory::Error::ConnectionError, 'error creating client'
  end
end
connected?() click to toggle source
# File lib/soar/registry/staff/directory/dynamo_db.rb, line 62
def connected?
  return @client.class.name == 'Aws::DynamoDB::Client'
end
fetch_identity(identity_id) click to toggle source

@param [String] identity_id primary key of the identity @return [Hash] the identity @raise [Soar::Registry::Staff::Directory::DynamoDb::Error::UniqueIdentifierNotFoundError] if primary key not found

# File lib/soar/registry/staff/directory/dynamo_db.rb, line 90
def fetch_identity(identity_id)
  options = {
    table_name: @table_name,
    key: {
      identity_id: identity_id
    }
  }
  identity = @client.get_item(options)
  raise Soar::Registry::Staff::Directory::Error::UniqueIdentifierNotFoundError, 'Unable to find identity' if identity.item.nil?
  identity.item
end
indexed_attributes() click to toggle source

@return [Array] a list of primary keys and global secondary indexes

# File lib/soar/registry/staff/directory/dynamo_db.rb, line 131
def indexed_attributes
  resp = @client.describe_table({
    table_name: @table_name
  })
  indexed_attributes = []
  resp['table']['key_schema'].each { |key_schema|
    indexed_attributes << key_schema['attribute_name']
  }
  resp['table']['global_secondary_indexes'].each { |index| 
    index['key_schema'].each { |key_schema| 
      indexed_attributes << key_schema['attribute_name']
    }
  }
  indexed_attributes
end
put_identity(entity) click to toggle source
# File lib/soar/registry/staff/directory/dynamo_db.rb, line 78
def put_identity(entity)
  @client.put_item({
    table_name: @table_name,
    item: Hashie.symbolize_keys(entity)
  })
end
ready?() click to toggle source
# File lib/soar/registry/staff/directory/dynamo_db.rb, line 66
def ready?
  begin
    #return @client.list_tables.table_names.class.name == 'Array'
    resp = @client.describe_table({
      table_name: @table_name
    })
    return resp.table.table_name == @table_name
  rescue Aws::DynamoDB::Errors::ResourceNotFoundException, Seahorse::Client::NetworkingError
    return false
  end
end
search_identities(identifier_attribute, identifier_value) click to toggle source

@param [String] identifier_attribute @param [String] identifier_value @return [Array] list of identities @raise [ArgumentError] if query or index is not specified

# File lib/soar/registry/staff/directory/dynamo_db.rb, line 108
def search_identities(identifier_attribute, identifier_value)
  raise ArgumentError, "Attribute is required" if identifier_attribute.nil?
  raise ArgumentError, 'Value is required' if identifier_value.nil?
  options = {
    table_name: @table_name,
    select: 'ALL_ATTRIBUTES',
    limit: LIMIT,
    key_condition_expression: "#{identifier_attribute} = :value",
    expression_attribute_values: {
      ":value": identifier_value 
    }
  }

  options.merge!({index_name: "#{identifier_attribute}_index"}) if INDEXED_ATTRIBUTES.include?(identifier_attribute)
  identity = @client.query(options)
  identity.items.map { |item| 
    Hashie.stringify_keys(item)
  }
end
uri() click to toggle source
# File lib/soar/registry/staff/directory/dynamo_db.rb, line 36
def uri
  @configuration['endpoint']
end