bcf47905ed0152345002e967e3bf63597e231b1a
[snaps.git] / snaps / openstack / create_user.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.openstack_creator import OpenStackIdentityObject
20 from snaps.openstack.os_credentials import OSCreds
21 from snaps.openstack.utils import keystone_utils
22
23 __author__ = 'spisarski'
24
25 logger = logging.getLogger('create_user')
26
27
28 class OpenStackUser(OpenStackIdentityObject):
29     """
30     Class responsible for managing a user in OpenStack
31     """
32
33     def __init__(self, os_creds, user_settings):
34         """
35         Constructor
36         :param os_creds: The OpenStack connection credentials
37         :param user_settings: The user settings
38         :return:
39         """
40         super(self.__class__, self).__init__(os_creds)
41
42         self.user_settings = user_settings
43         self.__user = None
44
45     def initialize(self):
46         """
47         Creates the user in OpenStack if it does not already exist
48         :return: The User domain object
49         """
50         super(self.__class__, self).initialize()
51
52         self.__user = keystone_utils.get_user(self._keystone,
53                                               self.user_settings.name)
54         return self.__user
55
56     def create(self, cleanup=False):
57         """
58         Creates a User if one does not already exist
59         :return: The User domain object
60         """
61         self.initialize()
62         if not self.__user:
63             self.__user = keystone_utils.create_user(self._keystone,
64                                                      self.user_settings)
65         return self.__user
66
67     def clean(self):
68         """
69         Cleanse environment of user
70         :return: void
71         """
72         if self.__user:
73             try:
74                 keystone_utils.delete_user(self._keystone, self.__user)
75             except NotFound:
76                 pass
77             self.__user = None
78
79     def get_user(self):
80         """
81         Returns the OpenStack user object populated in create()
82         :return: the Object or None if not created
83         """
84         return self.__user
85
86     def get_os_creds(self, project_name=None):
87         """
88         Returns an OSCreds object based on this user account and a project
89         :param project_name: the name of the project to leverage in the
90                              credentials
91         :return:
92         """
93         return OSCreds(
94             username=self.user_settings.name,
95             password=self.user_settings.password,
96             auth_url=self._os_creds.auth_url,
97             project_name=project_name,
98             identity_api_version=self._os_creds.identity_api_version,
99             user_domain_name=self._os_creds.user_domain_name,
100             user_domain_id=self._os_creds.user_domain_id,
101             project_domain_name=self._os_creds.project_domain_name,
102             project_domain_id=self._os_creds.project_domain_id,
103             interface=self._os_creds.interface,
104             proxy_settings=self._os_creds.proxy_settings,
105             cacert=self._os_creds.cacert)
106
107
108 class UserSettings:
109     def __init__(self, **kwargs):
110
111         """
112         Constructor
113         :param name: the user's name (required)
114         :param password: the user's password (required)
115         :param project_name: the user's primary project name (optional)
116         :param domain_name: the user's domain name (default='default'). For v3
117                             APIs
118         :param email: the user's email address (optional)
119         :param enabled: denotes whether or not the user is enabled
120                         (default True)
121         :param roles: dict where key is the role's name and value is the name
122                       the project to associate with the role
123         """
124
125         self.name = kwargs.get('name')
126         self.password = kwargs.get('password')
127         self.project_name = kwargs.get('project_name')
128         self.email = kwargs.get('email')
129         self.domain_name = kwargs.get('domain_name', 'Default')
130         self.enabled = kwargs.get('enabled', True)
131         self.roles = kwargs.get('roles', dict())
132
133         if not self.name or not self.password:
134             raise UserSettingsException(
135                 'The attributes name and password are required for '
136                 'UserSettings')
137
138         if not isinstance(self.enabled, bool):
139             raise UserSettingsException('The attribute enabled must be of type'
140                                         ' boolean')
141
142
143 class UserSettingsException(Exception):
144     """
145     Raised when there is a problem with the values set in the UserSettings
146     class
147     """