forked from brocaar/chirpstack-application-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_profile.go
370 lines (321 loc) · 9.64 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
package storage
import (
"context"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
"github.com/brocaar/lora-app-server/internal/config"
"github.com/brocaar/loraserver/api/ns"
)
// 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"`
}
// Validate validates the service-profile data.
func (sp ServiceProfile) Validate() error {
return nil
}
// CreateServiceProfile creates the given service-profile.
func CreateServiceProfile(db sqlx.Ext, sp *ServiceProfile) error {
if err := sp.Validate(); err != nil {
return errors.Wrap(err, "validate error")
}
now := time.Now()
spID := uuid.NewV4()
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(db, sp.NetworkServerID)
if err != nil {
return errors.Wrap(err, "get network-server error")
}
nsClient, err := config.C.NetworkServer.Pool.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(context.Background(), &ns.CreateServiceProfileRequest{
ServiceProfile: &sp.ServiceProfile,
})
if err != nil {
return handleGrpcError(err, "create service-profile error")
}
log.WithField("id", spID).Info("service-profile created")
return nil
}
// GetServiceProfile returns the service-profile matching the given id.
func GetServiceProfile(db sqlx.Queryer, id uuid.UUID) (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")
}
n, err := GetNetworkServer(db, sp.NetworkServerID)
if err != nil {
return sp, errors.Wrap(err, "get network-server errror")
}
nsClient, err := config.C.NetworkServer.Pool.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(context.Background(), &ns.GetServiceProfileRequest{
Id: id.Bytes(),
})
if err != nil {
return sp, handleGrpcError(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(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(db, sp.NetworkServerID)
if err != nil {
return errors.Wrap(err, "get network-server error")
}
nsClient, err := config.C.NetworkServer.Pool.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(context.Background(), &ns.UpdateServiceProfileRequest{
ServiceProfile: &sp.ServiceProfile,
})
if err != nil {
return handleGrpcError(err, "update service-profile error")
}
log.WithField("id", spID).Info("service-profile updated")
return nil
}
// DeleteServiceProfile deletes the service-profile matching the given id.
func DeleteServiceProfile(db sqlx.Ext, id uuid.UUID) error {
n, err := GetNetworkServerForServiceProfileID(db, id)
if err != nil {
return errors.Wrap(err, "get network-server error")
}
nsClient, err := config.C.NetworkServer.Pool.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(context.Background(), &ns.DeleteServiceProfileRequest{
Id: id.Bytes(),
})
if err != nil && grpc.Code(err) != codes.NotFound {
return handleGrpcError(err, "delete service-profile error")
}
log.WithField("id", id).Info("service-profile deleted")
return nil
}
// GetServiceProfileCount returns the total number of service-profiles.
func GetServiceProfileCount(db sqlx.Queryer) (int, error) {
var count int
err := sqlx.Get(db, &count, "select count(*) from service_profile")
if err != nil {
return 0, handlePSQLError(Select, err, "select error")
}
return count, nil
}
// GetServiceProfileCountForOrganizationID returns the total number of
// service-profiles for the given organization id.
func GetServiceProfileCountForOrganizationID(db sqlx.Queryer, organizationID int64) (int, error) {
var count int
err := sqlx.Get(db, &count, "select count(*) from service_profile where organization_id = $1", organizationID)
if err != nil {
return 0, handlePSQLError(Select, err, "select error")
}
return count, nil
}
// GetServiceProfileCountForUser returns the total number of service-profiles
// for the given username.
func GetServiceProfileCountForUser(db sqlx.Queryer, username string) (int, error) {
var count int
err := sqlx.Get(db, &count, `
select
count(sp.*)
from service_profile sp
inner join organization o
on o.id = sp.organization_id
inner join organization_user ou
on ou.organization_id = o.id
inner join "user" u
on u.id = ou.user_id
where
u.username = $1`,
username,
)
if err != nil {
return 0, handlePSQLError(Select, err, "select error")
}
return count, nil
}
// GetServiceProfiles returns a slice of service-profiles.
func GetServiceProfiles(db sqlx.Queryer, limit, offset int) ([]ServiceProfileMeta, error) {
var sps []ServiceProfileMeta
err := sqlx.Select(db, &sps, `
select *
from service_profile
order by name
limit $1 offset $2`,
limit,
offset,
)
if err != nil {
return nil, handlePSQLError(Select, err, "select error")
}
return sps, nil
}
// GetServiceProfilesForOrganizationID returns a slice of service-profiles
// for the given organization id.
func GetServiceProfilesForOrganizationID(db sqlx.Queryer, organizationID int64, limit, offset int) ([]ServiceProfileMeta, error) {
var sps []ServiceProfileMeta
err := sqlx.Select(db, &sps, `
select *
from service_profile
where
organization_id = $1
order by name
limit $2 offset $3`,
organizationID,
limit,
offset,
)
if err != nil {
return nil, handlePSQLError(Select, err, "select error")
}
return sps, nil
}
// GetServiceProfilesForUser returns a slice of service-profile for the given
// username.
func GetServiceProfilesForUser(db sqlx.Queryer, username string, limit, offset int) ([]ServiceProfileMeta, error) {
var sps []ServiceProfileMeta
err := sqlx.Select(db, &sps, `
select
sp.*
from service_profile sp
inner join organization o
on o.id = sp.organization_id
inner join organization_user ou
on ou.organization_id = o.id
inner join "user" u
on u.id = ou.user_id
where
u.username = $1
order by sp.name
limit $2 offset $3`,
username,
limit,
offset,
)
if err != nil {
return nil, handlePSQLError(Select, err, "select error")
}
return sps, nil
}
// DeleteAllServiceProfilesForOrganizationID deletes all service-profiles
// given an organization id.
func DeleteAllServiceProfilesForOrganizationID(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(db, sp.ServiceProfileID)
if err != nil {
return errors.Wrap(err, "delete service-profile error")
}
}
return nil
}