Skip to content

Commit

Permalink
Allow overriding the base path for a URL with custom endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
rileykarson committed Jun 4, 2019
1 parent ac095b1 commit 0f722d9
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 49 deletions.
65 changes: 36 additions & 29 deletions api/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 6 additions & 2 deletions provider/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions templates/terraform/custom_expand/self_link_from_name.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion templates/terraform/examples/base_configs/test_file.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion templates/terraform/post_create/labels.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion templates/terraform/pre_delete/detach_network.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion templates/terraform/pre_delete/modify_delete_url.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 5 additions & 5 deletions templates/terraform/resource.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 0f722d9

Please sign in to comment.