forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipaddress.go
105 lines (87 loc) · 2.33 KB
/
ipaddress.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
package gcetasks
import (
"fmt"
"github.com/golang/glog"
"google.golang.org/api/compute/v1"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/gce"
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
)
//go:generate fitask -type=IPAddress
type IPAddress struct {
Name *string
Address *string
}
func (e *IPAddress) Find(c *fi.Context) (*IPAddress, error) {
actual, err := e.find(c.Cloud.(*gce.GCECloud))
if actual != nil && err == nil {
if e.Address == nil {
e.Address = actual.Address
}
}
return actual, err
}
func (e *IPAddress) find(cloud *gce.GCECloud) (*IPAddress, error) {
r, err := cloud.Compute.Addresses.Get(cloud.Project, cloud.Region, *e.Name).Do()
if err != nil {
if gce.IsNotFound(err) {
return nil, nil
}
return nil, fmt.Errorf("error listing IPAddresss: %v", err)
}
actual := &IPAddress{}
actual.Address = &r.Address
actual.Name = &r.Name
return actual, nil
}
var _ fi.HasAddress = &IPAddress{}
func (e *IPAddress) FindAddress(context *fi.Context) (*string, error) {
actual, err := e.find(context.Cloud.(*gce.GCECloud))
if err != nil {
return nil, fmt.Errorf("error querying for IPAddress: %v", err)
}
if actual == nil {
return nil, nil
}
return actual.Address, nil
}
func (e *IPAddress) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
func (_ *IPAddress) CheckChanges(a, e, changes *IPAddress) error {
if a != nil {
if changes.Name != nil {
return fi.CannotChangeField("Name")
}
if changes.Address != nil {
return fi.CannotChangeField("Address")
}
}
return nil
}
func (_ *IPAddress) RenderGCE(t *gce.GCEAPITarget, a, e, changes *IPAddress) error {
addr := &compute.Address{
Name: *e.Name,
Address: fi.StringValue(e.Address),
Region: t.Cloud.Region,
}
if a == nil {
glog.Infof("GCE creating address: %q", addr.Name)
_, err := t.Cloud.Compute.Addresses.Insert(t.Cloud.Project, t.Cloud.Region, addr).Do()
if err != nil {
return fmt.Errorf("error creating IPAddress: %v", err)
}
} else {
return fmt.Errorf("Cannot apply changes to IPAddress: %v", changes)
}
return nil
}
type terraformAddress struct {
Name *string `json:"name"`
}
func (_ *IPAddress) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *IPAddress) error {
tf := &terraformAddress{
Name: e.Name,
}
return t.RenderResource("google_compute_address", *e.Name, tf)
}