This repository was archived by the owner on Sep 17, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathoas.go
More file actions
157 lines (145 loc) · 3.22 KB
/
Copy pathoas.go
File metadata and controls
157 lines (145 loc) · 3.22 KB
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
package models
import (
"fmt"
"github.com/ModuleAB/ModuleAB/server/common"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
"github.com/astaxie/beego/validation"
"github.com/pborman/uuid"
)
type Oas struct {
Id string `orm:"pk;size(36)" json:"id" valid:"Match(/^[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}$/)"`
Endpoint string `json:"endpoint" valid:"Required"`
VaultName string `orm:"size(32) json:"vaultName" valid:"Required"`
VaultId string `orm:"size(32) json:"vaultId" valid:"Required"`
BackupSets []*BackupSets `orm:"reverse(many)"`
Jobs []*OasJobs `orm:"reverse(many)"`
}
func init() {
if prefix := beego.AppConfig.String("database::mysqlprefex"); prefix != "" {
orm.RegisterModelWithPrefix(prefix, new(Oas))
} else {
orm.RegisterModel(new(Oas))
}
}
func AddOas(a *Oas) (string, error) {
beego.Debug("[M] Got data:", a)
o := orm.NewOrm()
err := o.Begin()
if err != nil {
return "", err
}
a.Id = uuid.New()
beego.Debug("[M] Got new id:", a.Id)
validator := new(validation.Validation)
valid, err := validator.Valid(a)
if err != nil {
o.Rollback()
return "", err
}
if !valid {
o.Rollback()
var errS string
for _, err := range validator.Errors {
errS = fmt.Sprintf("%s, %s:%s", errS, err.Key, err.Message)
}
return "", fmt.Errorf("Bad info: %s", errS)
}
beego.Debug("[M] Got new data:", a)
_, err = o.Insert(a)
if err != nil {
o.Rollback()
return "", err
}
beego.Debug("[M] Oas info saved")
o.Commit()
return a.Id, nil
}
func DeleteOas(a *Oas) error {
beego.Debug("[M] Got data:", a)
o := orm.NewOrm()
err := o.Begin()
if err != nil {
return err
}
validator := new(validation.Validation)
valid, err := validator.Valid(a)
if err != nil {
o.Rollback()
return err
}
if !valid {
o.Rollback()
var errS string
for _, err := range validator.Errors {
errS = fmt.Sprintf("%s, %s:%s", errS, err.Key, err.Message)
}
return fmt.Errorf("Bad info: %s", errS)
}
_, err = o.Delete(a)
if err != nil {
o.Rollback()
return err
}
o.Commit()
return nil
}
func UpdateOas(a *Oas) error {
beego.Debug("[M] Got data:", a)
o := orm.NewOrm()
err := o.Begin()
if err != nil {
return err
}
validator := new(validation.Validation)
valid, err := validator.Valid(a)
if err != nil {
o.Rollback()
return err
}
if !valid {
o.Rollback()
var errS string
for _, err := range validator.Errors {
errS = fmt.Sprintf("%s, %s:%s", errS, err.Key, err.Message)
}
return fmt.Errorf("Bad info: %s", errS)
}
_, err = o.Update(a)
if err != nil {
o.Rollback()
return err
}
o.Commit()
return nil
}
// If get all, just use &Oas{}
func GetOas(cond *Oas, limit, index int) ([]*Oas, error) {
r := make([]*Oas, 0)
o := orm.NewOrm()
q := o.QueryTable("oas")
if cond.Id != "" {
q = q.Filter("id", cond.Id)
}
if cond.Endpoint != "" {
q = q.Filter("endpoint", cond.Endpoint)
}
if cond.VaultName != "" {
q = q.Filter("vault_name", cond.VaultName)
}
if limit > 0 {
q = q.Limit(limit)
}
if index > 0 {
q = q.Offset(index)
}
_, err := q.All(&r)
if err != nil {
return nil, err
}
for _, v := range r {
o.LoadRelated(v, "BackupSets", common.RelDepth)
o.LoadRelated(v, "Jobs", common.RelDepth)
}
return r, nil
}