summaryrefslogtreecommitdiffstats
path: root/snaps/openstack/create_network.py
blob: 3d50eb500a606029f748b35c7a2e7f154bd928d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# 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.
import logging

import enum
from neutronclient.common.exceptions import NetworkNotFoundClient, Unauthorized

from snaps.config.network import NetworkConfig, SubnetConfig, PortConfig
from snaps.openstack.openstack_creator import OpenStackNetworkObject
from snaps.openstack.utils import neutron_utils, keystone_utils

__author__ = 'spisarski'

logger = logging.getLogger('OpenStackNetwork')


class OpenStackNetwork(OpenStackNetworkObject):
    """
    Class responsible for managing a network in OpenStack
    """

    def __init__(self, os_creds, network_settings):
        """
        Constructor - all parameters are required
        :param os_creds: The credentials to connect with OpenStack
        :param network_settings: The settings used to create a network
        """
        super(self.__class__, self).__init__(os_creds)

        self.network_settings = network_settings

        # Attributes instantiated on create()
        self.__network = None

    def initialize(self):
        """
        Loads the existing OpenStack network/subnet
        :return: The Network domain object or None
        """
        super(self.__class__, self).initialize()

        try:
            keystone = keystone_utils.keystone_client(self._os_creds)
            self.__network = neutron_utils.get_network(
                self._neutron, keystone,
                network_settings=self.network_settings,
                project_name=self._os_creds.project_name)
        except Unauthorized as e:
            logger.warn('Unable to lookup network with name %s - %s',
                        self.network_settings.name, e)

        return self.__network

    def create(self):
        """
        Responsible for creating not only the network but then a private
        subnet, router, and an interface to the router.
        :return: the Network domain object
        """
        self.initialize()

        if not self.__network:
            self.__network = neutron_utils.create_network(
                self._neutron, self._os_creds, self.network_settings)
            logger.debug(
                'Network [%s] created successfully' % self.__network.id)

        return self.__network

    def clean(self):
        """
        Removes and deletes all items created in reverse order.
        """
        if self.__network:
            try:
                neutron_utils.delete_network(self._neutron, self.__network)
            except NetworkNotFoundClient:
                pass
            self.__network = None

    def get_network(self):
        """
        Returns the created OpenStack network object
        :return: the OpenStack network object
        """
        return self.__network


class NetworkSettings(NetworkConfig):
    """
    Class to hold the configuration settings required for creating OpenStack
    Network objects
    deprecated
    """

    def __init__(self, **kwargs):
        from warnings import warn
        warn('Use snaps.config.network.NetworkConfig instead',
             DeprecationWarning)
        super(self.__class__, self).__init__(**kwargs)


class IPv6Mode(enum.Enum):
    """
    A rule's direction
    deprecated - use snaps.config.network.IPv6Mode
    """
    slaac = 'slaac'
    stateful = 'dhcpv6-stateful'
    stateless = 'dhcpv6-stateless'


class SubnetSettings(SubnetConfig):
    """
    Class to hold the configuration settings required for creating OpenStack
    Subnet objects
    deprecated
    """

    def __init__(self, **kwargs):
        from warnings import warn
        warn('Use snaps.config.network.SubnetConfig instead',
             DeprecationWarning)
        super(self.__class__, self).__init__(**kwargs)


class PortSettings(PortConfig):
    """
    Class to hold the configuration settings required for creating OpenStack
    Subnet objects
    deprecated
    """

    def __init__(self, **kwargs):
        from warnings import warn
        warn('Use snaps.config.network.PortConfig instead',
             DeprecationWarning)
        super(self.__class__, self).__init__(**kwargs)