Skip to content
Merged
92 changes: 89 additions & 3 deletions src/azure-cli/azure/cli/command_modules/resource/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -1958,7 +1958,7 @@

helps['tag'] = """
type: group
short-summary: Manage resource tags.
short-summary: Tags Management on a resource.
Comment thread
Grayer123 marked this conversation as resolved.
Outdated
"""

helps['tag add-value'] = """
Expand All @@ -1972,18 +1972,104 @@

helps['tag create'] = """
type: command
short-summary: Create a tag in the subscription.
short-summary: Create tags on a specific resource.
long-summary: >
The az tag create cmdlet with an id creates or updates the entire set of tags on a resource, resource group or subscription.
Comment thread
Grayer123 marked this conversation as resolved.
Outdated
This operation allows adding or replacing the entire set of tags on the specified resource, resource group or subscription.
The specified entity can have a maximum of 50 tags.
parameters:
- name: --name -n
short-summary: The name of the tag to create.
- name: --subscription
short-summary: Name or ID of subscription. You can configure the default subscription using az account set -s NAME_OR_ID.
- name: --resource-id
short-summary: The resource identifier for the entity being tagged. A resource, a resource group or a subscription may be tagged.
- name: --tags
short-summary: The tags to be applied on the resource.
examples:
- name: Create a tag in the subscription.
text: >
az tag create --name MyTag
- name: Creates or updates the entire set of tags on a subscription.
Comment thread
Grayer123 marked this conversation as resolved.
Outdated
text: >
az tag create --resource-id /subscriptions/{subId} --tags Dept=Finance Status=Normal
- name: Creates or updates the entire set of tags on a resource group.
Comment thread
Grayer123 marked this conversation as resolved.
Outdated
text: >
az tag create --resource-id /subscriptions/{sub-id}/resourcegroups/{rg} --tags Dept=Finance Status=Normal
- name: Creates or updates the entire set of tags on a resource.
Comment thread
Grayer123 marked this conversation as resolved.
Outdated
text: >
az tag create --resource-id /subscriptions/{sub-id}/resourcegroups/{rg}/providers/Microsoft.Compute/virtualMachines/{vmName} --tags Dept=Finance Status=Normal
"""

helps['tag delete'] = """
type: command
short-summary: Delete a tag in the subscription.
short-summary: Delete tags on a specific resource.
long-summary:
The az tag delete cmdlet with an id deletes the entire set of tags on a resource, resource group or subscription.
Comment thread
Grayer123 marked this conversation as resolved.
Outdated
parameters:
- name: --name -n
short-summary: The name of the tag to be deleted.
- name: --resource-id
short-summary: The resource identifier for the entity being tagged. A resource, a resource group or a subscription may be tagged.
Comment thread
Grayer123 marked this conversation as resolved.
examples:
- name: Delete a tag from the subscription.
text: >
az tag delete --name MyTag
- name: Delete the entire set of tags on a subscription.
text: >
az tag delete --resource-id /subscriptions/{sub-id}
- name: Deletes the entire set of tags on a resource group.
Comment thread
Grayer123 marked this conversation as resolved.
Outdated
text: >
az tag delete --resource-id /subscriptions/{sub-id}/resourcegroups/{rg}
- name: Deletes the entire set of tags on a resource.
text: >
az tag delete --resource-id /subscriptions/{sub-id}/resourcegroups/{rg}/providers/Microsoft.Compute/virtualMachines/{vmName}
"""

helps['tag list'] = """
type: command
short-summary: Lists the entire set of tags on a specific resource.
Comment thread
Grayer123 marked this conversation as resolved.
Outdated
long-summary: The az tag list cmdlet with an id lists the entire set of tags on a resource, resource group or subscription.
Comment thread
Grayer123 marked this conversation as resolved.
Outdated
parameters:
- name: --resource-id
short-summary: The resource identifier for the entity being tagged. A resource, a resource group or a subscription may be tagged.
examples:
- name: Lists the entire set of tags on a subscription.
Comment thread
Grayer123 marked this conversation as resolved.
Outdated
text: >
az tag list --resource-id /subscriptions/{sub-id}
- name: Lists the entire set of tags on a resource group.
Comment thread
Grayer123 marked this conversation as resolved.
Outdated
text: >
az tag list --resource-id /subscriptions/{sub-id}/resourcegroups/{rg}
- name: Lists the entire set of tags on a resource.
Comment thread
Grayer123 marked this conversation as resolved.
Outdated
text: >
az tag list --resource-id /subscriptions/{sub-id}/resourcegroups/{rg}/providers/Microsoft.Compute/virtualMachines/{vmName}
"""

helps['tag update'] = """
type: command
short-summary: Selectively updates the set of tags on a specific resource.
Comment thread
Grayer123 marked this conversation as resolved.
Outdated
long-summary: >
The az tag update cmdlet with an id selectively updates the set of tags on a resource, resource group or subscription.
Comment thread
Grayer123 marked this conversation as resolved.
Outdated
This operation allows replacing, merging or selectively deleting tags on the specified resource, resource group or subscription.
The specified entity can have a maximum of 50 tags at the end of the operation.
The 'replace' option replaces the entire set of existing tags with a new set.
The 'merge' option allows adding tags with new names and updating the values of tags with existing names.
The 'delete' option allows selectively deleting tags based on given names or name/value pairs.
parameters:
- name: --resource-id
short-summary: The resource identifier for the entity being tagged. A resource, a resource group or a subscription may be tagged.
- name: --operation
short-summary: The update operation. Options are Merge, Replace and Delete.
- name: --tags
short-summary: The tags to be updated on the resource.
examples:
- name: Selectively updates the set of tags on a subscription with "merge" Operation.
Comment thread
Grayer123 marked this conversation as resolved.
Outdated
text: >
az tag update --resource-id /subscriptions/{sub-id} --operation merge --tags key1=value1 key3=value3
- name: Selectively updates the set of tags on a resource group with "replace" Operation.
Comment thread
Grayer123 marked this conversation as resolved.
Outdated
text: >
az tag update --resource-id /subscriptions/{sub-id}/resourcegroups/{rg} --operation replace --tags key1=value1 key3=value3
- name: Selectively updates the set of tags on a resource with "delete" Operation.
Comment thread
Grayer123 marked this conversation as resolved.
Outdated
text: >
az tag update --resource-id /subscriptions/{sub-id}/resourcegroups/{rg}/providers/Microsoft.Compute/virtualMachines/{vmName} --operation delete --tags key1=value1
"""
5 changes: 5 additions & 0 deletions src/azure-cli/azure/cli/command_modules/resource/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,11 @@ def load_arguments(self, _):
with self.argument_context('tag') as c:
c.argument('tag_name', options_list=['--name', '-n'])
c.argument('tag_value', options_list='--value')
c.argument('resource_id', options_list='--resource-id',
help='The resource identifier for the tagged entity. A resource, a resource group or a subscription may be tagged.')
Comment thread
Grayer123 marked this conversation as resolved.
Outdated
c.argument('tags', tags_type)
c.argument('operation', options_list='--operation',
Comment thread
Grayer123 marked this conversation as resolved.
Outdated
help='The update operation: options include Merge, Replace and Delete.')
Comment thread
Grayer123 marked this conversation as resolved.
Outdated

with self.argument_context('lock') as c:
c.argument('lock_name', options_list=['--name', '-n'], validator=validate_lock_parameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,11 @@ def load_command_table(self, _):
g.custom_command('unregister', 'unregister_feature')

# Tag commands
with self.command_group('tag', resource_tag_sdk) as g:
g.command('list', 'list')
g.command('create', 'create_or_update')
g.command('delete', 'delete')
with self.command_group('tag', resource_tag_sdk, resource_type=ResourceType.MGMT_RESOURCE_RESOURCES) as g:
g.custom_command('list', 'get_tag_at_scope')
g.custom_command('create', 'create_or_update_tag_at_scope')
g.custom_command('delete', 'delete_tag_at_scope')
Comment thread
Grayer123 marked this conversation as resolved.
Outdated
g.custom_command('update', 'update_tag_at_scope', min_api='2019-10-01')
g.command('add-value', 'create_or_update_value')
g.command('remove-value', 'delete_value')

Expand Down
37 changes: 37 additions & 0 deletions src/azure-cli/azure/cli/command_modules/resource/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -2537,6 +2537,43 @@ def list_resource_links(cmd, scope=None, filter_string=None):
# endregion


# region tags
def get_tag_at_scope(cmd, resource_id=None):
rcf = _resource_client_factory(cmd.cli_ctx)
if resource_id is not None:
return rcf.tags.get_at_scope(scope=resource_id)

return rcf.tags.list()


def create_or_update_tag_at_scope(cmd, resource_id=None, tags=None, tag_name=None):
rcf = _resource_client_factory(cmd.cli_ctx)
if resource_id is not None:
if tags is None or bool(tags) is False:
Comment thread
Grayer123 marked this conversation as resolved.
Outdated
raise IncorrectUsageError("Tags could not be empty.")
Tags = cmd.get_models('Tags')
tag_obj = Tags(tags=tags)
return rcf.tags.create_or_update_at_scope(scope=resource_id, properties=tag_obj)

return rcf.tags.create_or_update(tag_name=tag_name)
Comment thread
Grayer123 marked this conversation as resolved.


def delete_tag_at_scope(cmd, resource_id=None, tag_name=None):
rcf = _resource_client_factory(cmd.cli_ctx)
if resource_id is not None:
return rcf.tags.delete_at_scope(scope=resource_id)
Comment thread
Grayer123 marked this conversation as resolved.

return rcf.tags.delete(tag_name=tag_name)


def update_tag_at_scope(cmd, resource_id, tags, operation):
rcf = _resource_client_factory(cmd.cli_ctx)
Tags = cmd.get_models('Tags')
tag_obj = Tags(tags=tags)
return rcf.tags.update_at_scope(scope=resource_id, properties=tag_obj, operation=operation)
Comment thread
Grayer123 marked this conversation as resolved.
# endregion


class _ResourceUtils: # pylint: disable=too-many-instance-attributes
def __init__(self, cli_ctx,
resource_group_name=None, resource_provider_namespace=None,
Expand Down
Loading