class Soar::Registry::Directory::Provider::DynamoDb

Constants

LIMIT

Attributes

client[RW]

Public Class Methods

new(credentials: , table: , configuration: ) click to toggle source

@param [Hash{String => String}] credentials @option credentials [String] username @option credentials [String] password @param [Hash{String => String}] table @option table [String] name @option table [Hash{String => String}] index

* partition_key (String) 
* sort_key (String)
* global_secondary_indexes (Array<String>)

@param [Hash{String => String}] configuration @option configuration [String] region @option configuration [String] endpoint

# File lib/soar/registry/directory/provider/dynamo_db.rb, line 28
def initialize(credentials: , table: , configuration: )
  @table_name = table['name']
  index = table['index']
  @partition_key = index['partition_key']
  @sort_key = index['sort_key'] if index.key?('sort_key')
  @global_secondary_indexes = index['global_secondary_index'] if index.key?('global_secondary_index')
  configuration['credentials'] = Aws::Credentials.new(credentials['username'], credentials['password'])
  @client = Aws::DynamoDB::Client.new(Hashie.symbolize_keys(configuration))
end

Public Instance Methods

fetch(primary_key) click to toggle source

@param [Hash{String => String}] primary_key @option primary_key [String] partition_key required @option primary_key [String] sort_key required only when defined in schema @return [Hash{String => Hash, String, Number, Array }] a single matching entry @raise [Soar::Registry::Directory::Error::NoEntriesFoundError] if primary key not found

# File lib/soar/registry/directory/provider/dynamo_db.rb, line 67
def fetch(primary_key)
  adapt_exceptions do
    options = {
      table_name: @table_name,
      key: primary_key
    }
    identity = @client.get_item(options)
    raise Soar::Registry::Directory::Error::NoEntriesFoundError, "No entries found for primary_key = #{primary_key}" if identity.item.nil?
    identity.item
  end
end
index() click to toggle source

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

# File lib/soar/registry/directory/provider/dynamo_db.rb, line 108
def index
  adapt_exceptions do
    resp = @client.describe_table({
      table_name: @table_name
    })
    indexed_attributes = []
    resp['table']['key_schema'].each { |key_schema|
      indexed_attributes << key_schema['attribute_name']
    } if resp['table'].key?('key_schema')

    resp['table']['global_secondary_indexes'].each { |index| 
      index['key_schema'].each { |key_schema| 
        indexed_attributes << key_schema['attribute_name']
      }
    } if resp['table'].key?('global_secondary_indexes')

    indexed_attributes
  end
end
put(entry) click to toggle source

@param [Hash{String => String, Hash, Number, Array}] entry @return [Boolean] @raise [Soar::Registry::Directory::Error::DuplicateEntryError]

# File lib/soar/registry/directory/provider/dynamo_db.rb, line 43
def put(entry)
  condition_expression = "attribute_not_exists(#{@partition_key})"
  condition_expression += " AND attribute_not_exists(#{@sort_key})" if @sort_key
  #@global_secondary_indexes.each { |global_secondary_index|
  #  condition_expression += " AND attribute_not_exists(#{global_secondary_index})"
  #} if @global_secondary_indexes

  adapt_exceptions do
    @client.put_item({
      table_name: @table_name,
      item: entry,
      condition_expression: condition_expression
    })
    return true
  end
end
recreate_table(name:, structure:) click to toggle source

Deletes existing table and creates a new one in its place @param [String] name the table name @param [Hash{String => Hash, Array, String, Number]] structure table structure @return [Boolean]

# File lib/soar/registry/directory/provider/dynamo_db.rb, line 134
def recreate_table(name:, structure:)
  adapt_exceptions do
    if @client.list_tables.table_names.include?(name) 
      result = @client.delete_table({
        table_name: name
      })
    end
    @client.create_table(Hashie.symbolize_keys(structure))
    return true
  end

end

Private Instance Methods

adapt_exceptions() { || ... } click to toggle source
# File lib/soar/registry/directory/provider/dynamo_db.rb, line 149
def adapt_exceptions
  begin
    yield
  rescue Seahorse::Client::NetworkingError => e
    raise Soar::Registry::Directory::Error::NetworkingError, e.message
  rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException => e
    raise Soar::Registry::Directory::Error::DuplicateEntryError, e.message
  rescue StandardError => e
    raise e, e.message
  end
end