Closing keystone sessions after done with them.
[snaps.git] / snaps / openstack / create_qos.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
16 import logging
17
18 import enum
19 from cinderclient.exceptions import NotFound
20
21 from snaps.config.qos import QoSConfig
22 from snaps.openstack.openstack_creator import OpenStackVolumeObject
23 from snaps.openstack.utils import cinder_utils
24
25 __author__ = 'spisarski'
26
27 logger = logging.getLogger('create_qos')
28
29 IMAGE_ACTIVE_TIMEOUT = 600
30 POLL_INTERVAL = 3
31 STATUS_ACTIVE = 'active'
32
33
34 class OpenStackQoS(OpenStackVolumeObject):
35     """
36     Class responsible for managing an qos in OpenStack
37     """
38
39     def __init__(self, os_creds, qos_settings):
40         """
41         Constructor
42         :param os_creds: The OpenStack connection credentials
43         :param qos_settings: The qos settings
44         :return:
45         """
46         super(self.__class__, self).__init__(os_creds)
47
48         self.qos_settings = qos_settings
49         self.__qos = None
50
51     def initialize(self):
52         """
53         Loads the existing QoS
54         :return: The QoS domain object or None
55         """
56         super(self.__class__, self).initialize()
57
58         self.__qos = cinder_utils.get_qos(
59             self._cinder, qos_settings=self.qos_settings)
60
61         return self.__qos
62
63     def create(self):
64         """
65         Creates the qos in OpenStack if it does not already exist and returns
66         the domain QoS object
67         :return: The QoS domain object or None
68         """
69         self.initialize()
70
71         if not self.__qos:
72             self.__qos = cinder_utils.create_qos(
73                 self._cinder, self.qos_settings)
74
75             logger.info(
76                 'Created qos with name - %s', self.qos_settings.name)
77
78         return self.__qos
79
80     def clean(self):
81         """
82         Cleanse environment of all artifacts
83         :return: void
84         """
85         if self.__qos:
86             try:
87                 cinder_utils.delete_qos(self._cinder, self.__qos)
88             except NotFound:
89                 pass
90
91         self.__qos = None
92
93         super(self.__class__, self).clean()
94
95     def get_qos(self):
96         """
97         Returns the domain QoS object as it was populated when create() was
98         called
99         :return: the object
100         """
101         return self.__qos
102
103
104 class Consumer(enum.Enum):
105     """
106     QoS Specification consumer types
107     deprecated - use snaps.config.qos.Consumer
108     """
109     front_end = 'front-end'
110     back_end = 'back-end'
111     both = 'both'
112
113
114 class QoSSettings(QoSConfig):
115     """
116     Class to hold the configuration settings required for creating OpenStack
117     QoS objects
118     deprecated
119     """
120
121     def __init__(self, **kwargs):
122         from warnings import warn
123         warn('Use snaps.config.qos.QoSConfig instead',
124              DeprecationWarning)
125         super(self.__class__, self).__init__(**kwargs)
126
127
128 class QoSCreationError(Exception):
129     """
130     Exception to be thrown when an qos cannot be created
131     """
132
133     def __init__(self, message):
134         Exception.__init__(self, message)