From fe6f9e7a4dbca1b1e6336e7ae402f4af65cd38e8 Mon Sep 17 00:00:00 2001 From: Hongbin Lu Date: Sun, 14 Apr 2019 21:47:05 +0000 Subject: [PATCH] Use ISO8601 format for timestamp fields Most OpenStack services use format ISO8601 for timestamp fields like 'created_at' or 'updated_at'. This commit makes Zun adopt this convention. Change-Id: I900b2f106d2ea0c044140077dfda39e1402a09ac --- .../samples/container-action-get-resp.json | 6 +++--- .../samples/container-actions-list-resp.json | 4 ++-- api-ref/source/samples/service-get-resp.json | 6 +++--- zun/api/controllers/v1/views/actions_view.py | 20 ++++++++++++++----- zun/api/controllers/v1/views/services_view.py | 8 +++++++- zun/common/utils.py | 4 ++++ 6 files changed, 34 insertions(+), 14 deletions(-) diff --git a/api-ref/source/samples/container-action-get-resp.json b/api-ref/source/samples/container-action-get-resp.json index b1555f8e5..2077c1ea3 100644 --- a/api-ref/source/samples/container-action-get-resp.json +++ b/api-ref/source/samples/container-action-get-resp.json @@ -3,9 +3,9 @@ "events": [ { "event": "container__do_container_start", - "finish_time": "2018-03-04 17:03:07+00:00", + "finish_time": "2018-03-04T17:03:07.000000", "result": "Success", - "start_time": "2018-03-04 17:02:57+00:00", + "start_time": "2018-03-04T17:02:57.000000", "traceback": null } ], @@ -13,6 +13,6 @@ "message": null, "project_id": "853719b303ef4858a195535eb520e58d", "request_id": "req-3293a3f1-b44c-4609-b8d2-d81b105636b8", - "start_time": "2018-03-04 17:02:54+00:00", + "start_time": "2018-03-04T17:02:54.000000", "user_id": "22e81669093742b7a74b1d715a9a5813" } diff --git a/api-ref/source/samples/container-actions-list-resp.json b/api-ref/source/samples/container-actions-list-resp.json index d8591fbc5..5a8f8f9b5 100644 --- a/api-ref/source/samples/container-actions-list-resp.json +++ b/api-ref/source/samples/container-actions-list-resp.json @@ -6,7 +6,7 @@ "message": null, "project_id": "853719b303ef4858a195535eb520e58d", "request_id": "req-25517360-b757-47d3-be45-0e8d2a01b36a", - "start_time": "2018-03-04 19:48:49+00:00", + "start_time": "2018-03-04T19:48:49.000000", "user_id": "22e81669093742b7a74b1d715a9a5813" }, { @@ -15,7 +15,7 @@ "message": null, "project_id": "853719b303ef4858a195535eb520e58d", "request_id": "req-3293a3f1-b44c-4609-b8d2-d81b105636b8", - "start_time": "2018-03-04 17:02:54+00:00", + "start_time": "2018-03-04T17:02:54.000000", "user_id": "22e81669093742b7a74b1d715a9a5813" } ] diff --git a/api-ref/source/samples/service-get-resp.json b/api-ref/source/samples/service-get-resp.json index 9e087367a..a481f8072 100644 --- a/api-ref/source/samples/service-get-resp.json +++ b/api-ref/source/samples/service-get-resp.json @@ -4,13 +4,13 @@ "binary": "zun-compute", "availability_zone": "nova", "state": "up", - "created_at": "2017-02-01 03:25:07+00:00", - "updated_at": "2017-02-01 06:13:07+00:00", + "created_at": "2017-02-01T03:25:07.000000", + "updated_at": "2017-02-01T06:13:07.000000", "report_count": 166, "disabled": false, "host": "instance-1", "forced_down": false, - "last_seen_up": "2017-02-01 06:13:07+00:00", + "last_seen_up": "2017-02-01T06:13:07.000000", "disabled_reason": null, "id": 1 } diff --git a/zun/api/controllers/v1/views/actions_view.py b/zun/api/controllers/v1/views/actions_view.py index ac40828fd..cb7a7e62d 100644 --- a/zun/api/controllers/v1/views/actions_view.py +++ b/zun/api/controllers/v1/views/actions_view.py @@ -11,8 +11,11 @@ # License for the specific language governing permissions and limitations # under the License. +import datetime import itertools +from zun.common import utils + _action_keys = ( 'action', @@ -39,7 +42,10 @@ def format_action(action): if key not in _action_keys: return - yield (key, value) + if isinstance(value, datetime.datetime): + yield (key, utils.strtime(value)) + else: + yield (key, value) return dict(itertools.chain.from_iterable( transform(k, v) for k, v in action.as_dict().items())) @@ -50,11 +56,15 @@ def format_event(event, show_traceback=False): if key not in _action_event_keys: return - if key == 'traceback' and not show_traceback: - # By default, non-admins are not allowed to see traceback details. - yield (key, None) + if isinstance(value, datetime.datetime): + yield (key, utils.strtime(value)) else: - yield (key, value) + if key == 'traceback' and not show_traceback: + # By default, non-admins are not allowed to see traceback + # details. + yield (key, None) + else: + yield (key, value) return dict(itertools.chain.from_iterable( transform(k, v) for k, v in event.as_dict().items())) diff --git a/zun/api/controllers/v1/views/services_view.py b/zun/api/controllers/v1/views/services_view.py index ea84c2ccd..901533911 100644 --- a/zun/api/controllers/v1/views/services_view.py +++ b/zun/api/controllers/v1/views/services_view.py @@ -11,8 +11,11 @@ # License for the specific language governing permissions and limitations # under the License. +import datetime import itertools +from zun.common import utils + _basic_keys = ( 'availability_zone', @@ -34,7 +37,10 @@ def format_service(service): def transform(key, value): if key not in _basic_keys: return - yield (key, value) + if isinstance(value, datetime.datetime): + yield (key, utils.strtime(value)) + else: + yield (key, value) return dict( itertools.chain.from_iterable( diff --git a/zun/common/utils.py b/zun/common/utils.py index 0e4b8eb0c..44872ca98 100644 --- a/zun/common/utils.py +++ b/zun/common/utils.py @@ -685,3 +685,7 @@ def decode_file_data(data): return base64.b64decode(data) except (TypeError, binascii.Error): raise exception.Base64Exception() + + +def strtime(at): + return at.strftime("%Y-%m-%dT%H:%M:%S.%f")