-
Notifications
You must be signed in to change notification settings - Fork 27
/
sinks_service.go
146 lines (119 loc) · 3.6 KB
/
sinks_service.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
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
// Adapted for Orb project, modifications licensed under MPL v. 2.0:
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
package sinks
import (
"context"
"github.com/ns1labs/orb/pkg/errors"
"github.com/ns1labs/orb/sinks/backend"
)
var (
ErrCreateSink = errors.New("failed to create Sink")
ErrConflictSink = errors.New("entity already exists")
ErrUnsupportedContentTypeSink = errors.New("unsupported content type")
ErrValidateSink = errors.New("failed to validate Sink")
)
func (svc sinkService) CreateSink(ctx context.Context, token string, sink Sink) (Sink, error) {
mfOwnerID, err := svc.identify(token)
if err != nil {
return Sink{}, err
}
sink.MFOwnerID = mfOwnerID
err = validateBackend(&sink)
if err != nil {
return Sink{}, err
}
id, err := svc.sinkRepo.Save(ctx, sink)
if err != nil {
return Sink{}, errors.Wrap(ErrCreateSink, err)
}
sink.ID = id
return sink, nil
}
func (svc sinkService) UpdateSink(ctx context.Context, token string, sink Sink) error {
skOwnerID, err := svc.identify(token)
if err != nil {
return err
}
if sink.Backend != "" || sink.Error != "" {
return errors.ErrUpdateEntity
}
sink.MFOwnerID = skOwnerID
return svc.sinkRepo.Update(ctx, sink)
}
func (svc sinkService) ListBackends(ctx context.Context, token string) ([]string, error) {
_, err := svc.identify(token)
if err != nil {
return []string{}, err
}
return backend.GetList(), nil
}
func (svc sinkService) ViewBackend(ctx context.Context, token string, key string) (backend.Backend, error) {
_, err := svc.identify(token)
if err != nil {
return nil, err
}
res := backend.GetBackend(key)
if res == nil {
return nil, errors.Wrap(errors.ErrNotFound, err)
}
return res, nil
}
func (svc sinkService) ViewSink(ctx context.Context, token string, key string) (Sink, error) {
_, err := svc.identify(token)
if err != nil {
return Sink{}, err
}
res, err := svc.sinkRepo.RetrieveById(ctx, key)
if err != nil {
return Sink{}, errors.Wrap(errors.ErrNotFound, err)
}
return res, nil
}
func (svc sinkService) ViewSinkInternal(ctx context.Context, ownerID string, key string) (Sink, error) {
res, err := svc.sinkRepo.RetrieveByOwnerAndId(ctx, ownerID, key)
if err != nil {
return Sink{}, errors.Wrap(errors.ErrNotFound, err)
}
return res, nil
}
func (svc sinkService) ListSinks(ctx context.Context, token string, pm PageMetadata) (Page, error) {
res, err := svc.identify(token)
if err != nil {
return Page{}, err
}
return svc.sinkRepo.RetrieveAll(ctx, res, pm)
}
func (svc sinkService) DeleteSink(ctx context.Context, token string, id string) error {
res, err := svc.identify(token)
if err != nil {
return err
}
return svc.sinkRepo.Remove(ctx, res, id)
}
func (svc sinkService) ValidateSink(ctx context.Context, token string, sink Sink) (Sink, error) {
mfOwnerID, err := svc.identify(token)
if err != nil {
return Sink{}, err
}
sink.MFOwnerID = mfOwnerID
err = validateBackend(&sink)
if err != nil {
return Sink{}, errors.Wrap(ErrValidateSink, err)
}
return sink, nil
}
func (svc sinkService) ChangeSinkStateInternal(ctx context.Context, sinkID string, msg string, ownerID string, state State) error {
return svc.sinkRepo.UpdateSinkState(ctx, sinkID, msg, ownerID, state)
}
func validateBackend(sink *Sink) error {
if backend.HaveBackend(sink.Backend) {
sink.State = Unknown
} else {
return ErrInvalidBackend
}
return nil
}