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.config.user import UserConfig
20 from snaps.openstack.openstack_creator import OpenStackIdentityObject
21 from snaps.openstack.os_credentials import OSCreds
22 from snaps.openstack.utils import keystone_utils
24 __author__ = 'spisarski'
26 logger = logging.getLogger('create_user')
29 class OpenStackUser(OpenStackIdentityObject):
31 Class responsible for managing a user in OpenStack
34 def __init__(self, os_creds, user_settings):
37 :param os_creds: The OpenStack connection credentials
38 :param user_settings: The user settings
41 super(self.__class__, self).__init__(os_creds)
43 self.user_settings = user_settings
48 Creates the user in OpenStack if it does not already exist
49 :return: The User domain object
51 super(self.__class__, self).initialize()
53 self.__user = keystone_utils.get_user(self._keystone,
54 self.user_settings.name)
57 def create(self, cleanup=False):
59 Creates a User if one does not already exist
60 :return: The User domain object
64 self.__user = keystone_utils.create_user(self._keystone,
70 Cleanse environment of user
75 keystone_utils.delete_user(self._keystone, self.__user)
82 Returns the OpenStack user object populated in create()
83 :return: the Object or None if not created
87 def get_os_creds(self, project_name=None):
89 Returns an OSCreds object based on this user account and a project
90 :param project_name: the name of the project to leverage in the
95 username=self.user_settings.name,
96 password=self.user_settings.password,
97 auth_url=self._os_creds.auth_url,
98 project_name=project_name,
99 identity_api_version=self._os_creds.identity_api_version,
100 image_api_version=self._os_creds.image_api_version,
101 network_api_version=self._os_creds.network_api_version,
102 compute_api_version=self._os_creds.compute_api_version,
103 heat_api_version=self._os_creds.heat_api_version,
104 volume_api_version=self._os_creds.volume_api_version,
105 user_domain_name=self._os_creds.user_domain_name,
106 user_domain_id=self._os_creds.user_domain_id,
107 project_domain_name=self._os_creds.project_domain_name,
108 project_domain_id=self._os_creds.project_domain_id,
109 interface=self._os_creds.interface,
110 proxy_settings=self._os_creds.proxy_settings,
111 cacert=self._os_creds.cacert)
114 class UserSettings(UserConfig):
116 Class to hold the configuration settings required for creating OpenStack
121 def __init__(self, **kwargs):
122 from warnings import warn
123 warn('Use snaps.config.user.UserConfig instead',
125 super(self.__class__, self).__init__(**kwargs)