def delete__forwardingRules(self, resource_id): self_link_values = parse_link( self_link=resource_id, extra_expected_values=['forwardingRules']) delete_res = self.gcloud_lib.delete_forwarding_rule( region=self_link_values['regions'], forwarding_rule_name=self_link_values['forwardingRules']) return delete_res
def delete__instances(self, resource_id): self_link_values = parse_link(self_link=resource_id, extra_expected_values=['instances']) delete_res = self.gcloud_lib.delete_instance( zone=self_link_values['zones'], instance_name=self_link_values['instances']) return delete_res
def dependency_resolver__storage_v1_b(self, resource_id): # check if there are any objects inside and add them all as dependencies all_objects_in_bucket = self.gcloud_lib.list_all_objects( bucket_name=parse_link(self_link=resource_id)['resource_name']) return [resource_id] + [ obj['selfLink'] for obj in all_objects_in_bucket.get('items', []) ]
def dependency_resolver__compute_v1_instances(self, resource_id): # The stack to return at end dependency_stack = [resource_id] instance_self_link = resource_id self_link_values = parse_link(self_link=instance_self_link, extra_expected_values=['instances']) # TODO: Handle case when self_link parsing fails. Raise Customer exception zone = self_link_values.get('zones') instance_name = self_link_values.get('instances') # Get list of resources referring to this instance (dependencies) try: instance_referrers = self.gcloud_lib.list_referrers_of_instance( zone=zone, instance=instance_name) except googleapiclient.errors.HttpError as ex: if int(ex.resp['status']) == 404: self.logger.debug( 'Assuming 404 to be resource already deleted') return [] else: raise ex # If there are no dependencies, the instance is idependent if 'items' not in instance_referrers: return dependency_stack # If there are dependencies: for referrer_details in instance_referrers['items']: # TODO: Handle if resource type could not be guessed # 1. Get the dependency type referrer_resource_type = get_resource_type( self_link=referrer_details['referrer']) # 2. Dynamic Determine Call the function that handles this type of resource function_to_resolve = getattr( self, f'dependency_resolver__{referrer_resource_type}', None) if not function_to_resolve: print( f'Dont know how to resolve referrer of type {referrer_resource_type}' ) continue referrer_stack = function_to_resolve(referrer_details['referrer']) # 3. If the function returns its own stack, push to existing stack if referrer_stack: dependency_stack.extend(referrer_stack) return dependency_stack
def delete__targetHttpProxies(self, resource_id): self_link_values = parse_link( self_link=resource_id, extra_expected_values=['targetHttpProxies']) if 'regions' in self_link_values: delete_res = self.gcloud_lib.delete_regional_http_proxy( region=self_link_values['regions'], http_proxy_name=self_link_values['targetHttpProxies']) elif 'global' in self_link_values: delete_res = self.gcloud_lib.delete_global_http_proxy( http_proxy_name=self_link_values['targetHttpProxies']) else: self.logger.warning( f'Could not figure out how to delete the HTTP proxy {resource_id}' ) delete_res = False return delete_res
def delete__urlMaps(self, resource_id): self_link_values = parse_link(self_link=resource_id, extra_expected_values=['urlMaps']) if 'regions' in self_link_values: delete_res = self.gcloud_lib.delete_regional_url_map( region=self_link_values['regions'], url_map_name=self_link_values['urlMaps']) elif 'global' in self_link_values: delete_res = self.gcloud_lib.delete_global_url_map( url_map_name=self_link_values['urlMaps']) else: self.logger.warning( f'Could not figure out how to delete the URL Map {resource_id}' ) delete_res = False return delete_res
def delete__backendServices(self, resource_id): self_link_values = parse_link( self_link=resource_id, extra_expected_values=['backendServices']) if 'regions' in self_link_values: delete_res = self.gcloud_lib.delete_regional_backend_service( region=self_link_values['regions'], backend_service_name=self_link_values['backendServices']) elif 'global' in self_link_values: delete_res = self.gcloud_lib.delete_global_backend_service( backend_service_name=self_link_values['backendServices']) else: self.logger.warning( 'Could not figure out how to delete backendService : {}'. format(resource_id)) delete_res = False return delete_res
def dependency_resolver__compute_v1_urlMaps(self, resource_id): to_return_stack = [resource_id] # 1. Checking http Proxies where this url map is listed self_link_values = parse_link(self_link=resource_id, extra_expected_values=['urlMaps']) if 'global' in self_link_values: all_target_http_proxies_maps = self.gcloud_lib.get_all_global_http_proxies( ) elif 'regions' in self_link_values: all_target_http_proxies_maps = self.gcloud_lib.get_all_regional_http_proxies( region=self_link_values['regions']) else: self.logger.warning( 'Could not resolve dependency for the URL MAP {}'.format( resource_id)) return to_return_stack for http_proxy in all_target_http_proxies_maps.get('items', []): if http_proxy.get('urlMap') == resource_id: to_return_stack.append(http_proxy['selfLink']) return to_return_stack
def dependency_resolver__compute_v1_backendServices(self, resource_id): """ Check for resources that refer this backendService and return them :param resource_id: Expected to be selfLink of BackendService :return: List """ to_return_stack = [resource_id] # 2. Checking all URL Maps where this is referred # TODO: Based on the load balancing scheme of the BackendService determine # if it can be referred global URL MAP or a local url map. # LoadBalancingScheme => EXTERNAL & INTERNAL_SELF_MANAGED -> Global || INTERNAL_MANAGED -> Regional self_link_values = parse_link(self_link=resource_id) all_url_maps = self.gcloud_lib.get_all_regional_url_maps( region=self_link_values['regions']) for url_map in all_url_maps.get('items', []): if url_map.get('defaultService', None) == resource_id: referrer_resource_id = url_map['selfLink'] referrer_resource_type = get_resource_type( self_link=referrer_resource_id) # Check if we have a function that can further resolve dependencies function_to_resolve = getattr( self, f'dependency_resolver__{referrer_resource_type}', None) if not function_to_resolve: print( f'Dont know how to resolve referrer of type {referrer_resource_type}' ) # At least add to stack what we have discovered to_return_stack.append(referrer_resource_id) continue # Call the corresponding function to resolve further dependecies referrer_stack = function_to_resolve(referrer_resource_id) if referrer_stack: to_return_stack.extend(referrer_stack) # 1. Checking Forwarding Rules where this backend service is listed all_forwarding_rules = self.gcloud_lib.get_all_forwarding_rules() if 'items' not in all_forwarding_rules: return to_return_stack for region, region_forwarding_rules_info in all_forwarding_rules[ 'items'].items(): # There are no forwarding rules in this region if 'forwardingRules' not in region_forwarding_rules_info: continue for forwarding_rule in region_forwarding_rules_info[ 'forwardingRules']: if 'backendService' in forwarding_rule and forwarding_rule[ 'backendService'] == resource_id: to_return_stack.append(forwarding_rule['selfLink']) return to_return_stack