Merge "Added the ability to create custom flavors with the deploy_venv.py application."
[snaps.git] / snaps / openstack / create_flavor.py
1 # Copyright (c) 2016 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 from novaclient.exceptions import NotFound
18
19 from snaps.openstack.utils import nova_utils
20
21 __author__ = 'spisarski'
22
23 logger = logging.getLogger('create_image')
24
25
26 class OpenStackFlavor:
27     """
28     Class responsible for creating a user in OpenStack
29     """
30
31     def __init__(self, os_creds, flavor_settings):
32         """
33         Constructor
34         :param os_creds: The OpenStack connection credentials
35         :param flavor_settings: The flavor settings
36         :return:
37         """
38         self.__os_creds = os_creds
39         self.flavor_settings = flavor_settings
40         self.__flavor = None
41         self.__nova = nova_utils.nova_client(self.__os_creds)
42
43     def create(self, cleanup=False):
44         """
45         Creates the image in OpenStack if it does not already exist
46         :param cleanup: Denotes whether or not this is being called for cleanup or not
47         :return: The OpenStack flavor object
48         """
49         self.__flavor = nova_utils.get_flavor_by_name(self.__nova, self.flavor_settings.name)
50         if self.__flavor:
51             logger.info('Found flavor with name - ' + self.flavor_settings.name)
52         elif not cleanup:
53             self.__flavor = nova_utils.create_flavor(self.__nova, self.flavor_settings)
54         else:
55             logger.info('Did not create flavor due to cleanup mode')
56
57         return self.__flavor
58
59     def clean(self):
60         """
61         Cleanse environment of all artifacts
62         :return: void
63         """
64         if self.__flavor:
65             try:
66                 nova_utils.delete_flavor(self.__nova, self.__flavor)
67             except NotFound:
68                 pass
69
70             self.__flavor = None
71
72     def get_flavor(self):
73         """
74         Returns the OpenStack flavor object
75         :return:
76         """
77         return self.__flavor
78
79
80 class FlavorSettings:
81     """
82     Configuration settings for OpenStack flavor creation
83     """
84
85     def __init__(self, config=None, name=None, flavor_id='auto', ram=None, disk=None, vcpus=None, ephemeral=0, swap=0,
86                  rxtx_factor=1.0, is_public=True):
87         """
88         Constructor
89         :param config: dict() object containing the configuration settings using the attribute names below as each
90                        member's the key and overrides any of the other parameters.
91         :param name: the flavor's name (required)
92         :param flavor_id: the string ID (default 'auto')
93         :param ram: the required RAM in MB (required)
94         :param disk: the size of the root disk in GB (required)
95         :param vcpus: the number of virtual CPUs (required)
96         :param ephemeral: the size of the ephemeral disk in GB (default 0)
97         :param swap: the size of the dedicated swap disk in GB (default 0)
98         :param rxtx_factor: the receive/transmit factor to be set on ports if backend supports
99                             QoS extension (default 1.0)
100         :param is_public: denotes whether or not the flavor is public (default True)
101         """
102
103         if config:
104             self.name = config.get('name')
105
106             if config.get('flavor_id'):
107                 self.flavor_id = config['flavor_id']
108             else:
109                 self.flavor_id = flavor_id
110
111             self.ram = config.get('ram')
112             self.disk = config.get('disk')
113             self.vcpus = config.get('vcpus')
114
115             if config.get('ephemeral'):
116                 self.ephemeral = config['ephemeral']
117             else:
118                 self.ephemeral = ephemeral
119
120             if config.get('swap'):
121                 self.swap = config['swap']
122             else:
123                 self.swap = swap
124
125             if config.get('rxtx_factor'):
126                 self.rxtx_factor = config['rxtx_factor']
127             else:
128                 self.rxtx_factor = rxtx_factor
129
130             if config.get('is_public') is not None:
131                 self.is_public = config['is_public']
132             else:
133                 self.is_public = is_public
134         else:
135             self.name = name
136             self.flavor_id = flavor_id
137             self.ram = ram
138             self.disk = disk
139             self.vcpus = vcpus
140             self.ephemeral = ephemeral
141             self.swap = swap
142             self.rxtx_factor = rxtx_factor
143             self.is_public = is_public
144
145         if not self.name or not self.ram or not self.disk or not self.vcpus:
146             raise Exception('The attributes name, ram, disk, and vcpus are required for FlavorSettings')
147
148         if not isinstance(self.ram, int):
149             raise Exception('The ram attribute must be a integer')
150
151         if not isinstance(self.disk, int):
152             raise Exception('The ram attribute must be a integer')
153
154         if not isinstance(self.vcpus, int):
155             raise Exception('The vcpus attribute must be a integer')
156
157         if self.ephemeral and not isinstance(self.ephemeral, int):
158             raise Exception('The ephemeral attribute must be an integer')
159
160         if self.swap and not isinstance(self.swap, int):
161             raise Exception('The swap attribute must be an integer')
162
163         if self.rxtx_factor and not isinstance(self.rxtx_factor, (int, float)):
164             raise Exception('The is_public attribute must be an integer or float')
165
166         if self.is_public and not isinstance(self.is_public, bool):
167             raise Exception('The is_public attribute must be a boolean')