Class: LambdaWrap::DynamoTable
- Inherits:
-
AwsService
- Object
- AwsService
- LambdaWrap::DynamoTable
- Defined in:
- lib/lambda_wrap/dynamo_db_manager.rb
Overview
The DynamoTable class simplifies Creation, Updating, and Destroying Dynamo DB Tables.
Instance Method Summary collapse
-
#delete(client, region = 'AWS_REGION') ⇒ Object
Deletes all DynamoDB tables that are prefixed with the @table_name specified in the constructor.
-
#deploy(environment_options, client, region = 'AWS_REGION') ⇒ Object
Deploys the DynamoDB Table to the target environment.
-
#initialize(options) ⇒ DynamoTable
constructor
Sets up the DynamoTable for the Dynamo DB Manager.
-
#teardown(environment_options, client, region = 'AWS_REGION') ⇒ Object
Deletes the DynamoDB table specified by the table_name and the Environment name (if append_environment_on_deploy) was specified.
- #to_s ⇒ Object
Constructor Details
#initialize(options) ⇒ DynamoTable
Sets up the DynamoTable for the Dynamo DB Manager. Preloading the configuration in the constructor.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/lambda_wrap/dynamo_db_manager.rb', line 99 def initialize() = { append_environment_on_deploy: false, read_capacity_units: 1, write_capacity_units: 1, local_secondary_indexes: nil, global_secondary_indexes: nil, attribute_definitions: [{ attribute_name: 'Id', attribute_type: 'S' }], key_schema: [{ attribute_name: 'Id', key_type: 'HASH' }] } = .reverse_merge() @table_name = [:table_name] raise ArgumentError, ':table_name is required.' unless @table_name @attribute_definitions = [:attribute_definitions] @key_schema = [:key_schema] # Verify that all of key_schema is defined in attribute_definitions defined_in_attribute_definitions_guard(@key_schema) @read_capacity_units = [:read_capacity_units] @write_capacity_units = [:write_capacity_units] provisioned_throughput_guard(read_capacity_units: @read_capacity_units, write_capacity_units: @write_capacity_units) unless @read_capacity_units >= 1 && @write_capacity_units >= 1 && (@read_capacity_units.is_a? Integer) && (@write_capacity_units.is_a? Integer) raise ArgumentExecption, 'Read and Write Capacity must be positive integers.' end @local_secondary_indexes = [:local_secondary_indexes] if @local_secondary_indexes && @local_secondary_indexes.length > 5 raise ArgumentError, 'Can only have 5 LocalSecondaryIndexes per table!' end if @local_secondary_indexes && !@local_secondary_indexes.empty? @local_secondary_indexes.each { |lsindex| defined_in_attribute_definitions_guard(lsindex[:key_schema]) } end @global_secondary_indexes = [:global_secondary_indexes] if @global_secondary_indexes && @global_secondary_indexes.length > 5 raise ArgumentError, 'Can only have 5 GlobalSecondaryIndexes per table1' end if @global_secondary_indexes && !@global_secondary_indexes.empty? @global_secondary_indexes.each do |gsindex| defined_in_attribute_definitions_guard(gsindex[:key_schema]) provisioned_throughput_guard(gsindex[:provisioned_throughput]) end end @append_environment_on_deploy = [:append_environment_on_deploy] end |
Instance Method Details
#delete(client, region = 'AWS_REGION') ⇒ Object
Deletes all DynamoDB tables that are prefixed with the @table_name specified in the constructor. This is an attempt to tear down all DynamoTables that were deployed with the environment name appended.
197 198 199 200 201 202 203 204 |
# File 'lib/lambda_wrap/dynamo_db_manager.rb', line 197 def delete(client, region = 'AWS_REGION') super puts "Deleting all tables with prefix: #{@table_name}." table_names = retrieve_prefixed_tables(@table_name) table_names.each { |table_name| delete_table(table_name) } puts "Deleted #{table_names.length} tables." table_names.length end |
#deploy(environment_options, client, region = 'AWS_REGION') ⇒ Object
Deploys the DynamoDB Table to the target environment. If the @append_environment_on_deploy option is set, the table_name will be appended with a hyphen and the environment name. This will attempt to Create or Update with the parameters specified from the constructor. This may take a LONG time for it will wait for any new indexes to be available.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/lambda_wrap/dynamo_db_manager.rb', line 158 def deploy(, client, region = 'AWS_REGION') super puts "Deploying Table: #{@table_name} to Environment: #{.name}" full_table_name = @table_name + (@append_environment_on_deploy ? "-#{.name}" : '') table_details = retrieve_table_details(full_table_name) if table_details.nil? create_table(full_table_name) else wait_until_table_is_available(full_table_name) if table_details[:table_status] != 'ACTIVE' update_table(full_table_name, table_details) end puts "Dynamo Table #{full_table_name} is now available." full_table_name end |
#teardown(environment_options, client, region = 'AWS_REGION') ⇒ Object
Deletes the DynamoDB table specified by the table_name and the Environment name (if append_environment_on_deploy) was specified. Otherwise just deletes the table.
184 185 186 187 188 189 190 |
# File 'lib/lambda_wrap/dynamo_db_manager.rb', line 184 def teardown(, client, region = 'AWS_REGION') super puts "Tearingdown Table: #{@table_name} from Environment: #{.name}" full_table_name = @table_name + (@append_environment_on_deploy ? "-#{.name}" : '') delete_table(full_table_name) full_table_name end |
#to_s ⇒ Object
206 207 208 209 |
# File 'lib/lambda_wrap/dynamo_db_manager.rb', line 206 def to_s return @table_name if @table_name && @table_name.is_a?(String) super end |