33import yaml
44import json
55import subprocess
6+ import time
7+
8+ from jsonschema import validate
69
710from .config import INSTANCES_PATH , ADDONS_PATH
8- from .meta import load_addons_meta
911
1012
1113def command (cmd , * args , output_type = "text" ):
@@ -23,6 +25,146 @@ def command(cmd, *args, output_type="text"):
2325get_plan_path = lambda instance_id : os .path .join (get_instance_path (instance_id ), "plan" ) # noqa
2426
2527
28+ INSTANCE_META_SCHEMA = {
29+ "type" : "object" ,
30+ "properties" : {
31+ "id" : {"type" : "string" },
32+ "details" : {
33+ "type" : "object" ,
34+ "properties" : {
35+ "service_id" : {"type" : "string" },
36+ "plan_id" : {"type" : "string" },
37+ "context" : {"type" : "object" },
38+ "parameters" : {
39+ 'oneOf' : [{'type' : 'object' }, {'type' : 'null' }]
40+ },
41+ },
42+ "required" : [
43+ "service_id" , "plan_id" , "context"
44+ ]
45+ },
46+ "last_operation" : {
47+ "type" : "object" ,
48+ "properties" : {
49+ "state" : {"type" : "string" },
50+ "description" : {"type" : "string" }
51+ }
52+ },
53+ "last_modified_time" : {"type" : "number" }
54+ },
55+ }
56+
57+
58+ def load_instance_meta (instance_id ):
59+ file = get_instance_file (instance_id )
60+ with open (file ) as f :
61+ data = json .load (f )
62+ validate (instance = data , schema = INSTANCE_META_SCHEMA )
63+ return data
64+
65+
66+ def dump_instance_meta (instance_id , data ):
67+ data ["last_modified_time " ] = time .time ()
68+ file = get_instance_file (instance_id )
69+ validate (instance = data , schema = INSTANCE_META_SCHEMA )
70+ with open (file , "w" ) as f :
71+ f .write (json .dumps (data , sort_keys = True , indent = 2 ))
72+
73+
74+ BINDING_META_SCHEMA = {
75+ "type" : "object" ,
76+ "properties" : {
77+ "id" : {"type" : "string" },
78+ "credentials" : {
79+ "type" : "object" ,
80+ },
81+ "last_operation" : {
82+ "type" : "object" ,
83+ "properties" : {
84+ "state" : {"type" : "string" },
85+ "description" : {"type" : "string" }
86+ }
87+ },
88+ "last_modified_time" : {"type" : "number" }
89+ }
90+ }
91+
92+
93+ def load_binding_meta (instance_id ):
94+ file = os .path .join (get_instance_path (instance_id ), "binding.json" )
95+ with open (file , 'r' ) as f :
96+ data = json .loads (f .read ())
97+ validate (instance = data , schema = INSTANCE_META_SCHEMA )
98+ return data
99+
100+
101+ def dump_binding_meta (instance_id , data ):
102+ data ["last_modified_time " ] = time .time ()
103+ file = os .path .join (get_instance_path (instance_id ), "binding.json" )
104+ validate (instance = data , schema = INSTANCE_META_SCHEMA )
105+ with open (file , "w" ) as f :
106+ f .write (json .dumps (data , sort_keys = True , indent = 2 ))
107+
108+
109+ ADDONS_META_SCHEMA = {
110+ "type" : "object" ,
111+ "patternProperties" : {
112+ ".*" : {
113+ "id" : {"type" : "string" },
114+ "name" : {"type" : "string" },
115+ "version" : {"type" : "string" },
116+ "bindable" : {"type" : "boolean" },
117+ "instances_retrievable" : {"type" : "boolean" },
118+ "bindings_retrievable" : {"type" : "boolean" },
119+ "allow_context_updates" : {"type" : "boolean" },
120+ "description" : {"type" : "string" },
121+ "tags" : {"type" : "string" },
122+ "requires" : {"type" : "array" },
123+ "metadata" : {"type" : "object" },
124+ "plan_updateable" : {"type" : "boolean" },
125+ "dashboard_client" : {"type" : "object" },
126+ "plans" : {
127+ "type" : "object" ,
128+ "id" : {"type" : "string" },
129+ "name" : {"type" : "string" },
130+ "description" : {"type" : "string" },
131+ "metadata" : {"type" : "object" },
132+ "free" : {"type" : "boolean" },
133+ "bindable" : {"type" : "boolean" },
134+ "binding_rotatable" : {"type" : "boolean" },
135+ "plan_updateable" : {"type" : "boolean" },
136+ "schemas" : {"type" : "object" },
137+ "maximum_polling_duration" : {"type" : "integer" },
138+ "maintenance_info" : {"type" : "object" },
139+ "required" : [
140+ "id" , "name" , "description"
141+ ]
142+ },
143+ "required" : [
144+ "id" , "name" , "description" , "bindable" , "version" , "plans"
145+ ]
146+ }
147+ }
148+ }
149+
150+
151+ def load_addons_meta ():
152+ file = os .path .join (ADDONS_PATH , "addons.json" )
153+ with open (file , 'r' ) as f :
154+ data = json .loads (f .read ())
155+ if not data :
156+ return {}
157+ validate (instance = data , schema = INSTANCE_META_SCHEMA )
158+ return data
159+
160+
161+ def dump_addons_meta (data ):
162+ file = os .path .join (ADDONS_PATH , "addons.json" )
163+ validate (instance = data , schema = INSTANCE_META_SCHEMA )
164+ with open (file , "w" ) as f :
165+ f .write (json .dumps (data , sort_keys = True , indent = 2 ))
166+
167+
26168def get_addon_meta (service_id ):
27169 services = load_addons_meta ()
28170 service = [addon for addon in [addons for _ , addons in services .items ()]
0 commit comments