Skip to content

Commit

Permalink
Added the tags to classic bm/vm (#874)
Browse files Browse the repository at this point in the history
  • Loading branch information
athiruma authored Nov 19, 2024
1 parent a4d9442 commit 1ca0068
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 10 deletions.
1 change: 1 addition & 0 deletions cloud_governance/common/clouds/ibm/account/ibm_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def __init__(self):
self.__environment_variables_dict = environment_variables.environment_variables_dict
self.__API_USERNAME = self.__environment_variables_dict.get('IBM_API_USERNAME', '')
self.__API_KEY = self.__environment_variables_dict.get('IBM_API_KEY', '')
self.__sl_client = None
try:
if self.__API_KEY and self.__API_USERNAME:
self.__sl_client = SoftLayer.create_client_from_env(username=self.__API_USERNAME,
Expand Down
53 changes: 53 additions & 0 deletions cloud_governance/common/clouds/ibm/classic/classic_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions cloud_governance/main/environment_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 39 additions & 5 deletions cloud_governance/policy/ibm/tag_resources.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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]
Expand All @@ -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}')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
8 changes: 5 additions & 3 deletions jenkins/clouds/ibm/hourly/tagging/tagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,20 @@ 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")
podman_run_cmd = get_podman_run_cmd(policy="tag_resources", account=account,
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)

0 comments on commit 1ca0068

Please sign in to comment.