Merge "Fix Malformed Table in Integration Tests doc"
[snaps.git] / snaps / config / 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
16
17 class FlavorConfig(object):
18     """
19     Configuration settings for OpenStack flavor creation
20     """
21
22     def __init__(self, **kwargs):
23         """
24         Constructor
25         :param name: the flavor's name (required)
26         :param flavor_id: the string ID (default 'auto')
27         :param ram: the required RAM in MB (required)
28         :param disk: the size of the root disk in GB (required)
29         :param vcpus: the number of virtual CPUs (required)
30         :param ephemeral: the size of the ephemeral disk in GB (default 0)
31         :param swap: the size of the dedicated swap disk in GB (default 0)
32         :param rxtx_factor: the receive/transmit factor to be set on ports if
33                             backend supports QoS extension (default 1.0)
34         :param is_public: denotes whether or not the flavor is public
35                           (default True)
36         :param metadata: freeform dict() for special metadata
37         """
38         self.name = kwargs.get('name')
39
40         if kwargs.get('flavor_id'):
41             self.flavor_id = kwargs['flavor_id']
42         else:
43             self.flavor_id = 'auto'
44
45         self.ram = kwargs.get('ram')
46         self.disk = kwargs.get('disk')
47         self.vcpus = kwargs.get('vcpus')
48
49         if kwargs.get('ephemeral'):
50             self.ephemeral = kwargs['ephemeral']
51         else:
52             self.ephemeral = 0
53
54         if kwargs.get('swap'):
55             self.swap = kwargs['swap']
56         else:
57             self.swap = 0
58
59         if kwargs.get('rxtx_factor'):
60             self.rxtx_factor = kwargs['rxtx_factor']
61         else:
62             self.rxtx_factor = 1.0
63
64         if kwargs.get('is_public') is not None:
65             self.is_public = kwargs['is_public']
66         else:
67             self.is_public = True
68
69         if kwargs.get('metadata'):
70             self.metadata = kwargs['metadata']
71         else:
72             self.metadata = None
73
74         if not self.name or not self.ram or not self.disk or not self.vcpus:
75             raise FlavorConfigError(
76                 'The attributes name, ram, disk, and vcpus are required for'
77                 'FlavorConfig')
78
79         if not isinstance(self.ram, int):
80             raise FlavorConfigError('The ram attribute must be a integer')
81
82         if not isinstance(self.disk, int):
83             raise FlavorConfigError('The ram attribute must be a integer')
84
85         if not isinstance(self.vcpus, int):
86             raise FlavorConfigError('The vcpus attribute must be a integer')
87
88         if self.ephemeral and not isinstance(self.ephemeral, int):
89             raise FlavorConfigError(
90                 'The ephemeral attribute must be an integer')
91
92         if self.swap and not isinstance(self.swap, int):
93             raise FlavorConfigError('The swap attribute must be an integer')
94
95         if self.rxtx_factor and not isinstance(self.rxtx_factor, (int, float)):
96             raise FlavorConfigError(
97                 'The is_public attribute must be an integer or float')
98
99         if self.is_public and not isinstance(self.is_public, bool):
100             raise FlavorConfigError(
101                 'The is_public attribute must be a boolean')
102
103
104 class FlavorConfigError(Exception):
105     """
106     Exception to be thrown when a flavor configuration is incorrect
107     """