CAL refactoring
[osm/SO.git] / rwcal / plugins / vala / rwcal_openstack / rift / rwcal / openstack / session / session_drv.py
1 #!/usr/bin/python
2
3 #
4 # Copyright 2017 RIFT.IO Inc
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 import logging
19 from .auth_drv import AuthDriver
20 from keystoneauth1 import session
21
22
23 class SessionDriver(object):
24 """
25 Authentication session class for openstack
26 """
27 def __init__(self, auth_method, version, cert_validate, logger = None, **kwargs):
28 """
29 Constructor for class SessionDriver
30 auth_method (string): At this point, only "password" based
31 authentication is supported. See AuthDriver.AuthMethod
32 for more details
33 version (string): Keystone API version
34 cert_validate (boolean): Boolean to indicate if certificate validation is required
35 logger (instance of logging.Logger)
36 kwargs a dictionary of following key/value pairs
37 {
38 username (string) : Username
39 password (string) : Password
40 auth_url (string) : Authentication URL
41 tenant_name(string): Tenant Name
42 user_domain_name (string) : User domain name
43 project_domain_name (string): Project domain name
44 region (string) : Region name
45 }
46
47 """
48 if logger is None:
49 self.log = logging.getLogger('rwcal.openstack.session')
50 self.log.setLevel(logging.DEBUG)
51 else:
52 self.log = logger
53
54 self._auth_url = kwargs['auth_url']
55
56 self._auth = AuthDriver(auth_method, version, logger, **kwargs)
57 self._sess = session.Session(auth=self._auth.auth_handle,
58 verify = cert_validate)
59
60 @property
61 def session(self):
62 return self._sess
63
64 @property
65 def auth_token(self):
66 """
67 Returns a valid Auth-Token
68 """
69 if not self._sess.auth.get_auth_state():
70 return self._sess.get_token()
71 else:
72 if self.will_expire_after():
73 self._sess.invalidate()
74 return self._sess.get_token()
75 else:
76 return self._sess.get_token()
77 @property
78 def auth_url(self):
79 return self._auth_url
80
81 def invalidate_auth_token(self):
82 """
83 This method will return a fresh token (in case of HTTP 401 response)
84 """
85 self._sess.invalidate()
86
87 @property
88 def auth_header(self):
89 return self._sess.auth.get_headers(self._sess)
90
91 @property
92 def project_id(self):
93 return self._sess.get_project_id()
94
95 @property
96 def user_id(self):
97 return self._sess.get_user_id()
98
99 def get_auth_state(self):
100 return self._sess.auth.get_auth_state()
101
102 def will_expire_after(self, timeout=180):
103 return self._sess.auth.auth_ref.will_expire_soon(stale_duration=timeout)
104
105
106