class Terraforming::Resource::DynamoDB

Public Class Methods

new(client) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 13
def initialize(client)
  @client = client
end
tf(client: Aws::DynamoDB::Client.new) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 5
def self.tf(client: Aws::DynamoDB::Client.new)
  self.new(client).tf
end
tfstate(client: Aws::DynamoDB::Client.new) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 9
def self.tfstate(client: Aws::DynamoDB::Client.new)
  self.new(client).tfstate
end

Public Instance Methods

tf() click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 17
def tf
  apply_template(@client, "tf/dynamo_db")
end
tfstate() click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 21
def tfstate
  tables.inject({}) do |resources, dynamo_db_table|
    attributes = {
      "arn"                       => dynamo_db_table["table_arn"],
      "id"                        => dynamo_db_table["table_name"],
      "name"                      => dynamo_db_table["table_name"],
      "read_capacity"             => dynamo_db_table["provisioned_throughput"]["read_capacity_units"].to_s,
      "stream_arn"                => dynamo_db_table["latest_stream_arn"].to_s,
      "stream_label"              => dynamo_db_table["latest_stream_label"].to_s,
      "write_capacity"            => dynamo_db_table["provisioned_throughput"]["write_capacity_units"].to_s
    }

    attributes.merge!(attribute_definitions(dynamo_db_table))
    attributes.merge!(global_indexes(dynamo_db_table))
    attributes.merge!(local_indexes(dynamo_db_table))
    attributes.merge!(key_schema(dynamo_db_table))
    attributes.merge!(point_in_time_summary(dynamo_db_table))
    attributes.merge!(sse_description(dynamo_db_table))
    attributes.merge!(stream_specification(dynamo_db_table))
    attributes.merge!(tags_of(dynamo_db_table))
    attributes.merge!(ttl_of(dynamo_db_table))

    resources["aws_dynamodb_table.#{module_name_of(dynamo_db_table)}"] = {
      "type"    => "aws_dynamodb_table",
      "primary" => {
        "id" => dynamo_db_table.table_name,
        "attributes" => attributes,
        "meta" => {
          "schema_version" => "1"
        }
      }
    }
  resources
  end
end

Private Instance Methods

attribute_definitions(dynamo_db_table) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 70
def attribute_definitions(dynamo_db_table)
  attributes = { "attribute.#" => dynamo_db_table["attribute_definitions"].length.to_s}
  dynamo_db_table["attribute_definitions"].each do |attr_defn|
    attributes.merge!(attributes_definitions_of(attr_defn))
  end
  attributes
end
attribute_hashcode(attr_defn) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 87
def attribute_hashcode(attr_defn)
  hashcode = Zlib.crc32(attr_defn.attribute_name+"-")
end
attributes_definitions_of(attr_defn) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 78
def attributes_definitions_of(attr_defn)
  hashcode = attribute_hashcode(attr_defn)
  attributes = {
    "attribute.#{hashcode}.name" => attr_defn.attribute_name,
    "attribute.#{hashcode}.type" => attr_defn.attribute_type,
  }
  attributes
end
dynamo_db_tables() click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 258
def dynamo_db_tables
  a = @client.list_tables.map(&:table_names).flatten
end
find_key(index,key_type) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 120
def find_key(index,key_type)
  index["key_schema"].each do |schema|
    if schema.key_type == key_type
      return schema.attribute_name
    else
      return ""
    end
  end
end
global_index_hashcode(global_sec_index) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 143
def global_index_hashcode(global_sec_index)
  Zlib.crc32(global_sec_index["index_name"]+"-")
end
global_index_non_key_attributes(global_sec_index) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 130
def global_index_non_key_attributes(global_sec_index)
  attributes = {}
  if !global_sec_index["projection"]["non_key_attributes"].nil?
    hashcode = global_index_hashcode(global_sec_index)
    attributes = {"global_secondary_index.#{hashcode}.non_key_attributes.#" => global_sec_index["projection"]["non_key_attributes"].length.to_s}
    (0..global_sec_index["projection"]["non_key_attributes"].length.to_i-1).each do |index|
      attributes.merge!({"global_secondary_index.#{hashcode}.non_key_attributes.#{index}" => global_sec_index["projection"]["non_key_attributes"][index]})
    end
  end
  attributes
end
global_indexes(dynamo_db_table) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 91
def global_indexes(dynamo_db_table)
  attributes = {}
  if dynamo_db_table["global_secondary_indexes"]
    attributes = { "global_secondary_index.#"  => dynamo_db_table["global_secondary_indexes"].length.to_s}
    dynamo_db_table["global_secondary_indexes"].each do |global_sec_index|
      attributes.merge!(global_secondary_indexes_of(global_sec_index))
    end
  end
  return attributes
end
global_indexes_of(global_sec_index) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 107
def global_indexes_of(global_sec_index)
  hashcode = global_index_hashcode(global_sec_index)
  attributes = {
    "global_secondary_index.#{hashcode}.hash_key" => find_key(global_sec_index,"HASH"),
    "global_secondary_index.#{hashcode}.name" => global_sec_index.index_name,
    "global_secondary_index.#{hashcode}.projection_type" => global_sec_index.projection.projection_type,
    "global_secondary_index.#{hashcode}.range_key" => find_key(global_sec_index,"RANGE"),
    "global_secondary_index.#{hashcode}.read_capacity" => global_sec_index.provisioned_throughput.read_capacity_units.to_s ,
    "global_secondary_index.#{hashcode}.write_capacity" => global_sec_index.provisioned_throughput.write_capacity_units.to_s,
  }
  attributes
end
global_secondary_indexes_of(global_sec_index) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 103
def global_secondary_indexes_of(global_sec_index)
  attributes = global_indexes_of(global_sec_index).merge!(global_index_non_key_attributes(global_sec_index))
end
key_schema(dynamo_db_table) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 186
def key_schema(dynamo_db_table)
  attributes = {}
  if dynamo_db_table["key_schema"]
    attributes = {"key_schema.#"  => dynamo_db_table["key_schema"].length.to_s}
    if !find_key(dynamo_db_table,"HASH").empty? 
      attributes.merge!({"hash_key" => find_key(dynamo_db_table,"HASH")})
    end
  end
  attributes
end
local_index_hashcode(local_index) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 182
def local_index_hashcode(local_index)
  Zlib.crc32(local_index["index_name"]+"-")
end
local_index_non_key_attributes(local_sec_index) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 170
def local_index_non_key_attributes(local_sec_index)
  attributes = {}
  if !local_sec_index["projection"]["non_key_attributes"].nil?
    hashcode = local_index_hashcode(local_sec_index)
    attributes = {"local_secondary_index.#{hashcode}.non_key_attributes.#" => local_sec_index["projection"]["non_key_attributes"].length.to_s}
    (0..local_sec_index["projection"]["non_key_attributes"].length.to_i-1).each do |index|
      attributes.merge!({"local_secondary_index.#{hashcode}.non_key_attributes.#{index}" => local_sec_index["projection"]["non_key_attributes"][index]})
    end
  end
  attributes
end
local_indexes(dynamo_db_table) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 147
def local_indexes(dynamo_db_table)
  attributes = {}
  if dynamo_db_table["local_secondary_indexes"]
    attributes = {"local_secondary_index.#"  => dynamo_db_table["local_secondary_indexes"].length.to_s}
    dynamo_db_table["local_secondary_indexes"].each do |local_sec_index|
      attributes.merge!(local_secondary_indexes_of(local_sec_index))
    end
  end
  return attributes
end
local_secondary_indexes_of(local_sec_index) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 158
def local_secondary_indexes_of(local_sec_index)
  attributes = {}
  hashcode = local_index_hashcode(local_sec_index)
  attributes.merge!("local_secondary_index.#{hashcode}.range_key" => find_key(local_sec_index,"RANGE")) if !find_key(local_sec_index,"RANGE").empty?
  attributes.merge!({
    "local_secondary_index.#{hashcode}.name" => local_sec_index.index_name,
    "local_secondary_index.#{hashcode}.projection_type" => local_sec_index.projection.projection_type,
  })
  attributes.merge!(local_index_non_key_attributes(local_sec_index))
  attributes
end
module_name_of(dynamo_db_table) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 277
def module_name_of(dynamo_db_table)
  normalize_module_name(dynamo_db_table['table_name'])
end
point_in_time_summary(dynamo_db_table) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 197
def point_in_time_summary(dynamo_db_table)
  resp = @client.describe_continuous_backups({
    table_name: dynamo_db_table["table_name"]
  })
  if resp.continuous_backups_description.point_in_time_recovery_description.point_in_time_recovery_status == "ENABLED"
    attributes = {"point_in_time_recovery.#" => 1.to_s}
    attributes.merge!({"point_in_time_recovery.0.enabled" => true.to_s})
  else
    attributes = {"point_in_time_recovery.#" => 0.to_s}
  end
end
sse_description(dynamo_db_table) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 209
def sse_description(dynamo_db_table)
  attributes = {}
  if dynamo_db_table.sse_description
    if dynamo_db_table.sse_description.status == "ENABLED"
      attributes = {"server_side_encryption.#" => 1.to_s}
      attributes.merge!({"server_side_encryption.0.enabled" => true.to_s})
    end
  else
    attributes.merge!({"server_side_encryption.#" => 0.to_s})
  end
  attributes
end
stream_specification(dynamo_db_table) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 222
def stream_specification(dynamo_db_table)
  attributes = {}
  if dynamo_db_table.stream_specification
    attributes = {"stream_view_type" => dynamo_db_table.stream_specification.stream_view_type} if dynamo_db_table.stream_specification.stream_enabled
  end
  attributes
end
tables() click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 59
def tables
  tables = []
  dynamo_db_tables.each do |table|
    attributes = @client.describe_table({
      table_name: table
    }).table
    tables << attributes
  end
  return tables
end
tags(dynamo_db_table) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 273
def tags(dynamo_db_table)
  resp = @client.list_tags_of_resource({resource_arn: dynamo_db_table.table_arn}).tags
end
tags_of(dynamo_db_table) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 246
def tags_of(dynamo_db_table)
  attributes = {}
  tags = tags(dynamo_db_table)
  if !tags.empty?
    attributes = { "tags.%" => tags.length.to_s }
    tags.each do |tag|
      attributes["tags.#{tag.key}"] = tag.value
    end
  end
  attributes
end
ttl_hashcode(attribute) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 242
def ttl_hashcode(attribute)
  Zlib.crc32(attribute)
end
ttl_of(dynamo_db_table) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 230
def ttl_of(dynamo_db_table)
  attributes = {}
  ttl = ttl_values(dynamo_db_table)
  if !ttl.empty?
    hashcode = ttl_hashcode(ttl.first)
    attributes = {"ttl.#" => 1.to_s} 
    attributes["ttl.#{hashcode}.attribute_name"] = ttl.first
    attributes["ttl.#{hashcode}.enabled"] = true.to_s
  end
  return attributes
end
ttl_values(dynamo_db_table) click to toggle source
# File lib/terraforming/resource/dynamo_db.rb, line 262
def ttl_values(dynamo_db_table)
  ttl = @client.describe_time_to_live({
    table_name: dynamo_db_table.table_name
  }).time_to_live_description
  if ttl.time_to_live_status == "ENABLED"
    return [ttl.attribute_name]
  else 
    return []
  end
end