Skip to content

Commit 344c666

Browse files
authored
Support terraform config generation from agent template (#241)
1 parent 9826935 commit 344c666

File tree

6 files changed

+96
-1
lines changed

6 files changed

+96
-1
lines changed

lib/core/terraform_config/agent.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
module TerraformConfig
4+
class Agent < Base
5+
attr_reader :name, :description, :tags
6+
7+
def initialize(name:, description: nil, tags: nil)
8+
super()
9+
10+
@name = name
11+
@description = description
12+
@tags = tags
13+
end
14+
15+
def to_tf
16+
block :resource, :cpln_agent, name do
17+
argument :name, name
18+
argument :description, description, optional: true
19+
argument :tags, tags, optional: true
20+
end
21+
end
22+
end
23+
end

lib/core/terraform_config/generator.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module TerraformConfig
44
class Generator # rubocop:disable Metrics/ClassLength
5-
SUPPORTED_TEMPLATE_KINDS = %w[gvc secret identity policy volumeset workload auditctx].freeze
5+
SUPPORTED_TEMPLATE_KINDS = %w[gvc secret identity policy volumeset workload auditctx agent].freeze
66
WORKLOAD_SPEC_KEYS = %i[
77
type
88
containers
@@ -113,6 +113,10 @@ def auditctx_config_params
113113
template.slice(:name, :description, :tags)
114114
end
115115

116+
def agent_config_params
117+
template.slice(:name, :description, :tags)
118+
end
119+
116120
def workload_config_params
117121
template
118122
.slice(:name, :description, :tags)

spec/command/terraform/generate_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
maintenance_envs
2525
maintenance-with-external-image
2626
audit_contexts
27+
agents
2728
].freeze
2829

2930
describe Command::Terraform::Generate do
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
describe TerraformConfig::Agent do
6+
let(:config) { described_class.new(**options) }
7+
8+
describe "#to_tf" do
9+
subject(:generated) { config.to_tf }
10+
11+
let(:options) do
12+
{
13+
name: "agent-name",
14+
description: "agent description",
15+
tags: { "tag1" => "true", "tag2" => "value" }
16+
}
17+
end
18+
19+
it "generates correct config" do
20+
expect(generated).to eq(
21+
<<~EXPECTED
22+
resource "cpln_agent" "agent-name" {
23+
name = "agent-name"
24+
description = "agent description"
25+
tags = {
26+
tag1 = "true"
27+
tag2 = "value"
28+
}
29+
}
30+
EXPECTED
31+
)
32+
end
33+
end
34+
end

spec/core/terraform_config/generator_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,4 +559,32 @@
559559
expect(main_tf_config.job).to be_nil
560560
end
561561
end
562+
563+
context "when template's kind is agent" do
564+
let(:template) do
565+
{
566+
"kind" => "agent",
567+
"name" => "agent-name",
568+
"description" => "agent description",
569+
"tags" => { "tag1" => "tag1_value", "tag2" => "tag2_value" }
570+
}
571+
end
572+
573+
it "generates correct terraform config and filename for it", :aggregate_failures do
574+
expected_filename = "agents.tf"
575+
576+
tf_configs = generator.tf_configs
577+
expect(tf_configs.count).to eq(1)
578+
579+
filenames = tf_configs.keys
580+
expect(filenames).to contain_exactly(expected_filename)
581+
582+
tf_config = tf_configs[expected_filename]
583+
expect(tf_config).to be_an_instance_of(TerraformConfig::Agent)
584+
585+
expect(tf_config.name).to eq("agent-name")
586+
expect(tf_config.description).to eq("agent description")
587+
expect(tf_config.tags).to eq(tag1: "tag1_value", tag2: "tag2_value")
588+
end
589+
end
562590
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: agent
2+
name: agent-name
3+
description: agent description
4+
tags:
5+
tag1: value1

0 commit comments

Comments
 (0)