Class: Terrafying::DynamoDb::StateStore
- Inherits:
-
Object
- Object
- Terrafying::DynamoDb::StateStore
- Defined in:
- lib/terrafying/dynamodb/state.rb
Instance Method Summary collapse
- #get ⇒ Object
-
#initialize(scope, opts = {}) ⇒ StateStore
constructor
A new instance of StateStore.
- #put(state) ⇒ Object
- #table ⇒ Object
Constructor Details
#initialize(scope, opts = {}) ⇒ StateStore
Returns a new instance of StateStore.
7 8 9 10 11 |
# File 'lib/terrafying/dynamodb/state.rb', line 7 def initialize(scope, opts = {}) @scope = scope @client = Terrafying::DynamoDb.client @table_name = Terrafying::DynamoDb.config.state_table end |
Instance Method Details
#get ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/terrafying/dynamodb/state.rb', line 13 def get @client.ensure_table(table) do resp = @client.query({ table_name: @table_name, limit: 1, key_conditions: { "scope" => { attribute_value_list: [@scope], comparison_operator: "EQ", } }, scan_index_forward: false, }) case resp.items.count when 0 then return nil when 1 then return resp.items.first["state"] else raise 'More than one item found when retrieving state. This is a bug and should never happen.' if resp.items.count != 1 end end end |
#put(state) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/terrafying/dynamodb/state.rb', line 34 def put(state) @client.ensure_table(table) do sha256 = Digest::SHA256.hexdigest(state) json = JSON.parse(state) @client.update_item({ table_name: @table_name, key: { "scope" => @scope, "serial" => json["serial"].to_i, }, return_values: "NONE", update_expression: "SET sha256 = :sha256, #state = :state", condition_expression: "attribute_not_exists(serial) OR sha256 = :sha256", expression_attribute_names: { "#state" => "state", }, expression_attribute_values: { ":sha256" => sha256, ":state" => state, } }) end end |
#table ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/terrafying/dynamodb/state.rb', line 58 def table { table_name: @table_name, attribute_definitions: [ { attribute_name: "scope", attribute_type: "S", }, { attribute_name: "serial", attribute_type: "N", } ], key_schema: [ { attribute_name: "scope", key_type: "HASH", }, { attribute_name: "serial", key_type: "RANGE", }, ], provisioned_throughput: { read_capacity_units: 1, write_capacity_units: 1, } } end |