Skip to content

Commit

Permalink
Put providers.tf & required_providers.tf to each app folder
Browse files Browse the repository at this point in the history
  • Loading branch information
zzaakiirr committed Oct 22, 2024
1 parent 40aba4c commit 63dda9a
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 54 deletions.
39 changes: 21 additions & 18 deletions lib/command/terraform/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,20 @@ 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|
config.instance_variable_set(:@app, app)
config.instance_variable_set(:@app, app.to_s)
generate_app_config
end
end

private

def generate_app_config
terraform_app_dir = recreate_terraform_app_dir

generate_required_providers(terraform_app_dir)
generate_providers(terraform_app_dir)

templates.each do |template|
generator = TerraformConfig::Generator.new(config: config, template: template)

Expand All @@ -52,6 +40,21 @@ def generate_app_config
end
end

def generate_required_providers(terraform_app_dir)
required_cpln_provider = TerraformConfig::RequiredProvider.new(
"cpln",
source: "controlplane-com/cpln",
version: "~> 1.0"
)

File.write(terraform_app_dir.join("required_providers.tf"), required_cpln_provider.to_tf)
end

def generate_providers(terraform_app_dir)
cpln_provider = TerraformConfig::Provider.new("cpln", org: config.org)
File.write(terraform_app_dir.join("providers.tf"), cpln_provider.to_tf)
end

def recreate_terraform_app_dir
full_path = terraform_dir.join(config.app)

Expand Down
22 changes: 22 additions & 0 deletions lib/core/terraform_config/provider.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module TerraformConfig
class Provider < Base
attr_reader :name, :options

def initialize(name, **options)
super()

@name = name
@options = options
end

def to_tf
block :provider, name do
options.each do |option, value|
argument option, value
end
end
end
end
end
4 changes: 4 additions & 0 deletions lib/core/terraform_config/required_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def initialize(name, **options)

def to_tf
block :terraform do
block :cloud do
argument :organization, "PLACEHOLDER"
end

block :required_providers do
argument name, options
end
Expand Down
79 changes: 43 additions & 36 deletions spec/command/terraform/generate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,32 @@
FileUtils.rm_rf GENERATOR_PLAYGROUND_PATH
end

it "generates terraform config files", :aggregate_failures do
config_file_paths.each { |config_file_path| expect(config_file_path).not_to exist }
shared_examples "generates terraform config files" do
specify do
all_config_paths.each { |path| expect(path).not_to exist }

expect(result[:status]).to eq(0)
expect(result[:status]).to eq(ExitCode::SUCCESS)
expect(result[:stderr]).to err_msg ? include(err_msg) : be_empty

expect(config_file_paths).to all(exist)
expect(expected_config_paths).to all(exist)
(all_config_paths - expected_config_paths).each { |path| expect(path).not_to exist }
end
end

shared_examples "does not generate any terraform config files" do |err_msg|
it "fails with an error" do
all_config_paths.each { |path| expect(path).not_to exist }

expect(result[:status]).to eq(ExitCode::ERROR_DEFAULT)
expect(result[:stderr]).to include(err_msg)

all_config_paths.each { |path| expect(path).not_to exist }
end
end

it_behaves_like "generates terraform config files" do
let(:expected_config_paths) { all_config_paths }
let(:err_msg) { nil }
end

context "when templates folder is empty" do
Expand All @@ -39,13 +59,9 @@
allow_any_instance_of(TemplateParser).to receive(:template_dir).and_return(template_dir) # rubocop:disable RSpec/AnyInstance
end

it "generates only common config files" do
config_file_paths.each { |config_file_path| expect(config_file_path).not_to exist }

expect(result[:stderr]).to include("No templates found in #{template_dir}")

expect(common_config_files).to all(exist)
app_config_files.each { |config_file_path| expect(config_file_path).not_to exist }
it_behaves_like "generates terraform config files" do
let(:expected_config_paths) { provider_config_paths }
let(:err_msg) { "No templates found in #{template_dir}" }
end
end

Expand All @@ -54,49 +70,40 @@
allow_any_instance_of(TemplateParser).to receive(:parse).and_raise("error") # rubocop:disable RSpec/AnyInstance
end

it "generates only common config files" do
config_file_paths.each { |config_file_path| expect(config_file_path).not_to exist }

expect(result[:stderr]).to include("Error parsing templates: error")

expect(common_config_files).to all(exist)
app_config_files.each { |config_file_path| expect(config_file_path).not_to exist }
it_behaves_like "generates terraform config files" do
let(:expected_config_paths) { provider_config_paths }
let(:err_msg) { "Error parsing templates: error" }
end
end

context "when --dir option is outside of project dir" do
let(:options) { ["-a", app, "--dir", GEM_TEMP_PATH.join("path-outside-of-project").to_s] }

it "aborts command execution" do
expect(result[:status]).to eq(ExitCode::ERROR_DEFAULT)
expect(result[:stderr]).to include(
"Directory to save terraform configuration files cannot be outside of current directory"
)
end
it_behaves_like "does not generate any terraform config files",
"Directory to save terraform configuration files cannot be outside of current directory"
end

context "when terraform config directory creation fails" do
before do
allow(FileUtils).to receive(:mkdir_p).and_raise("error")
end

it "aborts command execution" do
expect(result[:status]).to eq(ExitCode::ERROR_DEFAULT)
expect(result[:stderr]).to include("error")
end
it_behaves_like "does not generate any terraform config files", "error"
end

def config_file_paths
common_config_files + app_config_files
def all_config_paths
provider_config_paths + template_config_paths
end

def common_config_files
[TERRAFORM_CONFIG_DIR_PATH.join("providers.tf")]
def provider_config_paths
%w[required_providers.tf providers.tf].map { |filename| config_path(filename) }
end

def app_config_files
%w[gvc.tf identities.tf secrets.tf policies.tf].map do |config_file_path|
TERRAFORM_CONFIG_DIR_PATH.join(app, config_file_path)
end
def template_config_paths
%w[gvc.tf identities.tf secrets.tf policies.tf].map { |filename| config_path(filename) }
end

def config_path(name)
TERRAFORM_CONFIG_DIR_PATH.join(app, name)
end
end
26 changes: 26 additions & 0 deletions spec/core/terraform_config/provider_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require "spec_helper"

describe TerraformConfig::Provider do
let(:config) { described_class.new(name, **options) }

describe "#to_tf" do
subject(:generated) { config.to_tf }

context "when provider is cpln" do
let(:name) { "cpln" }
let(:options) { { org: "test-org" } }

it "generates correct config" do
expect(generated).to eq(
<<~EXPECTED
provider "cpln" {
org = "test-org"
}
EXPECTED
)
end
end
end
end
3 changes: 3 additions & 0 deletions spec/core/terraform_config/required_provider_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
expect(generated).to eq(
<<~EXPECTED
terraform {
cloud {
organization = "PLACEHOLDER"
}
required_providers {
cpln = {
source = "controlplane-com/cpln"
Expand Down

0 comments on commit 63dda9a

Please sign in to comment.