def ProjectNameToBinding(project_name, tag_value, location=None): """Returns the binding name given a project name and tag value. Requires binding list permission. Args: project_name: project name provided, fully qualified resource name tag_value: tag value to match the binding name to location: region or zone Returns: binding_name Raises: InvalidInputError: project not found """ service = ServiceFns['tagBindings']() with endpoints.CrmEndpointOverrides(location): req = ListResourceFns['tagBindings'](parent=project_name) response = service.List(req) for bn in response.tagBindings: if bn.tagValue == tag_value: return bn.name raise InvalidInputError( 'Binding not found for parent [{}], tagValue [{}]'.format( project_name, tag_value))
def Run(self, args): messages = tags.TagMessages() if args.tag_value.find("tagValues/") == 0: tag_value = args.tag_value else: tag_value = tag_utils.GetTagValueFromNamespacedName( args.tag_value).name binding_name = "/".join( ["tagBindings", quote(args.parent, safe=""), tag_value]) del_req = messages.CloudresourcemanagerTagBindingsDeleteRequest( name=binding_name) location = args.location if args.IsSpecified("location") else None with endpoints.CrmEndpointOverrides(location): service = tags.TagBindingsService() op = service.Delete(del_req) if args.async_ or op.done: return op else: return operations.WaitForOperation( op, "Waiting for TagBinding for resource [{}] and tag value [{}] to be " "deleted with [{}]".format(args.parent, args.tag_value, op.name), service=service)
def Run(self, args): messages = tags.TagMessages() if args.tag_value.find("tagValues/") == 0: tag_value = args.tag_value else: tag_value = tag_utils.GetTagValueFromNamespacedName( args.tag_value).name tag_binding = messages.TagBinding(parent=args.parent, tagValue=tag_value) create_req = messages.CloudresourcemanagerTagBindingsCreateRequest( tagBinding=tag_binding) location = args.location if args.IsSpecified("location") else None with endpoints.CrmEndpointOverrides(location): service = tags.TagBindingsService() op = service.Create(create_req) if args.async_ or op.done: return op else: return operations.WaitForOperation( op, "Waiting for TagBinding for parent [{}] and tag value [{}] to be " "created with [{}]".format(args.parent, args.tag_value, op.name), service=service)
def GetResourceFromNamespacedName(namespaced_name, resource_type): """Gets the resource from the namespaced name. Args: namespaced_name: Could be the resource name or namespaced name resource_type: the type of the resource ie: 'tagKeys', 'tagValues'. Used to determine which GET function to call Returns: resource """ # TagKeys and TagValues GET require the global CRM API endpoint. with endpoints.CrmEndpointOverrides('global'): service = ServiceFns[resource_type]() req = GetResourceFns[resource_type](name=namespaced_name) response = service.Get(req) return response
def GetTagValueFromNamespacedName(namespaced_name): """Gets the tag value from the namespaced name. Args: namespaced_name: The namespaced name of the tag value Returns: TagValue resource Raises: InvalidInputError: bad input """ parts = namespaced_name.split('/') if len(parts) != 3: raise InvalidInputError( 'TagValue namespaced name [{}] invalid. It should be the permanent ID ' 'or namespaced name; for example: tagValues/7890123456 or ' 'ORGANIZATION_ID/TAG_KEY_SHORT_NAME/TAG_VALUE_SHORT_NAME' .format(namespaced_name)) name = GetTagKeyFromNamespacedName('/'.join(parts[:2])).name next_page_token = None # ListTagValues call requires the global CRM API endpoint. with endpoints.CrmEndpointOverrides('global'): while True: response = ListTagValues(name, next_page_token) for value in response.tagValues: if value.namespacedName == namespaced_name: return value if not response.nextPageToken: break next_page_token = response.nextPageToken raise InvalidInputError( 'TagValue [{}] not found. It should be the permanent ID or namespaced ' 'name; for example: tagValues/7890123456 or ' 'ORGANIZATION_ID/TAG_KEY_SHORT_NAME/TAG_VALUE_SHORT_NAME' .format(namespaced_name))
def GetTagKeyFromNamespacedName(namespaced_name): """Gets the tag key from the namespaced name. Args: namespaced_name: Namespaced name of a tag key Returns: TagKey resource Raises: InvalidInputError: bad input """ parts = namespaced_name.split('/') if len(parts) != 2: raise InvalidInputError( 'TagKey namespaced name [{}] invalid. It should be the permanent ID or ' 'namespaced name; for example: tagKeys/123456789012 or ' 'ORGANIZATION_ID/TAG_KEY_SHORT_NAME' .format(namespaced_name)) next_page_token = None name = '/'.join(['organizations', parts[0]]) # ListTagKeys call requires the global CRM API endpoint. with endpoints.CrmEndpointOverrides('global'): while True: response = ListTagKeys(name, namespaced_name, next_page_token) for key in response.tagKeys: if key.namespacedName == namespaced_name: return key if not response.nextPageToken: break next_page_token = response.nextPageToken raise InvalidInputError('TagKey [{}] not found'.format(namespaced_name))
def Run(self, args): location = args.location if args.IsSpecified('location') else None with endpoint_utils.CrmEndpointOverrides(location): return operations.GetOperationV3(args.id)