Skip to content

Commit

Permalink
TPG 3.0.0 changes (#230)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and rileykarson committed Nov 13, 2019
1 parent d8f1980 commit d0e4cb1
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 126 deletions.
2 changes: 1 addition & 1 deletion google/batcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (b *RequestBatcher) stop() {
// may choose to use a key with method if needed to diff GET/read and
// POST/create)
//
// As an example, for google_project_service and google_project_services, the
// As an example, for google_project_service, the
// batcher is called to batch services.batchEnable() calls for a project
// $PROJECT. The calling code uses the template
// "serviceusage:projects/$PROJECT/services:batchEnable", which mirrors the HTTP request:
Expand Down
20 changes: 1 addition & 19 deletions google/cloudscheduler_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,7 @@ func GetCloudSchedulerJobApiObject(d TerraformResourceData, config *Config) (map
}

func expandCloudSchedulerJobName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
var jobName string
project, err := getProject(d, config)
if err != nil {
return nil, err
}

region, err := getRegion(d, config)
if err != nil {
return nil, err
}

if v, ok := d.GetOk("name"); ok {
jobName = fmt.Sprintf("projects/%s/locations/%s/jobs/%s", project, region, v.(string))
} else {
err := fmt.Errorf("The name is missing for the job cannot be empty")
return nil, err
}

return jobName, nil
return replaceVars(d, config, "projects/{{project}}/locations/{{region}}/jobs/{{name}}")
}

func expandCloudSchedulerJobDescription(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
Expand Down
10 changes: 0 additions & 10 deletions google/compute_forwarding_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,6 @@ func GetComputeForwardingRuleApiObject(d TerraformResourceData, config *Config)
} else if v, ok := d.GetOkExists("backend_service"); !isEmptyValue(reflect.ValueOf(backendServiceProp)) && (ok || !reflect.DeepEqual(v, backendServiceProp)) {
obj["backendService"] = backendServiceProp
}
ipVersionProp, err := expandComputeForwardingRuleIpVersion(d.Get("ip_version"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("ip_version"); !isEmptyValue(reflect.ValueOf(ipVersionProp)) && (ok || !reflect.DeepEqual(v, ipVersionProp)) {
obj["ipVersion"] = ipVersionProp
}
loadBalancingSchemeProp, err := expandComputeForwardingRuleLoadBalancingScheme(d.Get("load_balancing_scheme"), d, config)
if err != nil {
return nil, err
Expand Down Expand Up @@ -190,10 +184,6 @@ func expandComputeForwardingRuleBackendService(v interface{}, d TerraformResourc
return url + v.(string), nil
}

func expandComputeForwardingRuleIpVersion(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeForwardingRuleLoadBalancingScheme(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down
20 changes: 1 addition & 19 deletions google/compute_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ func GetComputeNetworkApiObject(d TerraformResourceData, config *Config) (map[st
} else if v, ok := d.GetOkExists("description"); !isEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
obj["description"] = descriptionProp
}
IPv4RangeProp, err := expandComputeNetworkIpv4Range(d.Get("ipv4_range"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("ipv4_range"); !isEmptyValue(reflect.ValueOf(IPv4RangeProp)) && (ok || !reflect.DeepEqual(v, IPv4RangeProp)) {
obj["IPv4Range"] = IPv4RangeProp
}
nameProp, err := expandComputeNetworkName(d.Get("name"), d, config)
if err != nil {
return nil, err
Expand All @@ -60,7 +54,7 @@ func GetComputeNetworkApiObject(d TerraformResourceData, config *Config) (map[st
autoCreateSubnetworksProp, err := expandComputeNetworkAutoCreateSubnetworks(d.Get("auto_create_subnetworks"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("auto_create_subnetworks"); !isEmptyValue(reflect.ValueOf(autoCreateSubnetworksProp)) && (ok || !reflect.DeepEqual(v, autoCreateSubnetworksProp)) {
} else if v, ok := d.GetOkExists("auto_create_subnetworks"); ok || !reflect.DeepEqual(v, autoCreateSubnetworksProp) {
obj["autoCreateSubnetworks"] = autoCreateSubnetworksProp
}
routingConfigProp, err := expandComputeNetworkRoutingConfig(nil, d, config)
Expand All @@ -70,25 +64,13 @@ func GetComputeNetworkApiObject(d TerraformResourceData, config *Config) (map[st
obj["routingConfig"] = routingConfigProp
}

return resourceComputeNetworkEncoder(d, config, obj)
}

func resourceComputeNetworkEncoder(d TerraformResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
if _, ok := d.GetOk("ipv4_range"); !ok {
obj["autoCreateSubnetworks"] = d.Get("auto_create_subnetworks")
}

return obj, nil
}

func expandComputeNetworkDescription(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeNetworkIpv4Range(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeNetworkName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down
6 changes: 1 addition & 5 deletions google/compute_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,7 @@ func expandComputeRouteTags(v interface{}, d TerraformResourceData, config *Conf

func expandComputeRouteNextHopGateway(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
if v == "default-internet-gateway" {
project, err := getProject(d, config)
if err != nil {
return nil, err
}
return fmt.Sprintf("projects/%s/global/gateways/default-internet-gateway", project), nil
return replaceVars(d, config, "projects/{{project}}/global/gateways/default-internet-gateway")
} else {
return v, nil
}
Expand Down
46 changes: 17 additions & 29 deletions google/compute_subnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"fmt"
"net"
"reflect"
"strings"

"github.com/apparentlymart/go-cidr/cidr"
)
Expand All @@ -43,13 +42,6 @@ func isShrinkageIpCidr(old, new, _ interface{}) bool {
return true
}

func splitSubnetID(id string) (region string, name string) {
parts := strings.Split(id, "/")
region = parts[0]
name = parts[1]
return
}

func GetComputeSubnetworkCaiObject(d TerraformResourceData, config *Config) (Asset, error) {
name, err := assetName(d, config, "//compute.googleapis.com/projects/{{project}}/regions/{{region}}/subnetworks/{{name}}")
if err != nil {
Expand Down Expand Up @@ -97,12 +89,6 @@ func GetComputeSubnetworkApiObject(d TerraformResourceData, config *Config) (map
} else if v, ok := d.GetOkExists("network"); !isEmptyValue(reflect.ValueOf(networkProp)) && (ok || !reflect.DeepEqual(v, networkProp)) {
obj["network"] = networkProp
}
enableFlowLogsProp, err := expandComputeSubnetworkEnableFlowLogs(d.Get("enable_flow_logs"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("enable_flow_logs"); ok || !reflect.DeepEqual(v, enableFlowLogsProp) {
obj["enableFlowLogs"] = enableFlowLogsProp
}
fingerprintProp, err := expandComputeSubnetworkFingerprint(d.Get("fingerprint"), d, config)
if err != nil {
return nil, err
Expand Down Expand Up @@ -130,7 +116,7 @@ func GetComputeSubnetworkApiObject(d TerraformResourceData, config *Config) (map
logConfigProp, err := expandComputeSubnetworkLogConfig(d.Get("log_config"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("log_config"); !isEmptyValue(reflect.ValueOf(logConfigProp)) && (ok || !reflect.DeepEqual(v, logConfigProp)) {
} else if v, ok := d.GetOkExists("log_config"); ok || !reflect.DeepEqual(v, logConfigProp) {
obj["logConfig"] = logConfigProp
}

Expand All @@ -157,10 +143,6 @@ func expandComputeSubnetworkNetwork(v interface{}, d TerraformResourceData, conf
return f.RelativeLink(), nil
}

func expandComputeSubnetworkEnableFlowLogs(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeSubnetworkFingerprint(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -216,21 +198,27 @@ func expandComputeSubnetworkRegion(v interface{}, d TerraformResourceData, confi

func expandComputeSubnetworkLogConfig(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
transformed := make(map[string]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
purpose, ok := d.GetOkExists("purpose")

if ok && purpose.(string) == "INTERNAL_HTTPS_LOAD_BALANCER" {
// Subnetworks for L7ILB do not accept any values for logConfig
return nil, nil
}
// send enable = false to ensure logging is disabled if there is no config
transformed["enable"] = false
return transformed, nil
}

raw := l[0]
original := raw.(map[string]interface{})

v, ok := d.GetOkExists("enable_flow_logs")

transformed := make(map[string]interface{})
if !ok || v.(bool) {
transformed["enable"] = true
transformed["aggregationInterval"] = original["aggregation_interval"]
transformed["flowSampling"] = original["flow_sampling"]
transformed["metadata"] = original["metadata"]
}
// The log_config block is specified, so logging should be enabled
transformed["enable"] = true
transformed["aggregationInterval"] = original["aggregation_interval"]
transformed["flowSampling"] = original["flow_sampling"]
transformed["metadata"] = original["metadata"]

return transformed, nil
}
8 changes: 0 additions & 8 deletions google/compute_url_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ func GetComputeUrlMapApiObject(d TerraformResourceData, config *Config) (map[str
return obj, nil
}

// ResourceRef only supports 1 type and UrlMap has references to a BackendBucket or BackendService. Just read the self_link string
// instead of extracting the name and making a self_link out of it.
func expandComputeUrlMapDefaultService(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -200,8 +198,6 @@ func expandComputeUrlMapPathMatcher(v interface{}, d TerraformResourceData, conf
return req, nil
}

// ResourceRef only supports 1 type and UrlMap has references to a BackendBucket or BackendService. Just read the self_link string
// instead of extracting the name and making a self_link out of it.
func expandComputeUrlMapPathMatcherDefaultService(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -248,8 +244,6 @@ func expandComputeUrlMapPathMatcherPathRulePaths(v interface{}, d TerraformResou
return v, nil
}

// ResourceRef only supports 1 type and UrlMap has references to a BackendBucket or BackendService. Just read the self_link string
// instead of extracting the name and making a self_link out of it.
func expandComputeUrlMapPathMatcherPathRuleService(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -309,8 +303,6 @@ func expandComputeUrlMapTestPath(v interface{}, d TerraformResourceData, config
return v, nil
}

// ResourceRef only supports 1 type and UrlMap has references to a BackendBucket or BackendService. Just read the self_link string
// instead of extracting the name and making a self_link out of it.
func expandComputeUrlMapTestService(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
1 change: 1 addition & 0 deletions google/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ var defaultClientScopes = []string{
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/ndev.clouddns.readwrite",
"https://www.googleapis.com/auth/devstorage.full_control",
"https://www.googleapis.com/auth/userinfo.email",
}

func (c *Config) LoadAndValidate() error {
Expand Down
21 changes: 1 addition & 20 deletions google/pubsub_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,26 +116,7 @@ func resourcePubsubSubscriptionEncoder(d TerraformResourceData, meta interface{}
}

func expandPubsubSubscriptionName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
project, err := getProject(d, config)
if err != nil {
return "", err
}

subscription := d.Get("name").(string)

re := regexp.MustCompile("projects\\/(.*)\\/subscriptions\\/(.*)")
match := re.FindStringSubmatch(subscription)
if len(match) == 3 {
// We need to preserve the behavior where the user passes the subscription name already in the long form,
// however we need it to be stored as the short form since it's used for the replaceVars in the URL.
// The unintuitive behavior is that if the user provides the long form, we use the project from there, not the one
// specified on the resource or provider.
// TODO(drebes): consider deprecating the long form behavior for 3.0
d.Set("project", match[1])
d.Set("name", match[2])
return subscription, nil
}
return fmt.Sprintf("projects/%s/subscriptions/%s", project, subscription), nil
return replaceVars(d, config, "projects/{{project}}/subscriptions/{{name}}")
}

func expandPubsubSubscriptionTopic(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
Expand Down
17 changes: 2 additions & 15 deletions google/redis_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@

package google

import (
"fmt"
"reflect"
)
import "reflect"

func GetRedisInstanceCaiObject(d TerraformResourceData, config *Config) (Asset, error) {
name, err := assetName(d, config, "//redis.googleapis.com/projects/{{project}}/locations/{{region}}/instances/{{name}}")
Expand Down Expand Up @@ -165,17 +162,7 @@ func expandRedisInstanceLocationId(v interface{}, d TerraformResourceData, confi
}

func expandRedisInstanceName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
project, err := getProject(d, config)
if err != nil {
return nil, err
}

region, err := getRegion(d, config)
if err != nil {
return nil, err
}

return fmt.Sprintf("projects/%s/locations/%s/instances/%s", project, region, v.(string)), nil
return replaceVars(d, config, "projects/{{project}}/locations/{{region}}/instances/{{name}}")
}

func expandRedisInstanceMemorySizeGb(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
Expand Down
1 change: 1 addition & 0 deletions google/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type TerraformResourceData interface {

type TerraformResourceDiff interface {
GetChange(string) (interface{}, interface{})
Get(string) interface{}
Clear(string) error
}

Expand Down
8 changes: 8 additions & 0 deletions google/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ func validateNonNegativeDuration() schema.SchemaValidateFunc {
}
}

func validateIpAddress(i interface{}, val string) ([]string, []error) {
ip := net.ParseIP(i.(string))
if ip == nil {
return nil, []error{fmt.Errorf("could not parse %q to IP address", val)}
}
return nil, nil
}

// StringNotInSlice returns a SchemaValidateFunc which tests if the provided value
// is of type string and that it matches none of the element in the invalid slice.
// if ignorecase is true, case is ignored.
Expand Down

0 comments on commit d0e4cb1

Please sign in to comment.