Refactoring of NetworkSettings to extend NetworkConfig
[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.config.flavor import FlavorConfig
20 from snaps.openstack.openstack_creator import OpenStackComputeObject
21 from snaps.openstack.utils import nova_utils
22
23 __author__ = 'spisarski'
24
25 logger = logging.getLogger('create_flavor')
26
27 MEM_PAGE_SIZE_ANY = {'hw:mem_page_size': 'any'}
28 MEM_PAGE_SIZE_LARGE = {'hw:mem_page_size': 'large'}
29
30
31 class OpenStackFlavor(OpenStackComputeObject):
32     """
33     Class responsible for creating a user in OpenStack
34     """
35
36     def __init__(self, os_creds, flavor_settings):
37         """
38         Constructor
39         :param os_creds: The OpenStack connection credentials
40         :param flavor_settings: a FlavorConfig instance
41         :return:
42         """
43         super(self.__class__, self).__init__(os_creds)
44
45         self.flavor_settings = flavor_settings
46         self.__flavor = None
47
48     def initialize(self):
49         """
50         Loads the existing OpenStack flavor
51         :return: The Flavor domain object or None
52         """
53         super(self.__class__, self).initialize()
54
55         self.__flavor = nova_utils.get_flavor_by_name(
56             self._nova, self.flavor_settings.name)
57         if self.__flavor:
58             logger.info('Found flavor with name - %s',
59                         self.flavor_settings.name)
60         return self.__flavor
61
62     def create(self):
63         """
64         Creates the image in OpenStack if it does not already exist
65         :return: The OpenStack flavor object
66         """
67         self.initialize()
68         if not self.__flavor:
69             self.__flavor = nova_utils.create_flavor(
70                 self._nova, self.flavor_settings)
71             if self.flavor_settings.metadata:
72                 nova_utils.set_flavor_keys(self._nova, self.__flavor,
73                                            self.flavor_settings.metadata)
74         else:
75             logger.info('Did not create flavor due to cleanup mode')
76
77         return self.__flavor
78
79     def clean(self):
80         """
81         Cleanse environment of all artifacts
82         :return: void
83         """
84         if self.__flavor:
85             try:
86                 nova_utils.delete_flavor(self._nova, self.__flavor)
87             except NotFound:
88                 pass
89
90             self.__flavor = None
91
92     def get_flavor(self):
93         """
94         Returns the OpenStack flavor object
95         :return:
96         """
97         return self.__flavor
98
99
100 class FlavorSettings(FlavorConfig):
101     """
102     Configuration settings for OpenStack flavor creation
103     """
104
105     def __init__(self, **kwargs):
106         from warnings import warn
107         warn('Use snaps.config.flavor.FlavorConfig instead',
108              DeprecationWarning)
109         super(self.__class__, self).__init__(**kwargs)