/
service.go
187 lines (169 loc) · 6.24 KB
/
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
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
package svc
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
mclient "github.com/micro/go-micro/v2/client"
v0proto "github.com/anaswaratrajan/ocis-jupyter/pkg/proto/v0"
olog "github.com/owncloud/ocis/ocis-pkg/log"
"github.com/owncloud/ocis/ocis-pkg/middleware"
settings "github.com/owncloud/ocis/settings/pkg/proto/v0"
ssvc "github.com/owncloud/ocis/settings/pkg/service/v0"
)
var (
// ErrMissingName defines the error if name is missing.
ErrMissingName = errors.New("missing a name")
bundleIDGreeting = "21fb587b-7b69-4aa6-b0a7-93c74af1918f"
settingIDGreeterPhrase = "b3584ea8-caec-4951-a2c1-92cbc70071b7"
// maxRetries indicates how many times to try a request for network reasons.
maxRetries = 5
)
// NewService returns a service implementation for HelloHandler.
func NewService() v0proto.HelloHandler {
return Hello{}
}
// Hello defines implements the business logic for HelloHandler.
type Hello struct {
// Add database handlers here.
}
// Greet implements the HelloHandler interface.
func (s Hello) Greet(ctx context.Context, req *v0proto.GreetRequest, rsp *v0proto.GreetResponse) error {
if req.Name == "" {
return ErrMissingName
}
phrase := getGreetingPhrase(ctx)
rsp.Message = fmt.Sprintf(phrase, req.Name)
return nil
}
func getGreetingPhrase(ctx context.Context) string {
ownAccountUUID := ctx.Value(middleware.UUIDKey)
if ownAccountUUID != nil {
// request to the settings service requires to have the account uuid of the authenticated user available in the context
rq := settings.GetValueByUniqueIdentifiersRequest{
AccountUuid: ownAccountUUID.(string),
SettingId: settingIDGreeterPhrase,
}
// TODO this won't work with a registry other than mdns. Look into Micro's client initialization.
// https://github.com/owncloud/ocis-hello/issues/74
valueService := settings.NewValueService("com.owncloud.api.settings", mclient.DefaultClient)
response, err := valueService.GetValueByUniqueIdentifiers(ctx, &rq)
if err == nil {
value, ok := response.Value.Value.Value.(*settings.Value_StringValue)
if ok {
trimmedPhrase := strings.Trim(
value.StringValue,
" \t",
)
if trimmedPhrase != "" {
return trimmedPhrase + " %s"
}
}
}
}
return "Hello %s"
}
// RegisterSettingsBundles pushes the settings bundle definitions for this extension to the ocis-settings service.
func RegisterSettingsBundles(l *olog.Logger) {
request := &settings.SaveBundleRequest{
Bundle: &settings.Bundle{
Id: bundleIDGreeting,
Name: "greeting",
DisplayName: "Greeting",
Extension: "ocis-jupyter",
Type: settings.Bundle_TYPE_DEFAULT,
Resource: &settings.Resource{
Type: settings.Resource_TYPE_SYSTEM,
},
Settings: []*settings.Setting{
{
Id: settingIDGreeterPhrase,
Name: "phrase",
DisplayName: "Phrase",
Description: "Phrase for replies on the greet request",
Resource: &settings.Resource{
Type: settings.Resource_TYPE_SYSTEM,
},
Value: &settings.Setting_StringValue{
StringValue: &settings.String{
Required: true,
Default: "Hello",
MaxLength: 15,
},
},
},
},
},
}
// TODO this won't work with a registry other than mdns. Look into Micro's client initialization.
// https://github.com/owncloud/ocis-proxy/issues/38
bundleService := settings.NewBundleService("com.owncloud.api.settings", mclient.DefaultClient)
response, err := bundleService.SaveBundle(context.Background(), request)
if err != nil {
l.Warn().Msg("error registering settings bundle at first try. retrying")
for i := 1; i <= maxRetries; i++ {
if _, err := bundleService.SaveBundle(context.Background(), request); err != nil {
l.Warn().
Str("bundle_name", request.Bundle.Name).
Str("attempt", fmt.Sprintf("%v/%v", strconv.Itoa(i), strconv.Itoa(maxRetries))).
Msgf("error creating bundle")
continue
} else {
l.Info().
Str("bundle_name", request.Bundle.Name).
Str("after", fmt.Sprintf("%v retries", strconv.Itoa(i))).
Str("bundleName", request.Bundle.Name).
Str("bundleId", request.Bundle.Id).
Msg("default settings bundle registered")
goto OUT
}
}
l.Err(err).Str("setting_name", request.Bundle.Name).Msg("bundle could not be registered. max number of retries reached")
} else {
l.Info().
Str("bundleName", response.Bundle.Name).
Str("bundleId", response.Bundle.Id).
Msg("default settings bundle registered")
}
OUT:
permissionRequests := []*settings.AddSettingToBundleRequest{
{
BundleId: ssvc.BundleUUIDRoleAdmin,
Setting: &settings.Setting{
Id: "d5f42c4b-e1b6-4b59-8eca-fc4b9e9f2320",
Resource: &settings.Resource{
Type: settings.Resource_TYPE_SETTING,
Id: settingIDGreeterPhrase,
},
Name: "phrase-admin-read",
Value: &settings.Setting_PermissionValue{
PermissionValue: &settings.Permission{
Operation: settings.Permission_OPERATION_READWRITE,
Constraint: settings.Permission_CONSTRAINT_OWN,
},
},
},
},
}
for i := range permissionRequests {
l.Debug().Str("setting_name", permissionRequests[i].Setting.Name).Str("bundle_id", permissionRequests[i].BundleId).Msg("registering setting to bundle")
if res, err := bundleService.AddSettingToBundle(context.Background(), permissionRequests[i]); err != nil {
go retryPermissionRequests(context.Background(), bundleService, permissionRequests[i], maxRetries, l)
} else {
l.Info().Str("setting_name", res.Setting.Name).Msg("permission registered")
}
}
}
// proposal: the retry logic should live in the settings service.
func retryPermissionRequests(ctx context.Context, bs settings.BundleService, setting *settings.AddSettingToBundleRequest, maxRetries int, l *olog.Logger) {
for i := 1; i < maxRetries; i++ {
if _, err := bs.AddSettingToBundle(ctx, setting); err != nil {
l.Warn().Str("setting_name", setting.Setting.Name).Str("attempt", fmt.Sprintf("%v/%v", strconv.Itoa(i), strconv.Itoa(maxRetries))).Msgf("error on add setting to bundle")
continue
}
l.Info().Str("setting_name", setting.Setting.Name).Str("after", fmt.Sprintf("%v retries", strconv.Itoa(i))).Msg("permission registered")
return
}
l.Error().Str("setting_name", setting.Setting.Name).Msg("setting could not be registered. max number of retries reached")
}