Refactor code to use oslo_utils.timeutils where appropriate

Where possible, changed all existing code to use the
standard oslo_utils.timeutils OpenStack helper module
instead of the core Python datetime module.

Change-Id: I8d778ba6f7cb0328bebd6bb8846ec1a74cf5f850
Closes-Bug: #1471691
This commit is contained in:
Julio
2015-07-09 07:22:05 -05:00
committed by Julio Ruano
parent 5eeeaf1468
commit 140ce90f67
17 changed files with 69 additions and 71 deletions

View File

@@ -10,12 +10,12 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import six
import time
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import timeutils
from senlin.common import context as req_context
from senlin.common import exception
@@ -167,17 +167,17 @@ class Action(object):
'outputs': self.outputs,
'depends_on': self.depends_on,
'depended_by': self.depended_by,
'created_time': datetime.datetime.utcnow(),
'created_time': timeutils.utcnow(),
'updated_time': self.updated_time,
'deleted_time': self.deleted_time,
'data': self.data,
}
if self.id:
values['updated_time'] = datetime.datetime.utcnow()
values['updated_time'] = timeutils.utcnow()
db_api.action_update(context, self.id, values)
else:
values['created_time'] = datetime.datetime.utcnow()
values['created_time'] = timeutils.utcnow()
action = db_api.action_create(context, values)
self.id = action.id

View File

@@ -10,10 +10,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import random
from oslo_log import log as logging
from oslo_utils import timeutils
from senlin.common import consts
from senlin.common import exception
@@ -426,7 +426,7 @@ class ClusterAction(base.Action):
cluster._load_runtime_data(self.context)
return self.RES_OK, ''
cluster.updated_time = datetime.datetime.utcnow()
cluster.updated_time = timeutils.utcnow()
cluster.status_reason = _('Cluster properties updated.')
res = cluster.store(self.context)
if not res:

View File

@@ -10,9 +10,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
from oslo_log import log as logging
from oslo_utils import timeutils
from senlin.db import api as db_api
from senlin.engine.actions import base
@@ -44,7 +43,7 @@ class PolicyAction(base.Action):
if self.action not in self.ACTIONS:
return self.RES_ERROR
self.store(start_time=datetime.datetime.utcnow(),
self.store(start_time=timeutils.utcnow(),
status=self.RUNNING)
cluster_id = kwargs.get('cluster_id')
@@ -63,12 +62,12 @@ class PolicyAction(base.Action):
# TODO(Qiming): Add DB API complete this.
self.store(end_time=datetime.datetime.utcnow(),
self.store(end_time=timeutils.utcnow(),
status=self.SUCCEEDED)
return self.RES_OK
def cancel(self):
self.store(end_time=datetime.datetime.utcnow(),
self.store(end_time=timeutils.utcnow(),
status=self.CANCELLED)
return self.RES_OK

View File

@@ -10,10 +10,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import logging
from oslo_log import log
from oslo_utils import timeutils
from senlin.common import exception
from senlin.common import i18n
@@ -166,7 +166,7 @@ class Event(object):
def critical(context, entity, action, status=None, status_reason=None,
timestamp=None):
timestamp = timestamp or datetime.datetime.utcnow()
timestamp = timestamp or timeutils.utcnow()
event = Event(timestamp, logging.CRITICAL, entity,
action=action, status=status, status_reason=status_reason,
user=context.user, project=context.project)
@@ -178,7 +178,7 @@ def critical(context, entity, action, status=None, status_reason=None,
def error(context, entity, action, status=None, status_reason=None,
timestamp=None):
timestamp = timestamp or datetime.datetime.utcnow()
timestamp = timestamp or timeutils.utcnow()
event = Event(timestamp, logging.ERROR, entity,
action=action, status=status, status_reason=status_reason,
user=context.user, project=context.project)
@@ -190,7 +190,7 @@ def error(context, entity, action, status=None, status_reason=None,
def warning(context, entity, action, status=None, status_reason=None,
timestamp=None):
timestamp = timestamp or datetime.datetime.utcnow()
timestamp = timestamp or timeutils.utcnow()
event = Event(timestamp, logging.WARNING, entity,
action=action, status=status, status_reason=status_reason,
user=context.user, project=context.project)
@@ -202,7 +202,7 @@ def warning(context, entity, action, status=None, status_reason=None,
def info(context, entity, action, status=None, status_reason=None,
timestamp=None):
timestamp = timestamp or datetime.datetime.utcnow()
timestamp = timestamp or timeutils.utcnow()
event = Event(timestamp, logging.INFO, entity,
action=action, status=status, status_reason=status_reason,
user=context.user, project=context.project)
@@ -214,7 +214,7 @@ def info(context, entity, action, status=None, status_reason=None,
def debug(context, entity, action, status=None, status_reason=None,
timestamp=None):
timestamp = timestamp or datetime.datetime.utcnow()
timestamp = timestamp or timeutils.utcnow()
event = Event(timestamp, logging.DEBUG, entity,
action=action, status=status, status_reason=status_reason,
user=context.user, project=context.project)

View File

@@ -10,10 +10,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import six
from oslo_log import log as logging
from oslo_utils import timeutils
from senlin.common import exception
from senlin.common.i18n import _
@@ -116,7 +116,7 @@ class Node(object):
db_api.node_update(context, self.id, values)
# TODO(Qiming): create event/log
else:
init_time = datetime.datetime.utcnow()
init_time = timeutils.utcnow()
self.init_time = init_time
values['init_time'] = init_time
node = db_api.node_create(context, values)
@@ -214,7 +214,7 @@ class Node(object):
'''Set status of the node.'''
values = {}
now = datetime.datetime.utcnow()
now = timeutils.utcnow()
if status == self.ACTIVE and self.status == self.CREATING:
self.created_time = values['created_time'] = now
elif status == self.ACTIVE and self.status == self.UPDATING:
@@ -310,7 +310,7 @@ class Node(object):
self.rt['profile'] = profile_base.Profile.load(context,
new_profile_id)
self.profile_id = new_profile_id
self.updated_time = datetime.datetime.utcnow()
self.updated_time = timeutils.utcnow()
self.store()
return res
@@ -318,7 +318,7 @@ class Node(object):
def do_join(self, context, cluster_id):
if self.cluster_id == cluster_id:
return True
timestamp = datetime.datetime.utcnow()
timestamp = timeutils.utcnow()
db_node = db_api.node_migrate(context, self.id, cluster_id,
timestamp)
self.cluster_id = cluster_id
@@ -332,7 +332,7 @@ class Node(object):
if self.cluster_id is None:
return True
timestamp = datetime.datetime.utcnow()
timestamp = timeutils.utcnow()
db_api.node_migrate(context, self.id, None, timestamp)
self.cluster_id = None
self.updated_time = timestamp

View File

@@ -10,7 +10,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
from oslo_utils import timeutils
from senlin.common import exception
@@ -104,7 +103,7 @@ class Policy(object):
def store(self, context):
'''Store the policy object into database table.'''
timestamp = datetime.datetime.utcnow()
timestamp = timeutils.utcnow()
values = {
'name': self.name,

View File

@@ -11,9 +11,9 @@
# under the License.
import copy
import datetime
from oslo_log import log as logging
from oslo_utils import timeutils
from senlin.common import exception
from senlin.common import schema
@@ -112,7 +112,7 @@ class Profile(object):
def store(self, ctx):
'''Store the profile into database and return its ID.'''
timestamp = datetime.datetime.utcnow()
timestamp = timeutils.utcnow()
values = {
'name': self.name,

View File

@@ -10,10 +10,11 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import json
import uuid
from oslo_utils import timeutils as tu
from senlin.db.sqlalchemy import api as db_api
from senlin.engine import parser
@@ -68,7 +69,7 @@ def create_cluster(ctx, profile, **kwargs):
'next_index': 1,
'timeout': 60,
'desired_capacity': 0,
'init_time': datetime.datetime.utcnow(),
'init_time': tu.utcnow(),
'status': 'INIT',
'status_reason': 'Just Initialized',
'metadata': {},

View File

@@ -10,9 +10,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import mock
from oslo_utils import timeutils as tu
from senlin.common import exception
from senlin.db.sqlalchemy import api as db_api
from senlin.tests.common import base
@@ -286,9 +287,8 @@ class DBAPIClusterTest(base.SenlinTestCase):
self.assertEqual(2, len(results))
def test_cluster_get_all_default_sort_dir(self):
dt = datetime.datetime
clusters = [shared.create_cluster(self.ctx, self.profile,
init_time=dt.utcnow())
init_time=tu.utcnow())
for x in range(3)]
st_db = db_api.cluster_get_all(self.ctx, sort_dir='asc')
@@ -298,9 +298,8 @@ class DBAPIClusterTest(base.SenlinTestCase):
self.assertEqual(clusters[2].id, st_db[2].id)
def test_cluster_get_all_str_sort_keys(self):
dt = datetime.datetime
clusters = [shared.create_cluster(self.ctx, self.profile,
created_time=dt.utcnow())
created_time=tu.utcnow())
for x in range(3)]
st_db = db_api.cluster_get_all(self.ctx, sort_keys='created_time')
@@ -322,9 +321,8 @@ class DBAPIClusterTest(base.SenlinTestCase):
self.assertEqual(expected_keys, used_sort_keys)
def test_cluster_get_all_marker(self):
dt = datetime.datetime
clusters = [shared.create_cluster(self.ctx, self.profile,
created_time=dt.utcnow())
created_time=tu.utcnow())
for x in range(3)]
cl_db = db_api.cluster_get_all(self.ctx, marker=clusters[1].id)
self.assertEqual(1, len(cl_db))

View File

@@ -10,7 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
from oslo_utils import timeutils as tu
from senlin.db.sqlalchemy import api as db_api
from senlin.tests.common import base
@@ -130,7 +130,7 @@ class DBAPIClusterPolicyTest(base.SenlinTestCase):
self.assertEqual(1, len(bindings))
self.assertIsNone(bindings[0].last_op)
timestamp = datetime.datetime.utcnow()
timestamp = tu.utcnow()
fields = {'last_op': timestamp}
db_api.cluster_policy_update(self.ctx, self.cluster.id, policy.id,
fields)

View File

@@ -10,9 +10,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import logging
from oslo_utils import timeutils as tu
from senlin.db.sqlalchemy import api as db_api
from senlin.tests.common import base
from senlin.tests.common import utils
@@ -34,7 +35,7 @@ class DBAPIEventTest(base.SenlinTestCase):
entity=None, action=None, status=None,
status_reason=None, deleted_time=None):
fake_timestamp = datetime.datetime.strptime(
fake_timestamp = tu.parse_strtime(
'2014-12-19 11:51:54.670244', '%Y-%m-%d %H:%M:%S.%f')
if entity:
@@ -71,9 +72,9 @@ class DBAPIEventTest(base.SenlinTestCase):
event = self.create_event(self.ctx)
ret_event = db_api.event_get(self.ctx, event.id)
self.assertIsNotNone(ret_event)
timestamp = datetime.datetime.strftime(ret_event.timestamp,
'%Y-%m-%d %H:%M:%S.%f')
self.assertEqual('2014-12-19 11:51:54.670244', timestamp)
tst_timestamp = tu.parse_strtime('2014-12-19 11:51:54.670244',
'%Y-%m-%d %H:%M:%S.%f')
self.assertEqual(tst_timestamp, ret_event.timestamp)
self.assertEqual(logging.INFO, ret_event.level)
self.assertEqual('', ret_event.obj_id)
self.assertEqual('', ret_event.obj_type)
@@ -168,13 +169,13 @@ class DBAPIEventTest(base.SenlinTestCase):
cluster1 = shared.create_cluster(self.ctx, self.profile)
event1 = self.create_event(self.ctx, entity=cluster1,
timestamp=datetime.datetime.utcnow(),
timestamp=tu.utcnow(),
action='action2')
event2 = self.create_event(self.ctx, entity=cluster1,
timestamp=datetime.datetime.utcnow(),
timestamp=tu.utcnow(),
action='action3')
event3 = self.create_event(self.ctx, entity=cluster1,
timestamp=datetime.datetime.utcnow(),
timestamp=tu.utcnow(),
action='action1')
events = db_api.event_get_all(self.ctx, sort_keys=['timestamp'])
@@ -204,7 +205,7 @@ class DBAPIEventTest(base.SenlinTestCase):
cluster2 = shared.create_cluster(self.ctx, self.profile)
# Simulate deleted events by setting 'deleted_time' to not-None
now = datetime.datetime.utcnow()
now = tu.utcnow()
self.create_event(self.ctx, entity=cluster1, deleted_time=now)
self.create_event(self.ctx, entity=cluster1)
self.create_event(self.ctx, entity=cluster2, deleted_time=now)

View File

@@ -10,10 +10,11 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import json
import six
from oslo_utils import timeutils as tu
from senlin.common import exception
from senlin.db.sqlalchemy import api as db_api
from senlin.tests.common import base
@@ -193,7 +194,7 @@ class DBAPINodeTest(base.SenlinTestCase):
node_ids = ['node1', 'node2', 'node3']
for v in node_ids:
shared.create_node(self.ctx, self.cluster, self.profile,
id=v, init_time=datetime.datetime.utcnow())
id=v, init_time=tu.utcnow())
nodes = db_api.node_get_all(self.ctx, limit=1)
self.assertEqual(1, len(nodes))
@@ -269,9 +270,8 @@ class DBAPINodeTest(base.SenlinTestCase):
self.assertEqual('001', nodes[2].id)
def test_node_get_all_default_sort_dir(self):
dt = datetime.datetime
nodes = [shared.create_node(self.ctx, None, self.profile,
init_time=dt.utcnow())
init_time=tu.utcnow())
for x in range(3)]
results = db_api.node_get_all(self.ctx, sort_dir='asc')
@@ -439,7 +439,7 @@ class DBAPINodeTest(base.SenlinTestCase):
def test_node_migrate_from_none(self):
node_orphan = shared.create_node(self.ctx, None, self.profile)
timestamp = datetime.datetime.utcnow()
timestamp = tu.utcnow()
node = db_api.node_migrate(self.ctx, node_orphan.id, self.cluster.id,
timestamp)
@@ -452,7 +452,7 @@ class DBAPINodeTest(base.SenlinTestCase):
def test_node_migrate_to_none(self):
node = shared.create_node(self.ctx, self.cluster, self.profile)
timestamp = datetime.datetime.utcnow()
timestamp = tu.utcnow()
node_new = db_api.node_migrate(self.ctx, node.id, None, timestamp)
self.assertEqual(timestamp, node_new.updated_time)
@@ -472,7 +472,7 @@ class DBAPINodeTest(base.SenlinTestCase):
self.assertEqual(2, cluster1.next_index)
self.assertEqual(1, cluster2.next_index)
timestamp = datetime.datetime.utcnow()
timestamp = tu.utcnow()
node_new = db_api.node_migrate(self.ctx, node.id, cluster2.id,
timestamp)
@@ -488,7 +488,7 @@ class DBAPINodeTest(base.SenlinTestCase):
self.assertEqual(2, cluster2.next_index)
# Migrate it back!
timestamp = datetime.datetime.utcnow()
timestamp = tu.utcnow()
node_new = db_api.node_migrate(self.ctx, node.id, cluster1.id,
timestamp)

View File

@@ -10,7 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
from oslo_utils import timeutils as tu
from senlin.common import exception
from senlin.db.sqlalchemy import api as db_api
@@ -209,7 +209,7 @@ class DBAPIPolicyTest(base.SenlinTestCase):
def test_policy_get_all_with_limit_marker(self):
ids = ['policy1', 'policy2', 'policy3']
for pid in ids:
timestamp = datetime.datetime.utcnow()
timestamp = tu.utcnow()
data = self.new_policy_data(id=pid, created_time=timestamp)
db_api.policy_create(self.ctx, data)
@@ -294,10 +294,9 @@ class DBAPIPolicyTest(base.SenlinTestCase):
self.assertEqual('002', policies[2].id)
def test_policy_get_all_default_sort_dir(self):
dt = datetime.datetime
policies = []
for x in range(3):
data = self.new_policy_data(created_time=dt.utcnow())
data = self.new_policy_data(created_time=tu.utcnow())
policies.append(db_api.policy_create(self.ctx, data))
results = db_api.policy_get_all(self.ctx, sort_dir='asc')

View File

@@ -10,9 +10,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import six
from oslo_utils import timeutils as tu
from senlin.common import exception
from senlin.db.sqlalchemy import api as db_api
from senlin.engine import parser
@@ -172,7 +173,7 @@ class DBAPIProfileTest(base.SenlinTestCase):
def test_profile_get_all_with_limit_marker(self):
ids = ['profile1', 'profile2', 'profile3']
for pid in ids:
timestamp = datetime.datetime.utcnow()
timestamp = tu.utcnow()
shared.create_profile(self.ctx, id=pid, created_time=timestamp)
# different limit settings
@@ -254,10 +255,9 @@ class DBAPIProfileTest(base.SenlinTestCase):
self.assertEqual('003', profiles[2].id)
def test_profile_get_all_default_sort_dir(self):
dt = datetime.datetime
profiles = []
for x in range(3):
profile = shared.create_profile(self.ctx, created_time=dt.utcnow())
profile = shared.create_profile(self.ctx, created_time=tu.utcnow())
profiles.append(profile)
results = db_api.profile_get_all(self.ctx, sort_dir='asc')

View File

@@ -10,7 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
from oslo_utils import timeutils as tu
from senlin.common import exception
from senlin.db.sqlalchemy import api as db_api
@@ -180,7 +180,7 @@ class DBAPIWebhookTest(base.SenlinTestCase):
for v in webhook_ids:
shared.create_webhook(self.ctx, self.obj_id, self.obj_type,
self.action, id=v,
created_time=datetime.datetime.utcnow())
created_time=tu.utcnow())
webhooks = db_api.webhook_get_all(self.ctx, limit=1)
self.assertEqual(1, len(webhooks))

View File

@@ -10,10 +10,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import webob
from oslo_utils import encodeutils
import webob
from oslo_utils import timeutils as tu
from senlin.common import serializers
from senlin.tests.common import base
@@ -28,7 +28,9 @@ class JSONResponseSerializerTest(base.SenlinTestCase):
self.assertEqual(expected, actual)
def test_to_json_with_date_format_value(self):
fixture = {"date": datetime.datetime(1, 3, 8, 2)}
test_date = tu.parse_strtime("0001-03-08T02:00:00",
'%Y-%m-%dT%H:%M:%S')
fixture = {"date": test_date}
expected = '{"date": "0001-03-08T02:00:00"}'
actual = serializers.JSONResponseSerializer().to_json(fixture)
self.assertEqual(expected, actual)

View File

@@ -10,10 +10,9 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import timeutils
from six.moves.urllib import parse
from senlin.common import exception
@@ -64,7 +63,7 @@ class Webhook(object):
:param context: Security context for DB operations.
"""
if not self.id:
self.created_time = datetime.datetime.utcnow()
self.created_time = timeutils.utcnow()
values = {
'name': self.name,
'user': self.user,