From 2630c3cac27f5dade5ab57cd8e18aa2569c1f56a Mon Sep 17 00:00:00 2001 From: Zakir Dzhamaliddinov Date: Thu, 3 Oct 2024 15:37:12 +0400 Subject: [PATCH] Add dir option & make app option non-required --- docs/commands.md | 2 +- lib/command/base.rb | 13 +++++++ lib/command/terraform/generate.rb | 45 ++++++++++++++++++++----- spec/command/terraform/generate_spec.rb | 17 +++++----- 4 files changed, 60 insertions(+), 17 deletions(-) diff --git a/docs/commands.md b/docs/commands.md index c6a1ac75..14fa758a 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -449,7 +449,7 @@ cpflow setup-app -a $APP_NAME - Generates terraform configuration files based on `controlplane.yml` and `templates/` config ```sh -cpflow terraform generate -a $APP_NAME +cpflow terraform generate ``` ### `version` diff --git a/lib/command/base.rb b/lib/command/base.rb index 38dedff1..a7c788d6 100644 --- a/lib/command/base.rb +++ b/lib/command/base.rb @@ -454,6 +454,19 @@ def self.add_app_identity_option(required: false) } } end + + def self.dir_option(required: false) + { + name: :dir, + params: { + banner: "DIR", + desc: "Output directory", + type: :string, + required: required + } + } + end + # rubocop:enable Metrics/MethodLength def self.all_options diff --git a/lib/command/terraform/generate.rb b/lib/command/terraform/generate.rb index 164d665f..99459a09 100644 --- a/lib/command/terraform/generate.rb +++ b/lib/command/terraform/generate.rb @@ -6,7 +6,8 @@ class Generate < Base SUBCOMMAND_NAME = "terraform" NAME = "generate" OPTIONS = [ - app_option(required: true) + app_option(required: false), + dir_option(required: false) ].freeze DESCRIPTION = "Generates terraform configuration files" LONG_DESCRIPTION = <<~DESC @@ -15,7 +16,32 @@ class Generate < Base WITH_INFO_HEADER = false def call + generate_common_configs + generate_app_configs + end + + private + + def generate_common_configs + cpln_provider = TerraformConfig::RequiredProvider.new( + "cpln", + source: "controlplane-com/cpln", + version: "~> 1.0" + ) + File.write(terraform_dir.join("providers.tf"), cpln_provider.to_tf) + end + + def generate_app_configs + Array(config.app || config.apps.keys).each do |app| + generate_app_config(app.to_s) + end + end + + def generate_app_config(app) + config.class.define_method(:app) { app } + + terraform_app_dir = recreate_terraform_app_dir(app) templates.each do |template| generator = TerraformConfig::Generator.new(config: config, template: template) @@ -23,14 +49,17 @@ def call # TODO: Delete line below after all template kinds are supported next unless %w[gvc identity].include?(template["kind"]) - File.write(terraform_dir.join(generator.filename), generator.tf_config.to_tf, mode: "a+") + File.write(terraform_app_dir.join(generator.filename), generator.tf_config.to_tf, mode: "a+") end end - private + def recreate_terraform_app_dir(app_path) + full_path = terraform_dir.join(app_path) + + FileUtils.rm_rf(full_path) + FileUtils.mkdir_p(full_path) - def cpln_provider - TerraformConfig::RequiredProvider.new("cpln", source: "controlplane-com/cpln", version: "~> 1.0") + full_path end def templates @@ -39,9 +68,9 @@ def templates end def terraform_dir - @terraform_dir ||= Cpflow.root_path.join("terraform").tap do |path| - FileUtils.rm_rf(path) - FileUtils.mkdir_p(path) + @terraform_dir ||= begin + full_path = config.options.fetch(:dir, Cpflow.root_path.join("terraform")) + Pathname.new(full_path).tap { |path| FileUtils.mkdir_p(path) } end end end diff --git a/spec/command/terraform/generate_spec.rb b/spec/command/terraform/generate_spec.rb index 4c42dff9..6c0676da 100644 --- a/spec/command/terraform/generate_spec.rb +++ b/spec/command/terraform/generate_spec.rb @@ -12,23 +12,24 @@ let!(:app) { dummy_test_app } before do - FileUtils.rm_r(GENERATOR_PLAYGROUND_PATH) if Dir.exist?(GENERATOR_PLAYGROUND_PATH) - FileUtils.mkdir_p GENERATOR_PLAYGROUND_PATH - + FileUtils.rm_rf(GENERATOR_PLAYGROUND_PATH) + FileUtils.mkdir_p(GENERATOR_PLAYGROUND_PATH) allow(Cpflow).to receive(:root_path).and_return(GENERATOR_PLAYGROUND_PATH) end after do - FileUtils.rm_r GENERATOR_PLAYGROUND_PATH + FileUtils.rm_rf GENERATOR_PLAYGROUND_PATH end it "generates terraform config files", :aggregate_failures do - config_file_paths = %w[providers.tf gvc.tf identities.tf].map do |config_file_path| - TERRAFORM_CONFIG_DIR_PATH.join(config_file_path) - end - config_file_paths.each { |config_file_path| expect(config_file_path).not_to exist } run_cpflow_command(described_class::SUBCOMMAND_NAME, described_class::NAME, "-a", app) expect(config_file_paths).to all(exist) end + + def config_file_paths + [TERRAFORM_CONFIG_DIR_PATH.join("providers.tf")] + %w[gvc.tf identities.tf].map do |config_file_path| + TERRAFORM_CONFIG_DIR_PATH.join(app, config_file_path) + end + end end