1# -*- coding: utf-8 -*- #
2# Copyright 2019 Google LLC. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#    http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15"""Delete command for the resource manager - Tag Values CLI."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import unicode_literals
20
21from googlecloudsdk.api_lib.resource_manager import tags
22from googlecloudsdk.calliope import base
23from googlecloudsdk.command_lib.resource_manager import operations
24from googlecloudsdk.command_lib.resource_manager import tag_arguments as arguments
25from googlecloudsdk.command_lib.resource_manager import tag_utils
26
27
28@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
29class Delete(base.Command):
30  """Deletes the specified TagValue resource.
31
32    Deletes the TagValue resource given the TagValue's parent and short name
33    or the TagValue's numeric id.
34  """
35
36  detailed_help = {
37      'EXAMPLES':
38          """
39          To delete a TagValue with id ``123'', run:
40
41            $ {command} tagValues/123
42
43          To delete a TagValue named ``dev'' with tagKey ``env'' under
44          organization ``456'',
45          run:
46
47            $ {command} 456/env/dev
48          """
49  }
50
51  @staticmethod
52  def Args(parser):
53    arguments.AddResourceNameArgToParser(parser)
54    arguments.AddAsyncArgToParser(parser)
55
56  def Run(self, args):
57    service = tags.TagValuesService()
58    messages = tags.TagMessages()
59
60    if args.RESOURCE_NAME.find('tagValues/') == 0:
61      tag_value = args.RESOURCE_NAME
62    else:
63      tag_value = tag_utils.GetTagValueFromNamespacedName(
64          args.RESOURCE_NAME).name
65
66    delete_request = messages.CloudresourcemanagerTagValuesDeleteRequest(
67        name=tag_value)
68
69    op = service.Delete(delete_request)
70
71    if args.async_:
72      return op
73
74    return operations.WaitForOperation(
75        op,
76        'Waiting for TagValue [{}] to be deleted'.format(tag_value),
77        service=service)
78