ref: b1757dc4f6693dd5f531696214c76690b0206f60 cisco/api/admin.go -rw-r--r-- 10.2 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package main

import (
	"cisco/sdk"
	"github.com/kataras/iris"
)

func admin_user_list(ctx iris.Context) {
	var result []cisco.User
	var users []DbUser

	DB.Find(&users)
	for _, user := range users {
		result = append(result, cisco.User{
			Username: user.Username,
			Email:    user.Email,
			Role:     user.Role,
		})
	}

	ctx.JSON(&result)
}

func admin_quota_get(ctx iris.Context) {
	var quota DbQuotas
	var result cisco.Quota

	name := ctx.Params().Get("name")
	DB.First(&quota, "role = ?", name)
	if quota.ID == 0 {
		APIError(ctx, 404, "Quota does not exist")
		return
	}

	result = cisco.Quota{
		Instances: quota.MaxInstances,
		Ipv4:      quota.MaxIpv4,
		Storage:   quota.MaxStorage,
		Invites:   quota.MaxInvites,
	}

	ctx.JSON(&result)
}

func admin_quota_list(ctx iris.Context) {
	var result []cisco.Quota
	var quotas []DbQuotas

	DB.Find(&quotas)
	for _, quota := range quotas {
		result = append(result, cisco.Quota{
			Role:      quota.Role,
			Instances: quota.MaxInstances,
			Ipv4:      quota.MaxIpv4,
			Storage:   quota.MaxStorage,
			Invites:   quota.MaxInvites,
		})
	}

	ctx.JSON(&result)
}

func admin_quota_create(ctx iris.Context) {
	var quota DbQuotas

	name := ctx.Params().Get("name")
	DB.First(&quota, "role = ?", name)

	if quota.ID != 0 {
		APIError(ctx, 409, "A quota with this name already exists, please update it or delete it.")
		return
	}

	quota.Role = name
	DB.Create(&quota)
	ctx.StatusCode(iris.StatusCreated)
}

func admin_quota_delete(ctx iris.Context) {
	var quota DbQuotas

	name := ctx.Params().Get("name")
	DB.First(&quota, "role = ?", name)

	if quota.ID == 0 {
		APIError(ctx, 404, "Cannot find the desired quota")
		return
	}

	quota.Role = name
	DB.Delete(&quota)
}

func admin_quota_instance(ctx iris.Context) {
	var instance cisco.QuotaInstanceUpdate
	var quota DbQuotas

	name := ctx.Params().Get("name")
	ctx.ReadJSON(&instance)

	DB.First(&quota, "role = ?", name)
	if quota.ID == 0 {
		APIError(ctx, 404, "Cannot find the desired quota")
		return
	}

	DB.Model(&quota).Update("MaxInstances", instance.Instance)
}

func admin_quota_ipv4(ctx iris.Context) {
	var ipv4 cisco.QuotaIpv4Update
	var quota DbQuotas

	name := ctx.Params().Get("name")
	ctx.ReadJSON(&ipv4)

	DB.First(&quota, "role = ?", name)
	if quota.ID == 0 {
		APIError(ctx, 404, "Cannot find the desired quota")
		return
	}

	DB.Model(&quota).Update("MaxIpv4", ipv4.Ipv4)
}

func admin_quota_storage(ctx iris.Context) {
	var storage cisco.QuotaStorageUpdate
	var quota DbQuotas

	name := ctx.Params().Get("name")
	ctx.ReadJSON(&storage)

	DB.First(&quota, "role = ?", name)
	if quota.ID == 0 {
		APIError(ctx, 404, "Cannot find the desired quota")
		return
	}

	DB.Model(&quota).Update("MaxStorage", storage.Storage)
}

func admin_quota_invites(ctx iris.Context) {
	var invites cisco.QuotaInvitesUpdate
	var quota DbQuotas

	name := ctx.Params().Get("name")
	ctx.ReadJSON(&invites)

	DB.First(&quota, "role = ?", name)
	if quota.ID == 0 {
		APIError(ctx, 404, "Cannot find the desired quota")
		return
	}

	DB.Model(&quota).Update("MaxInvites", invites.Invites)
}

func admin_user_info(ctx iris.Context) {
	var result cisco.UserInfo
	var user DbUser
	var tokens []DbToken
	var keys []DbSSHKey
	var instances []DbInstance
	var ipv4s []DbIpv4
	var invites []DbInvite

	username := ctx.Params().Get("username")
	DB.First(&user, "username = ?", username)
	if user.ID == 0 {
		APIError(ctx, 404, "Could not find the user")
		return
	}

	DB.Find(&tokens, "user = ?", user.ID)
	DB.Find(&keys, "user = ?", user.ID)
	DB.Find(&instances, "user = ?", user.ID)
	DB.Find(&ipv4s, "user = ?", user.ID)
	DB.Find(&invites, "user = ?", user.ID)

	for _, token := range tokens {
		result.Tokens = append(result.Tokens, cisco.Token{
			Key:         token.Key,
			Secret:      token.Secret,
			Description: token.Description,
			Created:     token.CreatedAt,
		})
	}

	for _, key := range keys {
		result.Keys = append(result.Keys, cisco.SSHKey{
			Key:         key.Key,
			Created:     key.CreatedAt,
			Fingerprint: key.Fingerprint,
		})
	}

	for _, instance := range instances {
		result.Instances = append(result.Instances, cisco.Instance{
			Uuid:         instance.Uuid,
			Name:         instance.Name,
			Ipv4:         "instance.Ipv4",
			Size:         instance.Size,
			OS:           instance.OS,
			Architecture: instance.Architecture,
			Type:         instance.Type,
			Created:      instance.CreatedAt,
		})
	}

	for _, ipv4 := range ipv4s {
		result.Ipv4s = append(result.Ipv4s, cisco.Ipv4{
			IP:       ipv4.IP,
			Instance: ipv4.Instance,
			Created:  ipv4.CreatedAt,
		})
	}

	for _, invite := range invites {
		result.Invites = append(result.Invites, cisco.Invite{
			Email:   invite.Email,
			Claimed: invite.Claimed,
			Created: invite.CreatedAt,
		})
	}

	result.User = cisco.User{
		Username: user.Username,
		Email:    user.Email,
		Role:     user.Role,
		Created:  user.CreatedAt,
	}

	ctx.JSON(result)
}

func admin_instance_create(ctx iris.Context) {
	var instanceType DbInstanceType

	name := ctx.Params().Get("name")
	DB.First(&instanceType, "name = ?", name)

	if instanceType.ID != 0 {
		APIError(ctx, 409, "An instance type with this name already exists, please update it or delete it.")
		return
	}

	instanceType.Name = name
	instanceType.CPU = 1
	instanceType.Memory = 256
	instanceType.Architecture = "x86_64"
	instanceType.Type = "container"
	instanceType.Enabled = false

	DB.Create(&instanceType)
	ctx.StatusCode(iris.StatusCreated)
}

func admin_instance_list(ctx iris.Context) {
	var result []cisco.InstanceType
	var instanceTypes []DbInstanceType

	DB.Find(&instanceTypes)
	for _, instanceType := range instanceTypes {
		result = append(result, cisco.InstanceType{
			Name:         instanceType.Name,
			CPU:          instanceType.CPU,
			Memory:       instanceType.Memory,
			Architecture: instanceType.Architecture,
			Type:         instanceType.Type,
			Enabled:      instanceType.Enabled,
		})
	}

	ctx.JSON(&result)
}

func admin_instance_get(ctx iris.Context) {
	var result cisco.InstanceType
	var instanceType DbInstanceType

	name := ctx.Params().Get("name")
	DB.First(&instanceType, "name = ?", name)
	if instanceType.ID == 0 {
		APIError(ctx, 404, "Cannot find the desired instance type")
		return
	}

	result = cisco.InstanceType{
		Name:         instanceType.Name,
		CPU:          instanceType.CPU,
		Memory:       instanceType.Memory,
		Architecture: instanceType.Architecture,
		Type:         instanceType.Type,
		Enabled:      instanceType.Enabled,
	}
	ctx.JSON(&result)
}

func admin_instance_delete(ctx iris.Context) {
	var instanceType DbInstanceType

	name := ctx.Params().Get("name")
	DB.First(&instanceType, "name = ?", name)

	if instanceType.ID == 0 {
		APIError(ctx, 404, "Cannot find the desired instance type")
		return
	}

	instanceType.Name = name
	DB.Delete(&instanceType)
}

func admin_instance_arch(ctx iris.Context) {
	var arch cisco.InstanceTypeArchUpdate
	var instanceType DbInstanceType

	name := ctx.Params().Get("name")
	ctx.ReadJSON(&arch)
	DB.First(&instanceType, "name = ?", name)

	if instanceType.ID == 0 {
		APIError(ctx, 404, "Cannot find the desired instance type")
		return
	}

	if ArchitectureIsValid(ctx, arch.Arch) != nil {
		return
	}

	DB.Model(&instanceType).Update("architecture", arch.Arch)
}

func admin_instance_cpu(ctx iris.Context) {
	var cpu cisco.InstanceTypeCPUUpdate
	var instanceType DbInstanceType

	name := ctx.Params().Get("name")
	ctx.ReadJSON(&cpu)
	DB.First(&instanceType, "name = ?", name)

	if instanceType.ID == 0 {
		APIError(ctx, 404, "Cannot find the desired instance type")
		return
	}

	DB.Model(&instanceType).Update("cpu", cpu.CPU)
}

func admin_instance_memory(ctx iris.Context) {
	var memory cisco.InstanceTypeMemoryUpdate
	var instanceType DbInstanceType

	name := ctx.Params().Get("name")
	ctx.ReadJSON(&memory)
	DB.First(&instanceType, "name = ?", name)

	if instanceType.ID == 0 {
		APIError(ctx, 404, "Cannot find the desired instance type")
		return
	}

	DB.Model(&instanceType).Update("memory", memory.Memory)
}

func admin_instance_type(ctx iris.Context) {
	var Itype cisco.InstanceTypeTypeUpdate
	var instanceType DbInstanceType

	name := ctx.Params().Get("name")
	ctx.ReadJSON(&Itype)
	DB.First(&instanceType, "name = ?", name)

	if instanceType.ID == 0 {
		APIError(ctx, 404, "Cannot find the desired instance type")
		return
	}

	if Itype.Type != "container" && Itype.Type != "virtual-machine" {
		APIError(ctx, 400, "Wrong instance type. Must be one of 'container' or 'virtual'.")
		return
	}

	DB.Model(&instanceType).Update("type", Itype.Type)
}

func admin_instance_enable(ctx iris.Context) {
	var enable cisco.InstanceTypeEnableUpdate
	var instanceType DbInstanceType

	name := ctx.Params().Get("name")
	ctx.ReadJSON(&enable)
	DB.First(&instanceType, "name = ?", name)

	if instanceType.ID == 0 {
		APIError(ctx, 404, "Cannot find the desired instance type")
		return
	}

	DB.Model(&instanceType).Update("enabled", enable.Enable)
}

func InitAdminRoutes(app *iris.Application) {
	app.Get("/admin/user/list", AdminAuth, admin_user_list)
	app.Get("/admin/user/{username:string}", AdminAuth, admin_user_info)

	app.Get("/admin/quota/list", AdminAuth, admin_quota_list)
	app.Get("/admin/quota/{name:string}", AdminAuth, admin_quota_get)
	app.Put("/admin/quota/{name:string}", AdminAuth, admin_quota_create)
	app.Delete("/admin/quota/{name:string}", AdminAuth, admin_quota_delete)
	app.Post("/admin/quota/instance/{name:string}", AdminAuth, admin_quota_instance)
	app.Post("/admin/quota/ipv4/{name:string}", AdminAuth, admin_quota_ipv4)
	app.Post("/admin/quota/storage/{name:string}", AdminAuth, admin_quota_storage)
	app.Post("/admin/quota/invites/{name:string}", AdminAuth, admin_quota_invites)

	app.Put("/admin/instance/{name:string}", AdminAuth, admin_instance_create)
	app.Get("/admin/instance/list", AdminAuth, admin_instance_list)
	app.Get("/admin/instance/{name:string}", AdminAuth, admin_instance_get)
	app.Delete("/admin/instance/{name:string}", AdminAuth, admin_instance_delete)
	app.Post("/admin/instance/arch/{name:string}", AdminAuth, admin_instance_arch)
	app.Post("/admin/instance/cpu/{name:string}", AdminAuth, admin_instance_cpu)
	app.Post("/admin/instance/memory/{name:string}", AdminAuth, admin_instance_memory)
	app.Post("/admin/instance/type/{name:string}", AdminAuth, admin_instance_type)
	app.Post("/admin/instance/enable/{name:string}", AdminAuth, admin_instance_enable)
}