Skip to content

Commit

Permalink
Support terraform config generation from agent template (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzaakiirr authored Nov 26, 2024
1 parent 9826935 commit 344c666
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 1 deletion.
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
6 changes: 5 additions & 1 deletion 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 auditctx].freeze
SUPPORTED_TEMPLATE_KINDS = %w[gvc secret identity policy volumeset workload auditctx agent].freeze
WORKLOAD_SPEC_KEYS = %i[
type
containers
Expand Down Expand Up @@ -113,6 +113,10 @@ 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
1 change: 1 addition & 0 deletions spec/command/terraform/generate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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
28 changes: 28 additions & 0 deletions spec/core/terraform_config/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -559,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

0 comments on commit 344c666

Please sign in to comment.