forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_helpers.go
178 lines (148 loc) · 5.84 KB
/
field_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
package google
import (
"fmt"
"regexp"
)
const (
globalLinkTemplate = "projects/%s/global/%s/%s"
globalLinkBasePattern = "projects/(.+)/global/%s/(.+)"
zonalLinkTemplate = "projects/%s/zones/%s/%s/%s"
zonalLinkBasePattern = "projects/(.+)/zones/(.+)/%s/(.+)"
zonalPartialLinkBasePattern = "zones/(.+)/%s/(.+)"
)
// ------------------------------------------------------------
// Field helpers
// ------------------------------------------------------------
func ParseNetworkFieldValue(network string, d TerraformResourceData, config *Config) (*GlobalFieldValue, error) {
return parseGlobalFieldValue("networks", network, "project", d, config, true)
}
func ParseSslCertificateFieldValue(sslCertificate string, d TerraformResourceData, config *Config) (*GlobalFieldValue, error) {
return parseGlobalFieldValue("sslCertificates", sslCertificate, "project", d, config, false)
}
func ParseHttpHealthCheckFieldValue(healthCheck string, d TerraformResourceData, config *Config) (*GlobalFieldValue, error) {
return parseGlobalFieldValue("httpHealthChecks", healthCheck, "project", d, config, false)
}
func ParseDiskFieldValue(disk string, d TerraformResourceData, config *Config) (*ZonalFieldValue, error) {
return parseZonalFieldValue("disks", disk, "project", "zone", d, config, false)
}
// ------------------------------------------------------------
// Base helpers used to create helpers for specific fields.
// ------------------------------------------------------------
type GlobalFieldValue struct {
Project string
Name string
resourceType string
}
func (f GlobalFieldValue) RelativeLink() string {
if len(f.Name) == 0 {
return ""
}
return fmt.Sprintf(globalLinkTemplate, f.Project, f.resourceType, f.Name)
}
// Parses a global field supporting 5 different formats:
// - https://www.googleapis.com/compute/ANY_VERSION/projects/{my_project}/global/{resource_type}/{resource_name}
// - projects/{my_project}/global/{resource_type}/{resource_name}
// - global/{resource_type}/{resource_name}
// - resource_name
// - "" (empty string). RelativeLink() returns empty if isEmptyValid is true.
//
// If the project is not specified, it first tries to get the project from the `projectSchemaField` and then fallback on the default project.
func parseGlobalFieldValue(resourceType, fieldValue, projectSchemaField string, d TerraformResourceData, config *Config, isEmptyValid bool) (*GlobalFieldValue, error) {
if len(fieldValue) == 0 {
if isEmptyValid {
return &GlobalFieldValue{resourceType: resourceType}, nil
}
return nil, fmt.Errorf("The global field for resource %s cannot be empty", resourceType)
}
r := regexp.MustCompile(fmt.Sprintf(globalLinkBasePattern, resourceType))
if parts := r.FindStringSubmatch(fieldValue); parts != nil {
return &GlobalFieldValue{
Project: parts[1],
Name: parts[2],
resourceType: resourceType,
}, nil
}
project, err := getProjectFromSchema(projectSchemaField, d, config)
if err != nil {
return nil, err
}
return &GlobalFieldValue{
Project: project,
Name: GetResourceNameFromSelfLink(fieldValue),
resourceType: resourceType,
}, nil
}
type ZonalFieldValue struct {
Project string
Zone string
Name string
resourceType string
}
func (f ZonalFieldValue) RelativeLink() string {
if len(f.Name) == 0 {
return ""
}
return fmt.Sprintf(zonalLinkTemplate, f.Project, f.Zone, f.resourceType, f.Name)
}
// Parses a zonal field supporting 5 different formats:
// - https://www.googleapis.com/compute/ANY_VERSION/projects/{my_project}/zones/{zone}/{resource_type}/{resource_name}
// - projects/{my_project}/zones/{zone}/{resource_type}/{resource_name}
// - zones/{zone}/{resource_type}/{resource_name}
// - resource_name
// - "" (empty string). RelativeLink() returns empty if isEmptyValid is true.
//
// If the project is not specified, it first tries to get the project from the `projectSchemaField` and then fallback on the default project.
// If the zone is not specified, it takes the value of `zoneSchemaField`.
func parseZonalFieldValue(resourceType, fieldValue, projectSchemaField, zoneSchemaField string, d TerraformResourceData, config *Config, isEmptyValid bool) (*ZonalFieldValue, error) {
if len(fieldValue) == 0 {
if isEmptyValid {
return &ZonalFieldValue{resourceType: resourceType}, nil
}
return nil, fmt.Errorf("The zonal field for resource %s cannot be empty.", resourceType)
}
r := regexp.MustCompile(fmt.Sprintf(zonalLinkBasePattern, resourceType))
if parts := r.FindStringSubmatch(fieldValue); parts != nil {
return &ZonalFieldValue{
Project: parts[1],
Zone: parts[2],
Name: parts[3],
resourceType: resourceType,
}, nil
}
project, err := getProjectFromSchema(projectSchemaField, d, config)
if err != nil {
return nil, err
}
r = regexp.MustCompile(fmt.Sprintf(zonalPartialLinkBasePattern, resourceType))
if parts := r.FindStringSubmatch(fieldValue); parts != nil {
return &ZonalFieldValue{
Project: project,
Zone: parts[1],
Name: parts[2],
resourceType: resourceType,
}, nil
}
if len(zoneSchemaField) == 0 {
return nil, fmt.Errorf("Invalid field format. Got '%s', expected format '%s'", fieldValue, fmt.Sprintf(globalLinkTemplate, "{project}", resourceType, "{name}"))
}
zone, ok := d.GetOk(zoneSchemaField)
if !ok {
return nil, fmt.Errorf("A zone must be specified")
}
return &ZonalFieldValue{
Project: project,
Zone: zone.(string),
Name: GetResourceNameFromSelfLink(fieldValue),
resourceType: resourceType,
}, nil
}
func getProjectFromSchema(projectSchemaField string, d TerraformResourceData, config *Config) (string, error) {
res, ok := d.GetOk(projectSchemaField)
if !ok || len(projectSchemaField) == 0 {
if config.Project != "" {
return config.Project, nil
}
return "", fmt.Errorf("project: required field is not set")
}
return res.(string), nil
}