diff --git a/api/resource.rb b/api/resource.rb index cc9256d4ffe5..54515e60f463 100644 --- a/api/resource.rb +++ b/api/resource.rb @@ -297,57 +297,64 @@ def exclude_if_not_in_version!(version) # In newer resources there is much less standardisation in terms of value. # Generally for them though, it's the product.base_url + resource.name def self_link_url - base_url = @__product.base_url + [@__product.base_url, self_link_uri].flatten.join + end + + # Returns the partial uri / relative path of a resource. In newer resources, + # this is the name. This fn is named self_link_uri for consistency, but + # could otherwise be considered to be "path" + def self_link_uri if @self_link.nil? - [base_url, [@base_url, '{{name}}'].join('/')].flatten.join + [@base_url, '{{name}}'].join('/') else - self_link = @self_link - [base_url, self_link].flatten.join + @self_link end end def collection_url - [ - @__product.base_url, - @base_url - ].flatten.join + [@__product.base_url, collection_uri].flatten.join end - def async_operation_url - raise 'Not an async resource' if async.nil? + def collection_uri + @base_url + end - [@__product.base_url, async.operation.base_url].flatten.join + def async_operation_url + [@__product.base_url, async_operation_uri].flatten.join end - def default_create_url - if @create_verb.nil? || @create_verb == :POST - collection_url - elsif @create_verb == :PUT - self_link_url - else - raise "unsupported create verb #{@create_verb}" - end + def async_operation_uri + raise 'Not an async resource' if async.nil? + async.operation.base_url end def full_create_url + [@__product.base_url, create_uri].flatten.join + end + + def create_uri if @create_url.nil? - default_create_url + if @create_verb.nil? || @create_verb == :POST + collection_uri + elsif @create_verb == :PUT + self_link_uri + else + raise "unsupported create verb #{@create_verb}" + end else - [ - @__product.base_url, - @create_url - ].flatten.join + @create_url end end def full_delete_url + [@__product.base_url, delete_uri].flatten.join + end + + def delete_uri if @delete_url.nil? - self_link_url + self_link_uri else - [ - @__product.base_url, - @delete_url - ].flatten.join + @delete_url end end diff --git a/provider/core.rb b/provider/core.rb index 671c5779b5ad..bf724b92ffd4 100644 --- a/provider/core.rb +++ b/provider/core.rb @@ -389,9 +389,13 @@ def custom_update_properties_by_url(properties, update_url) end def update_url(resource, url_part) - return resource.self_link_url if url_part.nil? + [resource.__product.base_url, update_uri(resource, url_part)].flatten.join + end + + def update_uri(resource, url_part) + return resource.self_link_uri if url_part.nil? - [resource.__product.base_url, url_part].flatten.join + url_part end # TODO(nelsonjr): Review all object interfaces and move to private methods diff --git a/templates/terraform/custom_expand/self_link_from_name.erb b/templates/terraform/custom_expand/self_link_from_name.erb index 00e3f89521b3..380369602984 100644 --- a/templates/terraform/custom_expand/self_link_from_name.erb +++ b/templates/terraform/custom_expand/self_link_from_name.erb @@ -25,7 +25,7 @@ func expand<%= prefix -%><%= titlelize_property(property) -%>(v interface{}, d T return "https://www.googleapis.com/compute/v1/" + v.(string), nil } else if strings.HasPrefix(v.(string), "regions/") || strings.HasPrefix(v.(string), "zones/") { // For regional or zonal resources which include their region or zone, just put the project in front. - url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/") + url, err := replaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/") if err != nil { return nil, err } @@ -34,7 +34,7 @@ func expand<%= prefix -%><%= titlelize_property(property) -%>(v interface{}, d T // Anything else is assumed to be a regional resource, with a partial link that begins with the resource name. // This isn't very likely - it's a last-ditch effort to extract something useful here. We can do a better job // as soon as MultiResourceRefs are working since we'll know the types that this field is supposed to point to. - url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/regions/{{region}}/") + url, err := replaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/regions/{{region}}/") if err != nil { return nil, err } diff --git a/templates/terraform/examples/base_configs/test_file.go.erb b/templates/terraform/examples/base_configs/test_file.go.erb index 08d9b2891f63..c62a7bd43b26 100644 --- a/templates/terraform/examples/base_configs/test_file.go.erb +++ b/templates/terraform/examples/base_configs/test_file.go.erb @@ -114,7 +114,7 @@ func testAccCheck<%= resource_name -%>Destroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) - url, err := replaceVarsForTest(rs, "<%= object.self_link_url -%>") + url, err := replaceVarsForTest(rs, "<%= "{{#{object.__product.name}BasePath}}#{object.self_link_uri}" -%>") if err != nil { return err } diff --git a/templates/terraform/post_create/labels.erb b/templates/terraform/post_create/labels.erb index 5c5e263a953f..1aace6e83ca7 100644 --- a/templates/terraform/post_create/labels.erb +++ b/templates/terraform/post_create/labels.erb @@ -16,7 +16,7 @@ if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(v)) && (ok || labelFingerprintProp := d.Get("label_fingerprint") obj["labelFingerprint"] = labelFingerprintProp - url, err = replaceVars(d, config, "<%= object.self_link_url -%>/setLabels") + url, err = replaceVars(d, config, "<%= "{{#{object.__product.name}BasePath}}#{object.self_link_uri}" -%>/setLabels") if err != nil { return err } diff --git a/templates/terraform/pre_delete/detach_network.erb b/templates/terraform/pre_delete/detach_network.erb index f06fe608b84e..e9f4722ffd15 100644 --- a/templates/terraform/pre_delete/detach_network.erb +++ b/templates/terraform/pre_delete/detach_network.erb @@ -3,7 +3,7 @@ if d.Get("networks.#").(int) > 0 { patched := make(map[string]interface{}) patched["networks"] = nil - url, err := replaceVars(d, config, "https://www.googleapis.com/dns/v1beta2/projects/{{project}}/policies/{{name}}") + url, err := replaceVars(d, config, "{{DnsBasePath}}projects/{{project}}/policies/{{name}}") if err != nil { return err } diff --git a/templates/terraform/pre_delete/modify_delete_url.erb b/templates/terraform/pre_delete/modify_delete_url.erb index e54915c38159..aa0b8545a0e8 100644 --- a/templates/terraform/pre_delete/modify_delete_url.erb +++ b/templates/terraform/pre_delete/modify_delete_url.erb @@ -2,7 +2,7 @@ // in theory, we should find a way to disable the default URL and not construct // both, but that's a problem for another day. Today, we cheat. log.Printf("[DEBUG] replacing URL %q with a custom delete URL", url) -url, err = replaceVars(d, config, "<%= object.__product.base_url -%><%=object.base_url-%>/{{name}}") +url, err = replaceVars(d, config, "<%= "{{#{object.__product.name}BasePath}}" -%><%=object.base_url-%>/{{name}}") if err != nil { return err } diff --git a/templates/terraform/resource.erb b/templates/terraform/resource.erb index cc2b519d92f1..fb4b50cb4762 100644 --- a/templates/terraform/resource.erb +++ b/templates/terraform/resource.erb @@ -141,7 +141,7 @@ func resource<%= resource_name -%>Create(d *schema.ResourceData, meta interface{ defer mutexKV.Unlock(lockName) <% end -%> - url, err := replaceVars(d, config, "<%= object.full_create_url -%>") + url, err := replaceVars(d, config, "<%= "{{#{object.__product.name}BasePath}}#{object.create_uri}" -%>") if err != nil { return err } @@ -199,7 +199,7 @@ func resource<%= resource_name -%>Create(d *schema.ResourceData, meta interface{ func resource<%= resource_name -%>Read(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - url, err := replaceVars(d, config, "<%= object.self_link_url -%>") + url, err := replaceVars(d, config, "<%= "{{#{object.__product.name}BasePath}}#{object.self_link_uri}" -%>") if err != nil { return err } @@ -327,7 +327,7 @@ if <%= props.map { |prop| "d.HasChange(\"#{prop.name.underscore}\")" }.join ' || defer mutexKV.Unlock(lockName) <% end -%> - url, err := replaceVars(d, config, "<%= update_url(object, key[:update_url]) -%>") + url, err := replaceVars(d, config, "<%= "{{#{object.__product.name}BasePath}}#{update_uri(object, key[:update_url])}" -%>") if err != nil { return err } @@ -417,7 +417,7 @@ if <%= props.map { |prop| "d.HasChange(\"#{prop.name.underscore}\")" }.join ' || defer mutexKV.Unlock(lockName) <% end -%> - url, err := replaceVars(d, config, "<%= update_url(object, object.update_url) -%>") + url, err := replaceVars(d, config, "<%= "{{#{object.__product.name}BasePath}}#{update_uri(object, object.update_url)}" -%>") if err != nil { return err } @@ -485,7 +485,7 @@ func resource<%= resource_name -%>Delete(d *schema.ResourceData, meta interface{ defer mutexKV.Unlock(lockName) <% end -%> - url, err := replaceVars(d, config, "<%= object.full_delete_url -%>") + url, err := replaceVars(d, config, "<%= "{{#{object.__product.name}BasePath}}#{object.delete_uri}" -%>") if err != nil { return err } diff --git a/third_party/terraform/data_sources/data_source_google_composer_image_versions.go b/third_party/terraform/data_sources/data_source_google_composer_image_versions.go index 5ced5c4d5cea..1750b5e487be 100644 --- a/third_party/terraform/data_sources/data_source_google_composer_image_versions.go +++ b/third_party/terraform/data_sources/data_source_google_composer_image_versions.go @@ -56,7 +56,7 @@ func dataSourceGoogleComposerImageVersionsRead(d *schema.ResourceData, meta inte return err } - url, err := replaceVars(d, config, "https://composer.googleapis.com/v1/projects/{{project}}/locations/{{region}}/imageVersions") + url, err := replaceVars(d, config, "{{ComposerBasePath}}projects/{{project}}/locations/{{region}}/imageVersions") if err != nil { return err } diff --git a/third_party/terraform/data_sources/data_source_tpu_tensorflow_versions.go b/third_party/terraform/data_sources/data_source_tpu_tensorflow_versions.go index 0c36811b48d1..56806f74ed39 100644 --- a/third_party/terraform/data_sources/data_source_tpu_tensorflow_versions.go +++ b/third_party/terraform/data_sources/data_source_tpu_tensorflow_versions.go @@ -45,7 +45,7 @@ func dataSourceTpuTensorFlowVersionsRead(d *schema.ResourceData, meta interface{ return err } - url, err := replaceVars(d, config, "https://tpu.googleapis.com/v1/projects/{{project}}/locations/{{zone}}/tensorflowVersions") + url, err := replaceVars(d, config, "{{TpuBasePath}}projects/{{project}}/locations/{{zone}}/tensorflowVersions") if err != nil { return err } diff --git a/third_party/terraform/tests/resource_access_context_manager_access_level_test.go.erb b/third_party/terraform/tests/resource_access_context_manager_access_level_test.go.erb index 4e6ac5e53463..49458fdf9b83 100644 --- a/third_party/terraform/tests/resource_access_context_manager_access_level_test.go.erb +++ b/third_party/terraform/tests/resource_access_context_manager_access_level_test.go.erb @@ -68,7 +68,7 @@ func testAccCheckAccessContextManagerAccessLevelDestroy(s *terraform.State) erro config := testAccProvider.Meta().(*Config) - url, err := replaceVarsForTest(rs, "https://accesscontextmanager.googleapis.com/v1beta/{{name}}") + url, err := replaceVarsForTest(rs, "{{AccessContextManagerBasePath}}{{name}}") if err != nil { return err } diff --git a/third_party/terraform/tests/resource_access_context_manager_access_policy_test.go.erb b/third_party/terraform/tests/resource_access_context_manager_access_policy_test.go.erb index 9a4af0ca4831..48ee0c3af320 100644 --- a/third_party/terraform/tests/resource_access_context_manager_access_policy_test.go.erb +++ b/third_party/terraform/tests/resource_access_context_manager_access_policy_test.go.erb @@ -68,7 +68,7 @@ func testAccCheckAccessContextManagerAccessPolicyDestroy(s *terraform.State) err config := testAccProvider.Meta().(*Config) - url, err := replaceVarsForTest(rs, "https://accesscontextmanager.googleapis.com/v1beta/accessPolicies/{{name}}") + url, err := replaceVarsForTest(rs, "{{AccessContextManagerBasePath}}accessPolicies/{{name}}") if err != nil { return err } diff --git a/third_party/terraform/tests/resource_access_context_manager_service_perimeter_test.go.erb b/third_party/terraform/tests/resource_access_context_manager_service_perimeter_test.go.erb index 7eee550c9172..cc3b8756aa0f 100644 --- a/third_party/terraform/tests/resource_access_context_manager_service_perimeter_test.go.erb +++ b/third_party/terraform/tests/resource_access_context_manager_service_perimeter_test.go.erb @@ -67,7 +67,7 @@ func testAccCheckAccessContextManagerServicePerimeterDestroy(s *terraform.State) config := testAccProvider.Meta().(*Config) - url, err := replaceVarsForTest(rs, "https://accesscontextmanager.googleapis.com/v1beta/{{name}}") + url, err := replaceVarsForTest(rs, "{{AccessContextManagerBasePath}}{{name}}") if err != nil { return err } diff --git a/third_party/terraform/tests/resource_compute_backend_bucket_signed_url_key_test.go b/third_party/terraform/tests/resource_compute_backend_bucket_signed_url_key_test.go index 23aaeaa903a6..7e4a9c52255d 100644 --- a/third_party/terraform/tests/resource_compute_backend_bucket_signed_url_key_test.go +++ b/third_party/terraform/tests/resource_compute_backend_bucket_signed_url_key_test.go @@ -86,7 +86,7 @@ func checkComputeBackendBucketSignedUrlKeyExists(s *terraform.State) (bool, erro config := testAccProvider.Meta().(*Config) keyName := rs.Primary.ID - url, err := replaceVarsForTest(rs, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/backendBuckets/{{backend_bucket}}") + url, err := replaceVarsForTest(rs, "{{ComputeBasePath}}projects/{{project}}/global/backendBuckets/{{backend_bucket}}") if err != nil { return false, err } diff --git a/third_party/terraform/tests/resource_compute_backend_service_signed_url_key_test.go b/third_party/terraform/tests/resource_compute_backend_service_signed_url_key_test.go index 7d22301aca6a..27fbfa363fff 100644 --- a/third_party/terraform/tests/resource_compute_backend_service_signed_url_key_test.go +++ b/third_party/terraform/tests/resource_compute_backend_service_signed_url_key_test.go @@ -86,7 +86,7 @@ func checkComputeBackendServiceSignedUrlKeyExists(s *terraform.State) (bool, err config := testAccProvider.Meta().(*Config) keyName := rs.Primary.ID - url, err := replaceVarsForTest(rs, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/backendServices/{{backend_service}}") + url, err := replaceVarsForTest(rs, "{{ComputeBasePath}}projects/{{project}}/global/backendServices/{{backend_service}}") if err != nil { return false, err }