Debian Codegraph @

neutronclient/neutron/v2_0/auto_allocated_topology.py

raw
1 # Copyright 2016 IBM
2 # All Rights Reserved
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # 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, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15 #
16
17 import argparse
18
19 from cliff import show
20 from oslo_serialization import jsonutils
21
22 from neutronclient._i18n import _
23 from neutronclient.common import exceptions
24 from neutronclient.neutron import v2_0
25
26
27 class ShowAutoAllocatedTopology(v2_0.NeutronCommand, show.ShowOne):
28 """Show the auto-allocated topology of a given tenant."""
29
30 resource = 'auto_allocated_topology'
31
32 def get_parser(self, prog_name):
33 parser = super(ShowAutoAllocatedTopology, self).get_parser(prog_name)
34 parser.add_argument(
35 '--dry-run',
36 help=_('Validate the requirements for auto-allocated-topology. '
37 '(Does not return a topology.)'),
38 action='store_true')
39 parser.add_argument(
40 '--tenant-id', metavar='tenant-id',
41 help=_('The owner tenant ID.'))
42 # Allow people to do
43 # neutron auto-allocated-topology-show <tenant-id>
44 # (Only useful to users who can look at other tenants' topologies.)
45 # We use a different name for this arg because the default will
46 # override whatever is in the named arg otherwise.
47 parser.add_argument(
48 'pos_tenant_id',
49 help=argparse.SUPPRESS, nargs='?')
50 return parser
51
52 def take_action(self, parsed_args):
53 client = self.get_client()
54 extra_values = v2_0.parse_args_to_dict(self.values_specs)
55 if extra_values:
56 raise exceptions.CommandError(
57 _("Invalid argument(s): --%s") % ', --'.join(extra_values))
58 tenant_id = parsed_args.tenant_id or parsed_args.pos_tenant_id
59 if parsed_args.dry_run:
60 data = client.validate_auto_allocated_topology_requirements(
61 tenant_id)
62 else:
63 data = client.get_auto_allocated_topology(tenant_id)
64 if self.resource in data:
65 for k, v in data[self.resource].items():
66 if isinstance(v, list):
67 value = ""
68 for _item in v:
69 if value:
70 value += "\n"
71 if isinstance(_item, dict):
72 value += jsonutils.dumps(_item)
73 else:
74 value += str(_item)
75 data[self.resource][k] = value
76 elif v == "dry-run=pass":
77 return ("dry-run",), ("pass",)
78 elif v is None:
79 data[self.resource][k] = ''
80 return zip(*sorted(data[self.resource].items()))
81 else:
82 return None
83
84
85 class DeleteAutoAllocatedTopology(v2_0.NeutronCommand):
86 """Delete the auto-allocated topology of a given tenant."""
87
88 resource = 'auto_allocated_topology'
89
90 def get_parser(self, prog_name):
91 parser = super(DeleteAutoAllocatedTopology, self).get_parser(prog_name)
92 parser.add_argument(
93 '--tenant-id', metavar='tenant-id',
94 help=_('The owner tenant ID.'))
95 # Allow people to do
96 # neutron auto-allocated-topology-delete <tenant-id>
97 # (Only useful to users who can look at other tenants' topologies.)
98 # We use a different name for this arg because the default will
99 # override whatever is in the named arg otherwise.
100 parser.add_argument(
101 'pos_tenant_id',
102 help=argparse.SUPPRESS, nargs='?')
103 return parser
104
105 def take_action(self, parsed_args):
106 client = self.get_client()
107 tenant_id = parsed_args.tenant_id or parsed_args.pos_tenant_id
108 client.delete_auto_allocated_topology(tenant_id)
109 # It tenant is None, let's be clear on what it means.
110 tenant_id = tenant_id or 'None (i.e. yours)'
111 print(_('Deleted topology for tenant %s.') % tenant_id,
112 file=self.app.stdout)