ref: 79803b98b551ad1d924594263038bc901f684233 cisco/api/quotas.go -rw-r--r-- 2.7 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
package main

import (
	"fmt"
	"github.com/kataras/iris"
)

func CheckInviteQuotas(ctx iris.Context) bool {
	var quotas DbQuotas
	var count int

	role := ctx.Values().GetString("Role")
	id := ctx.Values().GetString("User")

	DB.First(&quotas, "role = ?", role)
	if quotas.ID == 0 {
		APIError(ctx, 500, "Could not find quotas for your role")
		return false
	}

	if quotas.MaxInvites == -1 {
		return true
	}

	DB.Model(&DbInvite{}).Where("user = ?", id).Count(&count)
	if count >= quotas.MaxInvites {
		quota := fmt.Sprintf("%d/%d", count, quotas.MaxInvites)
		APIError(ctx, 403, "Your quotas for invites have been reached ("+quota+"), you cannot create another one. If you want an upgrade, please contact one of the admins")
		return false
	}

	return true
}

func CheckInstanceQuotas(ctx iris.Context) bool {
	var quotas DbQuotas
	var count int

	role := ctx.Values().GetString("Role")
	id := ctx.Values().GetString("User")

	DB.First(&quotas, "role = ?", role)
	if quotas.ID == 0 {
		APIError(ctx, 500, "Could not find quotas for your role")
		return false
	}

	if quotas.MaxInstances == -1 {
		return true
	}

	DB.Model(&DbInstance{}).Where("user = ?", id).Count(&count)
	if count >= quotas.MaxInstances {
		quota := fmt.Sprintf("%d/%d", count, quotas.MaxInstances)
		APIError(ctx, 403, "Your quotas for instances have been reached ("+quota+"), you cannot create another one. If you want an upgrade, please contact one of the admins")
		return false
	}

	return true
}

func CheckStorageQuotas(ctx iris.Context, size uint) bool {
	var quotas DbQuotas
	var instances []DbInstance
	var currentSize uint

	role := ctx.Values().GetString("Role")
	id := ctx.Values().GetString("User")

	DB.First(&quotas, "role = ?", role)
	if quotas.ID == 0 {
		APIError(ctx, 500, "Could not find quotas for your role")
		return false
	}

	if quotas.MaxStorage == -1 {
		return true
	}

	DB.Find(&instances, "user = ?", id)
	for _, inst := range instances {
		currentSize += inst.Size
	}

	if currentSize+size > uint(quotas.MaxStorage) {
		quota := fmt.Sprintf("%d/%d", currentSize, quotas.MaxStorage)
		APIError(ctx, 403, "Your quotas for storage have been reached ("+quota+"). If you want an upgrade, please contact one of the admins")
		return false

	}

	return true
}

func CheckSSHKeys(ctx iris.Context) bool {
	var entries []DbSSHKey

	user := ctx.Values().Get("User")
	DB.Find(&entries, "user = ?", user)

	if len(entries) == 0 {
		APIError(ctx, 403, "Please add an SSH key to your account before creating an instance")
		return false
	}

	return true
}

func CheckQuota(ctx iris.Context, name string) bool {
	var quotas DbQuotas

	DB.First(&quotas, "role = ?", name)
	if quotas.ID == 0 {
		APIError(ctx, 404, "Could not find quotas for this name")
		return false
	}

	return true
}