File: tag.py

package info (click to toggle)
python-neutronclient 1%3A11.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 2,068 kB
  • sloc: python: 21,778; makefile: 17; sh: 10
file content (107 lines) | stat: -rw-r--r-- 4,222 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from neutronclient._i18n import _
from neutronclient.common import exceptions
from neutronclient.neutron import v2_0 as neutronv20


# List of resources can be set tag
TAG_RESOURCES = ['network', 'subnet', 'port', 'router', 'subnetpool']


def _convert_resource_args(client, parsed_args):
    resource_type = client.get_resource_plural(parsed_args.resource_type)
    resource_id = neutronv20.find_resourceid_by_name_or_id(
        client, parsed_args.resource_type, parsed_args.resource)
    return resource_type, resource_id


def _add_common_arguments(parser):
    parser.add_argument('--resource-type',
                        choices=TAG_RESOURCES,
                        dest='resource_type',
                        required=True,
                        help=_('Resource Type.'))
    parser.add_argument('--resource',
                        required=True,
                        help=_('Resource name or ID.'))


class AddTag(neutronv20.NeutronCommand):
    """Add a tag into the resource."""

    def get_parser(self, prog_name):
        parser = super(AddTag, self).get_parser(prog_name)
        _add_common_arguments(parser)
        parser.add_argument('--tag',
                            required=True,
                            help=_('Tag to be added.'))
        return parser

    def take_action(self, parsed_args):
        client = self.get_client()
        if not parsed_args.tag:
            raise exceptions.CommandError(
                _('Cannot add an empty value as tag'))
        resource_type, resource_id = _convert_resource_args(client,
                                                            parsed_args)
        client.add_tag(resource_type, resource_id, parsed_args.tag)


class ReplaceTag(neutronv20.NeutronCommand):
    """Replace all tags on the resource."""

    def get_parser(self, prog_name):
        parser = super(ReplaceTag, self).get_parser(prog_name)
        _add_common_arguments(parser)
        parser.add_argument('--tag',
                            metavar='TAG',
                            action='append',
                            dest='tags',
                            required=True,
                            help=_('Tag (This option can be repeated).'))
        return parser

    def take_action(self, parsed_args):
        client = self.get_client()
        resource_type, resource_id = _convert_resource_args(client,
                                                            parsed_args)
        body = {'tags': parsed_args.tags}
        client.replace_tag(resource_type, resource_id, body)


class RemoveTag(neutronv20.NeutronCommand):
    """Remove a tag on the resource."""

    def get_parser(self, prog_name):
        parser = super(RemoveTag, self).get_parser(prog_name)
        _add_common_arguments(parser)
        tag_opt = parser.add_mutually_exclusive_group()
        tag_opt.add_argument('--all',
                             action='store_true',
                             help=_('Remove all tags on the resource.'))
        tag_opt.add_argument('--tag',
                             help=_('Tag to be removed.'))
        return parser

    def take_action(self, parsed_args):
        if not parsed_args.all and not parsed_args.tag:
            raise exceptions.CommandError(
                _("--all or --tag must be specified"))
        client = self.get_client()
        resource_type, resource_id = _convert_resource_args(client,
                                                            parsed_args)
        if parsed_args.all:
            client.remove_tag_all(resource_type, resource_id)
        else:
            client.remove_tag(resource_type, resource_id, parsed_args.tag)