/
registry.go
43 lines (34 loc) · 1.14 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
package resourcestatus
import (
"context"
"fmt"
"github.com/seal-io/walrus/pkg/dao/types/status"
)
// resourceTypes indicate supported resource type and function to get status.
var resourceTypes map[string]getStatusFunc
// getStatusFunc is function use resource id to get resource status.
type getStatusFunc func(ctx context.Context, resourceType, name string) (*status.Status, error)
func init() {
resourceTypes = map[string]getStatusFunc{
"google_sql_database_instance": getSQLDatabaseInstance,
"google_redis_instance": getRedisInstance,
"google_compute_instance": getComputeInstance,
}
}
// IsSupported indicate whether the resource type is supported.
func IsSupported(resourceType string) bool {
_, ok := resourceTypes[resourceType]
return ok
}
// Get resource status by resource type and name.
func Get(ctx context.Context, resourceType, name string) (*status.Status, error) {
getFunc, exist := resourceTypes[resourceType]
if !exist {
return nil, fmt.Errorf("unsupported resource type: %s", resourceType)
}
st, err := getFunc(ctx, resourceType, name)
if err != nil {
return &status.Status{}, err
}
return st, nil
}