forked from brocaar/chirpstack-application-server
/
integrations.go
183 lines (168 loc) · 4.34 KB
/
integrations.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
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
package storage
import (
"context"
"database/sql"
"encoding/json"
"time"
"github.com/jmoiron/sqlx"
"github.com/lib/pq"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/kamicuu/chirpstack-application-server/internal/logging"
)
// Integration represents an integration.
type Integration struct {
ID int64 `db:"id"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
ApplicationID int64 `db:"application_id"`
Kind string `db:"kind"`
Settings json.RawMessage `db:"settings"`
}
// CreateIntegration creates the given Integration.
func CreateIntegration(ctx context.Context, db sqlx.Queryer, i *Integration) error {
now := time.Now()
err := sqlx.Get(db, &i.ID, `
insert into integration (
created_at,
updated_at,
application_id,
kind,
settings
) values ($1, $2, $3, $4, $5) returning id`,
now,
now,
i.ApplicationID,
i.Kind,
i.Settings,
)
if err != nil {
switch err := err.(type) {
case *pq.Error:
switch err.Code.Name() {
case "unique_violation":
return ErrAlreadyExists
default:
return errors.Wrap(err, "insert error")
}
default:
return errors.Wrap(err, "insert error")
}
}
i.CreatedAt = now
i.UpdatedAt = now
log.WithFields(log.Fields{
"id": i.ID,
"kind": i.Kind,
"application_id": i.ApplicationID,
"ctx_id": ctx.Value(logging.ContextIDKey),
}).Info("integration created")
return nil
}
// GetIntegration returns the Integration for the given id.
func GetIntegration(ctx context.Context, db sqlx.Queryer, id int64) (Integration, error) {
var i Integration
err := sqlx.Get(db, &i, "select * from integration where id = $1", id)
if err != nil {
if err == sql.ErrNoRows {
return i, ErrDoesNotExist
}
return i, errors.Wrap(err, "select error")
}
return i, nil
}
// GetIntegrationByApplicationID returns the Integration for the given
// application id and kind.
func GetIntegrationByApplicationID(ctx context.Context, db sqlx.Queryer, applicationID int64, kind string) (Integration, error) {
var i Integration
err := sqlx.Get(db, &i, "select * from integration where application_id = $1 and kind = $2", applicationID, kind)
if err != nil {
if err == sql.ErrNoRows {
return i, ErrDoesNotExist
}
return i, errors.Wrap(err, "select error")
}
return i, nil
}
// GetIntegrationsForApplicationID returns the integrations for the given
// application id.
func GetIntegrationsForApplicationID(ctx context.Context, db sqlx.Queryer, applicationID int64) ([]Integration, error) {
var is []Integration
err := sqlx.Select(db, &is, `
select *
from integration
where application_id = $1
order by kind`,
applicationID,
)
if err != nil {
return nil, errors.Wrap(err, "select error")
}
return is, nil
}
// UpdateIntegration updates the given Integration.
func UpdateIntegration(ctx context.Context, db sqlx.Execer, i *Integration) error {
now := time.Now()
res, err := db.Exec(`
update integration
set
updated_at = $2,
application_id = $3,
kind = $4,
settings = $5
where
id = $1`,
i.ID,
now,
i.ApplicationID,
i.Kind,
i.Settings,
)
if err != nil {
switch err := err.(type) {
case *pq.Error:
switch err.Code.Name() {
case "unique_violation":
return ErrAlreadyExists
default:
return errors.Wrap(err, "update error")
}
default:
return errors.Wrap(err, "update error")
}
}
ra, err := res.RowsAffected()
if err != nil {
return errors.Wrap(err, "get rows affected error")
}
if ra == 0 {
return ErrDoesNotExist
}
i.UpdatedAt = now
log.WithFields(log.Fields{
"id": i.ID,
"kind": i.Kind,
"application_id": i.ApplicationID,
"ctx_id": ctx.Value(logging.ContextIDKey),
}).Info("integration updated")
return nil
}
// DeleteIntegration deletes the integration matching the given id.
func DeleteIntegration(ctx context.Context, db sqlx.Execer, id int64) error {
res, err := db.Exec("delete from integration where id = $1", id)
if err != nil {
return errors.Wrap(err, "delete error")
}
ra, err := res.RowsAffected()
if err != nil {
return errors.Wrap(err, "get rows affected error")
}
if ra == 0 {
return ErrDoesNotExist
}
log.WithFields(log.Fields{
"id": id,
"ctx_id": ctx.Value(logging.ContextIDKey),
}).Info("integration deleted")
return nil
}