Skip to content

Commit

Permalink
Merge branch 'terraform-feature' of github.com:shakacode/control-plan…
Browse files Browse the repository at this point in the history
…e-flow into terraform-feature-fixes
  • Loading branch information
zzaakiirr committed Nov 26, 2024
2 parents 1be9c15 + 344c666 commit b4740db
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 2 deletions.
23 changes: 23 additions & 0 deletions lib/core/terraform_config/agent.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module TerraformConfig
class Agent < Base
attr_reader :name, :description, :tags

def initialize(name:, description: nil, tags: nil)
super()

@name = name
@description = description
@tags = tags
end

def to_tf
block :resource, :cpln_agent, name do
argument :name, name
argument :description, description, optional: true
argument :tags, tags, optional: true
end
end
end
end
23 changes: 23 additions & 0 deletions lib/core/terraform_config/audit_context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module TerraformConfig
class AuditContext < Base
attr_reader :name, :description, :tags

def initialize(name:, description: nil, tags: nil)
super()

@name = name
@description = description
@tags = tags
end

def to_tf
block :resource, :cpln_audit_context, name do
argument :name, name
argument :description, description, optional: true
argument :tags, tags, optional: true
end
end
end
end
17 changes: 15 additions & 2 deletions lib/core/terraform_config/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module TerraformConfig
class Generator # rubocop:disable Metrics/ClassLength
SUPPORTED_TEMPLATE_KINDS = %w[gvc secret identity policy volumeset workload].freeze
SUPPORTED_TEMPLATE_KINDS = %w[gvc secret identity policy volumeset workload auditctx agent].freeze
WORKLOAD_SPEC_KEYS = %i[
type
containers
Expand Down Expand Up @@ -45,6 +45,8 @@ def filename
"gvc.tf"
when "workload"
"#{template[:name]}.tf"
when "auditctx"
"audit_contexts.tf"
else
"#{kind.pluralize}.tf"
end
Expand All @@ -55,8 +57,11 @@ def tf_config
end

def config_class
if kind == "volumeset"
case kind
when "volumeset"
TerraformConfig::VolumeSet
when "auditctx"
TerraformConfig::AuditContext
else
TerraformConfig.const_get(kind.capitalize)
end
Expand Down Expand Up @@ -105,6 +110,14 @@ def volumeset_config_params
template.slice(:name, :description, :tags).merge(gvc: gvc).merge(specs)
end

def auditctx_config_params
template.slice(:name, :description, :tags)
end

def agent_config_params
template.slice(:name, :description, :tags)
end

def workload_config_params
template
.slice(:name, :description, :tags)
Expand Down
2 changes: 2 additions & 0 deletions spec/command/terraform/generate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
maintenance
maintenance_envs
maintenance-with-external-image
audit_contexts
agents
].freeze

describe Command::Terraform::Generate do
Expand Down
34 changes: 34 additions & 0 deletions spec/core/terraform_config/agent_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require "spec_helper"

describe TerraformConfig::Agent do
let(:config) { described_class.new(**options) }

describe "#to_tf" do
subject(:generated) { config.to_tf }

let(:options) do
{
name: "agent-name",
description: "agent description",
tags: { "tag1" => "true", "tag2" => "value" }
}
end

it "generates correct config" do
expect(generated).to eq(
<<~EXPECTED
resource "cpln_agent" "agent-name" {
name = "agent-name"
description = "agent description"
tags = {
tag1 = "true"
tag2 = "value"
}
}
EXPECTED
)
end
end
end
34 changes: 34 additions & 0 deletions spec/core/terraform_config/audit_context_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require "spec_helper"

describe TerraformConfig::AuditContext do
let(:config) { described_class.new(**options) }

describe "#to_tf" do
subject(:generated) { config.to_tf }

let(:options) do
{
name: "audit-context-name",
description: "audit context description",
tags: { "tag1" => "true", "tag2" => "value" }
}
end

it "generates correct config" do
expect(generated).to eq(
<<~EXPECTED
resource "cpln_audit_context" "audit-context-name" {
name = "audit-context-name"
description = "audit context description"
tags = {
tag1 = "true"
tag2 = "value"
}
}
EXPECTED
)
end
end
end
56 changes: 56 additions & 0 deletions spec/core/terraform_config/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,34 @@
end
end

context "when template's kind is auditctx" do
let(:template) do
{
"kind" => "auditctx",
"name" => "audit-context-name",
"description" => "audit context description",
"tags" => { "tag1" => "tag1_value", "tag2" => "tag2_value" }
}
end

it "generates correct terraform config and filename for it", :aggregate_failures do
expected_filename = "audit_contexts.tf"

tf_configs = generator.tf_configs
expect(tf_configs.count).to eq(1)

filenames = tf_configs.keys
expect(filenames).to contain_exactly(expected_filename)

tf_config = tf_configs[expected_filename]
expect(tf_config).to be_an_instance_of(TerraformConfig::AuditContext)

expect(tf_config.name).to eq("audit-context-name")
expect(tf_config.description).to eq("audit context description")
expect(tf_config.tags).to eq(tag1: "tag1_value", tag2: "tag2_value")
end
end

context "when template's kind is workload" do
let(:template) do
{
Expand Down Expand Up @@ -531,4 +559,32 @@
expect(main_tf_config.job).to be_nil
end
end

context "when template's kind is agent" do
let(:template) do
{
"kind" => "agent",
"name" => "agent-name",
"description" => "agent description",
"tags" => { "tag1" => "tag1_value", "tag2" => "tag2_value" }
}
end

it "generates correct terraform config and filename for it", :aggregate_failures do
expected_filename = "agents.tf"

tf_configs = generator.tf_configs
expect(tf_configs.count).to eq(1)

filenames = tf_configs.keys
expect(filenames).to contain_exactly(expected_filename)

tf_config = tf_configs[expected_filename]
expect(tf_config).to be_an_instance_of(TerraformConfig::Agent)

expect(tf_config.name).to eq("agent-name")
expect(tf_config.description).to eq("agent description")
expect(tf_config.tags).to eq(tag1: "tag1_value", tag2: "tag2_value")
end
end
end
5 changes: 5 additions & 0 deletions spec/dummy/.controlplane/templates/agent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: agent
name: agent-name
description: agent description
tags:
tag1: value1
5 changes: 5 additions & 0 deletions spec/dummy/.controlplane/templates/audit_context.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: auditctx
name: audit-context-name
description: audit context description
tags:
tag1: value1

0 comments on commit b4740db

Please sign in to comment.