summaryrefslogtreecommitdiff
path: root/openstackclient/network/v2/subnet.py
blob: 794c787f07e33c8991c289b0bfd88bafa3660e12 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#   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.
#

"""Subnet action implementations"""
import copy

from json.encoder import JSONEncoder

from openstackclient.common import command
from openstackclient.common import parseractions
from openstackclient.common import utils
from openstackclient.identity import common as identity_common


def _format_allocation_pools(data):
    pool_formatted = ['%s-%s' % (pool.get('start', ''), pool.get('end', ''))
                      for pool in data]
    return ','.join(pool_formatted)


def _format_host_routes(data):
    try:
        return '\n'.join([JSONEncoder().encode(route) for route in data])
    except (TypeError, KeyError):
        return ''


_formatters = {
    'allocation_pools': _format_allocation_pools,
    'dns_nameservers': utils.format_list,
    'host_routes': _format_host_routes,
}


def _get_columns(item):
    columns = list(item.keys())
    if 'tenant_id' in columns:
        columns.remove('tenant_id')
        columns.append('project_id')
    return tuple(sorted(columns))


def convert_entries_to_nexthop(entries):
    # Change 'gateway' entry to 'nexthop'
    changed_entries = copy.deepcopy(entries)
    for entry in changed_entries:
        entry['nexthop'] = entry['gateway']
        del entry['gateway']

    return changed_entries


def convert_entries_to_gateway(entries):
    # Change 'nexhop' entry to 'gateway'
    changed_entries = copy.deepcopy(entries)
    for entry in changed_entries:
        entry['gateway'] = entry['nexthop']
        del entry['nexthop']

    return changed_entries


def _get_attrs(client_manager, parsed_args):
    attrs = {}
    if parsed_args.name is not None:
        attrs['name'] = str(parsed_args.name)

    if 'project' in parsed_args and parsed_args.project is not None:
        identity_client = client_manager.identity
        project_id = identity_common.find_project(
            identity_client,
            parsed_args.project,
            parsed_args.project_domain,
        ).id
        attrs['tenant_id'] = project_id

    client = client_manager.network
    attrs['network_id'] = client.find_network(parsed_args.network,
                                              ignore_missing=False).id

    if parsed_args.subnet_pool is not None:
        subnet_pool = client.find_subnet_pool(parsed_args.subnet_pool,
                                              ignore_missing=False)
        attrs['subnetpool_id'] = subnet_pool.id

    if parsed_args.use_default_subnet_pool:
        attrs['use_default_subnetpool'] = True
    if parsed_args.gateway.lower() != 'auto':
        if parsed_args.gateway.lower() == 'none':
            attrs['gateway_ip'] = None
        else:
            attrs['gateway_ip'] = parsed_args.gateway
    if parsed_args.prefix_length is not None:
        attrs['prefixlen'] = parsed_args.prefix_length
    if parsed_args.subnet_range is not None:
        attrs['cidr'] = parsed_args.subnet_range
    if parsed_args.ip_version is not None:
        attrs['ip_version'] = parsed_args.ip_version
    if parsed_args.ipv6_ra_mode is not None:
        attrs['ipv6_ra_mode'] = parsed_args.ipv6_ra_mode
    if parsed_args.ipv6_address_mode is not None:
        attrs['ipv6_address_mode'] = parsed_args.ipv6_address_mode
    if parsed_args.allocation_pools is not None:
        attrs['allocation_pools'] = parsed_args.allocation_pools
    if parsed_args.enable_dhcp is not None:
        attrs['enable_dhcp'] = parsed_args.enable_dhcp
    if parsed_args.dns_nameservers is not None:
        attrs['dns_nameservers'] = parsed_args.dns_nameservers
    if parsed_args.host_routes is not None:
        # Change 'gateway' entry to 'nexthop' to match the API
        attrs['host_routes'] = convert_entries_to_nexthop(
            parsed_args.host_routes)

    return attrs


class CreateSubnet(command.ShowOne):
    """Create a subnet"""

    def get_parser(self, prog_name):
        parser = super(CreateSubnet, self).get_parser(prog_name)
        parser.add_argument(
            'name',
            help='New subnet name',
        )
        parser.add_argument(
            '--project',
            metavar='<project>',
            help="Owner's project (name or ID)",
        )
        identity_common.add_project_domain_option_to_parser(parser)
        subnet_pool_group = parser.add_mutually_exclusive_group()
        subnet_pool_group.add_argument(
            '--subnet-pool',
            metavar='<subnet-pool>',
            help='Subnet pool from which this subnet will obtain a CIDR '
                 '(Name or ID)',
        )
        subnet_pool_group.add_argument(
            '--use-default-subnet-pool',
            action='store_true',
            help='Use default subnet pool for --ip-version',
        )
        parser.add_argument(
            '--prefix-length',
            metavar='<prefix-length>',
            help='Prefix length for subnet allocation from subnetpool',
        )
        parser.add_argument(
            '--subnet-range',
            metavar='<subnet-range>',
            help='Subnet range in CIDR notation '
                 '(required if --subnet-pool is not specified, '
                 'optional otherwise)',
        )
        parser.add_argument(
            '--allocation-pool',
            metavar='start=<ip-address>,end=<ip-address>',
            dest='allocation_pools',
            action=parseractions.MultiKeyValueAction,
            required_keys=['start', 'end'],
            help='Allocation pool IP addresses for this subnet '
                 'e.g.: start=192.168.199.2,end=192.168.199.254 '
                 '(This option can be repeated)',
        )
        dhcp_enable_group = parser.add_mutually_exclusive_group()
        dhcp_enable_group.add_argument(
            '--dhcp',
            dest='enable_dhcp',
            action='store_true',
            default=True,
            help='Enable DHCP (default)',
        )
        dhcp_enable_group.add_argument(
            '--no-dhcp',
            dest='enable_dhcp',
            action='store_false',
            help='Disable DHCP',
        )
        parser.add_argument(
            '--dns-nameserver',
            metavar='<dns-nameserver>',
            action='append',
            dest='dns_nameservers',
            help='DNS name server for this subnet '
                 '(This option can be repeated)',
        )
        parser.add_argument(
            '--gateway',
            metavar='<gateway>',
            default='auto',
            help="Specify a gateway for the subnet.  The three options are: "
                 "  <ip-address>: Specific IP address to use as the gateway "
                 "  'auto':       Gateway address should automatically be "
                 "                chosen from within the subnet itself "
                 "  'none':       This subnet will not use a gateway "
                 "e.g.: --gateway 192.168.9.1, --gateway auto, --gateway none"
                 "(default is 'auto')",
        )
        parser.add_argument(
            '--host-route',
            metavar='destination=<subnet>,gateway=<ip-address>',
            dest='host_routes',
            action=parseractions.MultiKeyValueAction,
            required_keys=['destination', 'gateway'],
            help='Additional route for this subnet '
                 'e.g.: destination=10.10.0.0/16,gateway=192.168.71.254 '
                 'destination: destination subnet (in CIDR notation) '
                 'gateway: nexthop IP address '
                 '(This option can be repeated)',
        )
        parser.add_argument(
            '--ip-version',
            type=int,
            default=4,
            choices=[4, 6],
            help='IP version (default is 4).  Note that when subnet pool is '
                 'specified, IP version is determined from the subnet pool '
                 'and this option is ignored.',
        )
        parser.add_argument(
            '--ipv6-ra-mode',
            choices=['dhcpv6-stateful', 'dhcpv6-stateless', 'slaac'],
            help='IPv6 RA (Router Advertisement) mode, '
                 'valid modes: [dhcpv6-stateful, dhcpv6-stateless, slaac]',
        )
        parser.add_argument(
            '--ipv6-address-mode',
            choices=['dhcpv6-stateful', 'dhcpv6-stateless', 'slaac'],
            help='IPv6 address mode, '
                 'valid modes: [dhcpv6-stateful, dhcpv6-stateless, slaac]',
        )
        parser.add_argument(
            '--network',
            required=True,
            metavar='<network>',
            help='Network this subnet belongs to (name or ID)',
        )

        return parser

    def take_action(self, parsed_args):
        client = self.app.client_manager.network
        attrs = _get_attrs(self.app.client_manager, parsed_args)
        obj = client.create_subnet(**attrs)
        columns = _get_columns(obj)
        data = utils.get_item_properties(obj, columns, formatters=_formatters)
        return (columns, data)


class DeleteSubnet(command.Command):
    """Delete subnet"""

    def get_parser(self, prog_name):
        parser = super(DeleteSubnet, self).get_parser(prog_name)
        parser.add_argument(
            'subnet',
            metavar="<subnet>",
            help="Subnet to delete (name or ID)",
        )
        return parser

    def take_action(self, parsed_args):
        client = self.app.client_manager.network
        client.delete_subnet(
            client.find_subnet(parsed_args.subnet))


class ListSubnet(command.Lister):
    """List subnets"""

    def get_parser(self, prog_name):
        parser = super(ListSubnet, self).get_parser(prog_name)
        parser.add_argument(
            '--long',
            action='store_true',
            default=False,
            help='List additional fields in output',
        )
        return parser

    def take_action(self, parsed_args):
        data = self.app.client_manager.network.subnets()

        headers = ('ID', 'Name', 'Network', 'Subnet')
        columns = ('id', 'name', 'network_id', 'cidr')
        if parsed_args.long:
            headers += ('Project', 'DHCP', 'Name Servers',
                        'Allocation Pools', 'Host Routes', 'IP Version',
                        'Gateway')
            columns += ('tenant_id', 'enable_dhcp', 'dns_nameservers',
                        'allocation_pools', 'host_routes', 'ip_version',
                        'gateway_ip')

        return (headers,
                (utils.get_item_properties(
                    s, columns,
                    formatters=_formatters,
                ) for s in data))


class ShowSubnet(command.ShowOne):
    """Show subnet details"""

    def get_parser(self, prog_name):
        parser = super(ShowSubnet, self).get_parser(prog_name)
        parser.add_argument(
            'subnet',
            metavar="<subnet>",
            help="Subnet to show (name or ID)",
        )
        return parser

    def take_action(self, parsed_args):
        obj = self.app.client_manager.network.find_subnet(parsed_args.subnet,
                                                          ignore_missing=False)
        columns = _get_columns(obj)
        data = utils.get_item_properties(obj, columns, formatters=_formatters)
        return (columns, data)