Debian Codegraph @

networking_l2gw/l2gatewayclient/l2gw_client_ext/_l2_gateway.py

raw
1 # Copyright 2015 OpenStack Foundation.
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 from neutronclient.common import extension
18 from neutronclient.common import utils
19 from oslo_serialization import jsonutils
20
21 from networking_l2gw._i18n import _
22
23 INTERFACE_DELIMITER = ";"
24 SEGMENTATION_ID_DELIMITER = "#"
25 INTERFACE_SEG_ID_DELIMITER = "|"
26
27
28 def _format_devices(l2_gateway):
29 try:
30 return '\n'.join([jsonutils.dumps(gateway) for gateway in
31 l2_gateway['devices']])
32 except (TypeError, KeyError):
33 return ''
34
35
36 class L2Gateway(extension.NeutronClientExtension):
37 resource = 'l2_gateway'
38 resource_plural = 'l2_gateways'
39 path = 'l2-gateways'
40 object_path = '/%s' % path
41 resource_path = '/%s/%%s' % path
42 versions = ['2.0']
43
44
45 def get_interface(interfaces):
46 interface_dict = []
47 for interface in interfaces:
48 if INTERFACE_SEG_ID_DELIMITER in interface:
49 int_name = interface.split(INTERFACE_SEG_ID_DELIMITER)[0]
50 segid = interface.split(INTERFACE_SEG_ID_DELIMITER)[1]
51 if SEGMENTATION_ID_DELIMITER in segid:
52 segid = segid.split(SEGMENTATION_ID_DELIMITER)
53 else:
54 segid = [segid]
55 interface_detail = {'name': int_name, 'segmentation_id': segid}
56 else:
57 interface_detail = {'name': interface}
58 interface_dict.append(interface_detail)
59 return interface_dict
60
61
62 def add_known_arguments(self, parser):
63 parser.add_argument(
64 '--device',
65 metavar='name=name,interface_names=INTERFACE-DETAILS',
66 action='append', dest='devices', type=utils.str2dict,
67 help=_('Device name and Interface-names of l2gateway. '
68 'INTERFACE-DETAILS is of form '
69 '\"<interface_name1>;[<interface_name2>]'
70 '[|<seg_id1>[#<seg_id2>]]\" '
71 '(--device option can be repeated)'))
72
73
74 def args2body(self, parsed_args):
75 if parsed_args.devices:
76 devices = parsed_args.devices
77 interfaces = []
78 else:
79 devices = []
80 device_dict = []
81 for device in devices:
82 if 'interface_names' in device.keys():
83 interface = device['interface_names']
84 if INTERFACE_DELIMITER in interface:
85 interface_dict = interface.split(INTERFACE_DELIMITER)
86 interfaces = get_interface(interface_dict)
87 else:
88 interfaces = get_interface([interface])
89 if 'name' in device.keys():
90 device = {'device_name': device['name'],
91 'interfaces': interfaces}
92 else:
93 device = {'interfaces': interfaces}
94 device_dict.append(device)
95 if parsed_args.name:
96 l2gw_name = parsed_args.name
97 body = {'l2_gateway': {'name': l2gw_name,
98 'devices': device_dict}, }
99 else:
100 body = {'l2_gateway': {'devices': device_dict}, }
101 return body
102
103
104 class L2GatewayCreate(extension.ClientExtensionCreate, L2Gateway):
105 """Create l2gateway information."""
106
107 shell_command = 'l2-gateway-create'
108
109 def add_known_arguments(self, parser):
110 parser.add_argument(
111 'name', metavar='<GATEWAY-NAME>',
112 help=_('Descriptive name for logical gateway.'))
113 add_known_arguments(self, parser)
114
115 def args2body(self, parsed_args):
116 body = args2body(self, parsed_args)
117 if parsed_args.tenant_id:
118 body['l2_gateway']['tenant_id'] = parsed_args.tenant_id
119 return body
120
121
122 class L2GatewayList(extension.ClientExtensionList, L2Gateway):
123 """List l2gateway that belongs to a given tenant."""
124
125 shell_command = 'l2-gateway-list'
126 _formatters = {'devices': _format_devices, }
127 list_columns = ['id', 'name', 'devices']
128 pagination_support = True
129 sorting_support = True
130
131
132 class L2GatewayShow(extension.ClientExtensionShow, L2Gateway):
133 """Show information of a given l2gateway."""
134
135 shell_command = 'l2-gateway-show'
136
137
138 class L2GatewayDelete(extension.ClientExtensionDelete, L2Gateway):
139 """Delete a given l2gateway."""
140
141 shell_command = 'l2-gateway-delete'
142
143
144 class L2GatewayUpdate(extension.ClientExtensionUpdate, L2Gateway):
145 """Update a given l2gateway."""
146
147 shell_command = 'l2-gateway-update'
148
149 def add_known_arguments(self, parser):
150 parser.add_argument(
151 '--name', metavar='name',
152 help=_('Descriptive name for logical gateway.'))
153 add_known_arguments(self, parser)
154
155 def args2body(self, parsed_args):
156 if parsed_args.devices:
157 body = args2body(self, parsed_args)
158 else:
159 body = {'l2_gateway': {'name': parsed_args.name}}
160 return body