Merge "Replace datetime calculations with utility functions"
This commit is contained in:
commit
e1872739c6
|
@ -10,6 +10,7 @@
|
||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
import datetime
|
||||||
import functools
|
import functools
|
||||||
import inspect
|
import inspect
|
||||||
import logging
|
import logging
|
||||||
|
@ -192,3 +193,35 @@ def parse_isotime(timestr):
|
||||||
raise ValueError(six.text_type(e))
|
raise ValueError(six.text_type(e))
|
||||||
except TypeError as e:
|
except TypeError as e:
|
||||||
raise ValueError(six.text_type(e))
|
raise ValueError(six.text_type(e))
|
||||||
|
|
||||||
|
|
||||||
|
def from_utcnow(**timedelta_kwargs):
|
||||||
|
"""Calculate the time in the future from utcnow.
|
||||||
|
|
||||||
|
:param \*\*timedelta_kwargs:
|
||||||
|
Passed directly to :class:`datetime.timedelta` to add to the current
|
||||||
|
time in UTC.
|
||||||
|
:returns:
|
||||||
|
The time in the future based on ``timedelta_kwargs``.
|
||||||
|
:rtype:
|
||||||
|
datetime.datetime
|
||||||
|
"""
|
||||||
|
now = datetime.datetime.utcnow()
|
||||||
|
delta = datetime.timedelta(**timedelta_kwargs)
|
||||||
|
return now + delta
|
||||||
|
|
||||||
|
|
||||||
|
def before_utcnow(**timedelta_kwargs):
|
||||||
|
"""Calculate the time in the past from utcnow.
|
||||||
|
|
||||||
|
:param \*\*timedelta_kwargs:
|
||||||
|
Passed directly to :class:`datetime.timedelta` to subtract from the
|
||||||
|
current time in UTC.
|
||||||
|
:returns:
|
||||||
|
The time in the past based on ``timedelta_kwargs``.
|
||||||
|
:rtype:
|
||||||
|
datetime.datetime
|
||||||
|
"""
|
||||||
|
now = datetime.datetime.utcnow()
|
||||||
|
delta = datetime.timedelta(**timedelta_kwargs)
|
||||||
|
return now - delta
|
||||||
|
|
|
@ -14,7 +14,6 @@
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
import datetime
|
|
||||||
import functools
|
import functools
|
||||||
|
|
||||||
from keystoneauth import _utils as utils
|
from keystoneauth import _utils as utils
|
||||||
|
@ -86,8 +85,7 @@ class AccessInfo(object):
|
||||||
norm_expires = utils.normalize_time(self.expires)
|
norm_expires = utils.normalize_time(self.expires)
|
||||||
# (gyee) should we move auth_token.will_expire_soon() to timeutils
|
# (gyee) should we move auth_token.will_expire_soon() to timeutils
|
||||||
# instead of duplicating code here?
|
# instead of duplicating code here?
|
||||||
soon = (datetime.datetime.utcnow() + datetime.timedelta(
|
soon = utils.from_utcnow(seconds=stale_duration)
|
||||||
seconds=stale_duration))
|
|
||||||
return norm_expires < soon
|
return norm_expires < soon
|
||||||
|
|
||||||
def has_service_catalog(self):
|
def has_service_catalog(self):
|
||||||
|
|
|
@ -10,8 +10,6 @@
|
||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import datetime
|
|
||||||
|
|
||||||
from keystoneauth import _utils as utils
|
from keystoneauth import _utils as utils
|
||||||
|
|
||||||
__all__ = ['DiscoveryList',
|
__all__ = ['DiscoveryList',
|
||||||
|
@ -38,8 +36,7 @@ class DiscoveryBase(dict):
|
||||||
|
|
||||||
self.id = id
|
self.id = id
|
||||||
self.status = status or 'stable'
|
self.status = status or 'stable'
|
||||||
self.updated = updated or (datetime.datetime.utcnow() -
|
self.updated = updated or utils.before_utcnow(days=_DEFAULT_DAYS_AGO)
|
||||||
datetime.timedelta(days=_DEFAULT_DAYS_AGO))
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def id(self):
|
def id(self):
|
||||||
|
|
|
@ -53,7 +53,7 @@ class Token(dict):
|
||||||
self.audit_id = audit_id or uuid.uuid4().hex
|
self.audit_id = audit_id or uuid.uuid4().hex
|
||||||
|
|
||||||
if not issued:
|
if not issued:
|
||||||
issued = datetime.datetime.utcnow() - datetime.timedelta(minutes=2)
|
issued = _utils.before_utcnow(minutes=2)
|
||||||
if not expires:
|
if not expires:
|
||||||
expires = issued + datetime.timedelta(hours=1)
|
expires = issued + datetime.timedelta(hours=1)
|
||||||
|
|
||||||
|
|
|
@ -76,7 +76,7 @@ class Token(dict):
|
||||||
self.methods.extend(methods)
|
self.methods.extend(methods)
|
||||||
|
|
||||||
if not issued:
|
if not issued:
|
||||||
issued = datetime.datetime.utcnow() - datetime.timedelta(minutes=2)
|
issued = _utils.before_utcnow(minutes=2)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.issued = issued
|
self.issued = issued
|
||||||
|
|
|
@ -11,11 +11,11 @@
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import abc
|
import abc
|
||||||
import datetime
|
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
import six
|
import six
|
||||||
|
|
||||||
|
from keystoneauth import _utils
|
||||||
from keystoneauth import access
|
from keystoneauth import access
|
||||||
from keystoneauth.auth import base
|
from keystoneauth.auth import base
|
||||||
from keystoneauth.auth import identity
|
from keystoneauth.auth import identity
|
||||||
|
@ -190,7 +190,7 @@ class CommonIdentityTests(object):
|
||||||
self.assertEqual(self.TEST_URL, auth_url)
|
self.assertEqual(self.TEST_URL, auth_url)
|
||||||
|
|
||||||
def _create_expired_auth_plugin(self, **kwargs):
|
def _create_expired_auth_plugin(self, **kwargs):
|
||||||
expires = datetime.datetime.utcnow() - datetime.timedelta(minutes=20)
|
expires = _utils.before_utcnow(minutes=20)
|
||||||
expired_token = self.get_auth_data(expires=expires)
|
expired_token = self.get_auth_data(expires=expires)
|
||||||
expired_auth_ref = access.create(body=expired_token)
|
expired_auth_ref = access.create(body=expired_token)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue