1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 from snaps import file_utils
18 from snaps.openstack.create_flavor import FlavorSettings
19 from snaps.openstack.create_instance import (
20 VmInstanceSettings, FloatingIpSettings)
21 from snaps.openstack.create_keypairs import KeypairSettings
22 from snaps.openstack.create_network import (
23 PortSettings, SubnetSettings, NetworkSettings)
24 from snaps.openstack.create_volume import VolumeSettings
25 from snaps.openstack.create_volume_type import (
26 VolumeTypeSettings, VolumeTypeEncryptionSettings, ControlLocation)
27 from snaps.openstack.utils import (
28 neutron_utils, nova_utils, heat_utils, glance_utils)
31 def create_network_settings(neutron, network):
33 Returns a NetworkSettings object
34 :param neutron: the neutron client
35 :param network: a SNAPS-OO Network domain object
38 return NetworkSettings(
39 name=network.name, network_type=network.type,
40 subnet_settings=create_subnet_settings(neutron, network))
43 def create_subnet_settings(neutron, network):
45 Returns a list of SubnetSettings objects for a given network
46 :param neutron: the OpenStack neutron client
47 :param network: the SNAPS-OO Network domain object
52 subnets = neutron_utils.get_subnets_by_network(neutron, network)
53 for subnet in subnets:
55 kwargs['cidr'] = subnet.cidr
56 kwargs['ip_version'] = subnet.ip_version
57 kwargs['name'] = subnet.name
58 kwargs['start'] = subnet.start
59 kwargs['end'] = subnet.end
60 kwargs['gateway_ip'] = subnet.gateway_ip
61 kwargs['enable_dhcp'] = subnet.enable_dhcp
62 kwargs['dns_nameservers'] = subnet.dns_nameservers
63 kwargs['host_routes'] = subnet.host_routes
64 kwargs['ipv6_ra_mode'] = subnet.ipv6_ra_mode
65 kwargs['ipv6_address_mode'] = subnet.ipv6_address_mode
66 out.append(SubnetSettings(**kwargs))
70 def create_volume_settings(volume):
72 Returns a VolumeSettings object
73 :param volume: a SNAPS-OO Volume object
76 return VolumeSettings(
77 name=volume.name, description=volume.description,
78 size=volume.size, type_name=volume.type,
79 availability_zone=volume.availability_zone,
80 multi_attach=volume.multi_attach)
83 def create_volume_type_settings(volume_type):
85 Returns a VolumeTypeSettings object
86 :param volume_type: a SNAPS-OO VolumeType object
90 if volume_type.encryption:
91 if (volume_type.encryption.control_location
92 == ControlLocation.front_end.value):
93 control = ControlLocation.front_end
95 control = ControlLocation.back_end
97 encrypt_settings = VolumeTypeEncryptionSettings(
98 name=volume_type.encryption.__class__,
99 provider_class=volume_type.encryption.provider,
100 control_location=control,
101 cipher=volume_type.encryption.cipher,
102 key_size=volume_type.encryption.key_size)
105 if volume_type.qos_spec:
106 qos_spec_name = volume_type.qos_spec.name
108 return VolumeTypeSettings(
109 name=volume_type.name, encryption=encrypt_settings,
110 qos_spec_name=qos_spec_name, public=volume_type.public)
113 def create_flavor_settings(flavor):
115 Returns a VolumeSettings object
116 :param flavor: a SNAPS-OO Volume object
118 return FlavorSettings(
119 name=flavor.name, flavor_id=flavor.id, ram=flavor.ram,
120 disk=flavor.disk, vcpus=flavor.vcpus, ephemeral=flavor.ephemeral,
121 swap=flavor.swap, rxtx_factor=flavor.rxtx_factor,
122 is_public=flavor.is_public)
125 def create_keypair_settings(heat_cli, stack, keypair, pk_output_key):
127 Instantiates a KeypairSettings object from a Keypair domain objects
128 :param heat_cli: the heat client
129 :param stack: the Stack domain object
130 :param keypair: the Keypair SNAPS domain object
131 :param pk_output_key: the key to the heat template's outputs for retrieval
132 of the private key file
133 :return: a KeypairSettings object
136 outputs = heat_utils.get_outputs(heat_cli, stack)
137 for output in outputs:
138 if output.key == pk_output_key:
141 key_file = file_utils.save_string_to_file(
142 output.value, str(guid), 0o400)
144 # Use outputs, file and resources for the KeypairSettings
145 return KeypairSettings(
146 name=keypair.name, private_filepath=key_file.name)
148 return KeypairSettings(name=keypair.name)
151 def create_vm_inst_settings(nova, neutron, server):
153 Returns a NetworkSettings object
154 :param nova: the nova client
155 :param neutron: the neutron client
156 :param server: a SNAPS-OO VmInst domain object
160 flavor_name = nova_utils.get_flavor_by_id(nova, server.flavor_id)
163 kwargs['name'] = server.name
164 kwargs['flavor'] = flavor_name
165 kwargs['port_settings'] = __create_port_settings(
166 neutron, server.networks)
167 kwargs['security_group_names'] = server.sec_grp_names
168 kwargs['floating_ip_settings'] = __create_floatingip_settings(
169 neutron, kwargs['port_settings'])
171 return VmInstanceSettings(**kwargs)
174 def __create_port_settings(neutron, networks):
176 Returns a list of port settings based on the networks parameter
177 :param neutron: the neutron client
178 :param networks: a dict where the key is the network name and the value
179 is a list of IP addresses
184 for net_name, ips in networks.items():
185 network = neutron_utils.get_network(neutron, network_name=net_name)
186 ports = neutron_utils.get_ports(neutron, network, ips)
190 kwargs['name'] = port.name
191 kwargs['network_name'] = network.name
192 kwargs['mac_address'] = port.mac_address
193 kwargs['allowed_address_pairs'] = port.allowed_address_pairs
194 kwargs['admin_state_up'] = port.admin_state_up
195 out.append(PortSettings(**kwargs))
200 def __create_floatingip_settings(neutron, port_settings):
202 Returns a list of FloatingIPSettings objects as they pertain to an
203 existing deployed server instance
204 :param neutron: the neutron client
205 :param port_settings: list of SNAPS-OO PortSettings objects
206 :return: a list of FloatingIPSettings objects or an empty list if no
207 floating IPs have been created
209 base_fip_name = 'fip-'
214 for port_setting in port_settings:
215 setting_port = neutron_utils.get_port(neutron, port_setting)
217 network = neutron_utils.get_network(
218 neutron, network_name=port_setting.network_name)
219 network_ports = neutron_utils.get_ports(neutron, network)
221 for setting_port in network_ports:
222 if port_setting.mac_address == setting_port.mac_address:
223 fip_ports.append((port_setting.name, setting_port))
226 floating_ips = neutron_utils.get_floating_ips(neutron, fip_ports)
228 for port_id, floating_ip in floating_ips:
229 router = neutron_utils.get_router_by_id(neutron, floating_ip.router_id)
230 setting_port = neutron_utils.get_port_by_id(
231 neutron, floating_ip.port_id)
233 kwargs['name'] = base_fip_name + str(fip_ctr)
234 kwargs['port_name'] = setting_port.name
235 kwargs['port_id'] = setting_port.id
236 kwargs['router_name'] = router.name
239 for ip_dict in setting_port.ips:
240 if ('ip_address' in ip_dict and
241 'subnet_id' in ip_dict and
242 ip_dict['ip_address'] == floating_ip.fixed_ip_address):
243 subnet = neutron_utils.get_subnet_by_id(
244 neutron, ip_dict['subnet_id'])
246 kwargs['subnet_name'] = subnet.name
248 out.append(FloatingIpSettings(**kwargs))
255 def determine_image_settings(glance, server, image_settings):
257 Returns a ImageSettings object from the list that matches the name in one
258 of the image_settings parameter
259 :param glance: the glance client
260 :param server: a SNAPS-OO VmInst domain object
261 :param image_settings: list of ImageSettings objects
262 :return: ImageSettings or None
265 for image_setting in image_settings:
266 image = glance_utils.get_image_by_id(glance, server.image_id)
267 if image and image.name == image_setting.name:
271 def determine_keypair_settings(heat_cli, stack, server, keypair_settings=None,
274 Returns a KeypairSettings object from the list that matches the
275 server.keypair_name value in the keypair_settings parameter if not None,
276 else if the output_key is not None, the output's value when contains the
277 string 'BEGIN RSA PRIVATE KEY', this value will be stored into a file and
278 encoded into the KeypairSettings object returned
279 :param heat_cli: the OpenStack heat client
280 :param stack: a SNAPS-OO Stack domain object
281 :param server: a SNAPS-OO VmInst domain object
282 :param keypair_settings: list of KeypairSettings objects
283 :param priv_key_key: the stack options that holds the private key value
284 :return: KeypairSettings or None
286 # Existing keypair being used by Heat Template
288 for keypair_setting in keypair_settings:
289 if server.keypair_name == keypair_setting.name:
290 return keypair_setting
292 # Keypair created by Heat template
294 outputs = heat_utils.get_outputs(heat_cli, stack)
295 for output in outputs:
296 if output.key == priv_key_key:
299 key_file = file_utils.save_string_to_file(
300 output.value, str(guid), 0o400)
302 # Use outputs, file and resources for the KeypairSettings
303 return KeypairSettings(
304 name=server.keypair_name, private_filepath=key_file.name)