Debian Codegraph @

magnum_tempest_plugin/tests/api/v1/models/clusterpatch_model.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import json
14
15 from magnum_tempest_plugin.common import models
16
17
18 class ClusterPatchData(models.BaseModel):
19 """Data that encapsulates clusterpatch attributes"""
20 pass
21
22
23 class ClusterPatchEntity(models.EntityModel):
24 """Entity Model that represents a single instance of ClusterPatchData"""
25 ENTITY_NAME = 'clusterpatch'
26 MODEL_TYPE = ClusterPatchData
27
28
29 class ClusterPatchCollection(models.CollectionModel):
30 """Collection Model that represents a list of ClusterPatchData objects"""
31 MODEL_TYPE = ClusterPatchData
32 COLLECTION_NAME = 'clusterpatchlist'
33
34 def to_json(self):
35 """Converts ClusterPatchCollection to json
36
37 Retrieves list from COLLECTION_NAME attribute and converts each object
38 to dict, appending it to a list. Then converts the entire list to json
39
40 This is required due to COLLECTION_NAME holding a list of objects that
41 needed to be converted to dict individually
42
43 :returns: json object
44 """
45
46 data = getattr(self, ClusterPatchCollection.COLLECTION_NAME)
47 collection = []
48 for d in data:
49 collection.append(d.to_dict())
50 return json.dumps(collection)
51
52 @classmethod
53 def from_dict(cls, data):
54 """Converts dict to ClusterPatchData
55
56 Converts data dict to list of ClusterPatchData objects and stores it
57 in COLLECTION_NAME
58
59 Example of dict data:
60
61 [{
62 "path": "/name",
63 "value": "myname",
64 "op": "replace"
65 }]
66
67 :param data: dict of patch data
68 :returns: json object
69 """
70
71 model = cls()
72 collection = []
73 for d in data:
74 collection.append(cls.MODEL_TYPE.from_dict(d))
75 setattr(model, cls.COLLECTION_NAME, collection)
76 return model