Class: TerraspacePluginAws::Interfaces::Backend::Table
- Inherits:
-
Base
- Object
- Base
- TerraspacePluginAws::Interfaces::Backend::Table
show all
- Defined in:
- lib/terraspace_plugin_aws/interfaces/backend/table.rb
Instance Method Summary
collapse
Methods inherited from Base
#initialize
Methods included from Logging
#logger
Methods included from Clients
#dynamodb, #s3, #secretsmanager, #ssm, #sts
Instance Method Details
#create ⇒ Object
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# File 'lib/terraspace_plugin_aws/interfaces/backend/table.rb', line 3
def create
table = @info["dynamodb_table"]
return unless table
if exist?(table)
logger.debug "Table already exist: #{table}"
c = TerraspacePluginAws::Interfaces::Config.instance.config
tag_existing(table) if c.tag_existing
else
logger.info "Creating dynamodb table: #{table}"
create_table(table)
end
end
|
#create_table(name) ⇒ Object
17
18
19
20
21
|
# File 'lib/terraspace_plugin_aws/interfaces/backend/table.rb', line 17
def create_table(name)
dynamodb.create_table(table_definition(name))
logger.info "Waiting for dynamodb table to finish creating..."
dynamodb.wait_until(:table_exists, table_name: name)
end
|
#exist?(name) ⇒ Boolean
93
94
95
96
97
98
|
# File 'lib/terraspace_plugin_aws/interfaces/backend/table.rb', line 93
def exist?(name)
dynamodb.describe_table(table_name: name)
true rescue Aws::DynamoDB::Errors::ResourceNotFoundException
false end
|
#secure(definition) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/terraspace_plugin_aws/interfaces/backend/table.rb', line 45
def secure(definition)
c = TerraspacePluginAws::Interfaces::Config.instance.config.dynamodb
return unless c.encryption
spec = {enabled: true}
if c.kms_master_key_id
spec[:sse_type] = c.sse_type spec[:kms_master_key_id] = c.kms_master_key_id
end
definition[:sse_specification] = spec
definition
end
|
#table_definition(name) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/terraspace_plugin_aws/interfaces/backend/table.rb', line 23
def table_definition(name)
definition = {
attribute_definitions: [
{
attribute_name: "LockID",
attribute_type: "S",
}
],
key_schema: [
{
attribute_name: "LockID",
key_type: "HASH",
}
],
billing_mode: "PAY_PER_REQUEST", table_name: name,
}
secure(definition)
tag(definition)
definition
end
|
#tag(definition) ⇒ Object
70
71
72
|
# File 'lib/terraspace_plugin_aws/interfaces/backend/table.rb', line 70
def tag(definition)
definition[:tags] = tags unless tags.empty?
end
|
#tag_existing(table_name) ⇒ Object
74
75
76
77
78
79
80
81
82
|
# File 'lib/terraspace_plugin_aws/interfaces/backend/table.rb', line 74
def tag_existing(table_name)
return if tags.empty?
resp = dynamodb.describe_table(table_name: table_name)
dynamodb.tag_resource(
resource_arn: resp.table.table_arn,
tags: tags
)
end
|
84
85
86
87
88
89
90
91
|
# File 'lib/terraspace_plugin_aws/interfaces/backend/table.rb', line 84
def tags
c = TerraspacePluginAws::Interfaces::Config.instance.config
tags = !c.dynamodb.tags.empty? ? c.dynamodb.tags : c.tags
tags = tags.map do |k,v|
{key: k.to_s, value: v}
end
end
|