11import os
2- import yaml
2+ import json
3+ from jsonschema import validate
34from .config import INSTANCES_PATH
45
5- INSTANCE_META_FILE = os .path .join (INSTANCES_PATH , "instance.yaml" )
6-
7- class InstanceMeta (object ):
8- """
9- {
10- "id": "instance_id",
11- "details": {
12- "service_id": "service_id",
13- "plan_id" : "plan_id",
14- "context" : {
15-
16- },
17- "parameters": {
18-
6+ INSTANCE_META_FILE = os .path .join (INSTANCES_PATH , "instance.json" )
7+ INSTANCE_META_SCHEMA = {
8+ "type" : "object" ,
9+ "properties" : {
10+ "id" : {"type" : "string" },
11+ "details" : {
12+ "type" : "object" ,
13+ "properties" : {
14+ "service_id" : {"type" : "string" },
15+ "plan_id" : {"type" : "string" },
16+ "context" : {"type" : "object" },
17+ "parameters" : {"type" : "object" },
1918 }
2019 },
2120 "last_operation" : {
22- "state": "Ready",
23- "description": "everything is ok."
21+ "type" : "object" ,
22+ "properties" : {
23+ "state" : {"type" : "string" },
24+ "description" : {"type" : "string" }
25+ }
2426 }
25- }
26- """
27- def __init__ (self , id , details , last_operation ):
28- self .id = id
29- self .details = details
30- self .last_operation = last_operation
31-
32- @classmethod
33- def load (cls , file = INSTANCE_META_FILE ):
34- with open (file ) as f :
35- data = yaml .load (f , Loader = yaml .Loader )
36- return cls (data ["id" ], data ["details" ], data ["last_operation" ])
27+ },
28+ }
29+
30+
31+ def load_instance_meta (file = INSTANCE_META_FILE ):
32+ with open (file ) as f :
33+ data = json .load (f )
34+ validate (instance = data , schema = INSTANCE_META_SCHEMA )
35+ return data
36+
3737
38- def dump (self , file = INSTANCE_META_FILE ):
39- with open (file , "w" ) as f :
40- yaml .dump (
41- {
42- "id" : self .id ,
43- "details" : self .details ,
44- "last_operation" : self .last_operation
45- },
46- Loader = yaml .Loader
47- )
38+ def dump_instance_meta (data , file = INSTANCE_META_FILE ):
39+ validate (instance = data , schema = INSTANCE_META_SCHEMA )
40+ with open (file , "w" ) as f :
41+ json .dump (f , data )
0 commit comments