Debian Codegraph @

neutronclient/neutron/v2_0/tag.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from neutronclient._i18n import _
14 from neutronclient.common import exceptions
15 from neutronclient.neutron import v2_0 as neutronv20
16
17
18 # List of resources can be set tag
19 TAG_RESOURCES = ['network', 'subnet', 'port', 'router', 'subnetpool']
20
21
22 def _convert_resource_args(client, parsed_args):
23 resource_type = client.get_resource_plural(parsed_args.resource_type)
24 resource_id = neutronv20.find_resourceid_by_name_or_id(
25 client, parsed_args.resource_type, parsed_args.resource)
26 return resource_type, resource_id
27
28
29 def _add_common_arguments(parser):
30 parser.add_argument('--resource-type',
31 choices=TAG_RESOURCES,
32 dest='resource_type',
33 required=True,
34 help=_('Resource Type.'))
35 parser.add_argument('--resource',
36 required=True,
37 help=_('Resource name or ID.'))
38
39
40 class AddTag(neutronv20.NeutronCommand):
41 """Add a tag into the resource."""
42
43 def get_parser(self, prog_name):
44 parser = super(AddTag, self).get_parser(prog_name)
45 _add_common_arguments(parser)
46 parser.add_argument('--tag',
47 required=True,
48 help=_('Tag to be added.'))
49 return parser
50
51 def take_action(self, parsed_args):
52 client = self.get_client()
53 if not parsed_args.tag:
54 raise exceptions.CommandError(
55 _('Cannot add an empty value as tag'))
56 resource_type, resource_id = _convert_resource_args(client,
57 parsed_args)
58 client.add_tag(resource_type, resource_id, parsed_args.tag)
59
60
61 class ReplaceTag(neutronv20.NeutronCommand):
62 """Replace all tags on the resource."""
63
64 def get_parser(self, prog_name):
65 parser = super(ReplaceTag, self).get_parser(prog_name)
66 _add_common_arguments(parser)
67 parser.add_argument('--tag',
68 metavar='TAG',
69 action='append',
70 dest='tags',
71 required=True,
72 help=_('Tag (This option can be repeated).'))
73 return parser
74
75 def take_action(self, parsed_args):
76 client = self.get_client()
77 resource_type, resource_id = _convert_resource_args(client,
78 parsed_args)
79 body = {'tags': parsed_args.tags}
80 client.replace_tag(resource_type, resource_id, body)
81
82
83 class RemoveTag(neutronv20.NeutronCommand):
84 """Remove a tag on the resource."""
85
86 def get_parser(self, prog_name):
87 parser = super(RemoveTag, self).get_parser(prog_name)
88 _add_common_arguments(parser)
89 tag_opt = parser.add_mutually_exclusive_group()
90 tag_opt.add_argument('--all',
91 action='store_true',
92 help=_('Remove all tags on the resource.'))
93 tag_opt.add_argument('--tag',
94 help=_('Tag to be removed.'))
95 return parser
96
97 def take_action(self, parsed_args):
98 if not parsed_args.all and not parsed_args.tag:
99 raise exceptions.CommandError(
100 _("--all or --tag must be specified"))
101 client = self.get_client()
102 resource_type, resource_id = _convert_resource_args(client,
103 parsed_args)
104 if parsed_args.all:
105 client.remove_tag_all(resource_type, resource_id)
106 else:
107 client.remove_tag(resource_type, resource_id, parsed_args.tag)