Skip to content

Commit 2360f12

Browse files
authored
Merge pull request #23 from jrafanie/split_ibm_cloud_into_cloud_and_classic_infra_credentials
Split ibm cloud credentials from ibm classic infra credentials
2 parents ffccb9f + 2827fc2 commit 2360f12

File tree

8 files changed

+171
-79
lines changed

8 files changed

+171
-79
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class ManageIQ::Providers::EmbeddedTerraform::AutomationManager::IbmClassicInfrastructureCredential < ManageIQ::Providers::EmbeddedTerraform::AutomationManager::TemplateCredential
2+
COMMON_ATTRIBUTES = [].freeze
3+
4+
EXTRA_ATTRIBUTES = [
5+
{
6+
:component => 'text-field',
7+
:label => N_('IBM Cloud Classic Infrastructure User Name'),
8+
:helperText => N_('The User Name for IBM Cloud Classic Infrastructure.'),
9+
:name => 'userid',
10+
:id => 'userid',
11+
:maxLength => 100,
12+
:isRequired => true,
13+
:validate => [{:type => 'required'}],
14+
},
15+
{
16+
:component => 'password-field',
17+
:label => N_('IBM Cloud Classic Infrastructure API Key'),
18+
:helperText => N_('The API key for IBM Cloud Classic Infrastructure.'),
19+
:name => 'password',
20+
:id => 'password',
21+
:type => 'password',
22+
:isRequired => true,
23+
:validate => [{:type => 'required'}],
24+
},
25+
].freeze
26+
27+
API_ATTRIBUTES = (COMMON_ATTRIBUTES + EXTRA_ATTRIBUTES).freeze
28+
29+
API_OPTIONS = {
30+
:type => 'cloud',
31+
:label => N_('IBM Cloud Classic Infrastructure'),
32+
:attributes => API_ATTRIBUTES
33+
}.freeze
34+
35+
def self.display_name(number = 1)
36+
n_('Credential (IBM Cloud Classic Infrastructure)', 'Credentials (IBM Cloud Classic Infrastructure)', number)
37+
end
38+
end
Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
class ManageIQ::Providers::EmbeddedTerraform::AutomationManager::IbmCloudCredential < ManageIQ::Providers::EmbeddedTerraform::AutomationManager::TemplateCredential
2-
COMMON_ATTRIBUTES = [
2+
COMMON_ATTRIBUTES = [].freeze
3+
4+
EXTRA_ATTRIBUTES = [
35
{
46
:component => 'password-field',
57
:label => N_('IBM Cloud API Key'),
6-
: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.'),
8+
:helperText => N_('The API key for IBM Cloud.'),
79
:name => 'auth_key',
810
:id => 'auth_key',
911
:type => 'password',
@@ -12,25 +14,6 @@ class ManageIQ::Providers::EmbeddedTerraform::AutomationManager::IbmCloudCredent
1214
},
1315
].freeze
1416

15-
EXTRA_ATTRIBUTES = [
16-
{
17-
:component => 'text-field',
18-
:label => N_('IBM Cloud Classic Infrastructure User Name'),
19-
: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.'),
20-
:name => 'classic_user',
21-
:id => 'classic_user',
22-
:maxLength => 100,
23-
},
24-
{
25-
:component => 'password-field',
26-
:label => N_('IBM Cloud Classic Infrastructure API Key'),
27-
: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.'),
28-
:name => 'classic_key',
29-
:id => 'classic_key',
30-
:type => 'password',
31-
},
32-
].freeze
33-
3417
API_ATTRIBUTES = (COMMON_ATTRIBUTES + EXTRA_ATTRIBUTES).freeze
3518

3619
API_OPTIONS = {
@@ -42,17 +25,4 @@ class ManageIQ::Providers::EmbeddedTerraform::AutomationManager::IbmCloudCredent
4225
def self.display_name(number = 1)
4326
n_('Credential (IBM Cloud)', 'Credentials (IBM Cloud)', number)
4427
end
45-
46-
def self.params_to_attributes(params)
47-
attrs = super.dup
48-
49-
attrs[:auth_key] = attrs.delete(:auth_key) if attrs.key?(:auth_key)
50-
51-
if %i[classic_user classic_key].any? { |opt| attrs.key?(opt) }
52-
attrs[:userid] = attrs.delete(:classic_user) if attrs.key?(:classic_user)
53-
attrs[:password] = attrs.delete(:classic_key) if attrs.key?(:classic_key)
54-
end
55-
56-
attrs
57-
end
5828
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module Terraform
2+
class Runner
3+
class IbmClassicInfrastructureCredential < Credential
4+
def self.auth_type
5+
"ManageIQ::Providers::EmbeddedTerraform::AutomationManager::IbmClassicInfrastructureCredential"
6+
end
7+
8+
# Modeled off of IBM Cloud provider for terraform:
9+
#
10+
# https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs#environment-variables
11+
#
12+
# Return connection_parameters as required for terraform_runner
13+
#
14+
def connection_parameters
15+
conn_params = []
16+
17+
ApiParams.add_param_if_present(conn_params, auth.userid, 'IAAS_CLASSIC_USERNAME')
18+
ApiParams.add_param_if_present(conn_params, auth.password, 'IAAS_CLASSIC_API_KEY')
19+
20+
conn_params
21+
end
22+
end
23+
end
24+
end

lib/terraform/runner/ibm_cloud_credential.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ def connection_parameters
1515
conn_params = []
1616

1717
ApiParams.add_param_if_present(conn_params, auth.auth_key, 'IC_API_KEY')
18-
ApiParams.add_param_if_present(conn_params, auth.userid, 'IAAS_CLASSIC_USERNAME')
19-
ApiParams.add_param_if_present(conn_params, auth.password, 'IAAS_CLASSIC_API_KEY')
2018

2119
conn_params
2220
end

spec/factories/authentication.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
:parent => :embedded_terraform_credential,
2828
:class => "ManageIQ::Providers::EmbeddedTerraform::AutomationManager::OpenstackCredential"
2929

30-
factory :embedded_terraform_ibmcloud_credential,
30+
factory :embedded_terraform_ibm_cloud_credential,
3131
:parent => :embedded_terraform_credential,
3232
:class => "ManageIQ::Providers::EmbeddedTerraform::AutomationManager::IbmCloudCredential"
33+
34+
factory :embedded_terraform_ibm_classic_infrastructure_credential,
35+
:parent => :embedded_terraform_credential,
36+
:class => "ManageIQ::Providers::EmbeddedTerraform::AutomationManager::IbmClassicInfrastructureCredential"
3337
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require 'terraform/runner'
2+
3+
RSpec.describe(Terraform::Runner::IbmClassicInfrastructureCredential) do
4+
it ".auth_type is the correct Authentication sub-class" do
5+
expect(described_class.auth_type).to(eq("ManageIQ::Providers::EmbeddedTerraform::AutomationManager::IbmClassicInfrastructureCredential"))
6+
end
7+
8+
context "with a credential object" do
9+
let(:auth) { FactoryBot.create(:embedded_terraform_ibm_classic_infrastructure_credential, auth_attributes) }
10+
let(:auth_attributes) do
11+
{
12+
:userid => 'iaas-classic-username',
13+
:password => 'iaas-classic-api-key',
14+
}
15+
end
16+
17+
let(:cred) { described_class.new(auth.id) }
18+
19+
# Modeled off of IBM Cloud provider for terraform:
20+
#
21+
# https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs#environment-variables
22+
#
23+
describe "#connection_parameters" do
24+
it "adds IAAS_CLASSIC_USERNAME, IAAS_CLASSIC_USERNAME if present" do
25+
auth.update!(:auth_key => '')
26+
expected = [
27+
{
28+
'name' => 'IAAS_CLASSIC_USERNAME',
29+
'value' => 'iaas-classic-username',
30+
'secured' => 'false',
31+
},
32+
{
33+
'name' => 'IAAS_CLASSIC_API_KEY',
34+
'value' => 'iaas-classic-api-key',
35+
'secured' => 'false',
36+
},
37+
]
38+
expect(cred.connection_parameters).to(eq(expected))
39+
end
40+
end
41+
end
42+
end

spec/lib/terraform/runner/ibm_cloud_credential_spec.rb

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
end
77

88
context "with a credential object" do
9-
let(:auth) { FactoryBot.create(:embedded_terraform_ibmcloud_credential, auth_attributes) }
9+
let(:auth) { FactoryBot.create(:embedded_terraform_ibm_cloud_credential, auth_attributes) }
1010
let(:auth_attributes) do
1111
{
1212
:auth_key => 'ibmcloud-api-key',
13-
:userid => 'iaas-classic-username',
14-
:password => 'iaas-classic-api-key',
1513
}
1614
end
1715

@@ -33,29 +31,6 @@
3331
]
3432
expect(cred.connection_parameters).to(eq(expected))
3533
end
36-
37-
it "adds IAAS_CLASSIC_USERNAME, IAAS_CLASSIC_USERNAME if present" do
38-
auth.update!(:auth_key => '')
39-
expected = [
40-
{
41-
'name' => 'IAAS_CLASSIC_USERNAME',
42-
'value' => 'iaas-classic-username',
43-
'secured' => 'false',
44-
},
45-
{
46-
'name' => 'IAAS_CLASSIC_API_KEY',
47-
'value' => 'iaas-classic-api-key',
48-
'secured' => 'false',
49-
},
50-
]
51-
expect(cred.connection_parameters).to(eq(expected))
52-
end
5334
end
54-
55-
# describe "#env_vars" do
56-
# it "returns an empty hash" do
57-
# expect(cred.env_vars).to(eq({}))
58-
# end
59-
# end
6035
end
6136
end

spec/models/manageiq/providers/embedded_terraform/automation_manager/credential_spec.rb

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -539,47 +539,88 @@
539539

540540
let(:params) do
541541
{
542-
:name => "IBM Cloud Credential",
543-
:auth_key => "secret1",
544-
:classic_user => "user1",
545-
:classic_key => "secret2",
542+
:name => "IBM Cloud Credential",
543+
:auth_key => "secret1",
546544
}
547545
end
548546
let(:queue_create_params) do
549547
{
550-
:name => "IBM Cloud Credential",
551-
:auth_key => ManageIQ::Password.encrypt("secret1"),
552-
:classic_user => "user1",
553-
:classic_key => ManageIQ::Password.encrypt("secret2"),
548+
:name => "IBM Cloud Credential",
549+
:auth_key => ManageIQ::Password.encrypt("secret1"),
554550
}
555551
end
556552
let(:params_to_attributes) do
557553
{
558554
:name => "IBM Cloud Credential",
559555
:auth_key => "secret1",
556+
}
557+
end
558+
let(:expected_values) do
559+
{
560+
:name => "IBM Cloud Credential",
561+
:auth_key_encrypted => ManageIQ::Password.encrypt("secret1"),
562+
:auth_key => "secret1"
563+
}
564+
end
565+
let(:params_to_attrs) { [] }
566+
let(:update_params) do
567+
{
568+
:name => "Updated Credential",
569+
:auth_key => "supersecret"
570+
}
571+
end
572+
let(:update_queue_params) do
573+
{
574+
:name => "Updated Credential",
575+
:auth_key => ManageIQ::Password.encrypt("supersecret")
576+
}
577+
end
578+
end
579+
end
580+
581+
context "IbmClassicInfrastructureCredential" do
582+
it_behaves_like 'an embedded_terraform credential' do
583+
let(:credential_class) { embedded_terraform::IbmClassicInfrastructureCredential }
584+
585+
let(:params) do
586+
{
587+
:name => "IBM Cloud Classic Infrastructure Credential",
588+
:userid => "user1",
589+
:password => "secret2",
590+
}
591+
end
592+
let(:queue_create_params) do
593+
{
594+
:name => "IBM Cloud Classic Infrastructure Credential",
595+
:userid => "user1",
596+
:password => ManageIQ::Password.encrypt("secret2"),
597+
}
598+
end
599+
let(:params_to_attributes) do
600+
{
601+
:name => "IBM Cloud Classic Infrastructure Credential",
560602
:userid => "user1",
561603
:password => "secret2",
562604
}
563605
end
564606
let(:expected_values) do
565607
{
566-
:name => "IBM Cloud Credential",
567-
:auth_key_encrypted => ManageIQ::Password.try_encrypt("secret1"),
608+
:name => "IBM Cloud Classic Infrastructure Credential",
568609
:userid => "user1",
569-
:password_encrypted => ManageIQ::Password.try_encrypt("secret2"),
610+
:password_encrypted => ManageIQ::Password.encrypt("secret2"),
570611
}
571612
end
572-
let(:params_to_attrs) { [:auth_key, :userid] }
613+
let(:params_to_attrs) { [] }
573614
let(:update_params) do
574615
{
575-
:name => "Updated Credential",
576-
:classic_key => "supersecret"
616+
:name => "Updated Credential",
617+
:password => "supersecret"
577618
}
578619
end
579620
let(:update_queue_params) do
580621
{
581-
:name => "Updated Credential",
582-
:classic_key => ManageIQ::Password.encrypt("supersecret")
622+
:name => "Updated Credential",
623+
:password => ManageIQ::Password.encrypt("supersecret")
583624
}
584625
end
585626
end

0 commit comments

Comments
 (0)