Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class ManageIQ::Providers::EmbeddedTerraform::AutomationManager::IbmClassicInfrastructureCredential < ManageIQ::Providers::EmbeddedTerraform::AutomationManager::TemplateCredential
COMMON_ATTRIBUTES = [].freeze

EXTRA_ATTRIBUTES = [
{
:component => 'text-field',
:label => N_('IBM Cloud Classic Infrastructure User Name'),
:helperText => N_('The User Name for IBM Cloud Classic Infrastructure.'),
:name => 'userid',
:id => 'userid',
:maxLength => 100,
:isRequired => true,
:validate => [{:type => 'required'}],
},
{
:component => 'password-field',
:label => N_('IBM Cloud Classic Infrastructure API Key'),
:helperText => N_('The API key for IBM Cloud Classic Infrastructure.'),
:name => 'password',
:id => 'password',
:type => 'password',
:isRequired => true,
:validate => [{:type => 'required'}],
},
].freeze
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed these to userid and password so we don't need to define params_to_attributes here.


API_ATTRIBUTES = (COMMON_ATTRIBUTES + EXTRA_ATTRIBUTES).freeze

API_OPTIONS = {
:type => 'cloud',
:label => N_('IBM Cloud Classic Infrastructure'),
:attributes => API_ATTRIBUTES
}.freeze

def self.display_name(number = 1)
n_('Credential (IBM Cloud Classic Infrastructure)', 'Credentials (IBM Cloud Classic Infrastructure)', number)
end
end
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
class ManageIQ::Providers::EmbeddedTerraform::AutomationManager::IbmCloudCredential < ManageIQ::Providers::EmbeddedTerraform::AutomationManager::TemplateCredential
COMMON_ATTRIBUTES = [
COMMON_ATTRIBUTES = [].freeze

EXTRA_ATTRIBUTES = [
{
:component => 'password-field',
:label => N_('IBM Cloud API Key'),
:helperText => N_('The API key for IBM Cloud. A value for this field is required if classic user name and classic API key are not provided. A valid connection must have value for IBM Cloud API Key or, IBM Cloud Classic Infrastructure User Name and IBM Cloud Classic Infrastructure API Key.'),
:helperText => N_('The API key for IBM Cloud.'),
:name => 'auth_key',
:id => 'auth_key',
:type => 'password',
Expand All @@ -12,25 +14,6 @@ class ManageIQ::Providers::EmbeddedTerraform::AutomationManager::IbmCloudCredent
},
].freeze

EXTRA_ATTRIBUTES = [
{
:component => 'text-field',
:label => N_('IBM Cloud Classic Infrastructure User Name'),
:helperText => N_('The User Name for IBM Cloud Classic Infrastructure. A value for this field is required when using classic IBM Cloud resources. A valid connection must have value for IBM Cloud API Key or, IBM Cloud Classic Infrastructure User Name and IBM Cloud Classic Infrastructure API Key.'),
:name => 'classic_user',
:id => 'classic_user',
:maxLength => 100,
},
{
:component => 'password-field',
:label => N_('IBM Cloud Classic Infrastructure API Key'),
:helperText => N_('The API key for IBM Cloud Classic Infrastructure A value for this field is required when using classic IBM Cloud resources. A valid connection must have value for IBM Cloud API Key or, IBM Cloud Classic Infrastructure User Name and IBM Cloud Classic Infrastructure API Key.'),
:name => 'classic_key',
:id => 'classic_key',
:type => 'password',
},
].freeze

API_ATTRIBUTES = (COMMON_ATTRIBUTES + EXTRA_ATTRIBUTES).freeze

API_OPTIONS = {
Expand All @@ -42,17 +25,4 @@ class ManageIQ::Providers::EmbeddedTerraform::AutomationManager::IbmCloudCredent
def self.display_name(number = 1)
n_('Credential (IBM Cloud)', 'Credentials (IBM Cloud)', number)
end

def self.params_to_attributes(params)
attrs = super.dup

attrs[:auth_key] = attrs.delete(:auth_key) if attrs.key?(:auth_key)

if %i[classic_user classic_key].any? { |opt| attrs.key?(opt) }
attrs[:userid] = attrs.delete(:classic_user) if attrs.key?(:classic_user)
attrs[:password] = attrs.delete(:classic_key) if attrs.key?(:classic_key)
end
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this just goes away if classic user/key are moved to the other class as auth_key is required, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure - @agrare ?


attrs
end
end
24 changes: 24 additions & 0 deletions lib/terraform/runner/ibm_classic_infrastructure_credential.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Terraform
class Runner
class IbmClassicInfrastructureCredential < Credential
def self.auth_type
"ManageIQ::Providers::EmbeddedTerraform::AutomationManager::IbmClassicInfrastructureCredential"
end

# Modeled off of IBM Cloud provider for terraform:
#
# https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs#environment-variables
#
# Return connection_parameters as required for terraform_runner
#
def connection_parameters
conn_params = []

ApiParams.add_param_if_present(conn_params, auth.userid, 'IAAS_CLASSIC_USERNAME')
ApiParams.add_param_if_present(conn_params, auth.password, 'IAAS_CLASSIC_API_KEY')

conn_params
end
end
end
end
2 changes: 0 additions & 2 deletions lib/terraform/runner/ibm_cloud_credential.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ def connection_parameters
conn_params = []

ApiParams.add_param_if_present(conn_params, auth.auth_key, 'IC_API_KEY')
ApiParams.add_param_if_present(conn_params, auth.userid, 'IAAS_CLASSIC_USERNAME')
ApiParams.add_param_if_present(conn_params, auth.password, 'IAAS_CLASSIC_API_KEY')

conn_params
end
Expand Down
6 changes: 5 additions & 1 deletion spec/factories/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
:parent => :embedded_terraform_credential,
:class => "ManageIQ::Providers::EmbeddedTerraform::AutomationManager::OpenstackCredential"

factory :embedded_terraform_ibmcloud_credential,
factory :embedded_terraform_ibm_cloud_credential,
:parent => :embedded_terraform_credential,
:class => "ManageIQ::Providers::EmbeddedTerraform::AutomationManager::IbmCloudCredential"

factory :embedded_terraform_ibm_classic_infrastructure_credential,
:parent => :embedded_terraform_credential,
:class => "ManageIQ::Providers::EmbeddedTerraform::AutomationManager::IbmClassicInfrastructureCredential"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'terraform/runner'

RSpec.describe(Terraform::Runner::IbmClassicInfrastructureCredential) do
it ".auth_type is the correct Authentication sub-class" do
expect(described_class.auth_type).to(eq("ManageIQ::Providers::EmbeddedTerraform::AutomationManager::IbmClassicInfrastructureCredential"))
end

context "with a credential object" do
let(:auth) { FactoryBot.create(:embedded_terraform_ibm_classic_infrastructure_credential, auth_attributes) }
let(:auth_attributes) do
{
:userid => 'iaas-classic-username',
:password => 'iaas-classic-api-key',
}
end

let(:cred) { described_class.new(auth.id) }

# Modeled off of IBM Cloud provider for terraform:
#
# https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs#environment-variables
#
describe "#connection_parameters" do
it "adds IAAS_CLASSIC_USERNAME, IAAS_CLASSIC_USERNAME if present" do
auth.update!(:auth_key => '')
expected = [
{
'name' => 'IAAS_CLASSIC_USERNAME',
'value' => 'iaas-classic-username',
'secured' => 'false',
},
{
'name' => 'IAAS_CLASSIC_API_KEY',
'value' => 'iaas-classic-api-key',
'secured' => 'false',
},
]
expect(cred.connection_parameters).to(eq(expected))
end
end
end
end
27 changes: 1 addition & 26 deletions spec/lib/terraform/runner/ibm_cloud_credential_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
end

context "with a credential object" do
let(:auth) { FactoryBot.create(:embedded_terraform_ibmcloud_credential, auth_attributes) }
let(:auth) { FactoryBot.create(:embedded_terraform_ibm_cloud_credential, auth_attributes) }
let(:auth_attributes) do
{
:auth_key => 'ibmcloud-api-key',
:userid => 'iaas-classic-username',
:password => 'iaas-classic-api-key',
}
end

Expand All @@ -33,29 +31,6 @@
]
expect(cred.connection_parameters).to(eq(expected))
end

it "adds IAAS_CLASSIC_USERNAME, IAAS_CLASSIC_USERNAME if present" do
auth.update!(:auth_key => '')
expected = [
{
'name' => 'IAAS_CLASSIC_USERNAME',
'value' => 'iaas-classic-username',
'secured' => 'false',
},
{
'name' => 'IAAS_CLASSIC_API_KEY',
'value' => 'iaas-classic-api-key',
'secured' => 'false',
},
]
expect(cred.connection_parameters).to(eq(expected))
end
end

# describe "#env_vars" do
# it "returns an empty hash" do
# expect(cred.env_vars).to(eq({}))
# end
# end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -539,47 +539,88 @@

let(:params) do
{
:name => "IBM Cloud Credential",
:auth_key => "secret1",
:classic_user => "user1",
:classic_key => "secret2",
:name => "IBM Cloud Credential",
:auth_key => "secret1",
}
end
let(:queue_create_params) do
{
:name => "IBM Cloud Credential",
:auth_key => ManageIQ::Password.encrypt("secret1"),
:classic_user => "user1",
:classic_key => ManageIQ::Password.encrypt("secret2"),
:name => "IBM Cloud Credential",
:auth_key => ManageIQ::Password.encrypt("secret1"),
}
end
let(:params_to_attributes) do
{
:name => "IBM Cloud Credential",
:auth_key => "secret1",
}
end
let(:expected_values) do
{
:name => "IBM Cloud Credential",
:auth_key_encrypted => ManageIQ::Password.encrypt("secret1"),
:auth_key => "secret1"
}
end
let(:params_to_attrs) { [] }
let(:update_params) do
{
:name => "Updated Credential",
:auth_key => "supersecret"
}
end
let(:update_queue_params) do
{
:name => "Updated Credential",
:auth_key => ManageIQ::Password.encrypt("supersecret")
}
end
end
end

context "IbmClassicInfrastructureCredential" do
it_behaves_like 'an embedded_terraform credential' do
let(:credential_class) { embedded_terraform::IbmClassicInfrastructureCredential }

let(:params) do
{
:name => "IBM Cloud Classic Infrastructure Credential",
:userid => "user1",
:password => "secret2",
}
end
let(:queue_create_params) do
{
:name => "IBM Cloud Classic Infrastructure Credential",
:userid => "user1",
:password => ManageIQ::Password.encrypt("secret2"),
}
end
let(:params_to_attributes) do
{
:name => "IBM Cloud Classic Infrastructure Credential",
:userid => "user1",
:password => "secret2",
}
end
let(:expected_values) do
{
:name => "IBM Cloud Credential",
:auth_key_encrypted => ManageIQ::Password.try_encrypt("secret1"),
:name => "IBM Cloud Classic Infrastructure Credential",
:userid => "user1",
:password_encrypted => ManageIQ::Password.try_encrypt("secret2"),
:password_encrypted => ManageIQ::Password.encrypt("secret2"),
}
end
let(:params_to_attrs) { [:auth_key, :userid] }
let(:params_to_attrs) { [] }
let(:update_params) do
{
:name => "Updated Credential",
:classic_key => "supersecret"
:name => "Updated Credential",
:password => "supersecret"
}
end
let(:update_queue_params) do
{
:name => "Updated Credential",
:classic_key => ManageIQ::Password.encrypt("supersecret")
:name => "Updated Credential",
:password => ManageIQ::Password.encrypt("supersecret")
}
end
end
Expand Down