generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 13
/
serviceNetworkingClient.go
110 lines (98 loc) · 4.93 KB
/
serviceNetworkingClient.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
110
package client
import (
"context"
"fmt"
"github.com/kyma-project/cloud-manager/pkg/kcp/provider/gcp/client"
"google.golang.org/api/cloudresourcemanager/v1"
"strconv"
"github.com/kyma-project/cloud-manager/pkg/composed"
"google.golang.org/api/option"
"google.golang.org/api/servicenetworking/v1"
)
type ServiceNetworkingClient interface {
ListServiceConnections(ctx context.Context, projectId, vpcId string) ([]*servicenetworking.Connection, error)
CreateServiceConnection(ctx context.Context, projectId, vpcId string, reservedIpRanges []string) (*servicenetworking.Operation, error)
// DeleteServiceConnection: Deletes a private service access connection.
// projectNumber: Project number which is different from project id. Get it by calling client.GetProjectNumber(ctx, projectId)
DeleteServiceConnection(ctx context.Context, projectId, vpcId string) (*servicenetworking.Operation, error)
PatchServiceConnection(ctx context.Context, projectId, vpcId string, reservedIpRanges []string) (*servicenetworking.Operation, error)
GetServiceNetworkingOperation(ctx context.Context, operationName string) (*servicenetworking.Operation, error)
}
func NewServiceNetworkingClient() client.ClientProvider[ServiceNetworkingClient] {
return client.NewCachedClientProvider(
func(ctx context.Context, saJsonKeyPath string) (ServiceNetworkingClient, error) {
httpClient, err := client.GetCachedGcpClient(ctx, saJsonKeyPath)
if err != nil {
return nil, err
}
svcNetClient, err := servicenetworking.NewService(ctx, option.WithHTTPClient(httpClient))
if err != nil {
return nil, fmt.Errorf("error obtaining GCP ServiceNetworking Client: [%w]", err)
}
crmService, err := cloudresourcemanager.NewService(ctx, option.WithHTTPClient(httpClient))
if err != nil {
return nil, fmt.Errorf("error obtaining GCP CRM Client: [%w]", err)
}
return NewServiceNetworkingClientForService(svcNetClient, crmService), nil
},
)
}
func NewServiceNetworkingClientForService(svcNet *servicenetworking.APIService, crmService *cloudresourcemanager.Service) ServiceNetworkingClient {
return &serviceNetworkingClient{svcNet: svcNet, crmService: crmService}
}
type serviceNetworkingClient struct {
svcNet *servicenetworking.APIService
crmService *cloudresourcemanager.Service
}
func (c *serviceNetworkingClient) PatchServiceConnection(ctx context.Context, projectId, vpcId string, reservedIpRanges []string) (*servicenetworking.Operation, error) {
logger := composed.LoggerFromCtx(ctx)
network := client.GetVPCPath(projectId, vpcId)
operation, err := c.svcNet.Services.Connections.Patch(client.ServiceNetworkingServiceConnectionName, &servicenetworking.Connection{
Network: network,
ReservedPeeringRanges: reservedIpRanges,
}).Force(true).Do()
logger.V(4).Info("PatchServiceConnection", "operation", operation, "err", err)
return operation, err
}
func (c *serviceNetworkingClient) DeleteServiceConnection(ctx context.Context, projectId, vpcId string) (*servicenetworking.Operation, error) {
logger := composed.LoggerFromCtx(ctx)
ProjectNumber, err := client.GetCachedProjectNumber(projectId, c.crmService)
if err != nil {
return nil, err
}
network := client.GetVPCPath(strconv.FormatInt(ProjectNumber, 10), vpcId)
operation, err := c.svcNet.Services.Connections.DeleteConnection(client.ServiceNetworkingServiceConnectionName, &servicenetworking.DeleteConnectionRequest{
ConsumerNetwork: network,
}).Do()
logger.V(4).Info("DeleteServiceConnection", "operation", operation, "err", err)
return operation, err
}
func (c *serviceNetworkingClient) ListServiceConnections(ctx context.Context, projectId, vpcId string) ([]*servicenetworking.Connection, error) {
logger := composed.LoggerFromCtx(ctx)
network := client.GetVPCPath(projectId, vpcId)
out, err := c.svcNet.Services.Connections.List(client.ServiceNetworkingServicePath).Network(network).Do()
if err != nil {
logger.Error(err, "ListServiceConnections", "projectId", projectId, "vpcId", vpcId)
return nil, err
}
return out.Connections, nil
}
func (c *serviceNetworkingClient) CreateServiceConnection(ctx context.Context, projectId, vpcId string, reservedIpRanges []string) (*servicenetworking.Operation, error) {
logger := composed.LoggerFromCtx(ctx)
network := client.GetVPCPath(projectId, vpcId)
operation, err := c.svcNet.Services.Connections.Create(client.ServiceNetworkingServicePath, &servicenetworking.Connection{
Network: network,
ReservedPeeringRanges: reservedIpRanges,
}).Do()
logger.V(4).Info("CreateServiceConnection", "operation", operation, "err", err)
return operation, err
}
func (c *serviceNetworkingClient) GetServiceNetworkingOperation(ctx context.Context, operationName string) (*servicenetworking.Operation, error) {
logger := composed.LoggerFromCtx(ctx)
operation, err := c.svcNet.Operations.Get(operationName).Do()
if err != nil {
logger.Error(err, "GetServiceNetworkingOperation", "operationName", operationName)
return nil, err
}
return operation, nil
}