Skip to content

Commit 02b0a80

Browse files
committed
refactored clp_envvar handling
1 parent 1c1f400 commit 02b0a80

2 files changed

Lines changed: 87 additions & 43 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import os
2+
from abc import ABCMeta
3+
4+
from cloudshell.api.cloudshell_api import ResourceAttribute
5+
6+
from cloudshell.iac.terraform.models.shell_helper import ShellHelperObject
7+
8+
9+
class BaseCloudProviderEnvVarHandler(metaclass=ABCMeta):
10+
def __init__(self):
11+
pass
12+
13+
def set_env_vars_based_on_clp(self):
14+
raise NotImplemented()
15+
16+
@staticmethod
17+
def get_attribute_value(clp_res_model, clp_attribute, attr_name_to_check, shell_helper, decrypt=False) -> str:
18+
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 ""
24+
25+
26+
class AWSCloudProviderEnvVarHandler(BaseCloudProviderEnvVarHandler):
27+
def __init__(self,
28+
clp_res_model: str, clp_resource_attributes: list[ResourceAttribute], shell_helper: ShellHelperObject):
29+
BaseCloudProviderEnvVarHandler.__init__(self)
30+
self._clp_res_model = clp_res_model
31+
self._clp_resource_attributes = clp_resource_attributes
32+
self._shell_helper = shell_helper
33+
34+
def set_aws_env_vars_based_on_clp(self):
35+
dec_access_key = ""
36+
dec_secret_key = ""
37+
region_flag = False
38+
39+
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"):
45+
os.environ["AWS_DEFAULT_REGION"] = attr.Value
46+
region_flag = True
47+
if not region_flag:
48+
raise ValueError("Region was not found on AWS Cloud Provider")
49+
50+
# We must check both keys exist...if not then the EC2 Execution Server profile would be used (Role)
51+
if dec_access_key and dec_secret_key:
52+
os.environ["AWS_ACCESS_KEY_ID"] = dec_access_key
53+
os.environ["AWS_SECRET_ACCESS_KEY"] = dec_secret_key
54+
55+
56+
class AzureCloudProviderEnvVarHandler(BaseCloudProviderEnvVarHandler):
57+
def __init__(self, clp_res_model, clp_resource_attributes, shell_helper):
58+
BaseCloudProviderEnvVarHandler.__init__(self)
59+
self._clp_res_model = clp_res_model
60+
self._clp_resource_attributes = clp_resource_attributes
61+
self._shell_helper = shell_helper
62+
63+
def _set_azure_env_vars_based_on_clp(self):
64+
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")
66+
if attr_val:
67+
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")
69+
if attr_val:
70+
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")
72+
if attr_val:
73+
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)
75+
if attr_val:
76+
os.environ["ARM_CLIENT_SECRET"] = attr_val

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

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33

44
from cloudshell.api.cloudshell_api import ResourceInfo
55

6-
from cloudshell.iac.terraform.constants import AZURE2G_MODEL, ATTRIBUTE_NAMES, AWS2G_MODEL, CLP_PROVIDER_MODELS
6+
from cloudshell.iac.terraform.constants import AZURE2G_MODEL, ATTRIBUTE_NAMES, AWS2G_MODEL, CLP_PROVIDER_MODELS, \
7+
AWS1G_MODEL, AZURE1G_MODEL
78
from cloudshell.iac.terraform.models.shell_helper import ShellHelperObject
9+
from cloudshell.iac.terraform.services.clp_envvar_handler import BaseCloudProviderEnvVarHandler, \
10+
AWSCloudProviderEnvVarHandler, AzureCloudProviderEnvVarHandler
811

912

1013
class ProviderHandler(object):
@@ -48,49 +51,14 @@ def _set_cloud_env_vars(
4851
shell_helper.sandbox_messages.write_message("initializing provider...")
4952
shell_helper.logger.info("Initializing Environment variables with CloudProvider details")
5053
clp_resource_attributes = clp_details.ResourceAttributes
54+
clp_handler = None
5155

52-
cloud_attr_name_prefix = ""
53-
if clp_res_model in [AZURE2G_MODEL, AWS2G_MODEL]:
54-
cloud_attr_name_prefix = clp_res_model + "."
56+
if clp_res_model in [AWS1G_MODEL, AWS2G_MODEL]:
57+
clp_handler = AWSCloudProviderEnvVarHandler(clp_res_model, clp_resource_attributes, shell_helper)
5558

56-
if clp_res_model in ['AWS EC2', AWS2G_MODEL]:
57-
ProviderHandler._set_aws_env_vars_based_on_clp(
58-
cloud_attr_name_prefix, clp_resource_attributes, shell_helper)
59-
elif clp_res_model in ['Microsoft Azure', AZURE2G_MODEL]:
60-
ProviderHandler._set_azure_env_vars_based_on_clp(
61-
cloud_attr_name_prefix, clp_resource_attributes, shell_helper)
59+
elif clp_res_model in [AZURE1G_MODEL, AZURE2G_MODEL]:
60+
clp_handler = AzureCloudProviderEnvVarHandler(clp_res_model, clp_resource_attributes, shell_helper)
6261

63-
@staticmethod
64-
def _set_azure_env_vars_based_on_clp(azure_attr_name_prefix, clp_resource_attributes, shell_helper):
65-
for attr in clp_resource_attributes:
66-
if attr.Name == azure_attr_name_prefix + "Azure Subscription ID":
67-
os.environ["ARM_SUBSCRIPTION_ID"] = attr.Value
68-
if attr.Name == azure_attr_name_prefix + "Azure Tenant ID":
69-
os.environ["ARM_TENANT_ID"] = attr.Value
70-
if attr.Name == azure_attr_name_prefix + "Azure Application ID":
71-
os.environ["ARM_CLIENT_ID"] = attr.Value
72-
if attr.Name == azure_attr_name_prefix + "Azure Application Key":
73-
dec_client_secret = shell_helper.api.DecryptPassword(attr.Value).Value
74-
os.environ["ARM_CLIENT_SECRET"] = dec_client_secret
75-
76-
@staticmethod
77-
def _set_aws_env_vars_based_on_clp(aws_attr_name_prefix, clp_resource_attributes, shell_helper):
78-
dec_access_key = ""
79-
dec_secret_key = ""
80-
region_flag = False
81-
82-
for attr in clp_resource_attributes:
83-
if attr.Name == aws_attr_name_prefix + "AWS Access Key ID":
84-
dec_access_key = shell_helper.api.DecryptPassword(attr.Value).Value
85-
if attr.Name == aws_attr_name_prefix + "AWS Secret Access Key":
86-
dec_secret_key = shell_helper.api.DecryptPassword(attr.Value).Value
87-
if attr.Name == aws_attr_name_prefix + "Region":
88-
os.environ["AWS_DEFAULT_REGION"] = attr.Value
89-
region_flag = True
90-
if not region_flag:
91-
raise ValueError("Region was not found on AWS Cloud Provider")
62+
if clp_handler:
63+
clp_handler.set_env_vars_based_on_clp()
9264

93-
# We must check both keys exist...if not then the EC2 Execution Server profile would be used (Role)
94-
if dec_access_key and dec_secret_key:
95-
os.environ["AWS_ACCESS_KEY_ID"] = dec_access_key
96-
os.environ["AWS_SECRET_ACCESS_KEY"] = dec_secret_key

0 commit comments

Comments
 (0)