-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathregional_utils.go
45 lines (39 loc) · 1.61 KB
/
regional_utils.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
package google
import (
"fmt"
"strings"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
)
//These functions are used by both the `resource_container_node_pool` and `resource_container_cluster` for handling regional clusters
func isZone(location string) bool {
return len(strings.Split(location, "-")) == 3
}
// getLocation attempts to get values in this order (if they exist):
// - location argument in the resource config
// - region argument in the resource config
// - zone argument in the resource config
// - zone argument set in the provider config
func getLocation(d TerraformResourceData, config *transport_tpg.Config) (string, error) {
if v, ok := d.GetOk("location"); ok {
return v.(string), nil
} else if v, isRegionalCluster := d.GetOk("region"); isRegionalCluster {
return v.(string), nil
} else {
// If region is not explicitly set, use "zone" (or fall back to the provider-level zone).
// For now, to avoid confusion, we require region to be set in the config to create a regional
// cluster rather than falling back to the provider-level region.
return getZone(d, config)
}
}
// getZone reads the "zone" value from the given resource data and falls back
// to provider's value if not given. If neither is provided, returns an error.
func getZone(d TerraformResourceData, config *transport_tpg.Config) (string, error) {
res, ok := d.GetOk("zone")
if !ok {
if config.Zone != "" {
return config.Zone, nil
}
return "", fmt.Errorf("Cannot determine zone: set in this resource, or set provider-level zone.")
}
return GetResourceNameFromSelfLink(res.(string)), nil
}