-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
role_config.go
81 lines (73 loc) · 1.89 KB
/
role_config.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
package backup
import (
"context"
"encoding/json"
instanceTypes "beryju.io/gravity/pkg/instance/types"
"beryju.io/gravity/pkg/roles/backup/types"
"github.com/swaggest/usecase"
"github.com/swaggest/usecase/status"
"go.uber.org/zap"
)
type RoleConfig struct {
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
Endpoint string `json:"endpoint"`
Bucket string `json:"bucket"`
Path string `json:"path"`
CronExpr string `json:"cronExpr"`
}
func (r *Role) decodeRoleConfig(raw []byte) *RoleConfig {
def := RoleConfig{
CronExpr: "0 */24 * * *",
}
if len(raw) < 1 {
return &def
}
err := json.Unmarshal(raw, &def)
if err != nil {
r.log.Warn("failed to decode role config", zap.Error(err))
}
return &def
}
type APIRoleConfigOutput struct {
Config RoleConfig `json:"config" required:"true"`
}
func (r *Role) APIRoleConfigGet() usecase.Interactor {
u := usecase.NewInteractor(func(ctx context.Context, input struct{}, output *APIRoleConfigOutput) error {
output.Config = *r.cfg
return nil
})
u.SetName("backup.get_role_config")
u.SetTitle("Backup role config")
u.SetTags("roles/backup")
return u
}
type APIRoleConfigInput struct {
Config RoleConfig `json:"config" required:"true"`
}
func (r *Role) APIRoleConfigPut() usecase.Interactor {
u := usecase.NewInteractor(func(ctx context.Context, input APIRoleConfigInput, output *struct{}) error {
jc, err := json.Marshal(input.Config)
if err != nil {
return status.Wrap(err, status.InvalidArgument)
}
_, err = r.i.KV().Put(
ctx,
r.i.KV().Key(
instanceTypes.KeyInstance,
instanceTypes.KeyRole,
types.KeyRole,
).String(),
string(jc),
)
if err != nil {
return status.Wrap(err, status.Internal)
}
return nil
})
u.SetName("backup.put_role_config")
u.SetTitle("Backup role config")
u.SetTags("roles/backup")
u.SetExpectedErrors(status.InvalidArgument, status.Internal)
return u
}