ref: 8f54cb40ef83bfe2fb560106e5e37d463ec6b3dd cisco/api/database.go -rw-r--r-- 1.6 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
package main

import (
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/sqlite"
)

type DbUser struct {
	gorm.Model
	Email    string
	Password string
	Username string
	Role     string
}

type DbToken struct {
	gorm.Model
	Key         string
	Secret      string
	Description string
	User        uint
}

type DbSSHKey struct {
	gorm.Model
	Key         string
	Fingerprint string
	User        uint
}

type DbQuotas struct {
	gorm.Model
	Role         string
	MaxInstances int
	MaxIpv4      int
	MaxStorage   int
	MaxInvites   int
}

type DbInstance struct {
	gorm.Model
	Uuid         string
	Name         string
	Ipv4         uint
	Size         uint
	OS           string
	Architecture string
	User         uint
	Type         string
	Status       string
	Ipv6         string
	LXCStatus    string
}

type DbIpv4 struct {
	gorm.Model
	IP       string
	Ipv6In   string
	Ipv6Out  string
	User     uint
	Instance string
}

type DbInvite struct {
	gorm.Model
	Email   string
	Sum     string
	Claimed bool
	User    uint
}

type DbInstanceType struct {
	gorm.Model
	Name         string
	Architecture string
	CPU          int
	Memory       int
	Enabled      bool
	Type         string
}

var DB *gorm.DB

func InitDB() {
	var err error

	DB, err = gorm.Open(Config.Database.Driver, Config.Database.Url)
	if err != nil {
		panic("Failed to connect to database:" + err.Error())
	}

	DB.AutoMigrate(&DbUser{})
	DB.AutoMigrate(&DbToken{})
	DB.AutoMigrate(&DbSSHKey{})
	DB.AutoMigrate(&DbQuotas{})
	DB.AutoMigrate(&DbInstance{})
	DB.AutoMigrate(&DbIpv4{})
	DB.AutoMigrate(&DbInvite{})
	DB.AutoMigrate(&DbInstanceType{})
}

func CloseDB() {
	DB.Close()
}