1 # Copyright (c) 2017 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 keystoneclient.exceptions import NotFound
18 from snaps.openstack.utils import keystone_utils, neutron_utils
20 __author__ = 'spisarski'
22 logger = logging.getLogger('create_image')
25 class OpenStackProject:
27 Class responsible for creating a project/project in OpenStack
30 def __init__(self, os_creds, project_settings):
33 :param os_creds: The OpenStack connection credentials
34 :param project_settings: The project's settings
37 self.__os_creds = os_creds
38 self.project_settings = project_settings
41 self.__keystone = None
43 def create(self, cleanup=False):
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
47 :return: The OpenStack Image object
49 self.__keystone = keystone_utils.keystone_client(self.__os_creds)
50 self.__project = keystone_utils.get_project(
51 keystone=self.__keystone, project_settings=self.project_settings)
54 'Found project with name - ' + self.project_settings.name)
56 self.__project = keystone_utils.create_project(
57 self.__keystone, self.project_settings)
59 logger.info('Did not create image due to cleanup mode')
65 Cleanse environment of all artifacts
69 # Delete security group 'default' if exists
70 neutron = neutron_utils.neutron_client(self.__os_creds)
71 default_sec_grp = neutron_utils.get_security_group(
73 tenant_id=self.__project.id)
76 neutron_utils.delete_security_group(
77 neutron, default_sec_grp)
83 keystone_utils.delete_project(self.__keystone, self.__project)
90 keystone_utils.delete_role(self.__keystone, self.__role)
95 def get_project(self):
97 Returns the OpenStack project object populated on create()
100 return self.__project
102 def assoc_user(self, user):
104 The user object to associate with the project
105 :param user: the OpenStack User domain object to associate with project
109 self.__role = keystone_utils.create_role(
110 self.__keystone, self.project_settings.name + '-role')
112 keystone_utils.grant_user_role_to_project(self.__keystone, self.__role,
113 user, self.__project)
116 class ProjectSettings:
118 Class to hold the configuration settings required for creating OpenStack
122 def __init__(self, **kwargs):
126 :param name: the project's name (required)
127 :param domain or domain_name: the project's domain name
128 (default = 'Default').
129 Field is used for v3 clients
130 :param description: the description (optional)
131 :param enabled: denotes whether or not the user is enabled
135 self.name = kwargs.get('name')
136 self.domain_name = kwargs.get(
137 'domain', kwargs.get('domain', 'Default'))
139 self.description = kwargs.get('description')
140 if kwargs.get('enabled') is not None:
141 self.enabled = kwargs['enabled']
146 raise ProjectSettingsError(
147 "The attribute name is required for ProjectSettings")
150 class ProjectSettingsError(Exception):
152 Exception to be thrown when project settings attributes are incorrect