forked from brocaar/chirpstack-application-server
/
service_profile.go
375 lines (320 loc) · 9.73 KB
/
service_profile.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package storage
import (
"context"
"strings"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"github.com/gofrs/uuid"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/brocaar/chirpstack-api/go/v3/ns"
"github.com/kamicuu/chirpstack-application-server/internal/backend/networkserver"
"github.com/kamicuu/chirpstack-application-server/internal/logging"
)
// ServiceProfile defines the service-profile.
type ServiceProfile struct {
NetworkServerID int64 `db:"network_server_id"`
OrganizationID int64 `db:"organization_id"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
Name string `db:"name"`
ServiceProfile ns.ServiceProfile `db:"-"`
}
// ServiceProfileMeta defines the service-profile meta record.
type ServiceProfileMeta struct {
ServiceProfileID uuid.UUID `db:"service_profile_id"`
NetworkServerID int64 `db:"network_server_id"`
OrganizationID int64 `db:"organization_id"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
Name string `db:"name"`
NetworkServerName string `db:"network_server_name"`
}
// Validate validates the service-profile data.
func (sp ServiceProfile) Validate() error {
if strings.TrimSpace(sp.Name) == "" || len(sp.Name) > 100 {
return ErrServiceProfileInvalidName
}
return nil
}
// CreateServiceProfile creates the given service-profile.
func CreateServiceProfile(ctx context.Context, db sqlx.Ext, sp *ServiceProfile) error {
if err := sp.Validate(); err != nil {
return errors.Wrap(err, "validate error")
}
spID, err := uuid.NewV4()
if err != nil {
return errors.Wrap(err, "new uuid v4 error")
}
now := time.Now()
sp.CreatedAt = now
sp.UpdatedAt = now
sp.ServiceProfile.Id = spID.Bytes()
_, err = db.Exec(`
insert into service_profile (
service_profile_id,
network_server_id,
organization_id,
created_at,
updated_at,
name
) values ($1, $2, $3, $4, $5, $6)`,
spID,
sp.NetworkServerID,
sp.OrganizationID,
sp.CreatedAt,
sp.UpdatedAt,
sp.Name,
)
if err != nil {
return handlePSQLError(Insert, err, "insert error")
}
n, err := GetNetworkServer(ctx, db, sp.NetworkServerID)
if err != nil {
return errors.Wrap(err, "get network-server error")
}
nsClient, err := networkserver.GetPool().Get(n.Server, []byte(n.CACert), []byte(n.TLSCert), []byte(n.TLSKey))
if err != nil {
return errors.Wrap(err, "get network-server client error")
}
_, err = nsClient.CreateServiceProfile(ctx, &ns.CreateServiceProfileRequest{
ServiceProfile: &sp.ServiceProfile,
})
if err != nil {
return errors.Wrap(err, "create service-profile error")
}
log.WithFields(log.Fields{
"id": spID,
"ctx_id": ctx.Value(logging.ContextIDKey),
}).Info("service-profile created")
return nil
}
// GetServiceProfile returns the service-profile matching the given id.
func GetServiceProfile(ctx context.Context, db sqlx.Queryer, id uuid.UUID, localOnly bool) (ServiceProfile, error) {
var sp ServiceProfile
row := db.QueryRowx(`
select
network_server_id,
organization_id,
created_at,
updated_at,
name
from service_profile
where
service_profile_id = $1`,
id,
)
if err := row.Err(); err != nil {
return sp, handlePSQLError(Select, err, "select error")
}
err := row.Scan(&sp.NetworkServerID, &sp.OrganizationID, &sp.CreatedAt, &sp.UpdatedAt, &sp.Name)
if err != nil {
return sp, handlePSQLError(Scan, err, "scan error")
}
if localOnly {
return sp, nil
}
n, err := GetNetworkServer(ctx, db, sp.NetworkServerID)
if err != nil {
return sp, errors.Wrap(err, "get network-server errror")
}
nsClient, err := networkserver.GetPool().Get(n.Server, []byte(n.CACert), []byte(n.TLSCert), []byte(n.TLSKey))
if err != nil {
return sp, errors.Wrap(err, "get network-server client error")
}
resp, err := nsClient.GetServiceProfile(ctx, &ns.GetServiceProfileRequest{
Id: id.Bytes(),
})
if err != nil {
return sp, errors.Wrap(err, "get service-profile error")
}
if resp.ServiceProfile == nil {
return sp, errors.New("service_profile must not be nil")
}
sp.ServiceProfile = *resp.ServiceProfile
return sp, nil
}
// UpdateServiceProfile updates the given service-profile.
func UpdateServiceProfile(ctx context.Context, db sqlx.Ext, sp *ServiceProfile) error {
if err := sp.Validate(); err != nil {
return errors.Wrap(err, "validate error")
}
spID, err := uuid.FromBytes(sp.ServiceProfile.Id)
if err != nil {
return errors.Wrap(err, "uuid from bytes error")
}
sp.UpdatedAt = time.Now()
res, err := db.Exec(`
update service_profile
set
updated_at = $2,
name = $3
where service_profile_id = $1`,
spID,
sp.UpdatedAt,
sp.Name,
)
if err != nil {
return handlePSQLError(Update, err, "update error")
}
ra, err := res.RowsAffected()
if err != nil {
return errors.Wrap(err, "get rows affected error")
}
if ra == 0 {
return ErrDoesNotExist
}
n, err := GetNetworkServer(ctx, db, sp.NetworkServerID)
if err != nil {
return errors.Wrap(err, "get network-server error")
}
nsClient, err := networkserver.GetPool().Get(n.Server, []byte(n.CACert), []byte(n.TLSCert), []byte(n.TLSKey))
if err != nil {
return errors.Wrap(err, "get network-server client error")
}
_, err = nsClient.UpdateServiceProfile(ctx, &ns.UpdateServiceProfileRequest{
ServiceProfile: &sp.ServiceProfile,
})
if err != nil {
return errors.Wrap(err, "update service-profile error")
}
log.WithFields(log.Fields{
"id": spID,
"ctx_id": ctx.Value(logging.ContextIDKey),
}).Info("service-profile updated")
return nil
}
// DeleteServiceProfile deletes the service-profile matching the given id.
func DeleteServiceProfile(ctx context.Context, db sqlx.Ext, id uuid.UUID) error {
n, err := GetNetworkServerForServiceProfileID(ctx, db, id)
if err != nil {
return errors.Wrap(err, "get network-server error")
}
nsClient, err := networkserver.GetPool().Get(n.Server, []byte(n.CACert), []byte(n.TLSCert), []byte(n.TLSKey))
if err != nil {
return errors.Wrap(err, "get network-server client error")
}
res, err := db.Exec("delete from service_profile where service_profile_id = $1", id)
if err != nil {
return handlePSQLError(Delete, err, "delete error")
}
ra, err := res.RowsAffected()
if err != nil {
return errors.Wrap(err, "get rows affected error")
}
if ra == 0 {
return ErrDoesNotExist
}
_, err = nsClient.DeleteServiceProfile(ctx, &ns.DeleteServiceProfileRequest{
Id: id.Bytes(),
})
if err != nil && grpc.Code(err) != codes.NotFound {
return errors.Wrap(err, "delete service-profile error")
}
log.WithFields(log.Fields{
"id": id,
"ctx_id": ctx.Value(logging.ContextIDKey),
}).Info("service-profile deleted")
return nil
}
// ServiceProfileFilters provides filters for filtering service profiles.
type ServiceProfileFilters struct {
UserID int64 `db:"user_id"`
OrganizationID int64 `db:"organization_id"`
NetworkServerID int64 `db:"network_server_id"`
// Limit and Offset are added for convenience so that this struct can
// be given as the arguments.
Limit int `db:"limit"`
Offset int `db:"offset"`
}
// SQL returns the SQL filters.
func (f ServiceProfileFilters) SQL() string {
var filters []string
if f.UserID != 0 {
filters = append(filters, "u.id = :user_id")
}
if f.OrganizationID != 0 {
filters = append(filters, "sp.organization_id = :organization_id")
}
if f.NetworkServerID != 0 {
filters = append(filters, "sp.network_server_id = :network_server_id")
}
if len(filters) == 0 {
return ""
}
return "where " + strings.Join(filters, " and ")
}
// GetServiceProfileCount returns the total number of service-profiles.
func GetServiceProfileCount(ctx context.Context, db sqlx.Queryer, filters ServiceProfileFilters) (int, error) {
query, args, err := sqlx.BindNamed(sqlx.DOLLAR, `
select
count(distinct sp.*)
from
service_profile sp
left join organization_user ou
on sp.organization_id = ou.organization_id
left join "user" u
on ou.user_id = u.id
`+filters.SQL(), filters)
if err != nil {
return 0, errors.Wrap(err, "named query error")
}
var count int
err = sqlx.Get(db, &count, query, args...)
if err != nil {
return 0, handlePSQLError(Select, err, "select error")
}
return count, nil
}
// GetServiceProfiles returns a slice of service-profiles.
func GetServiceProfiles(ctx context.Context, db sqlx.Queryer, filters ServiceProfileFilters) ([]ServiceProfileMeta, error) {
query, args, err := sqlx.BindNamed(sqlx.DOLLAR, `
select
sp.*,
ns.name as network_server_name
from
service_profile sp
inner join network_server ns
on sp.network_server_id = ns.id
left join organization_user ou
on sp.organization_id = ou.organization_id
left join "user" u
on ou.user_id = u.id
`+filters.SQL()+`
group by
sp.service_profile_id,
sp.name,
network_server_name
order by
sp.name
limit :limit
offset :offset
`, filters)
if err != nil {
return nil, errors.Wrap(err, "named query error")
}
var sps []ServiceProfileMeta
err = sqlx.Select(db, &sps, query, args...)
if err != nil {
return nil, handlePSQLError(Select, err, "select error")
}
return sps, nil
}
// DeleteAllServiceProfilesForOrganizationID deletes all service-profiles
// given an organization id.
func DeleteAllServiceProfilesForOrganizationID(ctx context.Context, db sqlx.Ext, organizationID int64) error {
var sps []ServiceProfileMeta
err := sqlx.Select(db, &sps, "select * from service_profile where organization_id = $1", organizationID)
if err != nil {
return handlePSQLError(Select, err, "select error")
}
for _, sp := range sps {
err = DeleteServiceProfile(ctx, db, sp.ServiceProfileID)
if err != nil {
return errors.Wrap(err, "delete service-profile error")
}
}
return nil
}