Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #283 from notjames/efs
Browse files Browse the repository at this point in the history
Extended terraforming to create EFS terraform resource.
  • Loading branch information
dtan4 authored Nov 16, 2016
2 parents c91c60d + d3183b9 commit d78824c
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/terraforming.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
require "terraforming/resource/eip"
require "terraforming/resource/elasti_cache_cluster"
require "terraforming/resource/elasti_cache_subnet_group"
require "terraforming/resource/efs_file_system"
require "terraforming/resource/elb"
require "terraforming/resource/iam_group"
require "terraforming/resource/iam_group_membership"
Expand Down
5 changes: 5 additions & 0 deletions lib/terraforming/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ def eip
execute(Terraforming::Resource::EIP, options)
end

desc "efs", "EFS"
def efs
execute(Terraforming::Resource::EFSFileSystem, options)
end

desc "elb", "ELB"
def elb
execute(Terraforming::Resource::ELB, options)
Expand Down
56 changes: 56 additions & 0 deletions lib/terraforming/resource/efs_file_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module Terraforming
module Resource
class EFSFileSystem
include Terraforming::Util

def self.tf(client: Aws::EFS::Client.new)
self.new(client).tf
end

def self.tfstate(client: Aws::EFS::Client.new)
self.new(client).tfstate
end

def initialize(client)
@client = client
end

def tf
apply_template(@client, "tf/elastic_filesystem")
end

def tfstate
file_systems.inject({}) do |resources, efs|
attributes = {
"creation_token" => efs.creation_token,
"id" => efs.file_system_id,
"performance_mode" => efs.performance_mode,
"tags.%" => "1",
"tags.Name" => efs.name,
}

resources["aws_efs_file_system.#{efs.file_system_id}"] = {
"type" => "aws_efs_file_system",
"depends_on" => [],
"primary" => {
"id" => efs.file_system_id,
"attributes" => attributes,
"meta" => {},
"tainted" => false,
},
"deposed" => [],
"provider" => "aws",
}

resources
end
end

private

def file_systems
@client.describe_file_systems.data.file_systems.flatten
end
end
end
end
18 changes: 18 additions & 0 deletions lib/terraforming/template/tf/elastic_filesystem.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<% file_systems.each_with_index do |efs, idx| -%>
resource "aws_efs_file_system" "<%= efs.file_system_id %>" {
<% if efs.creation_token -%>
creation_token = "<%= efs.creation_token %>"
<% end -%>
<% if efs.file_system_id -%>
file_system_id = "<%= efs.file_system_id %>"
<% end -%>
<% if efs.performance_mode -%>
performance_mode = "<%= efs.performance_mode %>"
<% end -%>
<% if efs.name -%>
tags {
Name = "<%= efs.name %>"
}
<% end -%>
}
<% end -%>
7 changes: 7 additions & 0 deletions spec/lib/terraforming/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ module Terraforming
it_behaves_like "CLI examples"
end

describe "efs" do
let(:klass) { Terraforming::Resource::EFSFileSystem }
let(:command) { :efs }

it_behaves_like "CLI examples"
end

describe "elb" do
let(:klass) { Terraforming::Resource::ELB }
let(:command) { :elb }
Expand Down
109 changes: 109 additions & 0 deletions spec/lib/terraforming/resource/efs_file_system_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
require "spec_helper"

module Terraforming
module Resource
describe EFSFileSystem do
let(:client) do
Aws::EFS::Client.new(stub_responses: true)
end

let(:efs_description_0) do
{
creation_time: Time.parse("2016-11-01 11:30:00 -0700"),
creation_token: "console-1234abcd-1234-abcd-a123-d34db33f0000",
file_system_id: "fs-0000abcd",
life_cycle_state: "available",
name: "efs_name_0",
number_of_mount_targets: 3,
owner_id: "999999999999",
performance_mode: "generalPurpose",
size_in_bytes: { value: 6144 },
}
end

let(:efs_description_1) do
{
creation_time: Time.parse("2016-10-24 11:42:21 -0700"),
creation_token: "console-0000abcd-4321-dcba-a123-d34db33f0000",
file_system_id: "fs-abcd1234",
life_cycle_state: "available",
name: "efs_name_1",
number_of_mount_targets: 3,
owner_id: "999999999999",
performance_mode: "generalPurpose",
size_in_bytes: { value: 23481234 },
}
end

before do
client.stub_responses(:describe_file_systems, file_systems: [efs_description_0, efs_description_1])
end

describe ".tf" do
it "should generate tf" do
expect(described_class.tf(client: client)).to eq <<-EOS
resource "aws_efs_file_system" "fs-0000abcd" {
creation_token = "console-1234abcd-1234-abcd-a123-d34db33f0000"
file_system_id = "fs-0000abcd"
performance_mode = "generalPurpose"
tags {
Name = "efs_name_0"
}
}
resource "aws_efs_file_system" "fs-abcd1234" {
creation_token = "console-0000abcd-4321-dcba-a123-d34db33f0000"
file_system_id = "fs-abcd1234"
performance_mode = "generalPurpose"
tags {
Name = "efs_name_1"
}
}
EOS
end
end

describe ".tfstate" do
it "should generate tfstate" do
expect(described_class.tfstate(client: client)).to eq({
"aws_efs_file_system.fs-0000abcd" => {
"type" => "aws_efs_file_system",
"depends_on" => [],
"primary" => {
"id" => "fs-0000abcd",
"meta" => {},
"tainted" => false,
"attributes" => {
"creation_token" => "console-1234abcd-1234-abcd-a123-d34db33f0000",
"id" => "fs-0000abcd",
"performance_mode" => "generalPurpose",
"tags.%" => "1",
"tags.Name" => "efs_name_0"
},
},
"deposed" => [],
"provider" => "aws",
},
"aws_efs_file_system.fs-abcd1234" => {
"type" => "aws_efs_file_system",
"depends_on" => [],
"primary" => {
"id" => "fs-abcd1234",
"meta" => {},
"tainted" => false,
"attributes" => {
"creation_token" => "console-0000abcd-4321-dcba-a123-d34db33f0000",
"id" => "fs-abcd1234",
"performance_mode" => "generalPurpose",
"tags.%" => "1",
"tags.Name" => "efs_name_1"
},
},
"deposed" => [],
"provider" => "aws",
}
})
end
end
end
end
end

0 comments on commit d78824c

Please sign in to comment.