12
12
from ingredients_db .models .network import Network , NetworkState
13
13
from ingredients_db .models .network_port import NetworkPort
14
14
from ingredients_db .models .public_key import PublicKey
15
+ from ingredients_db .models .region import Region , RegionState
15
16
from ingredients_db .models .task import Task
17
+ from ingredients_db .models .zones import Zone , ZoneState
16
18
from ingredients_http .request_methods import RequestMethods
17
19
from ingredients_http .route import Route
18
20
from ingredients_http .router import Router
@@ -34,19 +36,28 @@ def __init__(self):
34
36
def create (self ):
35
37
request : RequestCreateInstance = cherrypy .request .model
36
38
37
- # TODO: allow multiple instances to be created at once add -# to the name
38
-
39
39
with cherrypy .request .db_session () as session :
40
40
project = cherrypy .request .project
41
41
42
42
instance = session .query (Instance ).filter (Instance .project_id == project .id ).filter (
43
43
Instance .name == request .name ).first ()
44
-
45
44
if instance is not None :
46
45
raise cherrypy .HTTPError (409 , 'An instance already exists with the requested name.' )
47
46
48
- image = session .query (Image ).filter (Image .id == request .image_id ).first ()
47
+ region = session .query (Region ).filter (Region .id == request .region_id ).first ()
48
+ if region is None :
49
+ raise cherrypy .HTTPError (404 , "A region with the requested id does not exist." )
50
+
51
+ if region .state != RegionState .CREATED :
52
+ raise cherrypy .HTTPError (412 ,
53
+ "The requested region is not in the following state: %s" %
54
+ RegionState .CREATED .value )
49
55
56
+ if region .schedulable is False :
57
+ raise cherrypy .HTTPError (412 , "The requested region is not currently schedulable." )
58
+
59
+ image = session .query (Image ).filter (Image .id == request .image_id ).filter (
60
+ Image .region_id == region .id ).first ()
50
61
if image is None :
51
62
raise cherrypy .HTTPError (404 , "An image with the requested id does not exist." )
52
63
@@ -64,15 +75,30 @@ def create(self):
64
75
# Image is public so don't error
65
76
pass
66
77
67
- network = session .query (Network ).filter (Network .id == request .network_id ).first ()
68
-
78
+ network = session .query (Network ).filter (Network .id == request .network_id ).filter (
79
+ Network . region_id == region . id ). first ()
69
80
if network is None :
70
81
raise cherrypy .HTTPError (404 , "A network with the requested id does not exist." )
71
82
72
83
if network .state != NetworkState .CREATED :
73
84
raise cherrypy .HTTPError (412 , "The requested network is not in the '%s' state" % (
74
85
NetworkState .CREATED .value ))
75
86
87
+ zone = None
88
+ if request .zone_id is not None :
89
+ zone = session .query (Zone ).filter (Zone .id == request .zone_id ).filter (
90
+ Zone .region_id == region .id ).first ()
91
+ if zone is None :
92
+ raise cherrypy .HTTPError (404 , "A zone with the requested id does not exist." )
93
+
94
+ if zone .state != ZoneState .CREATED :
95
+ raise cherrypy .HTTPError (412 ,
96
+ "The requested zone is not in the following state: %s" %
97
+ ZoneState .CREATED .value )
98
+
99
+ if zone .schedulable is False :
100
+ raise cherrypy .HTTPError (412 , "The requested zone is not currently schedulable." )
101
+
76
102
network_port = NetworkPort ()
77
103
network_port .network_id = network .id
78
104
session .add (network_port )
@@ -84,6 +110,11 @@ def create(self):
84
110
instance .project_id = project .id
85
111
instance .network_port_id = network_port .id
86
112
instance .tags = request .tags
113
+
114
+ instance .region_id = region .id
115
+ if zone is not None :
116
+ instance .zone_id = zone .id
117
+
87
118
session .add (instance )
88
119
session .flush ()
89
120
@@ -120,11 +151,25 @@ def get(self, instance_id: uuid.UUID):
120
151
@cherrypy .tools .model_params (cls = ParamsListInstance )
121
152
@cherrypy .tools .model_out_pagination (cls = ResponseInstance )
122
153
@cherrypy .tools .enforce_policy (policy_name = "instances:list" )
123
- def list (self , image_id : uuid . UUID , limit : int , marker : uuid .UUID ):
154
+ def list (self , image_id , region_id , zone_id , limit : int , marker : uuid .UUID ):
124
155
# TODO: allow filtering by tags
125
156
project = cherrypy .request .project
126
157
starting_query = Query (Instance ).filter (Instance .project_id == project .id )
127
- return self .mount .paginate (Instance , ResponseInstance , limit , marker , starting_query = starting_query )
158
+ if image_id is not None :
159
+ starting_query = starting_query .filter (Instance .image_id == image_id )
160
+ if region_id is not None :
161
+ with cherrypy .request .db_session () as session :
162
+ region = session .query (Region ).filter (Region .id == region_id ).first ()
163
+ if region is None :
164
+ raise cherrypy .HTTPError (404 , "A region with the requested id does not exist." )
165
+ starting_query = starting_query .filter (Instance .region_id == region .id )
166
+ if zone_id is not None :
167
+ with cherrypy .request .db_session () as session :
168
+ zone = session .query (Zone ).filter (Zone .id == zone_id ).first ()
169
+ if zone is None :
170
+ raise cherrypy .HTTPError (404 , "A zone with the requested id does not exist." )
171
+ starting_query = starting_query .filter (Instance .zone_id == zone_id )
172
+ return self .paginate (Instance , ResponseInstance , limit , marker , starting_query = starting_query )
128
173
129
174
@Route (route = '{instance_id}' , methods = [RequestMethods .DELETE ])
130
175
@cherrypy .tools .project_scope ()
@@ -240,13 +285,17 @@ def action_image(self, instance_id: uuid.UUID):
240
285
raise cherrypy .HTTPError (409 , "Can only image an instance in the following state: %s" %
241
286
InstanceState .STOPPED .value )
242
287
288
+ region = session .query (Region ).join (Zone , Region .id == Zone .region_id ).filter (
289
+ Zone .id == instance .zone_id ).one ()
290
+
243
291
instance .state = InstanceState .IMAGING
244
292
245
293
image = Image ()
246
294
image .name = request .name
247
295
image .file_name = str (instance .id )
248
296
image .visibility = request .visibility
249
297
image .project_id = instance .project_id
298
+ image .region_id = region .id
250
299
251
300
session .add (image )
252
301
session .flush ()
0 commit comments