@@ -31,6 +31,7 @@ import {
3131import {
3232 NotImplemented ,
3333 errIfExists ,
34+ errIfInvalidDiskSize ,
3435 getStartAndEndTime ,
3536 getTimestamps ,
3637 paginated ,
@@ -156,6 +157,7 @@ export const handlers = makeHandlers({
156157 const project = lookupProject ( params . path )
157158
158159 errIfExists ( db . disks , { name : body . name , project_id : project . id } )
160+ errIfInvalidDiskSize ( params . path , body )
159161
160162 const { name, description, size, disk_source } = body
161163 const newDisk : Json < Api . Disk > = {
@@ -243,8 +245,69 @@ export const handlers = makeHandlers({
243245
244246 errIfExists ( db . instances , { name : body . name , project_id : project . id } )
245247
248+ const instanceId = uuid ( )
249+
250+ for ( const diskParams of body . disks || [ ] ) {
251+ if ( diskParams . type === 'create' ) {
252+ errIfExists ( db . disks , { name : diskParams . name , project_id : project . id } )
253+ errIfInvalidDiskSize ( params . path , diskParams )
254+ const { size, name, description, disk_source } = diskParams
255+ const newDisk : Json < Api . Disk > = {
256+ id : uuid ( ) ,
257+ name,
258+ description,
259+ size,
260+ project_id : project . id ,
261+ state : { state : 'attached' , instance : instanceId } ,
262+ device_path : '/mnt/disk' ,
263+ block_size : disk_source . type === 'blank' ? disk_source . block_size : 4096 ,
264+ ...getTimestamps ( ) ,
265+ }
266+ db . disks . push ( newDisk )
267+ } else {
268+ const disk = lookupDisk ( { ...params . path , diskName : diskParams . name } )
269+ disk . state = { state : 'attached' , instance : instanceId }
270+ }
271+ }
272+
273+ if ( body . network_interfaces ?. type === 'default' ) {
274+ db . networkInterfaces . push ( {
275+ id : uuid ( ) ,
276+ description : 'The default network interface' ,
277+ instance_id : instanceId ,
278+ primary : true ,
279+ mac : '00:00:00:00:00:00' ,
280+ ip : '127.0.0.1' ,
281+ name : 'default' ,
282+ vpc_id : uuid ( ) ,
283+ subnet_id : uuid ( ) ,
284+ ...getTimestamps ( ) ,
285+ } )
286+ } else if ( body . network_interfaces ?. type === 'create' ) {
287+ body . network_interfaces . params . forEach (
288+ ( { name, description, ip, subnet_name, vpc_name } , i ) => {
289+ db . networkInterfaces . push ( {
290+ id : uuid ( ) ,
291+ name,
292+ description,
293+ instance_id : instanceId ,
294+ primary : i === 0 ? true : false ,
295+ mac : '00:00:00:00:00:00' ,
296+ ip : ip || '127.0.0.1' ,
297+ vpc_id : lookupVpc ( { ...params . path , vpcName : vpc_name } ) . id ,
298+ subnet_id : lookupVpcSubnet ( {
299+ ...params . path ,
300+ vpcName : vpc_name ,
301+ subnetName : subnet_name ,
302+ } ) . id ,
303+ ...getTimestamps ( ) ,
304+ } )
305+ }
306+ )
307+ }
308+
246309 const newInstance : Json < Api . Instance > = {
247- id : uuid ( ) ,
310+ id : instanceId ,
248311 project_id : project . id ,
249312 ...pick ( body , 'name' , 'description' , 'hostname' , 'memory' , 'ncpus' ) ,
250313 ...getTimestamps ( ) ,
0 commit comments