-
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.
Create GVC and identity terraform configs from templates (#230)
- Loading branch information
Showing
12 changed files
with
509 additions
and
18 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,75 @@ | ||
# 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 | ||
|
||
def gvc_config # rubocop:disable Metrics/MethodLength | ||
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 | ||
|
||
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,55 @@ | ||
# frozen_string_literal: true | ||
|
||
module TerraformConfig | ||
class Gvc < Base | ||
attr_reader :name, :description, :tags, :domain, :locations, :pull_secrets, :env, :load_balancer | ||
|
||
def initialize( # rubocop:disable Metrics/ParameterLists | ||
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 | ||
|
||
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.fetch(:trusted_proxies, nil), 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
Oops, something went wrong.