diff --git a/cloud_governance/common/clouds/ibm/classic/classic_operations.py b/cloud_governance/common/clouds/ibm/classic/classic_operations.py index 43150727..c9f06253 100644 --- a/cloud_governance/common/clouds/ibm/classic/classic_operations.py +++ b/cloud_governance/common/clouds/ibm/classic/classic_operations.py @@ -2,6 +2,7 @@ from typeguard import typechecked from cloud_governance.common.clouds.ibm.account.ibm_account import IBMAccount +from cloud_governance.common.jira.jira import logger from cloud_governance.common.logger.logger_time_stamp import logger_time_stamp @@ -97,3 +98,55 @@ def get_virtual_machine_tags(self, vm_id: str): """ tags = self._sl_client.call('SoftLayer_Virtual_Guest', 'getTagReferences', id=vm_id) return self.__filter_tag_names(tag_references=tags) + + def update_baremetal_tags(self, tags: list, hardware_id: str): + """ + This method updates the hardware tags + :param tags: + :param hardware_id: + :return: + """ + tag_names = [] + for tag in tags: + key, value = tag.split(':') + tag_names.append(f"{key.strip()}:{value.strip()}") + error = '' + try: + response = self._sl_client.call('SoftLayer_Hardware_Server', + 'setTags', ','.join(tag_names), id=hardware_id) + if response: + logger.info(f'Tags are added to the hardware: {hardware_id} : {tag_names}') + else: + logger.error( + f'Tags are not added to the hardware: {hardware_id} - , something might fail') + except Exception as err: + error = str(err) + response = False + logger.error(f'{err}') + return response, error + + def update_virtual_machine_tags(self, tags: list, virtual_machine_id: str): + """ + This method updates the virtual machine tags + :param tags: + :param virtual_machine_id: + :return: + """ + tag_names = [] + for tag in tags: + key, value = tag.split(':') + tag_names.append(f"{key.strip()}:{value.strip()}") + error = '' + try: + response = self._sl_client.call('SoftLayer_Virtual_Guest', + 'setTags', ','.join(tag_names), id=virtual_machine_id) + if response: + logger.info(f'Tags are added to the VirtualMachine: {virtual_machine_id} : {tags}') + else: + logger.error( + f'Tags are not added to the VirtualMachine: {virtual_machine_id} - , something might fail') + except Exception as err: + error = str(err) + response = False + logger.error(f'{err}') + return response, error diff --git a/cloud_governance/main/environment_variables.py b/cloud_governance/main/environment_variables.py index 578115c3..d13ef906 100644 --- a/cloud_governance/main/environment_variables.py +++ b/cloud_governance/main/environment_variables.py @@ -148,6 +148,8 @@ def __init__(self): self._environment_variables_dict['IBM_API_KEY'] = EnvironmentVariables.get_env('IBM_API_KEY', '') self._environment_variables_dict['USAGE_REPORTS_APIKEY'] = EnvironmentVariables.get_env('USAGE_REPORTS_APIKEY', '') + self._environment_variables_dict['IBM_CUSTOM_TAGS_LIST'] = EnvironmentVariables.get_env('IBM_CUSTOM_TAGS_LIST', + '') self._environment_variables_dict['IBM_CLOUD_API_KEY'] = EnvironmentVariables.get_env('IBM_CLOUD_API_KEY', '') if (self._environment_variables_dict['USAGE_REPORTS_APIKEY'] or diff --git a/cloud_governance/policy/ibm/tag_resources.py b/cloud_governance/policy/ibm/tag_resources.py index f978588c..a68ad158 100644 --- a/cloud_governance/policy/ibm/tag_resources.py +++ b/cloud_governance/policy/ibm/tag_resources.py @@ -1,5 +1,6 @@ from functools import wraps +from cloud_governance.common.clouds.ibm.classic.classic_operations import ClassicOperations from cloud_governance.common.clouds.ibm.developer_tools.schematic_operations import SchematicOperations from cloud_governance.common.clouds.ibm.tagging.global_tagging_operations import GlobalTaggingOperations from cloud_governance.common.clouds.ibm.vpc.vpc_infra_operations import VpcInfraOperations @@ -31,11 +32,14 @@ def __init__(self): self.vpc_infra_operations = VpcInfraOperations() self.tag_operations = GlobalTaggingOperations() self.schematic_operations = SchematicOperations() + self._classic_operations = ClassicOperations() self.__env_config = self.vpc_infra_operations.env_config self.__ibm_custom_tags_list = self.__env_config.IBM_CUSTOM_TAGS_LIST \ - if hasattr(self.__env_config, 'IBM_CUSTOM_TAGS_LIST') else None + if hasattr(self.__env_config, 'IBM_CUSTOM_TAGS_LIST') \ + else self.__env_config.environment_variables_dict.get('IBM_CUSTOM_TAGS_LIST') self.resource_to_tag = self.__env_config.RESOURCE_TO_TAG \ - if hasattr(self.__env_config, 'RESOURCE_TO_TAG') else None + if hasattr(self.__env_config, 'RESOURCE_TO_TAG') \ + else self.__env_config.environment_variables_dict.get('RESOURCE_TO_TAG') @get_resources_wrapper @logger_time_stamp @@ -154,6 +158,24 @@ def get_baremetal_servers_crn(self): """ return self.vpc_infra_operations.get_all_baremetal_servers() + @logger_time_stamp + def get_classic_baremetals_crn(self): + """ + This method returns all classic baremetals crn's' + :return: + """ + hardware_ids = self._classic_operations.get_hardware_ids() + return hardware_ids + + @logger_time_stamp + def get_classic_virtual_machines_crn(self): + """ + This method returns all classic baremetals crn's' + :return: + """ + virtual_machine_ids = self._classic_operations.get_virtual_machine_ids() + return virtual_machine_ids + @logger_time_stamp def tag_all_vpc_resources(self): """ @@ -178,7 +200,9 @@ def tag_all_vpc_resources(self): "load_balancers", "schematics_workspaces", "baremetal_servers", - "images" + "images", + "classic_baremetals", + "classic_virtual_machines" ] if self.resource_to_tag and self.resource_to_tag in vpc_resources: vpc_resources = [self.resource_to_tag] @@ -188,8 +212,18 @@ def tag_all_vpc_resources(self): for vpc_resource in vpc_resources: message = 'tagged are added to all resources' resources_crn = getattr(self, f'get_{vpc_resource}_crn')() - logger.info(f"Started the tagging operation for {vpc_resource}") - ok, errors = self.tag_operations.update_tags(resources_crn, tags=tags_list) + if vpc_resource == 'classic_baremetals': + for resource in resources_crn: + ok, errors = self._classic_operations.update_baremetal_tags(tags=tags_list, + hardware_id=resource.get('id')) + elif vpc_resource == 'classic_virtual_machines': + for resource in resources_crn: + ok, errors = self._classic_operations.update_virtual_machine_tags(tags=tags_list, + virtual_machine_id= + resource.get('id')) + else: + logger.info(f"Started the tagging operation for {vpc_resource}") + ok, errors = self.tag_operations.update_tags(resources_crn, tags=tags_list) if not ok: message = 'Unable to tag all resources' logger.info(f'{message}, please find the servers that are not tagged: {errors}') diff --git a/cloud_governance/policy/policy_operations/ibm/tagging/tagging_operations.py b/cloud_governance/policy/policy_operations/ibm/tagging/tagging_operations.py index 520b63a7..cf3f5158 100644 --- a/cloud_governance/policy/policy_operations/ibm/tagging/tagging_operations.py +++ b/cloud_governance/policy/policy_operations/ibm/tagging/tagging_operations.py @@ -33,7 +33,15 @@ def _filter_common_tags(self, user_tags: list, resource_tags: list): tags = [] for tag in user_tags: if tag not in resource_tags: - tags.append(tag) + if ',' in tag: + multiple_tags = [] + for multiple_tag in tag.split(','): + key, value = multiple_tag.split(':') + multiple_tags.append(f"{key.strip()}:{value.strip()}") + tags.extend(multiple_tags) + else: + key, value = tag.split(':') + tags.append(f"{key.strip()}:{value.strip()}") return tags def _filter_remove_tags(self, user_tags: list, resource_tags: list): @@ -58,4 +66,4 @@ def softlayer_operation(self, softlayer_name: str, softlayer_method: str, resour @param tags: @return: """ - return self._sl_client.call(softlayer_name,softlayer_method, tags, id=resource_id) + return self._sl_client.call(softlayer_name, softlayer_method, tags, id=resource_id) diff --git a/jenkins/clouds/ibm/hourly/tagging/tagging.py b/jenkins/clouds/ibm/hourly/tagging/tagging.py index b4f4a7e0..09959f9a 100644 --- a/jenkins/clouds/ibm/hourly/tagging/tagging.py +++ b/jenkins/clouds/ibm/hourly/tagging/tagging.py @@ -55,12 +55,12 @@ def get_podman_run_cmd(volume_mounts: list = None, **kwargs): 'log_level': "INFO", 'policy': 'tag_baremetal', "PUBLIC_CLOUD_NAME": "IBM", "tag_custom": tag_custom} baremetal_cmd = get_podman_run_cmd(volume_mounts=volume_mounts_targets, **input_env_keys) -run_cmd(baremetal_cmd) +# run_cmd(baremetal_cmd) run_cmd("echo Run IBM tag Virtual Machines") input_env_keys['policy'] = 'tag_vm' virtual_machine_cmd = get_podman_run_cmd(volume_mounts=volume_mounts_targets, **input_env_keys) -run_cmd(virtual_machine_cmd) +# run_cmd(virtual_machine_cmd) # Run tag resources run_cmd("echo Run tag resources command") @@ -68,5 +68,7 @@ def get_podman_run_cmd(volume_mounts: list = None, **kwargs): IBM_CLOUD_API_KEY=IBM_CLOUD_API_KEY, IBM_CUSTOM_TAGS_LIST=IBM_CUSTOM_TAGS_LIST, PUBLIC_CLOUD_NAME="IBM", - IBM_ACCOUNT_ID=IBM_ACCOUNT_ID_PERFORMANCE_SCALE) + IBM_ACCOUNT_ID=IBM_ACCOUNT_ID_PERFORMANCE_SCALE, + IBM_API_USERNAME=IBM_API_USERNAME, + IBM_API_KEY=IBM_API_KEY, ) run_cmd(podman_run_cmd)