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/audit_context.rb b/lib/core/terraform_config/audit_context.rb new file mode 100644 index 00000000..d79eb79c --- /dev/null +++ b/lib/core/terraform_config/audit_context.rb @@ -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 diff --git a/lib/core/terraform_config/generator.rb b/lib/core/terraform_config/generator.rb index 763a53a3..df6e856f 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].freeze + SUPPORTED_TEMPLATE_KINDS = %w[gvc secret identity policy volumeset workload auditctx agent].freeze WORKLOAD_SPEC_KEYS = %i[ type containers @@ -45,6 +45,8 @@ def filename "gvc.tf" when "workload" "#{template[:name]}.tf" + when "auditctx" + "audit_contexts.tf" else "#{kind.pluralize}.tf" end @@ -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 @@ -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) diff --git a/spec/command/terraform/generate_spec.rb b/spec/command/terraform/generate_spec.rb index ac17b508..d24b6526 100644 --- a/spec/command/terraform/generate_spec.rb +++ b/spec/command/terraform/generate_spec.rb @@ -23,6 +23,8 @@ maintenance 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/audit_context_spec.rb b/spec/core/terraform_config/audit_context_spec.rb new file mode 100644 index 00000000..92e4b2be --- /dev/null +++ b/spec/core/terraform_config/audit_context_spec.rb @@ -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 diff --git a/spec/core/terraform_config/generator_spec.rb b/spec/core/terraform_config/generator_spec.rb index 31e36107..33a4eefc 100644 --- a/spec/core/terraform_config/generator_spec.rb +++ b/spec/core/terraform_config/generator_spec.rb @@ -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 { @@ -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 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 diff --git a/spec/dummy/.controlplane/templates/audit_context.yml b/spec/dummy/.controlplane/templates/audit_context.yml new file mode 100644 index 00000000..d28312c7 --- /dev/null +++ b/spec/dummy/.controlplane/templates/audit_context.yml @@ -0,0 +1,5 @@ +kind: auditctx +name: audit-context-name +description: audit context description +tags: + tag1: value1