Commit 1536c4bd authored by Daisuke Fujita's avatar Daisuke Fujita

Merge pull request #161 from manabusakai/add-redshift

Add Redshift
parents 4198e090 511cff32
......@@ -93,6 +93,7 @@ Commands:
terraforming r53r # Route53 Record
terraforming r53z # Route53 Hosted Zone
terraforming rds # RDS
terraforming rs # Redshift
terraforming rt # Route Table
terraforming rta # Route Table Association
terraforming s3 # S3
......
......@@ -33,6 +33,7 @@ require "terraforming/resource/internet_gateway"
require "terraforming/resource/network_acl"
require "terraforming/resource/network_interface"
require "terraforming/resource/rds"
require "terraforming/resource/redshift"
require "terraforming/resource/route_table"
require "terraforming/resource/route_table_association"
require "terraforming/resource/route53_record"
......
......@@ -125,6 +125,11 @@ module Terraforming
execute(Terraforming::Resource::RDS, options)
end
desc "rs", "Redshift"
def rs
execute(Terraforming::Resource::Redshift, options)
end
desc "rt", "Route Table"
def rt
execute(Terraforming::Resource::RouteTable, options)
......
module Terraforming
module Resource
class Redshift
include Terraforming::Util
def self.tf(client: Aws::Redshift::Client.new)
self.new(client).tf
end
def self.tfstate(client: Aws::Redshift::Client.new)
self.new(client).tfstate
end
def initialize(client)
@client = client
end
def tf
apply_template(@client, "tf/redshift")
end
def tfstate
clusters.inject({}) do |resources, cluster|
attributes = {
"cluster_identifier" => cluster.cluster_identifier,
"cluster_type" => cluster.number_of_nodes == 1 ? "single-node" : "multi-node",
"node_type" => cluster.node_type,
"master_password" => "xxxxxxxx",
"master_username" => cluster.master_username,
"availability_zone" => cluster.availability_zone,
"preferred_maintenance_window" => cluster.preferred_maintenance_window,
"cluster_parameter_group_name" => cluster.cluster_parameter_groups[0].parameter_group_name,
"automated_snapshot_retention_period" => cluster.automated_snapshot_retention_period.to_s,
"port" => cluster.endpoint.port.to_s,
"cluster_version" => cluster.cluster_version,
"allow_version_upgrade" => cluster.allow_version_upgrade.to_s,
"number_of_nodes" => cluster.number_of_nodes.to_s,
"publicly_accessible" => cluster.publicly_accessible.to_s,
"encrypted" => cluster.encrypted.to_s,
"skip_final_snapshot" => "true",
}
attributes["database_name"] = cluster.db_name if cluster.db_name
resources["aws_redshift_cluster.#{module_name_of(cluster)}"] = {
"type" => "aws_redshift_cluster",
"primary" => {
"id" => cluster.cluster_identifier,
"attributes" => attributes
}
}
resources
end
end
private
def clusters
@client.describe_clusters.clusters
end
def module_name_of(cluster)
normalize_module_name(cluster.cluster_identifier)
end
end
end
end
<% clusters.each do |cluster| -%>
resource "aws_redshift_cluster" "<%= cluster.cluster_identifier %>" {
cluster_identifier = "<%= cluster.cluster_identifier %>"
<%- if cluster.db_name -%>
database_name = "<%= cluster.db_name %>"
<%- end -%>
cluster_type = "<%= cluster.number_of_nodes == 1 ? "single-node" : "multi-node" %>"
node_type = "<%= cluster.node_type %>"
master_password = "xxxxxxxx"
master_username = "<%= cluster.master_username %>"
availability_zone = "<%= cluster.availability_zone %>"
preferred_maintenance_window = "<%= cluster.preferred_maintenance_window %>"
cluster_parameter_group_name = "<%= cluster.cluster_parameter_groups[0].parameter_group_name %>"
automated_snapshot_retention_period = "<%= cluster.automated_snapshot_retention_period %>"
port = "<%= cluster.endpoint.port %>"
cluster_version = "<%= cluster.cluster_version %>"
allow_version_upgrade = "<%= cluster.allow_version_upgrade %>"
number_of_nodes = "<%= cluster.number_of_nodes %>"
publicly_accessible = "<%= cluster.publicly_accessible %>"
encrypted = "<%= cluster.encrypted %>"
skip_final_snapshot = "true"
}
<% end -%>
......@@ -200,6 +200,13 @@ module Terraforming
it_behaves_like "CLI examples"
end
describe "rs" do
let(:klass) { Terraforming::Resource::Redshift }
let(:command) { :rs }
it_behaves_like "CLI examples"
end
describe "rt" do
let(:klass) { Terraforming::Resource::RouteTable }
let(:command) { :rt }
......
require "spec_helper"
module Terraforming
module Resource
describe Redshift do
let(:client) do
Aws::Redshift::Client.new(stub_responses: true)
end
let(:clusters) do
[
{
cluster_identifier: "test",
node_type: "dc1.large",
cluster_status: "available",
modify_status: nil,
master_username: "testuser",
db_name: "testdb",
endpoint: {
address: "test.xxxxxxxxxxxx.ap-northeast-1.redshift.amazonaws.com",
port: 5439
},
cluster_create_time: Time.parse("2016-01-01T00:00:00Z"),
automated_snapshot_retention_period: 1,
cluster_security_groups: [],
vpc_security_groups: [],
cluster_parameter_groups: [
{
parameter_group_name: "default.redshift-1.0",
parameter_apply_status: "in-sync",
cluster_parameter_status_list: []
}
],
cluster_subnet_group_name: "test",
vpc_id: "vpc-xxxxxxxx",
availability_zone: "ap-northeast-1c",
preferred_maintenance_window: "fri:15:00-fri:15:30",
pending_modified_values: {
master_user_password: nil,
node_type: nil,
number_of_nodes: nil,
cluster_type: nil,
cluster_version: nil,
automated_snapshot_retention_period: nil,
cluster_identifier: nil
},
cluster_version: "1.0",
allow_version_upgrade: true,
number_of_nodes: 2,
publicly_accessible: true,
encrypted: true,
restore_status: {
status: "completed",
current_restore_rate_in_mega_bytes_per_second: 20.000,
snapshot_size_in_mega_bytes: 10000,
progress_in_mega_bytes: 10000,
elapsed_time_in_seconds: 500,
estimated_time_to_completion_in_seconds: 0
},
hsm_status: nil,
cluster_snapshot_copy_status: nil,
cluster_public_key: "ssh-rsa AAAAB3NzaC1yc2E... Amazon-Redshift\n",
cluster_nodes: [
{
node_role: "LEADER",
private_ip_address: "10.0.0.1",
public_ip_address: "192.0.2.1"
},
{
node_role: "COMPUTE-0",
private_ip_address: "10.0.0.2",
public_ip_address: "192.0.2.2"
},
{
node_role: "COMPUTE-1",
private_ip_address: "10.0.0.3",
public_ip_address: "192.0.2.3"
}
],
elastic_ip_status: nil,
cluster_revision_number: "1026",
tags: [],
kms_key_id: nil
}
]
end
before do
client.stub_responses(:describe_clusters, clusters: clusters)
end
describe ".tf" do
it "should generate tf" do
expect(described_class.tf(client: client)).to eq <<-EOS
resource "aws_redshift_cluster" "test" {
cluster_identifier = "test"
database_name = "testdb"
cluster_type = "multi-node"
node_type = "dc1.large"
master_password = "xxxxxxxx"
master_username = "testuser"
availability_zone = "ap-northeast-1c"
preferred_maintenance_window = "fri:15:00-fri:15:30"
cluster_parameter_group_name = "default.redshift-1.0"
automated_snapshot_retention_period = "1"
port = "5439"
cluster_version = "1.0"
allow_version_upgrade = "true"
number_of_nodes = "2"
publicly_accessible = "true"
encrypted = "true"
skip_final_snapshot = "true"
}
EOS
end
end
describe ".tfstate" do
it "should generate tfstate" do
expect(described_class.tfstate(client: client)).to eq({
"aws_redshift_cluster.test" => {
"type" => "aws_redshift_cluster",
"primary" => {
"id" => "test",
"attributes" => {
"cluster_identifier" => "test",
"database_name" => "testdb",
"cluster_type" => "multi-node",
"node_type" => "dc1.large",
"master_password" => "xxxxxxxx",
"master_username" => "testuser",
"availability_zone" => "ap-northeast-1c",
"preferred_maintenance_window" => "fri:15:00-fri:15:30",
"cluster_parameter_group_name" => "default.redshift-1.0",
"automated_snapshot_retention_period" => "1",
"port" => "5439",
"cluster_version" => "1.0",
"allow_version_upgrade" => "true",
"number_of_nodes" => "2",
"publicly_accessible" => "true",
"encrypted" => "true",
"skip_final_snapshot" => "true",
}
}
},
})
end
end
end
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment