-
Notifications
You must be signed in to change notification settings - Fork 301
/
grpc_webhooks.go
134 lines (120 loc) · 4.62 KB
/
grpc_webhooks.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
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package web
import (
"context"
"strconv"
pbtypes "github.com/gogo/protobuf/types"
"go.thethings.network/lorawan-stack/pkg/auth/rights"
"go.thethings.network/lorawan-stack/pkg/ttnpb"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
func setTotalHeader(ctx context.Context, total uint64) {
grpc.SetHeader(ctx, metadata.Pairs("x-total-count", strconv.FormatUint(total, 10)))
}
// appendImplicitWebhookGetPaths appends implicit ttnpb.ApplicationWebhook get paths to paths.
func appendImplicitWebhookGetPaths(paths ...string) []string {
return append(append(make([]string, 0, 2+len(paths)),
"base_url",
"format",
), paths...)
}
type webhookRegistryRPC struct {
webhooks WebhookRegistry
templates *TemplateStore
}
// NewWebhookRegistryRPC returns a new webhook registry gRPC server.
func NewWebhookRegistryRPC(webhooks WebhookRegistry, templates *TemplateStore) ttnpb.ApplicationWebhookRegistryServer {
return &webhookRegistryRPC{
webhooks: webhooks,
templates: templates,
}
}
func (s webhookRegistryRPC) GetFormats(ctx context.Context, _ *pbtypes.Empty) (*ttnpb.ApplicationWebhookFormats, error) {
fs := make(map[string]string, len(formats))
for key, val := range formats {
fs[key] = val.Name
}
return &ttnpb.ApplicationWebhookFormats{
Formats: fs,
}, nil
}
func (s webhookRegistryRPC) GetTemplate(ctx context.Context, req *ttnpb.GetApplicationWebhookTemplateRequest) (*ttnpb.ApplicationWebhookTemplate, error) {
return s.templates.GetTemplate(ctx, req)
}
func (s webhookRegistryRPC) ListTemplates(ctx context.Context, req *ttnpb.ListApplicationWebhookTemplatesRequest) (*ttnpb.ApplicationWebhookTemplates, error) {
return s.templates.ListTemplates(ctx, req)
}
func (s webhookRegistryRPC) Get(ctx context.Context, req *ttnpb.GetApplicationWebhookRequest) (*ttnpb.ApplicationWebhook, error) {
if err := rights.RequireApplication(ctx, req.ApplicationIdentifiers, ttnpb.RIGHT_APPLICATION_TRAFFIC_READ); err != nil {
return nil, err
}
return s.webhooks.Get(ctx, req.ApplicationWebhookIdentifiers, appendImplicitWebhookGetPaths(req.FieldMask.Paths...))
}
func (s webhookRegistryRPC) List(ctx context.Context, req *ttnpb.ListApplicationWebhooksRequest) (*ttnpb.ApplicationWebhooks, error) {
if err := rights.RequireApplication(ctx, req.ApplicationIdentifiers, ttnpb.RIGHT_APPLICATION_TRAFFIC_READ); err != nil {
return nil, err
}
webhooks, err := s.webhooks.List(ctx, req.ApplicationIdentifiers, appendImplicitWebhookGetPaths(req.FieldMask.Paths...))
if err != nil {
return nil, err
}
defer func() {
if err == nil {
setTotalHeader(ctx, uint64(len(webhooks)))
}
}()
return &ttnpb.ApplicationWebhooks{
Webhooks: webhooks,
}, nil
}
func (s webhookRegistryRPC) Set(ctx context.Context, req *ttnpb.SetApplicationWebhookRequest) (*ttnpb.ApplicationWebhook, error) {
if err := rights.RequireApplication(ctx, req.ApplicationIdentifiers,
ttnpb.RIGHT_APPLICATION_SETTINGS_BASIC,
ttnpb.RIGHT_APPLICATION_TRAFFIC_READ,
ttnpb.RIGHT_APPLICATION_TRAFFIC_DOWN_WRITE,
); err != nil {
return nil, err
}
return s.webhooks.Set(ctx, req.ApplicationWebhookIdentifiers, appendImplicitWebhookGetPaths(req.FieldMask.Paths...),
func(webhook *ttnpb.ApplicationWebhook) (*ttnpb.ApplicationWebhook, []string, error) {
if webhook != nil {
return &req.ApplicationWebhook, req.FieldMask.Paths, nil
}
return &req.ApplicationWebhook, append(req.FieldMask.Paths,
"ids.application_ids",
"ids.webhook_id",
), nil
},
)
}
func (s webhookRegistryRPC) Delete(ctx context.Context, req *ttnpb.ApplicationWebhookIdentifiers) (*pbtypes.Empty, error) {
if err := rights.RequireApplication(ctx, req.ApplicationIdentifiers,
ttnpb.RIGHT_APPLICATION_SETTINGS_BASIC,
ttnpb.RIGHT_APPLICATION_TRAFFIC_READ,
ttnpb.RIGHT_APPLICATION_TRAFFIC_DOWN_WRITE,
); err != nil {
return nil, err
}
_, err := s.webhooks.Set(ctx, *req, nil,
func(webhook *ttnpb.ApplicationWebhook) (*ttnpb.ApplicationWebhook, []string, error) {
return nil, nil, nil
},
)
if err != nil {
return nil, err
}
return ttnpb.Empty, nil
}