-
Notifications
You must be signed in to change notification settings - Fork 33
/
notification.go
58 lines (54 loc) · 1.91 KB
/
notification.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
package storage
import (
"github.com/pkg/errors"
"github.com/viant/endly"
"github.com/viant/endly/service/system/cloud/gcp"
"google.golang.org/api/storage/v1"
)
// SetNotification setup notification
func (s *service) SetNotification(context *endly.Context, request *SetupNotificationRequest) (*SetupNotificationResponse, error) {
response := &SetupNotificationResponse{}
return response, s.setupNotification(context, request, response)
}
func (s *service) setupNotification(context *endly.Context, request *SetupNotificationRequest, response *SetupNotificationResponse) error {
client, err := GetClient(context)
if err != nil {
return err
}
service := storage.NewNotificationsService(client.service)
if request.ProjectID == "" && client.CredConfig != nil {
request.ProjectID = client.CredConfig.ProjectID
}
request.Topic = gcp.ExpandMeta(context, request.Topic)
listCall := service.List(request.Bucket)
listCall.Context(client.Context())
listCall.UserProject(request.ProjectID)
list, err := listCall.Do()
if err != nil {
return err
}
byTopic := make(map[string]*storage.Notification)
for i, notification := range list.Items {
byTopic[notification.Topic] = list.Items[i]
deleteCall := service.Delete(request.Bucket, notification.Id)
deleteCall.Context(client.Context())
deleteCall.UserProject(request.ProjectID)
if err = deleteCall.Do(); err != nil {
return err
}
}
_, ok := byTopic[request.Topic]
if ok {
deleteCall := service.Delete(request.Bucket, request.Notification.Id)
deleteCall.Context(client.Context())
deleteCall.UserProject(request.ProjectID)
}
insertCall := service.Insert(request.Bucket, &request.Notification)
insertCall.Context(client.Context())
insertCall.UserProject(request.ProjectID)
response.Notification, err = insertCall.Do()
if err != nil {
err = errors.Wrapf(err, "failed to set notification bucket: %v, topic: %v", request.Bucket, request.Topic)
}
return err
}