def update_resource_tags(tag_action, resource, tags): client = tag_action.session.client( 'azure.mgmt.resource.ResourceManagementClient') # resource group type if is_resource_group(resource): params_patch = ResourceGroupPatchable(tags=tags) client.resource_groups.update( resource['name'], params_patch, ) # other Azure resources else: # deserialize the original object az_resource = GenericResource.deserialize(resource) if not tag_action.manager.tag_operation_enabled(az_resource.type): raise NotImplementedError( 'Cannot tag resource with type {0}'.format( az_resource.type)) api_version = tag_action.session.resource_api_version( resource['id']) # create a PATCH object with only updates to tags tags_patch = GenericResource(tags=tags) client.resources.update_by_id(resource['id'], api_version, tags_patch)
def process(self, resources): session = utils.local_session(self.manager.session_factory) client = self.manager.get_client( 'azure.mgmt.resource.ResourceManagementClient') for resource in resources: # get existing tags tags = resource.get('tags', {}) # add or update tags new_tags = self.data.get('tags') or { self.data.get('tag'): self.data.get('value') } for key in new_tags: tags[key] = new_tags[key] # resource group type if self.manager.type == 'resourcegroup': params_patch = ResourceGroupPatchable(tags=tags) client.resource_groups.update( resource['name'], params_patch, ) # other Azure resources else: az_resource = GenericResource.deserialize(resource) api_version = session.resource_api_version(az_resource.id) az_resource.tags = tags client.resources.create_or_update_by_id( resource['id'], api_version, az_resource)
def update_resource_tags(tag_action, resource, tags): client = tag_action.session.client( 'azure.mgmt.resource.ResourceManagementClient') # resource group type if tag_action.manager.type == 'resourcegroup': params_patch = ResourceGroupPatchable(tags=tags) client.resource_groups.update( resource['name'], params_patch, ) # other Azure resources else: # deserialize the original object az_resource = GenericResource.deserialize(resource) if not tag_action.manager.tag_operation_enabled(az_resource.type): raise NotImplementedError( 'Cannot tag resource with type {0}'.format( az_resource.type)) api_version = tag_action.session.resource_api_version( resource['id']) # create a GenericResource object with the required parameters generic_resource = GenericResource( location=az_resource.location, tags=tags, properties=az_resource.properties, kind=az_resource.kind, managed_by=az_resource.managed_by, identity=az_resource.identity) client.resources.update_by_id(resource['id'], api_version, generic_resource)
def update_resource_tags(self, session, client, resource, tags): # resource group type if self.manager.type == 'resourcegroup': params_patch = ResourceGroupPatchable( tags=tags ) client.resource_groups.update( resource['name'], params_patch, ) # other Azure resources else: az_resource = GenericResource.deserialize(resource) api_version = session.resource_api_version(az_resource.id) az_resource.tags = tags client.resources.create_or_update_by_id(resource['id'], api_version, az_resource)
def update_resource_tags(tag_action, resource, tags): client = tag_action.session.client( 'azure.mgmt.resource.ResourceManagementClient') # resource group type if tag_action.manager.type == 'resourcegroup': params_patch = ResourceGroupPatchable(tags=tags) client.resource_groups.update( resource['name'], params_patch, ) # other Azure resources else: # generic armresource tagging isn't supported yet Github issue #2637 if tag_action.manager.type == 'armresource': raise NotImplementedError('Cannot tag generic ARM resources.') api_version = tag_action.session.resource_api_version( resource['id']) # deserialize the original object az_resource = GenericResource.deserialize(resource) # create a GenericResource object with the required parameters generic_resource = GenericResource( location=az_resource.location, tags=tags, properties=az_resource.properties, kind=az_resource.kind, managed_by=az_resource.managed_by, sku=az_resource.sku, identity=az_resource.identity) try: client.resources.update_by_id(resource['id'], api_version, generic_resource) except Exception as e: log.error("Failed to update tags for the resource.\n" "Type: {0}.\n" "Name: {1}.\n" "Error: {2}".format(resource['type'], resource['name'], e))
def update_resource_tags(self, resource, tags): # resource group type if self.manager.type == 'resourcegroup': params_patch = ResourceGroupPatchable(tags=tags) self.client.resource_groups.update( resource['name'], params_patch, ) # other Azure resources else: if self.manager.type == 'armresource': raise NotImplementedError('Cannot tag generic ARM resources.') az_resource = GenericResource.deserialize(resource) api_version = self.session.resource_api_version(az_resource.id) az_resource.tags = tags self.client.resources.create_or_update_by_id(resource['id'], api_version, az_resource)