bc8078922257163816b02c481e7ba0ec820441b9
[snaps.git] / snaps / openstack / create_project.py
1 # Copyright (c) 2017 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 keystoneclient.exceptions import NotFound
18 from snaps.openstack.utils import keystone_utils
19
20 __author__ = 'spisarski'
21
22 logger = logging.getLogger('create_image')
23
24
25 class OpenStackProject:
26     """
27     Class responsible for creating a project/project in OpenStack
28     """
29
30     def __init__(self, os_creds, project_settings):
31         """
32         Constructor
33         :param os_creds: The OpenStack connection credentials
34         :param project_settings: The project's settings
35         :return:
36         """
37         self.__os_creds = os_creds
38         self.project_settings = project_settings
39         self.__project = None
40         self.__role = None
41         self.__keystone = None
42
43     def create(self, cleanup=False):
44         """
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
48         """
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)
52         if self.__project:
53             logger.info(
54                 'Found project with name - ' + self.project_settings.name)
55         elif not cleanup:
56             self.__project = keystone_utils.create_project(
57                 self.__keystone, self.project_settings)
58         else:
59             logger.info('Did not create image due to cleanup mode')
60
61         return self.__project
62
63     def clean(self):
64         """
65         Cleanse environment of all artifacts
66         :return: void
67         """
68         if self.__project:
69             try:
70                 keystone_utils.delete_project(self.__keystone, self.__project)
71             except NotFound:
72                 pass
73             self.__project = None
74
75         if self.__role:
76             try:
77                 keystone_utils.delete_role(self.__keystone, self.__role)
78             except NotFound:
79                 pass
80             self.__project = None
81
82     def get_project(self):
83         """
84         Returns the OpenStack project object populated on create()
85         :return:
86         """
87         return self.__project
88
89     def assoc_user(self, user):
90         """
91         The user object to associate with the project
92         :param user: the OpenStack User domain object to associate with project
93         :return:
94         """
95         if not self.__role:
96             self.__role = keystone_utils.create_role(
97                 self.__keystone, self.project_settings.name + '-role')
98
99         keystone_utils.grant_user_role_to_project(self.__keystone, self.__role,
100                                                   user, self.__project)
101
102
103 class ProjectSettings:
104     """
105     Class to hold the configuration settings required for creating OpenStack
106     project objects
107     """
108
109     def __init__(self, **kwargs):
110
111         """
112         Constructor
113         :param name: the project's name (required)
114         :param domain or domain_name: the project's domain name
115                                       (default = 'Default').
116                                       Field is used for v3 clients
117         :param description: the description (optional)
118         :param enabled: denotes whether or not the user is enabled
119                         (default True)
120         """
121
122         self.name = kwargs.get('name')
123         self.domain_name = kwargs.get(
124             'domain', kwargs.get('domain', 'Default'))
125
126         self.description = kwargs.get('description')
127         if kwargs.get('enabled') is not None:
128             self.enabled = kwargs['enabled']
129         else:
130             self.enabled = True
131
132         if not self.name:
133             raise ProjectSettingsError(
134                 "The attribute name is required for ProjectSettings")
135
136
137 class ProjectSettingsError(Exception):
138     """
139     Exception to be thrown when project settings attributes are incorrect
140     """