forked from go-squads/saga-scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlxd.go
105 lines (90 loc) · 2.09 KB
/
lxd.go
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 (
log "github.com/sirupsen/logrus"
)
type lxd struct {
ID string `db:"id" json:"id"`
Name string `db:"name" json:"name"`
Address string `db:"address" json:"address"`
Description string `db:"description" json:"description"`
}
func (l *lxd) getLxd(db PostgresQL) error {
rows, err := db.Queryx("SELECT id, name, address, description FROM lxd WHERE id=$1 LIMIT 1", l.ID)
if err != nil {
return err
}
if rows.Next() {
err = rows.StructScan(&l)
if err != nil {
return err
}
}
return nil
}
func getLxds(db PostgresQL) ([]lxd, error) {
var result []lxd
rows, err := db.Queryx("SELECT id, name, address, description FROM lxd")
if err != nil {
return nil, err
}
for rows.Next() {
var temp lxd
err = rows.StructScan(&temp)
if err != nil {
return nil, err
}
result = append(result, temp)
}
return result, nil
}
func (l *lxd) getLxdByIP(db PostgresQL) error {
rows, err := db.Queryx("SELECT id, address FROM lxd WHERE address=$1 LIMIT 1", l.Address)
if err != nil {
return err
}
if rows.Next() {
err = rows.StructScan(&l)
if err != nil {
return err
}
}
return nil
}
func (l *lxd) insertLxd(db PostgresQL) error {
_, err := db.NamedExec("INSERT INTO lxd (id, name, address, description) VALUES (:id, :name, :address, :description)", l)
if err != nil {
return err
}
return nil
}
func (l *lxd) getLxdIDByName(db PostgresQL) error {
rows, err := db.Queryx("SELECT id, name, address, description FROM lxd WHERE name = $1", l.Name)
if err != nil {
return err
}
defer rows.Close()
if rows.Next() {
err = rows.StructScan(&l)
if err != nil {
return err
}
}
return nil
}
func (l *lxd) getLxdNameAndAddressByID(db PostgresQL) (string, string) {
rows, err := db.Queryx("SELECT name, address FROM lxd WHERE id=$1", l.ID)
if err != nil {
log.Error(err.Error())
return "", ""
}
defer rows.Close()
lxdData := lxd{}
if rows.Next() {
if err = rows.StructScan(&lxdData); err != nil {
log.Error(err.Error())
return "", ""
}
}
log.Infof("lxd name: %s", lxdData.Name)
return lxdData.Address, lxdData.Name
}