-
Notifications
You must be signed in to change notification settings - Fork 41
/
registry.go
68 lines (53 loc) · 1.38 KB
/
registry.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
package resourcelog
import (
"context"
"errors"
"github.com/seal-io/walrus/pkg/operator/alibaba/key"
"github.com/seal-io/walrus/pkg/operator/types"
"github.com/seal-io/walrus/utils/log"
)
// resourceTypes indicate supported resource type and their functions.
var resourceTypes map[string]getLoggableResource
type getLoggableResource func(ctx context.Context) (types.LoggableResource, error)
func init() {
resourceTypes = map[string]getLoggableResource{
"alicloud_instance": getEcsInstance,
}
}
// Supported indicate whether the resource is supported to get log.
func Supported(_ context.Context, k string) (bool, error) {
resourceType, _, ok := key.Decode(k)
if !ok {
return false, errors.New("invalid key")
}
_, exist := resourceTypes[resourceType]
if !exist {
return false, nil
}
return true, nil
}
// Log get resource log by key.
func Log(ctx context.Context, k string, options types.LogOptions) error {
supported, err := Supported(ctx, k)
if err != nil {
return err
}
if !supported {
return errors.New("unsupported resource type")
}
resourceType, name, ok := key.Decode(k)
if !ok {
return errors.New("invalid key")
}
res := resourceTypes[resourceType]
fs, err := res(ctx)
if err != nil {
return err
}
err = fs.Log(ctx, name, options)
if err != nil {
log.Warnf("error get log resource %s/%s: %v", resourceType, name, err)
return err
}
return nil
}