diff --git a/lib/core/terraform_config/agent.rb b/lib/core/terraform_config/agent.rb new file mode 100644 index 00000000..08508e1d --- /dev/null +++ b/lib/core/terraform_config/agent.rb @@ -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 diff --git a/lib/core/terraform_config/generator.rb b/lib/core/terraform_config/generator.rb index 2b7871ed..eb432b47 100644 --- a/lib/core/terraform_config/generator.rb +++ b/lib/core/terraform_config/generator.rb @@ -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 @@ -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) diff --git a/spec/command/terraform/generate_spec.rb b/spec/command/terraform/generate_spec.rb index b2c16ad6..d24b6526 100644 --- a/spec/command/terraform/generate_spec.rb +++ b/spec/command/terraform/generate_spec.rb @@ -24,6 +24,7 @@ maintenance_envs maintenance-with-external-image audit_contexts + agents ].freeze describe Command::Terraform::Generate do diff --git a/spec/core/terraform_config/agent_spec.rb b/spec/core/terraform_config/agent_spec.rb new file mode 100644 index 00000000..efd9228b --- /dev/null +++ b/spec/core/terraform_config/agent_spec.rb @@ -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 diff --git a/spec/core/terraform_config/generator_spec.rb b/spec/core/terraform_config/generator_spec.rb index 2dfc7b31..903f2323 100644 --- a/spec/core/terraform_config/generator_spec.rb +++ b/spec/core/terraform_config/generator_spec.rb @@ -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 diff --git a/spec/dummy/.controlplane/templates/agent.yml b/spec/dummy/.controlplane/templates/agent.yml new file mode 100644 index 00000000..162234c8 --- /dev/null +++ b/spec/dummy/.controlplane/templates/agent.yml @@ -0,0 +1,5 @@ +kind: agent +name: agent-name +description: agent description +tags: + tag1: value1