Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the resource instances #877

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import json
from urllib.parse import urlparse, parse_qs

from ibm_platform_services import ResourceControllerV2

from cloud_governance.common.clouds.ibm.account.ibm_authenticator import IBMAuthenticator


class PlatformServiceOperations(IBMAuthenticator):

def __init__(self):
super().__init__()
self.__client = ResourceControllerV2(authenticator=self.iam_authenticator)

def get_resource_instances(self):
"""
This method returns all the service instances
:return:
"""
responses = self.__client.list_resource_instances().get_result()
resources = responses['resources']
while responses.get('next_url'):
parsed_url = urlparse(responses['next_url'])
params = parse_qs(parsed_url.query)
if params and 'start' in params:
start = params['start'][0]
responses = self.__client.list_resource_instances(start=start).get_result()
resources.extend(responses['resources'])
resource_instances = []
for resource in resources:
if resource['type'] == 'resource_instance':
resource_instances.append(resource)
return {'global': resource_instances}
162 changes: 38 additions & 124 deletions cloud_governance/policy/ibm/tag_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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.platform_services.platform_service_operations import PlatformServiceOperations
from cloud_governance.common.clouds.ibm.tagging.global_tagging_operations import GlobalTaggingOperations
from cloud_governance.common.clouds.ibm.vpc.vpc_infra_operations import VpcInfraOperations
from cloud_governance.common.logger.init_logger import logger
Expand Down Expand Up @@ -33,6 +34,7 @@ def __init__(self):
self.tag_operations = GlobalTaggingOperations()
self.schematic_operations = SchematicOperations()
self._classic_operations = ClassicOperations()
self.platform_operations = PlatformServiceOperations()
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') \
Expand All @@ -50,118 +52,28 @@ def get_virtual_servers_crn(self):
"""
return self.vpc_infra_operations.get_all_instances()

@get_resources_wrapper
@logger_time_stamp
def get_images_crn(self):
"""
This method returns all virtual server crn's
:return:
"""
return self.vpc_infra_operations.get_all_images()

@get_resources_wrapper
@logger_time_stamp
def get_placement_groups_crn(self):
"""
This method returns all placement group crn's
:return:
"""
return self.vpc_infra_operations.get_all_placement_groups()

@get_resources_wrapper
@logger_time_stamp
def get_volumes_crn(self):
"""
This method returns all volumes crn's
:return:
"""
return self.vpc_infra_operations.get_all_volumes()

@get_resources_wrapper
@logger_time_stamp
def get_floating_ips_crn(self):
"""
This method returns all floating ips crn's'
:return:
"""
return self.vpc_infra_operations.get_all_floating_ips()

@get_resources_wrapper
@logger_time_stamp
def get_vpcs_crn(self):
"""
This method returns all vpcs crn's'
:return:
"""
return self.vpc_infra_operations.get_all_vpcs()

@get_resources_wrapper
@logger_time_stamp
def get_virtual_network_interfaces_crn(self):
"""
This method returns all virtual network interfaces crn's'
:return:
"""
return self.vpc_infra_operations.get_all_virtual_network_interfaces()

@get_resources_wrapper
@logger_time_stamp
def get_security_groups_crn(self):
"""
This method returns all virtual security_groups crn's'
:return:
"""
return self.vpc_infra_operations.get_all_security_groups()

@get_resources_wrapper
@logger_time_stamp
def get_public_gateways_crn(self):
"""
This method returns all virtual public_gateways crn's'
:return:
"""
return self.vpc_infra_operations.get_all_public_gateways()

@get_resources_wrapper
@logger_time_stamp
def get_vpc_endpoint_gateways_crn(self):
"""
This method returns all vpc endpoint gateways crn's'
:return:
"""
return self.vpc_infra_operations.get_all_vpc_endpoint_gateways()

@get_resources_wrapper
@logger_time_stamp
def get_schematics_workspaces_crn(self):
"""
This method returns all schematics workspaces crn's'
This method returns all schematics workspaces crn's
:return:
"""
return self.schematic_operations.get_all_workspaces()

@get_resources_wrapper
@logger_time_stamp
def get_load_balancers_crn(self):
"""
This method returns all load balancers crn's'
:return:
"""
return self.vpc_infra_operations.get_all_load_balancers()

@get_resources_wrapper
@logger_time_stamp
def get_baremetal_servers_crn(self):
def get_resource_instances_crn(self):
"""
This method returns all baremetals crn's
This method returns all resource instances crn's
:return:
"""
return self.vpc_infra_operations.get_all_baremetal_servers()
return self.platform_operations.get_resource_instances()

@logger_time_stamp
def get_classic_baremetals_crn(self):
"""
This method returns all classic baremetals crn's'
This method returns all classic baremetals crn's
:return:
"""
hardware_ids = self._classic_operations.get_hardware_ids()
Expand All @@ -170,14 +82,14 @@ def get_classic_baremetals_crn(self):
@logger_time_stamp
def get_classic_virtual_machines_crn(self):
"""
This method returns all classic baremetals crn's'
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):
def tag_all_resources(self):
"""
This method tags all Virtual PrivateCloud Resources
:return:
Expand All @@ -188,54 +100,56 @@ def tag_all_vpc_resources(self):
'str format. i.e "key:value, env:test"'}
tags_list = self.__ibm_custom_tags_list.split(',')
vpc_resources = [
"resource_instances",
"virtual_servers",
"placement_groups",
"volumes",
"floating_ips",
"vpcs",
"virtual_network_interfaces",
"security_groups",
"public_gateways",
"vpc_endpoint_gateways",
"load_balancers",
"schematics_workspaces",
"baremetal_servers",
"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]
logger.info(f"Running tag operation on total of {len(vpc_resources)} resources")
errors = []
messages = {}
errors = messages = {}
for vpc_resource in vpc_resources:
message = 'tagged are added to all resources'
resources_crn = getattr(self, f'get_{vpc_resource}_crn')()
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'))
ok, errors_list = self._classic_operations. \
update_baremetal_tags(tags=tags_list, hardware_id=resource.get('id'))
self.process_ok_and_errors(ok, errors_list, messages, vpc_resource, errors)
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'))
ok, errors_list = self._classic_operations. \
update_virtual_machine_tags(tags=tags_list, virtual_machine_id=resource.get('id'))
self.process_ok_and_errors(ok, errors_list, messages, vpc_resource, errors)
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}')
errors.update({vpc_resource: message, 'crns': errors})
else:
messages.update({vpc_resource: message})
ok, errors_list = self.tag_operations.update_tags(resources_crn, tags=tags_list)
self.process_ok_and_errors(ok, errors_list, messages, vpc_resource, errors)

return {'errors': errors, 'messages': messages}

def process_ok_and_errors(self, ok: bool, errors_list: any, messages: dict, resource: str, errors: dict):
"""
This method processes ok and errors
:param errors:
:param ok:
:param errors_list:
:param messages:
:param resource:
:return:
"""
success_message = 'tagged are added to all resources'
if not ok:
message = 'Unable to tag all resources'
logger.info(f'{message}, please find the servers that are not tagged: {errors_list}')
errors.update({resource: message, 'crns': errors_list})
else:
messages.update({resource: success_message})

def run(self):
"""
This method runs the tag operations
:return:
"""
return self.tag_all_vpc_resources()
return self.tag_all_resources()
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ def test_tag_all_vpc_resources():
environment_variables.IBM_CUSTOM_TAGS_LIST = "cost-center: test"
environment_variables.IBM_ACCOUNT_ID = "test"
tag_resources = TagResources()
res = tag_resources.tag_all_vpc_resources()
res = tag_resources.tag_all_resources()
assert res.get('messages').get('virtual_servers')