From 9826935e50271739f04340f8493bc8452f4d8223 Mon Sep 17 00:00:00 2001 From: Zakir Dzhamaliddinov Date: Tue, 26 Nov 2024 12:36:48 +0300 Subject: [PATCH 1/2] Support terraform config generation from auditctx template (#242) --- .gitignore | 4 +-- lib/core/terraform_config/audit_context.rb | 23 +++++++++++++ lib/core/terraform_config/generator.rb | 13 +++++-- spec/command/terraform/generate_spec.rb | 1 + .../terraform_config/audit_context_spec.rb | 34 +++++++++++++++++++ spec/core/terraform_config/generator_spec.rb | 28 +++++++++++++++ .../.controlplane/templates/audit_context.yml | 5 +++ 7 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 lib/core/terraform_config/audit_context.rb create mode 100644 spec/core/terraform_config/audit_context_spec.rb create mode 100644 spec/dummy/.controlplane/templates/audit_context.yml diff --git a/.gitignore b/.gitignore index a977a4d4..b0cb4689 100644 --- a/.gitignore +++ b/.gitignore @@ -18,5 +18,5 @@ /spec/dummy/.controlplane/controlplane*-tmp-*.yml # Generated configs -terraform/ -.controlplane/ +/terraform/ +/.controlplane/ 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 f0cd9b3a..2b7871ed 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].freeze WORKLOAD_SPEC_KEYS = %i[ type containers @@ -44,6 +44,8 @@ def filename "gvc.tf" when "workload" "#{template[:name]}.tf" + when "auditctx" + "audit_contexts.tf" else "#{kind.pluralize}.tf" end @@ -54,8 +56,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 @@ -104,6 +109,10 @@ 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 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..b2c16ad6 100644 --- a/spec/command/terraform/generate_spec.rb +++ b/spec/command/terraform/generate_spec.rb @@ -23,6 +23,7 @@ maintenance maintenance_envs maintenance-with-external-image + audit_contexts ].freeze describe Command::Terraform::Generate do 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 976380fe..2dfc7b31 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 { 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 From 344c66612a94ee3e16fb234193378c3a1213217c Mon Sep 17 00:00:00 2001 From: Zakir Dzhamaliddinov Date: Tue, 26 Nov 2024 12:47:57 +0300 Subject: [PATCH 2/2] Support terraform config generation from agent template (#241) --- lib/core/terraform_config/agent.rb | 23 +++++++++++++ lib/core/terraform_config/generator.rb | 6 +++- spec/command/terraform/generate_spec.rb | 1 + spec/core/terraform_config/agent_spec.rb | 34 ++++++++++++++++++++ spec/core/terraform_config/generator_spec.rb | 28 ++++++++++++++++ spec/dummy/.controlplane/templates/agent.yml | 5 +++ 6 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 lib/core/terraform_config/agent.rb create mode 100644 spec/core/terraform_config/agent_spec.rb create mode 100644 spec/dummy/.controlplane/templates/agent.yml 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