generated from kyma-project/template-repository
/
kcpVpcPeering.go
74 lines (68 loc) · 1.73 KB
/
kcpVpcPeering.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
package dsl
import (
"context"
"errors"
cloudcontrolv1beta1 "github.com/kyma-project/cloud-manager/api/cloud-control/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func CreateKcpVpcPeering(ctx context.Context, clnt client.Client, obj *cloudcontrolv1beta1.VpcPeering, opts ...ObjAction) error {
if obj == nil {
obj = &cloudcontrolv1beta1.VpcPeering{}
}
NewObjActions(opts...).
Append(
WithNamespace(DefaultKcpNamespace),
).
ApplyOnObject(obj)
if obj.Name == "" {
return errors.New("the KCP VpcPeering must have name set")
}
err := clnt.Get(ctx, client.ObjectKeyFromObject(obj), obj)
if err == nil {
// already exists
return nil
}
if client.IgnoreNotFound(err) != nil {
// some error
return err
}
err = clnt.Create(ctx, obj)
return err
}
func WithKcpVpcPeeringRemoteRef(ns, name string) ObjAction {
return &objAction{
f: func(obj client.Object) {
x := obj.(*cloudcontrolv1beta1.VpcPeering)
if x.Spec.RemoteRef.Namespace == "" {
x.Spec.RemoteRef.Namespace = ns
}
if x.Spec.RemoteRef.Name == "" {
x.Spec.RemoteRef.Name = name
}
},
}
}
func WithKcpVpcPeeringSpecScope(scopeName string) ObjAction {
return &objAction{
f: func(obj client.Object) {
x := obj.(*cloudcontrolv1beta1.VpcPeering)
if x.Spec.Scope.Name == "" {
x.Spec.Scope.Name = scopeName
}
},
}
}
func WithKcpVpcPeeringSpecAws(remoteVpcId, remoteAccountId, remoteRegion string) ObjAction {
return &objAction{
f: func(obj client.Object) {
x := obj.(*cloudcontrolv1beta1.VpcPeering)
if x.Spec.VpcPeering.Aws == nil {
x.Spec.VpcPeering.Aws = &cloudcontrolv1beta1.AwsVpcPeering{
RemoteVpcId: remoteVpcId,
RemoteAccountId: remoteAccountId,
RemoteRegion: remoteRegion,
}
}
},
}
}