diff --git a/google/batcher.go b/google/batcher.go index 85aa0005c..caa6b4595 100644 --- a/google/batcher.go +++ b/google/batcher.go @@ -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: diff --git a/google/cloudscheduler_job.go b/google/cloudscheduler_job.go index e221980d9..9574f90ba 100644 --- a/google/cloudscheduler_job.go +++ b/google/cloudscheduler_job.go @@ -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) { diff --git a/google/compute_forwarding_rule.go b/google/compute_forwarding_rule.go index 73c160eee..fe4b74b87 100644 --- a/google/compute_forwarding_rule.go +++ b/google/compute_forwarding_rule.go @@ -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 @@ -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 } diff --git a/google/compute_network.go b/google/compute_network.go index 69b834096..7e85d7e60 100644 --- a/google/compute_network.go +++ b/google/compute_network.go @@ -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 @@ -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) @@ -70,14 +64,6 @@ 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 } @@ -85,10 +71,6 @@ func expandComputeNetworkDescription(v interface{}, d TerraformResourceData, con 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 } diff --git a/google/compute_route.go b/google/compute_route.go index 647e9bea9..2be178eb1 100644 --- a/google/compute_route.go +++ b/google/compute_route.go @@ -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 } diff --git a/google/compute_subnetwork.go b/google/compute_subnetwork.go index dc4c7187b..5ddafdaa6 100644 --- a/google/compute_subnetwork.go +++ b/google/compute_subnetwork.go @@ -18,7 +18,6 @@ import ( "fmt" "net" "reflect" - "strings" "github.com/apparentlymart/go-cidr/cidr" ) @@ -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 { @@ -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 @@ -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 } @@ -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 } @@ -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 } diff --git a/google/compute_url_map.go b/google/compute_url_map.go index 70176b890..84bf5898f 100644 --- a/google/compute_url_map.go +++ b/google/compute_url_map.go @@ -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/google/config.go b/google/config.go index 806fe119b..d34a88281 100644 --- a/google/config.go +++ b/google/config.go @@ -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 { diff --git a/google/pubsub_subscription.go b/google/pubsub_subscription.go index a698121ac..8a9cbf0cd 100644 --- a/google/pubsub_subscription.go +++ b/google/pubsub_subscription.go @@ -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) { diff --git a/google/redis_instance.go b/google/redis_instance.go index e1c7a52a1..bbb70d596 100644 --- a/google/redis_instance.go +++ b/google/redis_instance.go @@ -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}}") @@ -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) { diff --git a/google/utils.go b/google/utils.go index f7c2348bd..760d35e84 100644 --- a/google/utils.go +++ b/google/utils.go @@ -29,6 +29,7 @@ type TerraformResourceData interface { type TerraformResourceDiff interface { GetChange(string) (interface{}, interface{}) + Get(string) interface{} Clear(string) error } diff --git a/google/validation.go b/google/validation.go index 07b169799..3a59f006b 100644 --- a/google/validation.go +++ b/google/validation.go @@ -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.