Merge "Modified code to support both Python 2.7 and 3.x"
[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
19 from snaps.openstack.utils import keystone_utils
20
21 __author__ = 'spisarski'
22
23 logger = logging.getLogger('create_image')
24
25
26 class OpenStackProject:
27     """
28     Class responsible for creating a project/project in OpenStack
29     """
30
31     def __init__(self, os_creds, project_settings):
32         """
33         Constructor
34         :param os_creds: The OpenStack connection credentials
35         :param project_settings: The project's settings
36         :return:
37         """
38         self.__os_creds = os_creds
39         self.project_settings = project_settings
40         self.__project = None
41         self.__role = None
42         self.__keystone = keystone_utils.keystone_client(self.__os_creds)
43
44     def create(self, cleanup=False):
45         """
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
49         """
50         self.__project = keystone_utils.get_project(keystone=self.__keystone,
51                                                     project_name=self.project_settings.name)
52         if self.__project:
53             logger.info('Found project with name - ' + self.project_settings.name)
54         elif not cleanup:
55             self.__project = keystone_utils.create_project(self.__keystone, self.project_settings)
56         else:
57             logger.info('Did not create image due to cleanup mode')
58
59         return self.__project
60
61     def clean(self):
62         """
63         Cleanse environment of all artifacts
64         :return: void
65         """
66         if self.__project:
67             try:
68                 keystone_utils.delete_project(self.__keystone, self.__project)
69             except NotFound:
70                 pass
71             self.__project = None
72
73         if self.__role:
74             try:
75                 keystone_utils.delete_role(self.__keystone, self.__role)
76             except NotFound:
77                 pass
78             self.__project = None
79
80     def get_project(self):
81         """
82         Returns the OpenStack project object populated on create()
83         :return:
84         """
85         return self.__project
86
87     def assoc_user(self, user):
88         """
89         The user object to associate with the project
90         :param user: the OpenStack user object to associate with project
91         :return:
92         """
93         if not self.__role:
94             self.__role = keystone_utils.create_role(self.__keystone, self.project_settings.name + '-role')
95
96         keystone_utils.assoc_user_to_project(self.__keystone, self.__role, user, self.__project)
97
98
99 class ProjectSettings:
100     """
101     Class to hold the configuration settings required for creating OpenStack project objects
102     """
103     def __init__(self, config=None, name=None, domain='default', description=None, enabled=True):
104
105         """
106         Constructor
107         :param config: dict() object containing the configuration settings using the attribute names below as each
108                        member's the key and overrides any of the other parameters.
109         :param name: the project's name (required)
110         :param domain: the project's domain name (default 'default'). Field is used for v3 clients
111         :param description: the description (optional)
112         :param enabled: denotes whether or not the user is enabled (default True)
113         """
114
115         if config:
116             self.name = config.get('name')
117             if config.get('domain'):
118                 self.domain = config['domain']
119             else:
120                 self.domain = domain
121
122             self.description = config.get('description')
123             if config.get('enabled') is not None:
124                 self.enabled = config['enabled']
125             else:
126                 self.enabled = enabled
127         else:
128             self.name = name
129             self.domain = domain
130             self.description = description
131             self.enabled = enabled
132
133         if not self.name:
134             raise Exception("The attribute name is required for ProjectSettings")