Ensure project IDs are handled correctly for Network/Subnets
[snaps.git] / snaps / openstack / utils / settings_utils.py
1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 #                    and others.  All rights reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain 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,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 import uuid
16
17 from snaps import file_utils
18 from snaps.config.flavor import FlavorConfig
19 from snaps.config.keypair import KeypairConfig
20 from snaps.config.network import SubnetConfig, PortConfig, NetworkConfig
21 from snaps.config.router import RouterConfig
22 from snaps.config.security_group import (
23     SecurityGroupRuleConfig, SecurityGroupConfig)
24 from snaps.config.vm_inst import VmInstanceConfig, FloatingIpConfig
25 from snaps.config.volume import VolumeConfig
26 from snaps.config.volume_type import (
27     ControlLocation,  VolumeTypeEncryptionConfig, VolumeTypeConfig)
28 from snaps.openstack.utils import (
29     neutron_utils, nova_utils, heat_utils, glance_utils)
30
31
32 def create_network_config(neutron, network):
33     """
34     Returns a NetworkConfig object
35     :param neutron: the neutron client
36     :param network: a SNAPS-OO Network domain object
37     :return:
38     """
39     return NetworkConfig(
40         name=network.name, network_type=network.type,
41         subnet_settings=create_subnet_config(neutron, network))
42
43
44 def create_security_group_config(neutron, security_group):
45     """
46     Returns a SecurityGroupConfig object
47     :param neutron: the neutron client
48     :param security_group: a SNAPS-OO SecurityGroup domain object
49     :return:
50     """
51     rules = neutron_utils.get_rules_by_security_group(neutron, security_group)
52
53     rule_settings = list()
54     for rule in rules:
55         rule_settings.append(SecurityGroupRuleConfig(
56             sec_grp_name=security_group.name, description=rule.description,
57             direction=rule.direction, ethertype=rule.ethertype,
58             port_range_min=rule.port_range_min,
59             port_range_max=rule.port_range_max, protocol=rule.protocol,
60             remote_group_id=rule.remote_group_id,
61             remote_ip_prefix=rule.remote_ip_prefix))
62
63     return SecurityGroupConfig(
64         name=security_group.name, description=security_group.description,
65         rule_settings=rule_settings)
66
67
68 def create_subnet_config(neutron, network):
69     """
70     Returns a list of SubnetConfig objects for a given network
71     :param neutron: the OpenStack neutron client
72     :param network: the SNAPS-OO Network domain object
73     :return: a list
74     """
75     out = list()
76
77     subnets = neutron_utils.get_subnets_by_network(neutron, network)
78     for subnet in subnets:
79         kwargs = dict()
80         kwargs['cidr'] = subnet.cidr
81         kwargs['ip_version'] = subnet.ip_version
82         kwargs['name'] = subnet.name
83         kwargs['start'] = subnet.start
84         kwargs['end'] = subnet.end
85         kwargs['gateway_ip'] = subnet.gateway_ip
86         kwargs['enable_dhcp'] = subnet.enable_dhcp
87         kwargs['dns_nameservers'] = subnet.dns_nameservers
88         kwargs['host_routes'] = subnet.host_routes
89         kwargs['ipv6_ra_mode'] = subnet.ipv6_ra_mode
90         kwargs['ipv6_address_mode'] = subnet.ipv6_address_mode
91         out.append(SubnetConfig(**kwargs))
92     return out
93
94
95 def create_router_config(neutron, router):
96     """
97     Returns a RouterConfig object
98     :param neutron: the neutron client
99     :param router: a SNAPS-OO Router domain object
100     :return:
101     """
102     ext_net_name = None
103
104     if router.external_network_id:
105         network = neutron_utils.get_network_by_id(
106             neutron, router.external_network_id)
107         if network:
108             ext_net_name = network.name
109
110     out_ports = list()
111     if router.port_subnets:
112         for port, subnets in router.port_subnets:
113             network = neutron_utils.get_network_by_id(
114                 neutron, port.network_id)
115
116             ip_addrs = list()
117             if network and router.external_fixed_ips:
118                 for ext_fixed_ips in router.external_fixed_ips:
119                     for subnet in subnets:
120                         if ext_fixed_ips['subnet_id'] == subnet.id:
121                             ip_addrs.append(ext_fixed_ips['ip_address'])
122             else:
123                 for ip in port.ips:
124                     ip_addrs.append(ip['ip_address'])
125
126             ports = neutron_utils.get_ports(neutron, network, ip_addrs)
127             for out_port in ports:
128                 out_ports.append(out_port)
129
130     port_settings = __create_port_configs(neutron, out_ports)
131
132     filtered_settings = list()
133     for port_setting in port_settings:
134         if port_setting.network_name != ext_net_name:
135             filtered_settings.append(port_setting)
136
137     return RouterConfig(
138         name=router.name, external_gateway=ext_net_name,
139         admin_state_up=router.admin_state_up,
140         port_settings=filtered_settings)
141
142
143 def create_volume_config(volume):
144     """
145     Returns a VolumeConfig object
146     :param volume: a SNAPS-OO Volume object
147     """
148
149     return VolumeConfig(
150         name=volume.name, description=volume.description,
151         size=volume.size, type_name=volume.type,
152         availability_zone=volume.availability_zone,
153         multi_attach=volume.multi_attach)
154
155
156 def create_volume_type_config(volume_type):
157     """
158     Returns a VolumeTypeConfig object
159     :param volume_type: a SNAPS-OO VolumeType object
160     """
161
162     control = None
163     if volume_type.encryption:
164         if (volume_type.encryption.control_location
165                 == ControlLocation.front_end.value):
166             control = ControlLocation.front_end
167         else:
168             control = ControlLocation.back_end
169
170     if volume_type and volume_type.encryption:
171         encrypt_settings = VolumeTypeEncryptionConfig(
172             name=volume_type.encryption.__class__,
173             provider_class=volume_type.encryption.provider,
174             control_location=control,
175             cipher=volume_type.encryption.cipher,
176             key_size=volume_type.encryption.key_size)
177     else:
178         encrypt_settings = None
179
180     qos_spec_name = None
181     if volume_type.qos_spec:
182         qos_spec_name = volume_type.qos_spec.name
183
184     return VolumeTypeConfig(
185         name=volume_type.name, encryption=encrypt_settings,
186         qos_spec_name=qos_spec_name, public=volume_type.public)
187
188
189 def create_flavor_config(flavor):
190     """
191     Returns a FlavorConfig object
192     :param flavor: a FlavorConfig object
193     """
194     return FlavorConfig(
195         name=flavor.name, flavor_id=flavor.id, ram=flavor.ram,
196         disk=flavor.disk, vcpus=flavor.vcpus, ephemeral=flavor.ephemeral,
197         swap=flavor.swap, rxtx_factor=flavor.rxtx_factor,
198         is_public=flavor.is_public)
199
200
201 def create_keypair_config(heat_cli, stack, keypair, pk_output_key):
202     """
203     Instantiates a KeypairConfig object from a Keypair domain objects
204     :param heat_cli: the heat client
205     :param stack: the Stack domain object
206     :param keypair: the Keypair SNAPS domain object
207     :param pk_output_key: the key to the heat template's outputs for retrieval
208                           of the private key file
209     :return: a KeypairConfig object
210     """
211     if pk_output_key:
212         outputs = heat_utils.get_outputs(heat_cli, stack)
213         for output in outputs:
214             if output.key == pk_output_key:
215                 # Save to file
216                 guid = uuid.uuid4()
217                 key_file = file_utils.save_string_to_file(
218                     output.value, str(guid), 0o400)
219
220                 # Use outputs, file and resources for the KeypairConfig
221                 return KeypairConfig(
222                     name=keypair.name, private_filepath=key_file.name)
223
224     return KeypairConfig(name=keypair.name)
225
226
227 def create_vm_inst_config(nova, neutron, server, project_id):
228     """
229     Returns a VmInstanceConfig object
230     note: if the server instance is not active, the PortSettings objects will
231     not be generated resulting in an invalid configuration
232     :param nova: the nova client
233     :param neutron: the neutron client
234     :param server: a SNAPS-OO VmInst domain object
235     :param project_id: the associated project ID
236     :return:
237     """
238
239     flavor_name = nova_utils.get_flavor_by_id(nova, server.flavor_id)
240
241     kwargs = dict()
242     kwargs['name'] = server.name
243     kwargs['flavor'] = flavor_name
244
245     kwargs['port_settings'] = __create_port_configs(neutron, server.ports)
246     kwargs['security_group_names'] = server.sec_grp_names
247     kwargs['floating_ip_settings'] = __create_floatingip_config(
248         neutron, kwargs['port_settings'], project_id)
249
250     return VmInstanceConfig(**kwargs)
251
252
253 def __create_port_configs(neutron, ports):
254     """
255     Returns a list of PortConfig objects based on the networks parameter
256     :param neutron: the neutron client
257     :param ports: a list of SNAPS-OO Port domain objects
258     :return:
259     """
260     out = list()
261
262     for port in ports:
263         if port.device_owner != 'network:dhcp':
264             ip_addrs = list()
265             for ip_dict in port.ips:
266                 subnet = neutron_utils.get_subnet_by_id(
267                     neutron, ip_dict['subnet_id'])
268                 ip_addrs.append({'subnet_name': subnet.name,
269                                  'ip': ip_dict['ip_address']})
270
271             network = neutron_utils.get_network_by_id(neutron, port.network_id)
272             kwargs = dict()
273             if port.name:
274                 kwargs['name'] = port.name
275             kwargs['network_name'] = network.name
276             kwargs['mac_address'] = port.mac_address
277             kwargs['allowed_address_pairs'] = port.allowed_address_pairs
278             kwargs['admin_state_up'] = port.admin_state_up
279             kwargs['ip_addrs'] = ip_addrs
280             out.append(PortConfig(**kwargs))
281
282     return out
283
284
285 def __create_floatingip_config(neutron, port_settings, project_id):
286     """
287     Returns a list of FloatingIpConfig objects as they pertain to an
288     existing deployed server instance
289     :param neutron: the neutron client
290     :param port_settings: list of SNAPS-OO PortConfig objects
291     :return: a list of FloatingIpConfig objects or an empty list if no
292              floating IPs have been created
293     """
294     base_fip_name = 'fip-'
295     fip_ctr = 1
296     out = list()
297
298     fip_ports = list()
299     for port_setting in port_settings:
300         setting_port = neutron_utils.get_port(
301             neutron, port_setting, project_id=project_id)
302         if setting_port:
303             network = neutron_utils.get_network(
304                 neutron, network_name=port_setting.network_name)
305             network_ports = neutron_utils.get_ports(neutron, network)
306             if network_ports:
307                 for setting_port in network_ports:
308                     if port_setting.mac_address == setting_port.mac_address:
309                         fip_ports.append((port_setting.name, setting_port))
310                         break
311
312     floating_ips = neutron_utils.get_port_floating_ips(neutron, fip_ports)
313
314     for port_id, floating_ip in floating_ips:
315         router = neutron_utils.get_router_by_id(neutron, floating_ip.router_id)
316         setting_port = neutron_utils.get_port_by_id(
317             neutron, floating_ip.port_id)
318         kwargs = dict()
319         kwargs['name'] = base_fip_name + str(fip_ctr)
320         kwargs['port_name'] = setting_port.name
321         kwargs['port_id'] = setting_port.id
322         kwargs['router_name'] = router.name
323
324         if setting_port:
325             for ip_dict in setting_port.ips:
326                 if ('ip_address' in ip_dict and
327                         'subnet_id' in ip_dict and
328                         ip_dict['ip_address'] == floating_ip.fixed_ip_address):
329                     subnet = neutron_utils.get_subnet_by_id(
330                         neutron, ip_dict['subnet_id'])
331                     if subnet:
332                         kwargs['subnet_name'] = subnet.name
333
334         out.append(FloatingIpConfig(**kwargs))
335
336         fip_ctr += 1
337
338     return out
339
340
341 def determine_image_config(glance, server, image_settings):
342     """
343     Returns a ImageConfig object from the list that matches the name in one
344     of the image_settings parameter
345     :param glance: the glance client
346     :param server: a SNAPS-OO VmInst domain object
347     :param image_settings: list of ImageConfig objects
348     :return: ImageConfig or None
349     """
350     if image_settings:
351         for image_setting in image_settings:
352             image = glance_utils.get_image_by_id(glance, server.image_id)
353             if image and image.name == image_setting.name:
354                 return image_setting
355
356
357 def determine_keypair_config(heat_cli, stack, server, keypair_settings=None,
358                              priv_key_key=None):
359     """
360     Returns a KeypairConfig object from the list that matches the
361     server.keypair_name value in the keypair_settings parameter if not None,
362     else if the output_key is not None, the output's value when contains the
363     string 'BEGIN RSA PRIVATE KEY', this value will be stored into a file and
364     encoded into the KeypairConfig object returned
365     :param heat_cli: the OpenStack heat client
366     :param stack: a SNAPS-OO Stack domain object
367     :param server: a SNAPS-OO VmInst domain object
368     :param keypair_settings: list of KeypairConfig objects
369     :param priv_key_key: the stack options that holds the private key value
370     :return: KeypairConfig or None
371     """
372     # Existing keypair being used by Heat Template
373     if keypair_settings:
374         for keypair_setting in keypair_settings:
375             if server.keypair_name == keypair_setting.name:
376                 return keypair_setting
377
378     # Keypair created by Heat template
379     if priv_key_key:
380         outputs = heat_utils.get_outputs(heat_cli, stack)
381         for output in outputs:
382             if output.key == priv_key_key:
383                 # Save to file
384                 guid = uuid.uuid4()
385                 key_file = file_utils.save_string_to_file(
386                     output.value, str(guid), 0o400)
387
388                 # Use outputs, file and resources for the KeypairConfig
389                 return KeypairConfig(
390                     name=server.keypair_name, private_filepath=key_file.name)