This repository has been archived by the owner on Sep 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 659
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #240 from brianknight10/192-nat-gateway
Add NAT Gateway Functionality
- Loading branch information
Showing
6 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module Terraforming | ||
module Resource | ||
class NATGateway | ||
include Terraforming::Util | ||
|
||
def self.tf(client: Aws::EC2::Client.new) | ||
self.new(client).tf | ||
end | ||
|
||
def self.tfstate(client: Aws::EC2::Client.new) | ||
self.new(client).tfstate | ||
end | ||
|
||
def initialize(client) | ||
@client = client | ||
end | ||
|
||
def tf | ||
apply_template(@client, "tf/nat_gateway") | ||
end | ||
|
||
def tfstate | ||
nat_gateways.inject({}) do |resources, nat_gateway| | ||
next resources if nat_gateway.nat_gateway_addresses.empty? | ||
|
||
attributes = { | ||
"id" => nat_gateway.nat_gateway_id, | ||
"allocation_id" => nat_gateway.nat_gateway_addresses[0].allocation_id, | ||
"subnet_id" => nat_gateway.subnet_id, | ||
"network_inferface_id" => nat_gateway.nat_gateway_addresses[0].network_interface_id, | ||
"private_ip" => nat_gateway.nat_gateway_addresses[0].private_ip, | ||
"public_ip" => nat_gateway.nat_gateway_addresses[0].public_ip, | ||
} | ||
resources["aws_nat_gateway.#{module_name_of(nat_gateway)}"] = { | ||
"type" => "aws_nat_gateway", | ||
"primary" => { | ||
"id" => nat_gateway.nat_gateway_id, | ||
"attributes" => attributes | ||
} | ||
} | ||
|
||
resources | ||
end | ||
end | ||
|
||
private | ||
|
||
def nat_gateways | ||
@client.describe_nat_gateways.nat_gateways | ||
end | ||
|
||
def module_name_of(nat_gateway) | ||
normalize_module_name(nat_gateway.nat_gateway_id) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<% nat_gateways.each do |nat_gateway| -%> | ||
<% unless nat_gateway.nat_gateway_addresses.empty? -%> | ||
resource "aws_nat_gateway" "<%= module_name_of(nat_gateway) %>" { | ||
allocation_id = "<%= nat_gateway.nat_gateway_addresses[0].allocation_id %>" | ||
subnet_id = "<%= nat_gateway.subnet_id %>" | ||
} | ||
|
||
<% end -%> | ||
<% end -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
require "spec_helper" | ||
|
||
module Terraforming | ||
module Resource | ||
describe NATGateway do | ||
let(:client) do | ||
Aws::EC2::Client.new(stub_responses: true) | ||
end | ||
|
||
let(:nat_gateways) do | ||
[ | ||
{ | ||
nat_gateway_id: "nat-0c5b68b2c4d64e037", | ||
subnet_id: "subnet-cd5645f7", | ||
nat_gateway_addresses: [ | ||
allocation_id: "eipalloc-b02a3c79", | ||
network_interface_id: "eni-03d4046f", | ||
private_ip: "10.0.3.6", | ||
public_ip: "52.5.3.67", | ||
] | ||
}, | ||
{ | ||
nat_gateway_id: "nat-0c5b68b2c4d64ea12", | ||
subnet_id: "subnet-cd564c9e", | ||
nat_gateway_addresses: [ | ||
allocation_id: "eipalloc-a03a3c79", | ||
network_interface_id: "eni-b6e4046f", | ||
private_ip: "10.0.4.6", | ||
public_ip: "54.4.5.68", | ||
] | ||
} | ||
] | ||
end | ||
|
||
before do | ||
client.stub_responses(:describe_nat_gateways, nat_gateways: nat_gateways) | ||
end | ||
|
||
describe ".tf" do | ||
it "should generate tf" do | ||
expect(described_class.tf(client: client)).to eq <<-EOS | ||
resource "aws_nat_gateway" "nat-0c5b68b2c4d64e037" { | ||
allocation_id = "eipalloc-b02a3c79" | ||
subnet_id = "subnet-cd5645f7" | ||
} | ||
resource "aws_nat_gateway" "nat-0c5b68b2c4d64ea12" { | ||
allocation_id = "eipalloc-a03a3c79" | ||
subnet_id = "subnet-cd564c9e" | ||
} | ||
EOS | ||
end | ||
end | ||
|
||
describe ".tfstate" do | ||
it "should generate tfstate" do | ||
expect(described_class.tfstate(client: client)).to eq({ | ||
"aws_nat_gateway.nat-0c5b68b2c4d64e037" => { | ||
"type" => "aws_nat_gateway", | ||
"primary" => { | ||
"id" => "nat-0c5b68b2c4d64e037", | ||
"attributes" => { | ||
"id" => "nat-0c5b68b2c4d64e037", | ||
"allocation_id" => "eipalloc-b02a3c79", | ||
"subnet_id" => "subnet-cd5645f7", | ||
"network_inferface_id" => "eni-03d4046f", | ||
"private_ip" => "10.0.3.6", | ||
"public_ip" => "52.5.3.67", | ||
} | ||
} | ||
}, | ||
"aws_nat_gateway.nat-0c5b68b2c4d64ea12" => { | ||
"type" => "aws_nat_gateway", | ||
"primary" => { | ||
"id" => "nat-0c5b68b2c4d64ea12", | ||
"attributes" => { | ||
"id" => "nat-0c5b68b2c4d64ea12", | ||
"allocation_id" => "eipalloc-a03a3c79", | ||
"subnet_id" => "subnet-cd564c9e", | ||
"network_inferface_id" => "eni-b6e4046f", | ||
"private_ip" => "10.0.4.6", | ||
"public_ip" => "54.4.5.68", | ||
} | ||
} | ||
}, | ||
}) | ||
end | ||
end | ||
end | ||
end | ||
end |