Skip to content

Commit

Permalink
Add dir option & make app option non-required
Browse files Browse the repository at this point in the history
  • Loading branch information
zzaakiirr committed Oct 3, 2024
1 parent 32e8790 commit d47aa92
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
14 changes: 14 additions & 0 deletions lib/command/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,20 @@ def self.add_app_identity_option(required: false)
}
}
end

def self.dir_option(required: false)
{
name: :dir,
params: {
aliases: ["-dir"],
banner: "DIR",
desc: "Output directory",
type: :string,
required: required
}
}
end

# rubocop:enable Metrics/MethodLength

def self.all_options
Expand Down
45 changes: 37 additions & 8 deletions lib/command/terraform/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,22 +16,50 @@ 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)

# 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
Expand All @@ -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
Expand Down
17 changes: 9 additions & 8 deletions spec/command/terraform/generate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d47aa92

Please sign in to comment.