ManageIQ / manageiq-providers-embedded_terraform

ManageIQ plugin for the Embedded Terraform provider.
Apache License 2.0
0 stars 11 forks source link

Fix Provision options for EmbeddedTerraform Stack #30

Closed agrare closed 5 months ago

agrare commented 5 months ago

The options column for a ServiceTerraformTemplate includes information that isn't necessary for a provision request and the options sent to the Stack.create_stack should be tailored to only include what is necessary.

Also this fixes the credential IDs being passed down since those values were in the options.config_info.provision hash.

Example :options from a ServiceTerraformTemplate

>> pp ServiceTerraformTemplate.first.options                             
=>                                                                   
{:config_info=>                                                      
  {:provision=>{:repository_id=>"1", :execution_ttl=>60, :log_output=>"on_error", :verbosity=>"0", :extra_vars=>{:FOO=>{:default=>"Bar"}}, :dialog_id=>"1", :configuration_script_payload_id=>"18", :credential_id=>"2", :fqname=>"/Service/Generic/StateMachines/GenericLifecycle/provision"},
   :retirement=>{:fqname=>"/Service/Generic/StateMachines/GenericLifecycle/Retire_Basic_Resource"}},
 :dialog=>{"dialog_text_box_1"=>""},                                 
 :provision_automate_timeout=>100}

Example provision_job_options which is what is sent to the Stack.create_stack

{:provision_job_options=>{"execution_ttl"=>60, "extra_vars"=>{"FOO"=>"Bar"}, "verbosity"=>"0", "credential"=>2}}

Example options sent to Terraform::Runner.run`

=> 25:     response = Terraform::Runner.run(
   26:       options[:input_vars],
   27:       template_path,
   28:       :credentials => options[:credentials],
   29:       :env_vars    => options[:env_vars]
(byebug) pp options
{:template_id=>18, :env_vars=>{}, :input_vars=>{"execution_ttl"=>60, "extra_vars"=>{"FOO"=>"Bar"}, "verbosity"=>"0"}, :credentials=>[2], :poll_interval=>1 minute, :git_checkout_tempdir=>"/tmp/embedded-terraform-runner-git20240523-157937-zvo18d"}
{:template_id=>18, :env_vars=>{}, :input_vars=>{"execution_ttl"=>60, "extra_vars"=>{"FOO"=>"Bar"}, "verbosity"=>"0"}, :credentials=>[2], :poll_interval=>1 minute, :git_checkout_tempdir=>"/tmp/embedded-terraform-runner-git20240523-157937-zvo18d"}
(byebug) options[:input_vars]
{"execution_ttl"=>60, "extra_vars"=>{"FOO"=>"Bar"}, "verbosity"=>"0"}
(byebug) template_path
"/tmp/embedded-terraform-runner-git20240523-157937-zvo18d/Google/terraform/hcl/singleVM"
(byebug) options[:credentials]
[2]
(byebug) options[:env_vars]
{}
miq-bot commented 5 months ago

Checked commit https://github.com/agrare/manageiq-providers-embedded_terraform/commit/6b4ff3dacd5d26ea39916314b251ab6075da15b4 with ruby 2.7.8, rubocop 1.56.3, haml-lint 0.51.0, and yamllint 3 files checked, 1 offense detected

app/models/service_terraform_template.rb

agrare commented 5 months ago

NOTE I am getting the following exception from Terraform::Runner:

[----] E, [2024-05-23T11:38:16.432267#157937:9ee8] ERROR -- evm: MIQ(MiqQueue#deliver) Message id: [3886], Error: [undefined method `id' for 2:Integer

            'connection_parameters' => Terraform::Runner::Credential.new(cred.id).connection_parameters
                                                                             ^^^
Did you mean?  i]
[----] E, [2024-05-23T11:38:16.432739#157937:9ee8] ERROR -- evm: [NoMethodError]: undefined method `id' for 2:Integer

            'connection_parameters' => Terraform::Runner::Credential.new(cred.id).connection_parameters
                                                                             ^^^
Did you mean?  i  Method:[block (2 levels) in <class:LogProxy>]
[----] E, [2024-05-23T11:38:16.432852#157937:9ee8] ERROR -- evm: /home/grare/adam/src/manageiq/manageiq-providers-embedded_terraform/lib/terraform/runner.rb:109:in `block in provider_connection_parameters'
/home/grare/adam/src/manageiq/manageiq-providers-embedded_terraform/lib/terraform/runner.rb:107:in `collect'
/home/grare/adam/src/manageiq/manageiq-providers-embedded_terraform/lib/terraform/runner.rb:107:in `provider_connection_parameters'
/home/grare/adam/src/manageiq/manageiq-providers-embedded_terraform/lib/terraform/runner.rb:129:in `create_stack_job'
/home/grare/adam/src/manageiq/manageiq-providers-embedded_terraform/lib/terraform/runner.rb:30:in `run_async'
/home/grare/adam/src/manageiq/manageiq-providers-embedded_terraform/app/models/manageiq/providers/embedded_terraform/automation_manager/job.rb:25:in `execute'

It seems that it is expecting authentication records: https://github.com/ManageIQ/manageiq-providers-embedded_terraform/blob/master/lib/terraform/runner.rb#L109

      def provider_connection_parameters(credentials)
        credentials.collect do |cred|
          {
            'connection_parameters' => Terraform::Runner::Credential.new(cred.id).connection_parameters
          }
        end
      end

But the doc mentions that it expects a list of IDs: https://github.com/ManageIQ/manageiq-providers-embedded_terraform/blob/master/lib/terraform/runner.rb#L24

# @param credentials [Array] List of Authentication object ids to provide to the terraform run
Fryguy commented 5 months ago

@agrare LGTM - do you want me to merge and then the Terraform runner thing fixed later, or do you expect it to be fixed in this PR also?

agrare commented 5 months ago

@Fryguy that's a different issue so I'll push a separate PR for that so you can merge this as is

Fryguy commented 5 months ago

Backported to radjabov in commit 5e8b39a7601823aa5941df4cc98c81de9c632047.

commit 5e8b39a7601823aa5941df4cc98c81de9c632047
Author: Jason Frey <fryguy9@gmail.com>
Date:   Fri May 24 10:23:32 2024 -0400

    Merge pull request #30 from agrare/fix_service_options

    Fix Provision options for EmbeddedTerraform Stack

    (cherry picked from commit ac7a4094bebafcc31fa556a9edd596eb43befa92)