Refactoring of NetworkSettings to extend NetworkConfig
[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.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
23
24 __author__ = 'spisarski'
25
26 logger = logging.getLogger('create_user')
27
28
29 class OpenStackUser(OpenStackIdentityObject):
30     """
31     Class responsible for managing a user in OpenStack
32     """
33
34     def __init__(self, os_creds, user_settings):
35         """
36         Constructor
37         :param os_creds: The OpenStack connection credentials
38         :param user_settings: The user settings
39         :return:
40         """
41         super(self.__class__, self).__init__(os_creds)
42
43         self.user_settings = user_settings
44         self.__user = None
45
46     def initialize(self):
47         """
48         Creates the user in OpenStack if it does not already exist
49         :return: The User domain object
50         """
51         super(self.__class__, self).initialize()
52
53         self.__user = keystone_utils.get_user(self._keystone,
54                                               self.user_settings.name)
55         return self.__user
56
57     def create(self, cleanup=False):
58         """
59         Creates a User if one does not already exist
60         :return: The User domain object
61         """
62         self.initialize()
63         if not self.__user:
64             self.__user = keystone_utils.create_user(self._keystone,
65                                                      self.user_settings)
66         return self.__user
67
68     def clean(self):
69         """
70         Cleanse environment of user
71         :return: void
72         """
73         if self.__user:
74             try:
75                 keystone_utils.delete_user(self._keystone, self.__user)
76             except NotFound:
77                 pass
78             self.__user = None
79
80     def get_user(self):
81         """
82         Returns the OpenStack user object populated in create()
83         :return: the Object or None if not created
84         """
85         return self.__user
86
87     def get_os_creds(self, project_name=None):
88         """
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
91                              credentials
92         :return:
93         """
94         return OSCreds(
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)
112
113
114 class UserSettings(UserConfig):
115     """
116     Class to hold the configuration settings required for creating OpenStack
117     user objects
118     deprecated
119     """
120
121     def __init__(self, **kwargs):
122         from warnings import warn
123         warn('Use snaps.config.user.UserConfig instead',
124              DeprecationWarning)
125         super(self.__class__, self).__init__(**kwargs)