Class: CredStash::Repository::DynamoDB

Inherits:
Object
  • Object
show all
Defined in:
lib/cred_stash/repository/dynamo_db.rb

Instance Method Summary collapse

Constructor Details

#initialize(client: nil) ⇒ DynamoDB

Returns a new instance of DynamoDB.



3
4
5
# File 'lib/cred_stash/repository/dynamo_db.rb', line 3

def initialize(client: nil)
  @client = client || Aws::DynamoDB::Client.new
end

Instance Method Details

#delete(item) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/cred_stash/repository/dynamo_db.rb', line 53

def delete(item)
  @client.delete_item(
    table_name: CredStash.config.table_name,
    key: {
      name: item.name,
      version: item.version
    }
  )
end

#get(name, version: nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/cred_stash/repository/dynamo_db.rb', line 7

def get(name, version: nil)
  select(name, limit: 1, version: version).first.tap do |item|
    unless item
      if version
        raise CredStash::ItemNotFound, "#{name} --version: #{version} is not found"
      else
        raise CredStash::ItemNotFound, "#{name} is not found"
      end
    end
  end
end

#listObject



47
48
49
50
51
# File 'lib/cred_stash/repository/dynamo_db.rb', line 47

def list
  fetch_all_items.map do |item|
    Item.new(name: item['name'], version: item['version'])
  end
end

#put(item) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cred_stash/repository/dynamo_db.rb', line 32

def put(item)
  @client.put_item(
    table_name: CredStash.config.table_name,
    item: {
      name: item.name,
      version: item.version,
      key: item.key,
      contents: item.contents,
      hmac: item.hmac
    },
    condition_expression: "attribute_not_exists(#name)",
    expression_attribute_names: { "#name" => "name" },
  )
end

#select(name, pluck: nil, limit: nil, version: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cred_stash/repository/dynamo_db.rb', line 19

def select(name, pluck: nil, limit: nil, version: nil)
  params = set_params(name, pluck: pluck, limit: limit, version: version)

  @client.query(params).items.map do |item|
    Item.new(
      key: item["key"],
      contents: item["contents"],
      name: item["name"],
      version: item["version"]
    )
  end
end

#setupObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cred_stash/repository/dynamo_db.rb', line 63

def setup
  @client.create_table(
    table_name: CredStash.config.table_name,
    key_schema: [
      { attribute_name: 'name', key_type: 'HASH' },
      { attribute_name: 'version', key_type: 'RANGE' },
    ],
    attribute_definitions: [
      { attribute_name: 'name', attribute_type: 'S' },
      { attribute_name: 'version', attribute_type: 'S' },
    ],
    provisioned_throughput: {
      read_capacity_units: 1,
      write_capacity_units: 1,
    },
  )
end