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
19 from snaps.openstack.utils import keystone_utils
21 __author__ = 'spisarski'
23 logger = logging.getLogger('create_image')
26 class OpenStackProject:
28 Class responsible for creating a project/project in OpenStack
31 def __init__(self, os_creds, project_settings):
34 :param os_creds: The OpenStack connection credentials
35 :param project_settings: The project's settings
38 self.__os_creds = os_creds
39 self.project_settings = project_settings
42 self.__keystone = None
44 def create(self, cleanup=False):
46 Creates the image in OpenStack if it does not already exist
47 :param cleanup: Denotes whether or not this is being called for cleanup or not
48 :return: The OpenStack Image object
50 self.__keystone = keystone_utils.keystone_client(self.__os_creds)
51 self.__project = keystone_utils.get_project(keystone=self.__keystone,
52 project_name=self.project_settings.name)
54 logger.info('Found project with name - ' + self.project_settings.name)
56 self.__project = keystone_utils.create_project(self.__keystone, self.project_settings)
58 logger.info('Did not create image due to cleanup mode')
64 Cleanse environment of all artifacts
69 keystone_utils.delete_project(self.__keystone, self.__project)
76 keystone_utils.delete_role(self.__keystone, self.__role)
81 def get_project(self):
83 Returns the OpenStack project object populated on create()
88 def assoc_user(self, user):
90 The user object to associate with the project
91 :param user: the OpenStack user object to associate with project
95 self.__role = keystone_utils.create_role(self.__keystone, self.project_settings.name + '-role')
97 keystone_utils.assoc_user_to_project(self.__keystone, self.__role, user, self.__project)
100 class ProjectSettings:
102 Class to hold the configuration settings required for creating OpenStack project objects
104 def __init__(self, config=None, name=None, domain='default', description=None, enabled=True):
108 :param config: dict() object containing the configuration settings using the attribute names below as each
109 member's the key and overrides any of the other parameters.
110 :param name: the project's name (required)
111 :param domain: the project's domain name (default 'default'). Field is used for v3 clients
112 :param description: the description (optional)
113 :param enabled: denotes whether or not the user is enabled (default True)
117 self.name = config.get('name')
118 if config.get('domain'):
119 self.domain = config['domain']
123 self.description = config.get('description')
124 if config.get('enabled') is not None:
125 self.enabled = config['enabled']
127 self.enabled = enabled
131 self.description = description
132 self.enabled = enabled
135 raise Exception("The attribute name is required for ProjectSettings")