Ensure project IDs are handled correctly for Network/Subnets
[snaps.git] / snaps / openstack / create_network.py
1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 #                    and others.  All rights reserved.
3 #
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:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 import logging
16
17 import enum
18 from neutronclient.common.exceptions import NetworkNotFoundClient, Unauthorized
19
20 from snaps.config.network import NetworkConfig, SubnetConfig, PortConfig
21 from snaps.openstack.openstack_creator import OpenStackNetworkObject
22 from snaps.openstack.utils import neutron_utils
23
24 __author__ = 'spisarski'
25
26 logger = logging.getLogger('OpenStackNetwork')
27
28
29 class OpenStackNetwork(OpenStackNetworkObject):
30     """
31     Class responsible for managing a network in OpenStack
32     """
33
34     def __init__(self, os_creds, network_settings):
35         """
36         Constructor - all parameters are required
37         :param os_creds: The credentials to connect with OpenStack
38         :param network_settings: The settings used to create a network
39         """
40         super(self.__class__, self).__init__(os_creds)
41
42         self.network_settings = network_settings
43
44         # Attributes instantiated on create()
45         self.__network = None
46
47     def initialize(self):
48         """
49         Loads the existing OpenStack network/subnet
50         :return: The Network domain object or None
51         """
52         super(self.__class__, self).initialize()
53
54         try:
55             self.__network = neutron_utils.get_network(
56                 self._neutron, network_settings=self.network_settings,
57                 project_id=self.project_id, os_creds=self._os_creds)
58         except Unauthorized as e:
59             logger.warn('Unable to lookup network with name %s - %s',
60                         self.network_settings.name, e)
61
62         return self.__network
63
64     def create(self):
65         """
66         Responsible for creating not only the network but then a private
67         subnet, router, and an interface to the router.
68         :return: the Network domain object
69         """
70         self.initialize()
71
72         if not self.__network:
73             self.__network = neutron_utils.create_network(
74                 self._neutron, self._os_creds, self.network_settings)
75             logger.debug(
76                 'Network [%s] created successfully' % self.__network.id)
77
78         return self.__network
79
80     def clean(self):
81         """
82         Removes and deletes all items created in reverse order.
83         """
84         if self.__network:
85             try:
86                 neutron_utils.delete_network(self._neutron, self.__network)
87             except NetworkNotFoundClient:
88                 pass
89             self.__network = None
90
91     def get_network(self):
92         """
93         Returns the created OpenStack network object
94         :return: the OpenStack network object
95         """
96         return self.__network
97
98
99 class NetworkSettings(NetworkConfig):
100     """
101     Class to hold the configuration settings required for creating OpenStack
102     Network objects
103     deprecated
104     """
105
106     def __init__(self, **kwargs):
107         from warnings import warn
108         warn('Use snaps.config.network.NetworkConfig instead',
109              DeprecationWarning)
110         super(self.__class__, self).__init__(**kwargs)
111
112
113 class IPv6Mode(enum.Enum):
114     """
115     A rule's direction
116     deprecated - use snaps.config.network.IPv6Mode
117     """
118     slaac = 'slaac'
119     stateful = 'dhcpv6-stateful'
120     stateless = 'dhcpv6-stateless'
121
122
123 class SubnetSettings(SubnetConfig):
124     """
125     Class to hold the configuration settings required for creating OpenStack
126     Subnet objects
127     deprecated
128     """
129
130     def __init__(self, **kwargs):
131         from warnings import warn
132         warn('Use snaps.config.network.SubnetConfig instead',
133              DeprecationWarning)
134         super(self.__class__, self).__init__(**kwargs)
135
136
137 class PortSettings(PortConfig):
138     """
139     Class to hold the configuration settings required for creating OpenStack
140     Subnet objects
141     deprecated
142     """
143
144     def __init__(self, **kwargs):
145         from warnings import warn
146         warn('Use snaps.config.network.PortConfig instead',
147              DeprecationWarning)
148         super(self.__class__, self).__init__(**kwargs)