Refactoring of RouterSettings to extend RouterConfig
[snaps.git] / snaps / openstack / create_router.py
index 6da5f8e..bf68347 100644 (file)
 import logging
 
 from neutronclient.common.exceptions import NotFound
-from snaps.openstack.create_network import PortSettings
+
+from snaps.config.router import RouterConfig
 from snaps.openstack.openstack_creator import OpenStackNetworkObject
-from snaps.openstack.utils import neutron_utils, keystone_utils
+from snaps.openstack.utils import neutron_utils
 
 __author__ = 'spisarski'
 
@@ -34,7 +35,7 @@ class OpenStackRouter(OpenStackNetworkObject):
         Constructor - all parameters are required
         :param os_creds: The credentials to connect with OpenStack
         :param router_settings: The settings used to create a router object
-                                (must be an instance of the RouterSettings
+                                (must be an instance of the RouterConfig
                                 class)
         """
         super(self.__class__, self).__init__(os_creds)
@@ -192,98 +193,15 @@ class RouterCreationError(Exception):
     """
 
 
-class RouterSettings:
+class RouterSettings(RouterConfig):
     """
-    Class representing a router configuration
+    Class to hold the configuration settings required for creating OpenStack
+    router objects
+    deprecated
     """
 
     def __init__(self, **kwargs):
-        """
-        Constructor - all parameters are optional
-        :param name: The router name.
-        :param project_name: The name of the project who owns the network. Only
-                             administrative users can specify a project ID
-                             other than their own. You cannot change this value
-                             through authorization policies.
-        :param external_gateway: Name of the external network to which to route
-        :param admin_state_up: The administrative status of the router.
-                               True = up / False = down (default True)
-        :param internal_subnets: List of subnet names to which to connect this
-                                 router for Floating IP purposes
-        :param port_settings: List of PortSettings objects
-        :return:
-        """
-        self.name = kwargs.get('name')
-        self.project_name = kwargs.get('project_name')
-        self.external_gateway = kwargs.get('external_gateway')
-
-        self.admin_state_up = kwargs.get('admin_state_up', True)
-        self.enable_snat = kwargs.get('enable_snat')
-        if kwargs.get('internal_subnets'):
-            self.internal_subnets = kwargs['internal_subnets']
-        else:
-            self.internal_subnets = list()
-
-        self.port_settings = list()
-        if kwargs.get('interfaces', kwargs.get('port_settings')):
-            interfaces = kwargs.get('interfaces', kwargs.get('port_settings'))
-            for interface in interfaces:
-                if isinstance(interface, PortSettings):
-                    self.port_settings.append(interface)
-                else:
-                    self.port_settings.append(
-                        PortSettings(**interface['port']))
-
-        if not self.name:
-            raise RouterSettingsError('Name is required')
-
-    def dict_for_neutron(self, neutron, os_creds):
-        """
-        Returns a dictionary object representing this object.
-        This is meant to be converted into JSON designed for use by the Neutron
-        API
-
-        TODO - expand automated testing to exercise all parameters
-        :param neutron: The neutron client to retrieve external network
-                        information if necessary
-        :param os_creds: The OpenStack credentials
-        :return: the dictionary object
-        """
-        out = dict()
-        ext_gw = dict()
-
-        if self.name:
-            out['name'] = self.name
-        if self.project_name:
-            keystone = keystone_utils.keystone_client(os_creds)
-            project = keystone_utils.get_project(
-                keystone=keystone, project_name=self.project_name)
-            project_id = None
-            if project:
-                project_id = project.id
-            if project_id:
-                out['tenant_id'] = project_id
-            else:
-                raise RouterSettingsError(
-                    'Could not find project ID for project named - ' +
-                    self.project_name)
-        if self.admin_state_up is not None:
-            out['admin_state_up'] = self.admin_state_up
-        if self.external_gateway:
-            ext_net = neutron_utils.get_network(
-                neutron, network_name=self.external_gateway)
-            if ext_net:
-                ext_gw['network_id'] = ext_net.id
-                out['external_gateway_info'] = ext_gw
-            else:
-                raise RouterSettingsError(
-                    'Could not find the external network named - ' +
-                    self.external_gateway)
-
-        return {'router': out}
-
-
-class RouterSettingsError(Exception):
-    """
-    Exception to be thrown when router settings attributes are incorrect
-    """
+        from warnings import warn
+        warn('Use snaps.config.router.RouterConfig instead',
+             DeprecationWarning)
+        super(self.__class__, self).__init__(**kwargs)