-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate Terraform config from gvc & identity templates
- Loading branch information
Showing
12 changed files
with
379 additions
and
10 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,77 @@ | ||
# frozen_string_literal: true | ||
|
||
module TerraformConfig | ||
class Generator | ||
attr_reader :config, :template | ||
|
||
def initialize(config:, template:) | ||
@config = config | ||
@template = template | ||
end | ||
|
||
def filename | ||
case template["kind"] | ||
when "gvc" | ||
"gvc.tf" | ||
when "identity" | ||
"identities.tf" | ||
else | ||
raise "Unsupported template kind - #{template['kind']}" | ||
end | ||
end | ||
|
||
def tf_config | ||
case template["kind"] | ||
when "gvc" | ||
gvc_config | ||
when "identity" | ||
identity_config | ||
else | ||
raise "Unsupported template kind - #{template['kind']}" | ||
end | ||
end | ||
|
||
private | ||
|
||
# rubocop:disable Metrics/MethodLength | ||
def gvc_config | ||
pull_secrets = template.dig("spec", "pullSecretLinks")&.map do |secret_link| | ||
secret_name = secret_link.split("/").last | ||
"cpln_secret.#{secret_name}.name" | ||
end | ||
|
||
load_balancer = template.dig("spec", "loadBalancer") | ||
|
||
TerraformConfig::Gvc.new( | ||
name: template["name"], | ||
description: template["description"], | ||
tags: template["tags"], | ||
domain: template.dig("spec", "domain"), | ||
env: env, | ||
pull_secrets: pull_secrets, | ||
locations: locations, | ||
load_balancer: load_balancer | ||
) | ||
end | ||
# rubocop:enable Metrics/MethodLength | ||
|
||
def identity_config | ||
TerraformConfig::Identity.new( | ||
gvc: "cpln_gvc.#{config.app}.name", # GVC name matches application name | ||
name: template["name"], | ||
description: template["description"], | ||
tags: template["tags"] | ||
) | ||
end | ||
|
||
def env | ||
template.dig("spec", "env").to_h { |env_var| [env_var["name"], env_var["value"]] } | ||
end | ||
|
||
def locations | ||
template.dig("spec", "staticPlacement", "locationLinks")&.map do |location_link| | ||
location_link.split("/").last | ||
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,57 @@ | ||
# frozen_string_literal: true | ||
|
||
module TerraformConfig | ||
class Gvc < Base | ||
attr_reader :name, :description, :tags, :domain, :locations, :pull_secrets, :env, :load_balancer | ||
|
||
# rubocop:disable Metrics/ParameterLists | ||
def initialize( | ||
name:, | ||
description: nil, | ||
tags: nil, | ||
domain: nil, | ||
locations: nil, | ||
pull_secrets: nil, | ||
env: nil, | ||
load_balancer: nil | ||
) | ||
super() | ||
|
||
@name = name | ||
@description = description | ||
@tags = tags | ||
@domain = domain | ||
@locations = locations | ||
@pull_secrets = pull_secrets | ||
@env = env | ||
@load_balancer = load_balancer&.transform_keys { |k| k.to_s.underscore.to_sym } | ||
end | ||
# rubocop:enable Metrics/ParameterLists | ||
|
||
def to_tf | ||
block :resource, :cpln_gvc, name do | ||
argument :name, name | ||
argument :description, description, optional: true | ||
argument :tags, tags, optional: true | ||
|
||
argument :domain, domain, optional: true | ||
argument :locations, locations, optional: true | ||
argument :pull_secrets, pull_secrets, optional: true | ||
argument :env, env, optional: true | ||
|
||
load_balancer_tf | ||
end | ||
end | ||
|
||
private | ||
|
||
def load_balancer_tf | ||
return if load_balancer.nil? | ||
|
||
block :load_balancer do | ||
argument :dedicated, load_balancer.fetch(:dedicated) | ||
argument :trusted_proxies, load_balancer[:trusted_proxies], optional: true | ||
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,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module TerraformConfig | ||
class Identity < Base | ||
attr_reader :gvc, :name, :description, :tags | ||
|
||
def initialize(gvc:, name:, description: nil, tags: nil) | ||
super() | ||
|
||
@gvc = gvc | ||
@name = name | ||
@description = description | ||
@tags = tags | ||
end | ||
|
||
def to_tf | ||
block :resource, :cpln_identity, name do | ||
argument :gvc, gvc | ||
|
||
argument :name, name | ||
argument :description, description, optional: true | ||
|
||
argument :tags, tags, optional: true | ||
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
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,92 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
describe TerraformConfig::Generator do | ||
let(:generator) { described_class.new(config: config, template: template) } | ||
|
||
let(:config) { instance_double(Config, org: "org-name", app: "app-name") } | ||
|
||
context "when template's kind is gvc" do | ||
let(:template) do | ||
{ | ||
"kind" => "gvc", | ||
"name" => config.app, | ||
"description" => "description", | ||
"tags" => { "tag1" => "tag1_value", "tag2" => "tag2_value" }, | ||
"spec" => { | ||
"domain" => "app.example.com", | ||
"env" => [ | ||
{ | ||
"name" => "DATABASE_URL", | ||
"value" => "postgres://the_user:the_password@postgres.#{config.app}.cpln.local:5432/#{config.app}" | ||
}, | ||
{ | ||
"name" => "RAILS_ENV", | ||
"value" => "production" | ||
}, | ||
{ | ||
"name" => "RAILS_SERVE_STATIC_FILES", | ||
"value" => "true" | ||
} | ||
], | ||
"staticPlacement" => { | ||
"locationLinks" => ["/org/#{config.org}/location/aws-us-east-2"] | ||
}, | ||
"pullSecretLinks" => ["/org/#{config.org}/secret/some-secret"], | ||
"loadBalancer" => { | ||
"dedicated" => true, | ||
"trustedProxies" => 1 | ||
} | ||
} | ||
} | ||
end | ||
|
||
it "generates correct terraform config and filename for it", :aggregate_failures do | ||
tf_config = generator.tf_config | ||
expect(tf_config).to be_an_instance_of(TerraformConfig::Gvc) | ||
|
||
expect(tf_config.name).to eq(config.app) | ||
expect(tf_config.description).to eq("description") | ||
expect(tf_config.tags).to eq("tag1" => "tag1_value", "tag2" => "tag2_value") | ||
|
||
expect(tf_config.domain).to eq("app.example.com") | ||
expect(tf_config.locations).to eq(["aws-us-east-2"]) | ||
expect(tf_config.pull_secrets).to eq(["cpln_secret.some-secret.name"]) | ||
expect(tf_config.env).to eq( | ||
{ | ||
"DATABASE_URL" => "postgres://the_user:the_password@postgres.#{config.app}.cpln.local:5432/#{config.app}", | ||
"RAILS_ENV" => "production", | ||
"RAILS_SERVE_STATIC_FILES" => "true" | ||
} | ||
) | ||
expect(tf_config.load_balancer).to eq({ "dedicated" => true, "trusted_proxies" => 1 }) | ||
|
||
tf_filename = generator.filename | ||
expect(tf_filename).to eq("gvc.tf") | ||
end | ||
end | ||
|
||
context "when template's kind is identity" do | ||
let(:template) do | ||
{ | ||
"kind" => "identity", | ||
"name" => "identity-name", | ||
"description" => "description", | ||
"tags" => { "tag1" => "tag1_value", "tag2" => "tag2_value" } | ||
} | ||
end | ||
|
||
it "generates correct terraform config and filename for it", :aggregate_failures do | ||
tf_config = generator.tf_config | ||
expect(tf_config).to be_an_instance_of(TerraformConfig::Identity) | ||
|
||
expect(tf_config.name).to eq("identity-name") | ||
expect(tf_config.description).to eq("description") | ||
expect(tf_config.tags).to eq("tag1" => "tag1_value", "tag2" => "tag2_value") | ||
|
||
tf_filename = generator.filename | ||
expect(tf_filename).to eq("identities.tf") | ||
end | ||
end | ||
end |
Oops, something went wrong.