forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute_instance_helpers.go
213 lines (187 loc) · 6.94 KB
/
compute_instance_helpers.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package google
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
computeBeta "google.golang.org/api/compute/v0.beta"
)
func expandAliasIpRanges(ranges []interface{}) []*computeBeta.AliasIpRange {
ipRanges := make([]*computeBeta.AliasIpRange, 0, len(ranges))
for _, raw := range ranges {
data := raw.(map[string]interface{})
ipRanges = append(ipRanges, &computeBeta.AliasIpRange{
IpCidrRange: data["ip_cidr_range"].(string),
SubnetworkRangeName: data["subnetwork_range_name"].(string),
})
}
return ipRanges
}
func flattenAliasIpRange(ranges []*computeBeta.AliasIpRange) []map[string]interface{} {
rangesSchema := make([]map[string]interface{}, 0, len(ranges))
for _, ipRange := range ranges {
rangesSchema = append(rangesSchema, map[string]interface{}{
"ip_cidr_range": ipRange.IpCidrRange,
"subnetwork_range_name": ipRange.SubnetworkRangeName,
})
}
return rangesSchema
}
func flattenScheduling(scheduling *computeBeta.Scheduling) []map[string]interface{} {
result := make([]map[string]interface{}, 0, 1)
schedulingMap := map[string]interface{}{
"on_host_maintenance": scheduling.OnHostMaintenance,
"preemptible": scheduling.Preemptible,
}
if scheduling.AutomaticRestart != nil {
schedulingMap["automatic_restart"] = *scheduling.AutomaticRestart
}
result = append(result, schedulingMap)
return result
}
func flattenAccessConfigs(accessConfigs []*computeBeta.AccessConfig) ([]map[string]interface{}, string) {
flattened := make([]map[string]interface{}, len(accessConfigs))
natIP := ""
for i, ac := range accessConfigs {
flattened[i] = map[string]interface{}{
"nat_ip": ac.NatIP,
"network_tier": ac.NetworkTier,
"assigned_nat_ip": ac.NatIP,
}
if ac.SetPublicPtr {
flattened[i]["public_ptr_domain_name"] = ac.PublicPtrDomainName
}
if natIP == "" {
natIP = ac.NatIP
}
}
return flattened, natIP
}
func flattenNetworkInterfaces(d *schema.ResourceData, config *Config, networkInterfaces []*computeBeta.NetworkInterface) ([]map[string]interface{}, string, string, string, error) {
flattened := make([]map[string]interface{}, len(networkInterfaces))
var region, internalIP, externalIP string
for i, iface := range networkInterfaces {
var ac []map[string]interface{}
ac, externalIP = flattenAccessConfigs(iface.AccessConfigs)
subnet, err := ParseSubnetworkFieldValue(iface.Subnetwork, d, config)
if err != nil {
return nil, "", "", "", err
}
region = subnet.Region
flattened[i] = map[string]interface{}{
"address": iface.NetworkIP,
"network_ip": iface.NetworkIP,
"network": ConvertSelfLinkToV1(iface.Network),
"subnetwork": ConvertSelfLinkToV1(iface.Subnetwork),
"subnetwork_project": subnet.Project,
"access_config": ac,
"alias_ip_range": flattenAliasIpRange(iface.AliasIpRanges),
}
// Instance template interfaces never have names, so they're absent
// in the instance template network_interface schema. We want to use the
// same flattening code for both resource types, so we avoid trying to
// set the name field when it's not set at the GCE end.
if iface.Name != "" {
flattened[i]["name"] = iface.Name
}
if internalIP == "" {
internalIP = iface.NetworkIP
}
}
return flattened, region, internalIP, externalIP, nil
}
func expandAccessConfigs(configs []interface{}) []*computeBeta.AccessConfig {
acs := make([]*computeBeta.AccessConfig, len(configs))
for i, raw := range configs {
data := raw.(map[string]interface{})
acs[i] = &computeBeta.AccessConfig{
Type: "ONE_TO_ONE_NAT",
NatIP: data["nat_ip"].(string),
NetworkTier: data["network_tier"].(string),
}
if ptr, ok := data["public_ptr_domain_name"]; ok && ptr != "" {
acs[i].SetPublicPtr = true
acs[i].PublicPtrDomainName = ptr.(string)
}
}
return acs
}
func expandNetworkInterfaces(d *schema.ResourceData, config *Config) ([]*computeBeta.NetworkInterface, error) {
configs := d.Get("network_interface").([]interface{})
ifaces := make([]*computeBeta.NetworkInterface, len(configs))
for i, raw := range configs {
data := raw.(map[string]interface{})
network := data["network"].(string)
subnetwork := data["subnetwork"].(string)
if (network == "" && subnetwork == "") || (network != "" && subnetwork != "") {
return nil, fmt.Errorf("exactly one of network or subnetwork must be provided")
}
nf, err := ParseNetworkFieldValue(network, d, config)
if err != nil {
return nil, fmt.Errorf("cannot determine self_link for network %q: %s", network, err)
}
subnetProjectField := fmt.Sprintf("network_interface.%d.subnetwork_project", i)
sf, err := ParseSubnetworkFieldValueWithProjectField(subnetwork, subnetProjectField, d, config)
if err != nil {
return nil, fmt.Errorf("cannot determine self_link for subnetwork %q: %s", subnetwork, err)
}
ifaces[i] = &computeBeta.NetworkInterface{
NetworkIP: data["network_ip"].(string),
Network: nf.RelativeLink(),
Subnetwork: sf.RelativeLink(),
AccessConfigs: expandAccessConfigs(data["access_config"].([]interface{})),
AliasIpRanges: expandAliasIpRanges(data["alias_ip_range"].([]interface{})),
}
// network_ip is deprecated. We want address to win if both are set.
if data["address"].(string) != "" {
ifaces[i].NetworkIP = data["address"].(string)
}
}
return ifaces, nil
}
func flattenServiceAccounts(serviceAccounts []*computeBeta.ServiceAccount) []map[string]interface{} {
result := make([]map[string]interface{}, len(serviceAccounts))
for i, serviceAccount := range serviceAccounts {
result[i] = map[string]interface{}{
"email": serviceAccount.Email,
"scopes": schema.NewSet(stringScopeHashcode, convertStringArrToInterface(serviceAccount.Scopes)),
}
}
return result
}
func expandServiceAccounts(configs []interface{}) []*computeBeta.ServiceAccount {
accounts := make([]*computeBeta.ServiceAccount, len(configs))
for i, raw := range configs {
data := raw.(map[string]interface{})
accounts[i] = &computeBeta.ServiceAccount{
Email: data["email"].(string),
Scopes: canonicalizeServiceScopes(convertStringSet(data["scopes"].(*schema.Set))),
}
if accounts[i].Email == "" {
accounts[i].Email = "default"
}
}
return accounts
}
func flattenGuestAccelerators(accelerators []*computeBeta.AcceleratorConfig) []map[string]interface{} {
acceleratorsSchema := make([]map[string]interface{}, len(accelerators))
for i, accelerator := range accelerators {
acceleratorsSchema[i] = map[string]interface{}{
"count": accelerator.AcceleratorCount,
"type": accelerator.AcceleratorType,
}
}
return acceleratorsSchema
}
func resourceInstanceTags(d *schema.ResourceData) *computeBeta.Tags {
// Calculate the tags
var tags *computeBeta.Tags
if v := d.Get("tags"); v != nil {
vs := v.(*schema.Set)
tags = new(computeBeta.Tags)
tags.Items = make([]string, vs.Len())
for i, v := range vs.List() {
tags.Items[i] = v.(string)
}
tags.Fingerprint = d.Get("tags_fingerprint").(string)
}
return tags
}