Skip to content

Commit

Permalink
Merge branch 'terraform-feature' of github.com:shakacode/control-plan…
Browse files Browse the repository at this point in the history
…e-flow into support-terraform-config-generation-from-policy-template
  • Loading branch information
zzaakiirr committed Oct 21, 2024
2 parents 080f3d6 + fa966c8 commit 66cb5b4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 23 deletions.
21 changes: 10 additions & 11 deletions lib/command/terraform/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class Generate < Base
SUBCOMMAND_NAME = "terraform"
NAME = "generate"
OPTIONS = [
app_option(required: false),
dir_option(required: false)
app_option,
dir_option
].freeze
DESCRIPTION = "Generates terraform configuration files"
LONG_DESCRIPTION = <<~DESC
Expand All @@ -34,14 +34,13 @@ def generate_common_configs

def generate_app_configs
Array(config.app || config.apps.keys).each do |app|
generate_app_config(app.to_s)
config.instance_variable_set(:@app, app)
generate_app_config
end
end

def generate_app_config(app)
config.class.define_method(:app) { app }

terraform_app_dir = recreate_terraform_app_dir(app)
def generate_app_config
terraform_app_dir = recreate_terraform_app_dir

templates.each do |template|
generator = TerraformConfig::Generator.new(config: config, template: template)
Expand All @@ -53,8 +52,8 @@ def generate_app_config(app)
end
end

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

unless File.expand_path(full_path).include?(Cpflow.root_path.to_s)
Shell.abort("Directory to save terraform configuration files cannot be outside of current directory")
Expand All @@ -71,13 +70,13 @@ def templates
template_files = Dir["#{parser.template_dir}/*.yml"]

if template_files.empty?
Shell.warn "No templates found in #{parser.template_dir}"
Shell.warn("No templates found in #{parser.template_dir}")
return []
end

parser.parse(template_files)
rescue StandardError => e
Shell.warn "Error parsing templates: #{e.message}"
Shell.warn("Error parsing templates: #{e.message}")
[]
end

Expand Down
4 changes: 1 addition & 3 deletions lib/core/terraform_config/gvc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ module TerraformConfig
class Gvc < Base
attr_reader :name, :description, :tags, :domain, :locations, :pull_secrets, :env, :load_balancer

# rubocop:disable Metrics/ParameterLists
def initialize(
def initialize( # rubocop:disable Metrics/ParameterLists
name:,
description: nil,
tags: nil,
Expand All @@ -26,7 +25,6 @@ def initialize(
@env = env
@load_balancer = load_balancer&.deep_underscore_keys&.deep_symbolize_keys
end
# rubocop:enable Metrics/ParameterLists

def to_tf
block :resource, :cpln_gvc, name do
Expand Down
4 changes: 1 addition & 3 deletions lib/core/terraform_config/secret.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# frozen_string_literal: true

module TerraformConfig
# rubocop:disable Metrics/ClassLength
class Secret < Base
class Secret < Base # rubocop:disable Metrics/ClassLength
REQUIRED_DATA_KEYS = {
"aws" => %i[secret_key access_key],
"azure-connector" => %i[url code],
Expand Down Expand Up @@ -128,5 +127,4 @@ def aws_based_tf(name, **kwargs)
end
end
end
# rubocop:enable Metrics/ClassLength
end
8 changes: 2 additions & 6 deletions spec/command/terraform/generate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
let(:template_dir) { "non-existing-folder" }

before do
# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(TemplateParser).to receive(:template_dir).and_return(template_dir)
# rubocop:enable RSpec/AnyInstance
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
Expand All @@ -53,9 +51,7 @@

context "when template parsing fails" do
before do
# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(TemplateParser).to receive(:parse).and_raise("error")
# rubocop:enable RSpec/AnyInstance
allow_any_instance_of(TemplateParser).to receive(:parse).and_raise("error") # rubocop:disable RSpec/AnyInstance
end

it "generates only common config files" do
Expand Down

0 comments on commit 66cb5b4

Please sign in to comment.