Debian Codegraph @

magnum_tempest_plugin/tests/api/v1/models/cluster_templatepatch_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 ClusterTemplatePatchData(models.BaseModel):
19 """Data that encapsulates clustertemplatepatch attributes"""
20 pass
21
22
23 class ClusterTemplatePatchEntity(models.EntityModel):
24 """Model that represents a single instance of ClusterTemplatePatchData"""
25 ENTITY_NAME = 'clustertemplatepatch'
26 MODEL_TYPE = ClusterTemplatePatchData
27
28
29 class ClusterTemplatePatchCollection(models.CollectionModel):
30 """Model that represents a list of ClusterTemplatePatchData objects"""
31 MODEL_TYPE = ClusterTemplatePatchData
32 COLLECTION_NAME = 'clustertemplatepatchlist'
33
34 def to_json(self):
35 """Converts ClusterTemplatePatchCollection 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
39 json
40
41 This is required due to COLLECTION_NAME holding a list of objects that
42 needed to be converted to dict individually
43
44 :returns: json object
45 """
46
47 data = getattr(self, ClusterTemplatePatchCollection.COLLECTION_NAME)
48 collection = []
49 for d in data:
50 collection.append(d.to_dict())
51 return json.dumps(collection)
52
53 @classmethod
54 def from_dict(cls, data):
55 """Converts dict to ClusterTemplatePatchData
56
57 Converts data dict to list of ClusterTemplatePatchData objects and
58 stores it in COLLECTION_NAME
59
60 Example of dict data:
61
62 [{
63 "path": "/name",
64 "value": "myname",
65 "op": "replace"
66 }]
67
68 :param data: dict of patch data
69 :returns: json object
70 """
71
72 model = cls()
73 collection = []
74 for d in data:
75 collection.append(cls.MODEL_TYPE.from_dict(d))
76 setattr(model, cls.COLLECTION_NAME, collection)
77 return model