Skip to content

Commit 946fda3

Browse files
committed
bug fixes
1 parent eb8cc40 commit 946fda3

3 files changed

Lines changed: 19 additions & 28 deletions

File tree

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import os
22
from abc import ABCMeta
33

4-
# from cloudshell.api.cloudshell_api import ResourceAttribute
5-
64
from cloudshell.iac.terraform.models.shell_helper import ShellHelperObject
75

86

@@ -14,13 +12,10 @@ def set_env_vars_based_on_clp(self):
1412
raise NotImplementedError()
1513

1614
@staticmethod
17-
def get_attribute_value(clp_res_model, clp_attribute, attr_name_to_check, shell_helper, decrypt=False) -> str:
15+
def does_attribute_match(clp_res_model, clp_attribute, attr_name_to_check) -> bool:
1816
if f"{clp_res_model}.{clp_attribute.Name}" == attr_name_to_check or clp_attribute.Name == attr_name_to_check:
19-
if decrypt:
20-
return shell_helper.api.DecryptPassword(clp_attribute.Value).Value
21-
else:
22-
return clp_attribute.Value
23-
return ""
17+
return True
18+
return False
2419

2520

2621
class AWSCloudProviderEnvVarHandler(BaseCloudProviderEnvVarHandler):
@@ -31,17 +26,17 @@ def __init__(self, clp_res_model: str, clp_resource_attributes: list,
3126
self._clp_resource_attributes = clp_resource_attributes
3227
self._shell_helper = shell_helper
3328

34-
def set_aws_env_vars_based_on_clp(self):
29+
def set_env_vars_based_on_clp(self):
3530
dec_access_key = ""
3631
dec_secret_key = ""
3732
region_flag = False
3833

3934
for attr in self._clp_resource_attributes:
40-
dec_access_key = self.get_attribute_value(
41-
self._clp_res_model, attr, "AWS Access Key ID", self._shell_helper, True)
42-
dec_secret_key = self.get_attribute_value(
43-
self._clp_res_model, attr, "AWS Secret Access Key", self._shell_helper, True)
44-
if self.get_attribute_value(self._clp_res_model, attr, self._shell_helper, "Region"):
35+
if self.does_attribute_match(self._clp_res_model, attr, "AWS Access Key ID"):
36+
dec_access_key = self._shell_helper.api.DecryptPassword(attr.Value).Value
37+
if self.does_attribute_match(self._clp_res_model, attr, "AWS Secret Access Key"):
38+
dec_secret_key = self._shell_helper.api.DecryptPassword(attr.Value).Value
39+
if self.does_attribute_match(self._clp_res_model, attr, "Region"):
4540
os.environ["AWS_DEFAULT_REGION"] = attr.Value
4641
region_flag = True
4742
if not region_flag:
@@ -60,17 +55,17 @@ def __init__(self, clp_res_model, clp_resource_attributes, shell_helper):
6055
self._clp_resource_attributes = clp_resource_attributes
6156
self._shell_helper = shell_helper
6257

63-
def _set_azure_env_vars_based_on_clp(self):
58+
def set_env_vars_based_on_clp(self):
6459
for attr in self._clp_resource_attributes:
65-
attr_val = self.get_attribute_value(self._clp_res_model, attr, self._shell_helper, "Azure Subscription ID")
60+
attr_val = self.does_attribute_match(self._clp_res_model, attr, self._shell_helper, "Azure Subscription ID")
6661
if attr_val:
6762
os.environ["ARM_SUBSCRIPTION_ID"] = attr_val
68-
attr_val = self.get_attribute_value(self._clp_res_model, attr, self._shell_helper, "Azure Tenant ID")
63+
attr_val = self.does_attribute_match(self._clp_res_model, attr, self._shell_helper, "Azure Tenant ID")
6964
if attr_val:
7065
os.environ["Azure Tenant ID"] = attr_val
71-
attr_val = self.get_attribute_value(self._clp_res_model, attr, self._shell_helper, "Azure Application ID")
66+
attr_val = self.does_attribute_match(self._clp_res_model, attr, self._shell_helper, "Azure Application ID")
7267
if attr_val:
7368
os.environ["ARM_CLIENT_ID"] = attr_val
74-
attr_val = self.get_attribute_value(self._clp_res_model, attr, self._shell_helper, "Azure Application Key", True)
69+
attr_val = self.does_attribute_match(self._clp_res_model, attr, self._shell_helper, "Azure Application Key", True)
7570
if attr_val:
7671
os.environ["ARM_CLIENT_SECRET"] = attr_val

package/cloudshell/iac/terraform/services/provider_handler.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ class ProviderHandler(object):
1313
def __init__(self, logger: Logger):
1414
self.logger = logger
1515

16-
@staticmethod
17-
def initialize_provider(shell_helper: ShellHelperObject):
16+
def initialize_provider(self, shell_helper: ShellHelperObject):
1817
clp_resource_name = shell_helper.attr_handler.get_attribute(ATTRIBUTE_NAMES.CLOUD_PROVIDER)
1918
if not clp_resource_name:
2019
return
@@ -28,11 +27,7 @@ def initialize_provider(shell_helper: ShellHelperObject):
2827

2928
try:
3029
if clp_res_model in CLP_PROVIDER_MODELS:
31-
ProviderHandler._set_cloud_env_vars(
32-
clp_details,
33-
clp_res_model,
34-
shell_helper
35-
)
30+
self._set_cloud_env_vars(clp_details, clp_res_model, shell_helper)
3631
else:
3732
shell_helper.logger.error(f"{clp_res_model} currently not supported")
3833
raise ValueError(f"{clp_res_model} currently not supported")

package/cloudshell/iac/terraform/terraform_shell.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def __init__(self, driver_context: ResourceCommandContext,
2020
self._tf_service = ObjectFactory.create_tf_service(driver_context)
2121
self._logger = logger
2222
self._config = config or TerraformShellConfig()
23+
self._provider_handler = ProviderHandler(self._logger)
2324

2425
def execute_terraform(self):
2526
# initialize a logger if logger wasn't passed during init
@@ -36,7 +37,7 @@ def _execute_procedure(self, sandbox_data_handler: SandboxDataHandler, shell_hel
3637
tf_proc_executer = ObjectFactory.create_tf_proc_executer(self._config, sandbox_data_handler,
3738
shell_helper, tf_working_dir)
3839
if tf_proc_executer.can_execute_run():
39-
ProviderHandler.initialize_provider(shell_helper)
40+
self._provider_handler.initialize_provider(shell_helper)
4041
tf_proc_executer.init_terraform()
4142
tf_proc_executer.tag_terraform()
4243
tf_proc_executer.plan_terraform()
@@ -67,7 +68,7 @@ def _destroy_procedure(self, sandbox_data_handler: SandboxDataHandler, shell_hel
6768
self._handle_error_output(shell_helper, "Destroy failed due to missing local directory")
6869

6970
try:
70-
ProviderHandler.initialize_provider(shell_helper)
71+
self._provider_handler.initialize_provider(shell_helper)
7172
tf_proc_executer = ObjectFactory.create_tf_proc_executer(self._config, sandbox_data_handler,
7273
shell_helper, tf_working_dir)
7374
if tf_proc_executer.can_destroy_run():

0 commit comments

Comments
 (0)