This repository has been archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathoperator.go
109 lines (85 loc) · 2.55 KB
/
operator.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
package google
import (
"context"
"fmt"
"google.golang.org/api/compute/v1"
"google.golang.org/api/option"
"github.com/seal-io/walrus/pkg/dao/model"
"github.com/seal-io/walrus/pkg/dao/types"
"github.com/seal-io/walrus/pkg/dao/types/status"
"github.com/seal-io/walrus/pkg/operator/google/resourcestatus"
gtypes "github.com/seal-io/walrus/pkg/operator/google/types"
optypes "github.com/seal-io/walrus/pkg/operator/types"
"github.com/seal-io/walrus/utils/hash"
)
const OperatorType = "Google"
func New(ctx context.Context, opts optypes.CreateOptions) (optypes.Operator, error) {
name := opts.Connector.ID.String()
cred, err := gtypes.GetCredential(opts.Connector.ConfigData)
if err != nil {
return nil, err
}
return Operator{
name: name,
cred: cred,
identifier: hash.SumStrings("google:", cred.Project, cred.Region, cred.Zone),
}, nil
}
type Operator struct {
name string
cred *gtypes.Credential
identifier string
}
func (o Operator) Type() optypes.Type {
return OperatorType
}
func (o Operator) IsConnected(ctx context.Context) error {
service, err := compute.NewService(ctx, option.WithCredentialsJSON([]byte(o.cred.Credentials)))
if err != nil {
return err
}
_, err = service.Regions.List(o.cred.Project).Do()
if err != nil {
return fmt.Errorf("error connect to google cloud: %w", err)
}
return nil
}
func (o Operator) Burst() int {
return 100
}
func (o Operator) ID() string {
return o.identifier
}
func (o Operator) GetKeys(
ctx context.Context,
component *model.ResourceComponent,
) (*types.ResourceComponentOperationKeys, error) {
return nil, nil
}
func (o Operator) GetStatus(ctx context.Context, resource *model.ResourceComponent) (*status.Status, error) {
st := &status.Status{}
if !resourcestatus.IsSupported(resource.Type) {
return st, nil
}
newCtx := context.WithValue(ctx, optypes.CredentialKey, o.cred)
nst, err := resourcestatus.Get(newCtx, resource.Type, resource.Name)
if err != nil {
return st, fmt.Errorf("error get resource %s:%s from %s: %w", resource.Type, resource.Name, o.name, err)
}
return nst, nil
}
func (o Operator) GetComponents(
ctx context.Context,
component *model.ResourceComponent,
) ([]*model.ResourceComponent, error) {
return nil, nil
}
func (o Operator) Log(ctx context.Context, s string, options optypes.LogOptions) error {
return nil
}
func (o Operator) Exec(ctx context.Context, s string, options optypes.ExecOptions) error {
return nil
}
func (o Operator) Label(ctx context.Context, component *model.ResourceComponent, m map[string]string) error {
return nil
}