generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 15
/
computeClient.go
91 lines (80 loc) · 3.4 KB
/
computeClient.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
package client
import (
"context"
"github.com/kyma-project/cloud-manager/components/lib/composed"
"net/http"
gcpclient "github.com/kyma-project/cloud-manager/components/kcp/pkg/provider/gcp/client"
"google.golang.org/api/compute/v1"
"google.golang.org/api/option"
)
type ComputeClient interface {
ListGlobalAddresses(ctx context.Context, projectId, vpc string) (*compute.AddressList, error)
CreatePscIpRange(ctx context.Context, projectId, vpcName, name, description, address string, prefixLength int64) (*compute.Operation, error)
DeleteIpRange(ctx context.Context, projectId, name string) (*compute.Operation, error)
GetIpRange(ctx context.Context, projectId, name string) (*compute.Address, error)
GetGlobalOperation(ctx context.Context, projectId, operationName string) (*compute.Operation, error)
}
func NewComputeClient() gcpclient.ClientProvider[ComputeClient] {
return gcpclient.NewCachedClientProvider(
func(ctx context.Context, httpClient *http.Client) (ComputeClient, error) {
client, err := compute.NewService(ctx, option.WithHTTPClient(httpClient))
if err != nil {
return nil, err
}
return newComputeClient(client), nil
},
)
}
func newComputeClient(svcCompute *compute.Service) ComputeClient {
return &computeClient{svcCompute: svcCompute}
}
type computeClient struct {
svcCompute *compute.Service
}
func (c *computeClient) GetIpRange(ctx context.Context, projectId, name string) (*compute.Address, error) {
logger := composed.LoggerFromCtx(ctx)
out, err := c.svcCompute.GlobalAddresses.Get(projectId, name).Do()
if err != nil {
logger.V(4).Info("GetIpRange", "err", err)
}
return out, err
}
func (c *computeClient) DeleteIpRange(ctx context.Context, projectId, name string) (*compute.Operation, error) {
logger := composed.LoggerFromCtx(ctx)
operation, err := c.svcCompute.GlobalAddresses.Delete(projectId, name).Do()
logger.V(4).Info("DeleteIpRange", "operation", operation, "err", err)
return operation, err
}
func (c *computeClient) CreatePscIpRange(ctx context.Context, projectId, vpcName, name, description, address string, prefixLength int64) (*compute.Operation, error) {
logger := composed.LoggerFromCtx(ctx)
operation, err := c.svcCompute.GlobalAddresses.Insert(projectId, &compute.Address{
Name: name,
Description: description,
Address: address,
PrefixLength: prefixLength,
Network: gcpclient.GetVPCPath(projectId, vpcName),
AddressType: string(gcpclient.AddressTypeInternal),
Purpose: string(gcpclient.IpRangePurposeVPCPeering),
}).Do()
logger.V(4).Info("CreatePscIpRange", "operation", operation, "err", err)
return operation, err
}
func (c *computeClient) ListGlobalAddresses(ctx context.Context, projectId, vpc string) (*compute.AddressList, error) {
logger := composed.LoggerFromCtx(ctx)
filter := gcpclient.GetNetworkFilter(projectId, vpc)
out, err := c.svcCompute.GlobalAddresses.List(projectId).Filter(filter).Do()
if err != nil {
logger.Error(err, "ListGlobalAddresses", "projectId", projectId, "vpc", vpc)
return nil, err
}
return out, nil
}
func (c *computeClient) GetGlobalOperation(ctx context.Context, projectId, operationName string) (*compute.Operation, error) {
logger := composed.LoggerFromCtx(ctx)
out, err := c.svcCompute.GlobalOperations.Get(projectId, operationName).Do()
if err != nil {
logger.Error(err, "GetGlobalOperation", "projectId", projectId, "operationName", operationName)
return nil, err
}
return out, nil
}