1 # Copyright (c) 2016 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 from novaclient.exceptions import NotFound
19 from snaps.config.flavor import FlavorConfig
20 from snaps.openstack.openstack_creator import OpenStackComputeObject
21 from snaps.openstack.utils import nova_utils
23 __author__ = 'spisarski'
25 logger = logging.getLogger('create_flavor')
27 MEM_PAGE_SIZE_ANY = {'hw:mem_page_size': 'any'}
28 MEM_PAGE_SIZE_LARGE = {'hw:mem_page_size': 'large'}
31 class OpenStackFlavor(OpenStackComputeObject):
33 Class responsible for creating a user in OpenStack
36 def __init__(self, os_creds, flavor_settings):
39 :param os_creds: The OpenStack connection credentials
40 :param flavor_settings: a FlavorConfig instance
43 super(self.__class__, self).__init__(os_creds)
45 self.flavor_settings = flavor_settings
50 Loads the existing OpenStack flavor
51 :return: The Flavor domain object or None
53 super(self.__class__, self).initialize()
55 self.__flavor = nova_utils.get_flavor_by_name(
56 self._nova, self.flavor_settings.name)
58 logger.info('Found flavor with name - %s',
59 self.flavor_settings.name)
64 Creates the image in OpenStack if it does not already exist
65 :return: The OpenStack flavor object
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)
75 logger.info('Did not create flavor due to cleanup mode')
81 Cleanse environment of all artifacts
86 nova_utils.delete_flavor(self._nova, self.__flavor)
94 Returns the OpenStack flavor object
100 class FlavorSettings(FlavorConfig):
102 Configuration settings for OpenStack flavor creation
105 def __init__(self, **kwargs):
106 from warnings import warn
107 warn('Use snaps.config.flavor.FlavorConfig instead',
109 super(self.__class__, self).__init__(**kwargs)