-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathvpc_dhcpoptions_association.go
131 lines (103 loc) · 3.63 KB
/
vpc_dhcpoptions_association.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
/*
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 awstasks
import (
"fmt"
"github.com/aws/aws-sdk-go/service/ec2"
"k8s.io/klog/v2"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"k8s.io/kops/upup/pkg/fi/cloudup/cloudformation"
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
"k8s.io/kops/upup/pkg/fi/cloudup/terraformWriter"
)
// +kops:fitask
type VPCDHCPOptionsAssociation struct {
Name *string
Lifecycle fi.Lifecycle
VPC *VPC
DHCPOptions *DHCPOptions
}
func (e *VPCDHCPOptionsAssociation) Find(c *fi.Context) (*VPCDHCPOptionsAssociation, error) {
cloud := c.Cloud.(awsup.AWSCloud)
vpcID := e.VPC.ID
dhcpOptionsID := e.DHCPOptions.ID
if vpcID == nil || dhcpOptionsID == nil {
return nil, nil
}
vpc, err := cloud.DescribeVPC(*vpcID)
if err != nil {
return nil, err
}
actual := &VPCDHCPOptionsAssociation{}
actual.VPC = &VPC{ID: vpc.VpcId}
actual.DHCPOptions = &DHCPOptions{ID: vpc.DhcpOptionsId}
// Prevent spurious changes
actual.Name = e.Name
actual.Lifecycle = e.Lifecycle
return actual, nil
}
func (e *VPCDHCPOptionsAssociation) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
func (s *VPCDHCPOptionsAssociation) CheckChanges(a, e, changes *VPCDHCPOptionsAssociation) error {
if e.VPC == nil {
return fi.RequiredField("VPC")
}
if e.DHCPOptions == nil {
return fi.RequiredField("DHCPOptions")
}
if a != nil && changes != nil {
if changes.VPC != nil {
// Should be impossible anyway because VPC is our primary key...
return fi.CannotChangeField("VPC")
}
}
return nil
}
func (_ *VPCDHCPOptionsAssociation) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *VPCDHCPOptionsAssociation) error {
if changes.DHCPOptions != nil {
klog.V(2).Infof("calling EC2 AssociateDhcpOptions")
request := &ec2.AssociateDhcpOptionsInput{
VpcId: e.VPC.ID,
DhcpOptionsId: e.DHCPOptions.ID,
}
_, err := t.Cloud.EC2().AssociateDhcpOptions(request)
if err != nil {
return fmt.Errorf("error creating VPCDHCPOptionsAssociation: %v", err)
}
}
return nil // no tags
}
type terraformVPCDHCPOptionsAssociation struct {
VPCID *terraformWriter.Literal `json:"vpc_id" cty:"vpc_id"`
DHCPOptionsID *terraformWriter.Literal `json:"dhcp_options_id" cty:"dhcp_options_id"`
}
func (_ *VPCDHCPOptionsAssociation) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *VPCDHCPOptionsAssociation) error {
tf := &terraformVPCDHCPOptionsAssociation{
VPCID: e.VPC.TerraformLink(),
DHCPOptionsID: e.DHCPOptions.TerraformLink(),
}
return t.RenderResource("aws_vpc_dhcp_options_association", *e.Name, tf)
}
type cloudformationVPCDHCPOptionsAssociation struct {
VpcId *cloudformation.Literal `json:"VpcId"`
DhcpOptionsId *cloudformation.Literal `json:"DhcpOptionsId"`
}
func (_ *VPCDHCPOptionsAssociation) RenderCloudformation(t *cloudformation.CloudformationTarget, a, e, changes *VPCDHCPOptionsAssociation) error {
tf := &cloudformationVPCDHCPOptionsAssociation{
VpcId: e.VPC.CloudformationLink(),
DhcpOptionsId: e.DHCPOptions.CloudformationLink(),
}
return t.RenderResource("AWS::EC2::VPCDHCPOptionsAssociation", *e.Name, tf)
}