Debian Codegraph @

neutronclient/osc/v2/subnet_onboard/subnet_onboard.py

raw
1 # Copyright (c) 2019 SUSE Linux Products GmbH
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 """Subnet onboard action implementation"""
18 import logging
19
20 from osc_lib.command import command
21 from osc_lib import exceptions
22
23 from neutronclient._i18n import _
24
25 LOG = logging.getLogger(__name__)
26
27
28 class NetworkOnboardSubnets(command.Command):
29 """Onboard network subnets into a subnet pool"""
30
31 def get_parser(self, prog_name):
32 parser = super(NetworkOnboardSubnets, self).get_parser(prog_name)
33 parser.add_argument(
34 'network',
35 metavar="<network>",
36 help=_("Onboard all subnets associated with this network")
37 )
38 parser.add_argument(
39 'subnetpool',
40 metavar="<subnetpool>",
41 help=_("Target subnet pool for onboarding subnets")
42 )
43 return parser
44
45 def take_action(self, parsed_args):
46 client = self.app.client_manager.neutronclient
47 subnetpool_id = _get_id(client, parsed_args.subnetpool, 'subnetpool')
48 network_id = _get_id(client, parsed_args.network, 'network')
49 body = {'network_id': network_id}
50 try:
51 client.onboard_network_subnets(subnetpool_id, body)
52 except Exception as e:
53 msg = (_("Failed to onboard subnets for network '%(n)s': %(e)s")
54 % {'n': parsed_args.network, 'e': e})
55 raise exceptions.CommandError(msg)
56
57
58 def _get_id(client, id_or_name, resource):
59 return client.find_resource(resource, str(id_or_name))['id']