Add ocloud watcher and tests
[pti/o2.git] / o2ims / domain / stx_object.py
1 # Copyright (C) 2021 Wind River Systems, Inc.\r
2 #\r
3 #  Licensed under the Apache License, Version 2.0 (the "License");\r
4 #  you may not use this file except in compliance with the License.\r
5 #  You may obtain a copy of the License at\r
6 #\r
7 #      http://www.apache.org/licenses/LICENSE-2.0\r
8 #\r
9 #  Unless required by applicable law or agreed to in writing, software\r
10 #  distributed under the License is distributed on an "AS IS" BASIS,\r
11 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12 #  See the License for the specific language governing permissions and\r
13 #  limitations under the License.\r
14 \r
15 # from dataclasses import dataclass\r
16 # import datetime\r
17 import json\r
18 \r
19 \r
20 class MismatchedModel(Exception):\r
21     pass\r
22 \r
23 \r
24 class StxGenericModel:\r
25     def __init__(self, api_response: dict = None) -> None:\r
26         if api_response:\r
27             self.id = api_response.uuid\r
28             self.content = json.dumps(api_response.to_dict())\r
29             self.updatetime = api_response.updated_at\r
30             self.createtime = api_response.created_at\r
31             self.name = api_response.name\r
32 \r
33     def is_outdated(self, newmodel) -> bool:\r
34         return self.updatetime < newmodel.updatetime\r
35 \r
36     def update_by(self, newmodel) -> None:\r
37         if self.id != newmodel.id:\r
38             raise MismatchedModel("Mismatched model")\r
39         self.name = newmodel.name\r
40 \r
41         self.content = newmodel.content\r
42         self.createtime = newmodel.createtime\r
43         self.updatetime = newmodel.updatetime\r