Refactoring of RouterSettings to extend RouterConfig
[snaps.git] / snaps / openstack / utils / settings_utils.py
index 2ab3c28..e85bd08 100644 (file)
 import uuid
 
 from snaps import file_utils
-from snaps.openstack.create_flavor import FlavorSettings
+from snaps.config.flavor import FlavorConfig
+from snaps.config.keypair import KeypairConfig
+from snaps.config.router import RouterConfig
 from snaps.openstack.create_instance import (
     VmInstanceSettings, FloatingIpSettings)
-from snaps.openstack.create_keypairs import KeypairSettings
 from snaps.openstack.create_network import (
     PortSettings, SubnetSettings, NetworkSettings)
+from snaps.openstack.create_security_group import (
+    SecurityGroupSettings, SecurityGroupRuleSettings)
 from snaps.openstack.create_volume import VolumeSettings
 from snaps.openstack.create_volume_type import (
     VolumeTypeSettings, VolumeTypeEncryptionSettings, ControlLocation)
@@ -40,6 +43,30 @@ def create_network_settings(neutron, network):
         subnet_settings=create_subnet_settings(neutron, network))
 
 
+def create_security_group_settings(neutron, security_group):
+    """
+    Returns a NetworkSettings object
+    :param neutron: the neutron client
+    :param security_group: a SNAPS-OO SecurityGroup domain object
+    :return:
+    """
+    rules = neutron_utils.get_rules_by_security_group(neutron, security_group)
+
+    rule_settings = list()
+    for rule in rules:
+        rule_settings.append(SecurityGroupRuleSettings(
+            sec_grp_name=security_group.name, description=rule.description,
+            direction=rule.direction, ethertype=rule.ethertype,
+            port_range_min=rule.port_range_min,
+            port_range_max=rule.port_range_max, protocol=rule.protocol,
+            remote_group_id=rule.remote_group_id,
+            remote_ip_prefix=rule.remote_ip_prefix))
+
+    return SecurityGroupSettings(
+        name=security_group.name, description=security_group.description,
+        rule_settings=rule_settings)
+
+
 def create_subnet_settings(neutron, network):
     """
     Returns a list of SubnetSettings objects for a given network
@@ -67,6 +94,60 @@ def create_subnet_settings(neutron, network):
     return out
 
 
+def create_router_settings(neutron, router):
+    """
+    Returns a RouterConfig object
+    :param neutron: the neutron client
+    :param router: a SNAPS-OO Router domain object
+    :return:
+    """
+    ext_net_name = None
+
+    if router.external_network_id:
+        network = neutron_utils.get_network_by_id(
+            neutron, router.external_network_id)
+        if network:
+            ext_net_name = network.name
+
+    ports_tuple_list = list()
+    if router.port_subnets:
+        for port, subnets in router.port_subnets:
+            network = neutron_utils.get_network_by_id(
+                neutron, port.network_id)
+
+            ip_addrs = list()
+            if network and router.external_fixed_ips:
+                for ext_fixed_ips in router.external_fixed_ips:
+                    for subnet in subnets:
+                        if ext_fixed_ips['subnet_id'] == subnet.id:
+                            ip_addrs.append(ext_fixed_ips['ip_address'])
+            else:
+                for ip in port.ips:
+                    ip_addrs.append(ip)
+
+            ip_list = list()
+            if len(ip_addrs) > 0:
+                for ip_addr in ip_addrs:
+                    if isinstance(ip_addr, dict):
+                        ip_list.append(ip_addr['ip_address'])
+                    else:
+                        ip_list.append(ip_addr)
+
+            ports_tuple_list.append((network, ip_list))
+
+    port_settings = __create_port_settings(neutron, ports_tuple_list)
+
+    filtered_settings = list()
+    for port_setting in port_settings:
+        if port_setting.network_name != ext_net_name:
+            filtered_settings.append(port_setting)
+
+    return RouterConfig(
+        name=router.name, external_gateway=ext_net_name,
+        admin_state_up=router.admin_state_up,
+        port_settings=filtered_settings)
+
+
 def create_volume_settings(volume):
     """
     Returns a VolumeSettings object
@@ -110,12 +191,12 @@ def create_volume_type_settings(volume_type):
         qos_spec_name=qos_spec_name, public=volume_type.public)
 
 
-def create_flavor_settings(flavor):
+def create_flavor_config(flavor):
     """
     Returns a VolumeSettings object
     :param flavor: a SNAPS-OO Volume object
     """
-    return FlavorSettings(
+    return FlavorConfig(
         name=flavor.name, flavor_id=flavor.id, ram=flavor.ram,
         disk=flavor.disk, vcpus=flavor.vcpus, ephemeral=flavor.ephemeral,
         swap=flavor.swap, rxtx_factor=flavor.rxtx_factor,
@@ -124,13 +205,13 @@ def create_flavor_settings(flavor):
 
 def create_keypair_settings(heat_cli, stack, keypair, pk_output_key):
     """
-    Instantiates a KeypairSettings object from a Keypair domain objects
+    Instantiates a KeypairConfig object from a Keypair domain objects
     :param heat_cli: the heat client
     :param stack: the Stack domain object
     :param keypair: the Keypair SNAPS domain object
     :param pk_output_key: the key to the heat template's outputs for retrieval
                           of the private key file
-    :return: a KeypairSettings object
+    :return: a KeypairConfig object
     """
     if pk_output_key:
         outputs = heat_utils.get_outputs(heat_cli, stack)
@@ -141,11 +222,11 @@ def create_keypair_settings(heat_cli, stack, keypair, pk_output_key):
                 key_file = file_utils.save_string_to_file(
                     output.value, str(guid), 0o400)
 
-                # Use outputs, file and resources for the KeypairSettings
-                return KeypairSettings(
+                # Use outputs, file and resources for the KeypairConfig
+                return KeypairConfig(
                     name=keypair.name, private_filepath=key_file.name)
 
-    return KeypairSettings(name=keypair.name)
+    return KeypairConfig(name=keypair.name)
 
 
 def create_vm_inst_settings(nova, neutron, server):
@@ -162,8 +243,15 @@ def create_vm_inst_settings(nova, neutron, server):
     kwargs = dict()
     kwargs['name'] = server.name
     kwargs['flavor'] = flavor_name
+
+    net_tuples = list()
+    for net_name, ips in server.networks.items():
+        network = neutron_utils.get_network(neutron, network_name=net_name)
+        if network:
+            net_tuples.append((network, ips))
+
     kwargs['port_settings'] = __create_port_settings(
-        neutron, server.networks)
+        neutron, net_tuples)
     kwargs['security_group_names'] = server.sec_grp_names
     kwargs['floating_ip_settings'] = __create_floatingip_settings(
         neutron, kwargs['port_settings'])
@@ -175,24 +263,32 @@ def __create_port_settings(neutron, networks):
     """
     Returns a list of port settings based on the networks parameter
     :param neutron: the neutron client
-    :param networks: a dict where the key is the network name and the value
-                     is a list of IP addresses
+    :param networks: a list of tuples where #1 is the SNAPS Network domain
+                     object and #2 is a list of IP addresses
     :return:
     """
     out = list()
 
-    for net_name, ips in networks.items():
-        network = neutron_utils.get_network(neutron, network_name=net_name)
+    for network, ips in networks:
         ports = neutron_utils.get_ports(neutron, network, ips)
         for port in ports:
-            kwargs = dict()
-            if port.name:
-                kwargs['name'] = port.name
-            kwargs['network_name'] = network.name
-            kwargs['mac_address'] = port.mac_address
-            kwargs['allowed_address_pairs'] = port.allowed_address_pairs
-            kwargs['admin_state_up'] = port.admin_state_up
-            out.append(PortSettings(**kwargs))
+            if port.device_owner != 'network:dhcp':
+                ip_addrs = list()
+                for ip_dict in port.ips:
+                    subnet = neutron_utils.get_subnet_by_id(
+                        neutron, ip_dict['subnet_id'])
+                    ip_addrs.append({'subnet_name': subnet.name,
+                                     'ip': ip_dict['ip_address']})
+
+                kwargs = dict()
+                if port.name:
+                    kwargs['name'] = port.name
+                kwargs['network_name'] = network.name
+                kwargs['mac_address'] = port.mac_address
+                kwargs['allowed_address_pairs'] = port.allowed_address_pairs
+                kwargs['admin_state_up'] = port.admin_state_up
+                kwargs['ip_addrs'] = ip_addrs
+                out.append(PortSettings(**kwargs))
 
     return out
 
@@ -252,14 +348,14 @@ def __create_floatingip_settings(neutron, port_settings):
     return out
 
 
-def determine_image_settings(glance, server, image_settings):
+def determine_image_config(glance, server, image_settings):
     """
-    Returns a ImageSettings object from the list that matches the name in one
+    Returns a ImageConfig object from the list that matches the name in one
     of the image_settings parameter
     :param glance: the glance client
     :param server: a SNAPS-OO VmInst domain object
-    :param image_settings: list of ImageSettings objects
-    :return: ImageSettings or None
+    :param image_settings: list of ImageConfig objects
+    :return: ImageConfig or None
     """
     if image_settings:
         for image_setting in image_settings:
@@ -268,20 +364,20 @@ def determine_image_settings(glance, server, image_settings):
                 return image_setting
 
 
-def determine_keypair_settings(heat_cli, stack, server, keypair_settings=None,
-                               priv_key_key=None):
+def determine_keypair_config(heat_cli, stack, server, keypair_settings=None,
+                             priv_key_key=None):
     """
-    Returns a KeypairSettings object from the list that matches the
+    Returns a KeypairConfig object from the list that matches the
     server.keypair_name value in the keypair_settings parameter if not None,
     else if the output_key is not None, the output's value when contains the
     string 'BEGIN RSA PRIVATE KEY', this value will be stored into a file and
-    encoded into the KeypairSettings object returned
+    encoded into the KeypairConfig object returned
     :param heat_cli: the OpenStack heat client
     :param stack: a SNAPS-OO Stack domain object
     :param server: a SNAPS-OO VmInst domain object
-    :param keypair_settings: list of KeypairSettings objects
+    :param keypair_settings: list of KeypairConfig objects
     :param priv_key_key: the stack options that holds the private key value
-    :return: KeypairSettings or None
+    :return: KeypairConfig or None
     """
     # Existing keypair being used by Heat Template
     if keypair_settings:
@@ -299,6 +395,6 @@ def determine_keypair_settings(heat_cli, stack, server, keypair_settings=None,
                 key_file = file_utils.save_string_to_file(
                     output.value, str(guid), 0o400)
 
-                # Use outputs, file and resources for the KeypairSettings
-                return KeypairSettings(
+                # Use outputs, file and resources for the KeypairConfig
+                return KeypairConfig(
                     name=server.keypair_name, private_filepath=key_file.name)