44e35a36ef52ab2a1ac73bbe09039df977280dc2
[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     def get_qos(self):
94         """
95         Returns the domain QoS object as it was populated when create() was
96         called
97         :return: the object
98         """
99         return self.__qos
100
101
102 class Consumer(enum.Enum):
103     """
104     QoS Specification consumer types
105     deprecated - use snaps.config.qos.Consumer
106     """
107     front_end = 'front-end'
108     back_end = 'back-end'
109     both = 'both'
110
111
112 class QoSSettings(QoSConfig):
113     """
114     Class to hold the configuration settings required for creating OpenStack
115     QoS objects
116     deprecated
117     """
118
119     def __init__(self, **kwargs):
120         from warnings import warn
121         warn('Use snaps.config.qos.QoSConfig instead',
122              DeprecationWarning)
123         super(self.__class__, self).__init__(**kwargs)
124
125
126 class QoSCreationError(Exception):
127     """
128     Exception to be thrown when an qos cannot be created
129     """
130
131     def __init__(self, message):
132         Exception.__init__(self, message)