Changes to KeypairSettings constructor to use kwargs.
[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 = None
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.__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)
53         if self.__project:
54             logger.info('Found project with name - ' + self.project_settings.name)
55         elif not cleanup:
56             self.__project = keystone_utils.create_project(self.__keystone, self.project_settings)
57         else:
58             logger.info('Did not create image due to cleanup mode')
59
60         return self.__project
61
62     def clean(self):
63         """
64         Cleanse environment of all artifacts
65         :return: void
66         """
67         if self.__project:
68             try:
69                 keystone_utils.delete_project(self.__keystone, self.__project)
70             except NotFound:
71                 pass
72             self.__project = None
73
74         if self.__role:
75             try:
76                 keystone_utils.delete_role(self.__keystone, self.__role)
77             except NotFound:
78                 pass
79             self.__project = None
80
81     def get_project(self):
82         """
83         Returns the OpenStack project object populated on create()
84         :return:
85         """
86         return self.__project
87
88     def assoc_user(self, user):
89         """
90         The user object to associate with the project
91         :param user: the OpenStack user object to associate with project
92         :return:
93         """
94         if not self.__role:
95             self.__role = keystone_utils.create_role(self.__keystone, self.project_settings.name + '-role')
96
97         keystone_utils.assoc_user_to_project(self.__keystone, self.__role, user, self.__project)
98
99
100 class ProjectSettings:
101     """
102     Class to hold the configuration settings required for creating OpenStack project objects
103     """
104     def __init__(self, config=None, name=None, domain='default', description=None, enabled=True):
105
106         """
107         Constructor
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)
114         """
115
116         if config:
117             self.name = config.get('name')
118             if config.get('domain'):
119                 self.domain = config['domain']
120             else:
121                 self.domain = domain
122
123             self.description = config.get('description')
124             if config.get('enabled') is not None:
125                 self.enabled = config['enabled']
126             else:
127                 self.enabled = enabled
128         else:
129             self.name = name
130             self.domain = domain
131             self.description = description
132             self.enabled = enabled
133
134         if not self.name:
135             raise Exception("The attribute name is required for ProjectSettings")