Skip to content
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
3 changes: 3 additions & 0 deletions src/azure-cli/azure/cli/command_modules/resource/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,9 @@
- name: Tag a web app with the key 'vmlist' and value 'vm1', using a resource identifier.
text: >
az resource tag --tags vmlist=vm1 --ids /subscriptions/{SubID}/resourceGroups/{ResourceGroup}/providers/Microsoft.Web/sites/{WebApp}
- name: Tag the virtual machine 'MyVm' with the key 'vmlist' and value 'vm1' incrementally, it doesn't empty the existing tags.
text: >
az resource tag --tags vmlist=vm1 -g MyResourceGroup -n MyVm --resource-type "Microsoft.Compute/virtualMachines" -i
- name: Tag a resource. (autogenerated)
text: |
az resource tag --ids /subscriptions/{SubID}/resourceGroups/{ResourceGroup}/providers/Microsoft.Web/sites/{WebApp} --tags vmlist=vm1
Expand Down
4 changes: 4 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 @@ -93,6 +93,10 @@ def load_arguments(self, _):
c.argument('scope', help='Fully-qualified scope for retrieving links.')
c.argument('filter_string', options_list=['--filter', c.deprecate(target='--filter-string', redirect='--filter', hide=True)], help='Filter string for limiting results.')

with self.argument_context('resource tag') as c:
c.argument('is_incremental', action='store_true', options_list=['--is-incremental', '-i'],
help='The option to add tags incrementally without deleting the original tags. If the key of new tag and original tag are duplicated, the original value will be overwritten.')

with self.argument_context('provider') as c:
c.ignore('top')
c.argument('resource_provider_namespace', options_list=['--namespace', '-n'], completer=get_providers_completion_list, help=_PROVIDER_HELP_TEXT)
Expand Down
18 changes: 13 additions & 5 deletions src/azure-cli/azure/cli/command_modules/resource/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -1343,9 +1343,9 @@ def update_resource(cmd, parameters, resource_ids=None,


# pylint: unused-argument
def tag_resource(cmd, tags, resource_ids=None,
resource_group_name=None, resource_provider_namespace=None,
parent_resource_path=None, resource_type=None, resource_name=None, api_version=None):
def tag_resource(cmd, tags, resource_ids=None, resource_group_name=None, resource_provider_namespace=None,
parent_resource_path=None, resource_type=None, resource_name=None, api_version=None,
is_incremental=None):
""" Updates the tags on an existing resource. To clear tags, specify the --tag option
without anything else. """
parsed_ids = _get_parsed_resource_ids(resource_ids) or [_create_parsed_id(cmd.cli_ctx,
Expand All @@ -1356,7 +1356,8 @@ def tag_resource(cmd, tags, resource_ids=None,
resource_name)]

return _single_or_collection(
[_get_rsrc_util_from_parsed_id(cmd.cli_ctx, id_dict, api_version).tag(tags) for id_dict in parsed_ids])
[_get_rsrc_util_from_parsed_id(cmd.cli_ctx, id_dict, api_version).tag(tags, is_incremental)
for id_dict in parsed_ids])


# pylint: unused-argument
Expand Down Expand Up @@ -2414,9 +2415,16 @@ def update(self, parameters):
self.api_version,
parameters)

def tag(self, tags):
def tag(self, tags, is_incremental=False):
resource = self.get_resource()

if is_incremental is True:
if not tags:
raise CLIError("When modifying tag incrementally, the parameters of tag must have specific values.")
if resource.tags:
resource.tags.update(tags)
tags = resource.tags

# please add the service type that needs to be requested with PATCH type here
# for example: the properties of RecoveryServices/vaults must be filled, and a PUT request that passes back
# to properties will fail due to the lack of properties, so the PATCH type should be used
Expand Down
Loading