forked from hashicorp/terraform-provider-google
/
regional_utils.go
55 lines (49 loc) · 1.91 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
46
47
48
49
50
51
52
53
54
55
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tpgresource
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
// - region argument in the provider 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 GetResourceNameFromSelfLink(v.(string)), nil
} else if v, isRegionalCluster := d.GetOk("region"); isRegionalCluster {
return GetResourceNameFromSelfLink(v.(string)), nil
} else {
if v, ok := d.GetOk("zone"); ok {
return GetResourceNameFromSelfLink(v.(string)), nil
} else {
if config.Region != "" {
return GetResourceNameFromSelfLink(config.Region), nil
} else if config.Zone != "" {
return GetResourceNameFromSelfLink(config.Zone), nil
} else {
return "", fmt.Errorf("Unable to determine location: region/zone not configured in resource/provider 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
}