Browse Source

Merge "Replace datetime calculations with utility functions"

Jenkins 3 years ago
parent
commit
e1872739c6

+ 33
- 0
keystoneauth/_utils.py View File

@@ -10,6 +10,7 @@
10 10
 #    License for the specific language governing permissions and limitations
11 11
 #    under the License.
12 12
 
13
+import datetime
13 14
 import functools
14 15
 import inspect
15 16
 import logging
@@ -192,3 +193,35 @@ def parse_isotime(timestr):
192 193
         raise ValueError(six.text_type(e))
193 194
     except TypeError as e:
194 195
         raise ValueError(six.text_type(e))
196
+
197
+
198
+def from_utcnow(**timedelta_kwargs):
199
+    """Calculate the time in the future from utcnow.
200
+
201
+    :param \*\*timedelta_kwargs:
202
+        Passed directly to :class:`datetime.timedelta` to add to the current
203
+        time in UTC.
204
+    :returns:
205
+        The time in the future based on ``timedelta_kwargs``.
206
+    :rtype:
207
+        datetime.datetime
208
+    """
209
+    now = datetime.datetime.utcnow()
210
+    delta = datetime.timedelta(**timedelta_kwargs)
211
+    return now + delta
212
+
213
+
214
+def before_utcnow(**timedelta_kwargs):
215
+    """Calculate the time in the past from utcnow.
216
+
217
+    :param \*\*timedelta_kwargs:
218
+        Passed directly to :class:`datetime.timedelta` to subtract from the
219
+        current time in UTC.
220
+    :returns:
221
+        The time in the past based on ``timedelta_kwargs``.
222
+    :rtype:
223
+        datetime.datetime
224
+    """
225
+    now = datetime.datetime.utcnow()
226
+    delta = datetime.timedelta(**timedelta_kwargs)
227
+    return now - delta

+ 1
- 3
keystoneauth/access.py View File

@@ -14,7 +14,6 @@
14 14
 # See the License for the specific language governing permissions and
15 15
 # limitations under the License.
16 16
 
17
-import datetime
18 17
 import functools
19 18
 
20 19
 from keystoneauth import _utils as utils
@@ -86,8 +85,7 @@ class AccessInfo(object):
86 85
         norm_expires = utils.normalize_time(self.expires)
87 86
         # (gyee) should we move auth_token.will_expire_soon() to timeutils
88 87
         # instead of duplicating code here?
89
-        soon = (datetime.datetime.utcnow() + datetime.timedelta(
90
-                seconds=stale_duration))
88
+        soon = utils.from_utcnow(seconds=stale_duration)
91 89
         return norm_expires < soon
92 90
 
93 91
     def has_service_catalog(self):

+ 1
- 4
keystoneauth/fixture/discovery.py View File

@@ -10,8 +10,6 @@
10 10
 # License for the specific language governing permissions and limitations
11 11
 # under the License.
12 12
 
13
-import datetime
14
-
15 13
 from keystoneauth import _utils as utils
16 14
 
17 15
 __all__ = ['DiscoveryList',
@@ -38,8 +36,7 @@ class DiscoveryBase(dict):
38 36
 
39 37
         self.id = id
40 38
         self.status = status or 'stable'
41
-        self.updated = updated or (datetime.datetime.utcnow() -
42
-                                   datetime.timedelta(days=_DEFAULT_DAYS_AGO))
39
+        self.updated = updated or utils.before_utcnow(days=_DEFAULT_DAYS_AGO)
43 40
 
44 41
     @property
45 42
     def id(self):

+ 1
- 1
keystoneauth/fixture/v2.py View File

@@ -53,7 +53,7 @@ class Token(dict):
53 53
         self.audit_id = audit_id or uuid.uuid4().hex
54 54
 
55 55
         if not issued:
56
-            issued = datetime.datetime.utcnow() - datetime.timedelta(minutes=2)
56
+            issued = _utils.before_utcnow(minutes=2)
57 57
         if not expires:
58 58
             expires = issued + datetime.timedelta(hours=1)
59 59
 

+ 1
- 1
keystoneauth/fixture/v3.py View File

@@ -76,7 +76,7 @@ class Token(dict):
76 76
         self.methods.extend(methods)
77 77
 
78 78
         if not issued:
79
-            issued = datetime.datetime.utcnow() - datetime.timedelta(minutes=2)
79
+            issued = _utils.before_utcnow(minutes=2)
80 80
 
81 81
         try:
82 82
             self.issued = issued

+ 2
- 2
keystoneauth/tests/unit/auth/test_identity_common.py View File

@@ -11,11 +11,11 @@
11 11
 # under the License.
12 12
 
13 13
 import abc
14
-import datetime
15 14
 import uuid
16 15
 
17 16
 import six
18 17
 
18
+from keystoneauth import _utils
19 19
 from keystoneauth import access
20 20
 from keystoneauth.auth import base
21 21
 from keystoneauth.auth import identity
@@ -190,7 +190,7 @@ class CommonIdentityTests(object):
190 190
         self.assertEqual(self.TEST_URL, auth_url)
191 191
 
192 192
     def _create_expired_auth_plugin(self, **kwargs):
193
-        expires = datetime.datetime.utcnow() - datetime.timedelta(minutes=20)
193
+        expires = _utils.before_utcnow(minutes=20)
194 194
         expired_token = self.get_auth_data(expires=expires)
195 195
         expired_auth_ref = access.create(body=expired_token)
196 196
 

Loading…
Cancel
Save