diff options
author | spisarski <s.pisarski@cablelabs.com> | 2017-11-17 09:49:52 -0700 |
---|---|---|
committer | spisarski <s.pisarski@cablelabs.com> | 2017-11-20 08:34:23 -0700 |
commit | 3482f6e28e26025043f61efb90892d886f5909cc (patch) | |
tree | a9a5209a5a21d893ed0f673d6b8a9cda73ff9652 /snaps/config/router.py | |
parent | 133b321125eeb4c22b2963dbd112a074cfeb6ab1 (diff) |
Refactoring of RouterSettings to extend RouterConfig
RouterSettings and neutron_utils have a runtime cyclical
dependency. This patch reduces this dependency and
deprecates the RouterSettings class.
JIRA: SNAPS-223
Change-Id: I6a2a5e6e6e86204e62148a57e3525da5862841cf
Signed-off-by: spisarski <s.pisarski@cablelabs.com>
Diffstat (limited to 'snaps/config/router.py')
-rw-r--r-- | snaps/config/router.py | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/snaps/config/router.py b/snaps/config/router.py new file mode 100644 index 0000000..db26870 --- /dev/null +++ b/snaps/config/router.py @@ -0,0 +1,113 @@ +# Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs") +# and others. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from snaps.openstack.create_network import PortSettings +from snaps.openstack.utils import neutron_utils, keystone_utils + + +class RouterConfig(object): + """ + Class representing a router configuration + """ + + 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 RouterConfigError('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 RouterConfigError( + '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 RouterConfigError( + 'Could not find the external network named - ' + + self.external_gateway) + + return {'router': out} + + +class RouterConfigError(Exception): + """ + Exception to be thrown when router settings attributes are incorrect + """ |