Skip to content

Commit

Permalink
Fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zzaakiirr committed Oct 21, 2024
1 parent 6538b03 commit ed31386
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
8 changes: 3 additions & 5 deletions lib/command/terraform/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def generate_common_configs

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
Expand All @@ -43,12 +43,10 @@ def generate_app_config
terraform_app_dir = recreate_terraform_app_dir

templates.each do |template|
# TODO: Raise error i/o ignoring invalid template kind after all template kinds are supported
next unless TerraformConfig::Generator::SUPPORTED_TEMPLATE_KINDS.include?(template["kind"])

generator = TerraformConfig::Generator.new(config: config, template: template)

File.write(terraform_app_dir.join(generator.filename), generator.tf_config.to_tf, mode: "a+")
rescue TerraformConfig::Generator::InvalidTemplateError => e
Shell.warn(e.message)
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/core/terraform_config/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module TerraformConfig
class Generator
SUPPORTED_TEMPLATE_KINDS = %w[gvc secret identity policy volumeset].freeze

InvalidTemplateError = Class.new(ArgumentError)

attr_reader :config, :template

def initialize(config:, template:)
Expand All @@ -27,7 +29,7 @@ def tf_config
def validate_template_kind!
return if SUPPORTED_TEMPLATE_KINDS.include?(kind)

raise ArgumentError, "Unsupported template kind: #{kind}"
raise InvalidTemplateError, "Unsupported template kind: #{kind}"
end

def config_class
Expand Down
18 changes: 18 additions & 0 deletions spec/command/terraform/generate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@
end
end

context "when InvalidTemplateError is raised" do
before do
allow_any_instance_of(TerraformConfig::Generator).to receive(:tf_config).and_raise( # rubocop:disable RSpec/AnyInstance
TerraformConfig::Generator::InvalidTemplateError, "Invalid template: error message"
)
end

it "generates common config files and warns about invalid template" do
config_file_paths.each { |config_file_path| expect(config_file_path).not_to exist }

expect(result[:status]).to eq(0)
expect(result[:stderr]).to include("Invalid template: error message")

expect(common_config_files).to all(exist)
app_config_files.each { |config_file_path| expect(config_file_path).not_to exist }
end
end

def config_file_paths
common_config_files + app_config_files
end
Expand Down

0 comments on commit ed31386

Please sign in to comment.