-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
vpc.go
178 lines (145 loc) · 3.96 KB
/
vpc.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package alitasks
import (
"fmt"
"github.com/denverdino/aliyungo/common"
"github.com/denverdino/aliyungo/ecs"
"k8s.io/klog/v2"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/aliup"
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
)
// +kops:fitask
type VPC struct {
Name *string
Lifecycle *fi.Lifecycle
ID *string
Region *string
CIDR *string
Shared *bool
Tags map[string]string
}
var _ fi.CompareWithID = &VPC{}
func (e *VPC) CompareWithID() *string {
return e.ID
}
func (e *VPC) Find(c *fi.Context) (*VPC, error) {
cloud := c.Cloud.(aliup.ALICloud)
request := &ecs.DescribeVpcsArgs{
RegionId: common.Region(cloud.Region()),
}
if e.ID != nil {
request.VpcId = fi.StringValue(e.ID)
}
vpcs, _, err := cloud.EcsClient().DescribeVpcs(request)
if err != nil {
return nil, fmt.Errorf("error listing VPCs: %v", err)
}
if len(vpcs) == 0 {
return nil, nil
}
var actual *VPC
for _, vpc := range vpcs {
if actual != nil {
return nil, fmt.Errorf("found multiple matching VPCs")
}
if vpc.VpcId == fi.StringValue(e.ID) {
actual = &VPC{
ID: fi.String(vpc.VpcId),
CIDR: fi.String(vpc.CidrBlock),
Name: fi.String(vpc.VpcName),
}
continue
}
if vpc.CidrBlock == fi.StringValue(e.CIDR) && vpc.VpcName == fi.StringValue(e.Name) {
actual = &VPC{
ID: fi.String(vpc.VpcId),
CIDR: fi.String(vpc.CidrBlock),
Name: fi.String(vpc.VpcName),
}
}
}
if actual != nil {
klog.V(2).Infof("found matching VPC %v", actual)
// Prevent spurious comparison failures
actual.Shared = e.Shared
if e.ID == nil {
e.ID = actual.ID
}
actual.Lifecycle = e.Lifecycle
actual.Name = e.Name
actual.Region = fi.String(cloud.Region())
return actual, nil
}
return nil, nil
}
func (s *VPC) CheckChanges(a, e, changes *VPC) error {
if a == nil {
if e.CIDR == nil {
return fi.RequiredField("CIDR")
}
if e.Name == nil {
return fi.RequiredField("Name")
}
} else {
if changes.CIDR != nil {
// TODO: Do we want to destroy & recreate the VPC?
return fi.CannotChangeField("CIDR")
}
}
return nil
}
func (e *VPC) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
func (_ *VPC) RenderALI(t *aliup.ALIAPITarget, a, e, changes *VPC) error {
if fi.BoolValue(e.Shared) && a == nil {
return fmt.Errorf("VPC with id %q not found", fi.StringValue(e.ID))
}
if a == nil {
if e.ID != nil && fi.StringValue(e.ID) != "" {
klog.V(2).Infof("Shared VPC with VPCID: %q", *e.ID)
return nil
}
klog.V(2).Infof("Creating VPC with CIDR: %q", *e.CIDR)
request := &ecs.CreateVpcArgs{
RegionId: common.Region(t.Cloud.Region()),
CidrBlock: fi.StringValue(e.CIDR),
VpcName: fi.StringValue(e.Name),
}
response, err := t.Cloud.EcsClient().CreateVpc(request)
if err != nil {
return fmt.Errorf("error creating VPC: %v", err)
}
e.ID = fi.String(response.VpcId)
}
return nil
}
type terraformVPC struct {
CIDR *string `json:"cidr_block,omitempty" cty:"cidr_block"`
Name *string `json:"vpc_name,omitempty" cty:"vpc_name"`
}
func (_ *VPC) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *VPC) error {
if err := t.AddOutputVariable("id", e.TerraformLink()); err != nil {
return err
}
tf := &terraformVPC{
CIDR: e.CIDR,
Name: e.Name,
}
return t.RenderResource("alicloud_vpc", *e.Name, tf)
}
func (e *VPC) TerraformLink() *terraform.Literal {
return terraform.LiteralProperty("alicloud_vpc", *e.Name, "id")
}