Skip to content

Commit

Permalink
Refactor to allow generated IAM tests to use import format overrides.…
Browse files Browse the repository at this point in the history
… This allows us to generate the cloud run IAM test
  • Loading branch information
slevenick committed Dec 5, 2019
1 parent 7622b8f commit 78b0410
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 254 deletions.
8 changes: 4 additions & 4 deletions api/resource/iam_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ class IamPolicy < Api::Object

# Allows us to override the base_url of the resource. This is required for Cloud Run as the
# IAM resources use an entirely different base URL from the actual resource
attr_reader :base_url_override
attr_reader :base_url

# Allows us to override the import format of the resource. Useful for Cloud Run where we need
# variables that are outside of the base_url qualifiers.
attr_reader :import_format_override
attr_reader :import_format

def validate
super
Expand All @@ -83,8 +83,8 @@ def validate
check :parent_resource_attribute, type: String, default: 'id'
check :test_project_name, type: String
check :iam_conditions_request_type, type: Symbol, allowed: %i[REQUEST_BODY QUERY_PARAM]
check :base_url_override, type: String
check :import_format_override, type: Array, item_type: String
check :base_url, type: String
check :import_format, type: Array, item_type: String
check(
:example_config_body,
type: String, default: 'templates/terraform/iam/iam_attributes.tf.erb'
Expand Down
4 changes: 2 additions & 2 deletions products/cloudrun/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ objects:
iam_policy: !ruby/object:Api::Resource::IamPolicy
method_name_separator: ':'
parent_resource_attribute: 'service'
base_url_override: v1/projects/{{project}}/locations/{{location}}/services/{{service}}
import_format_override: ["projects/{{project}}/locations/{{location}}/services/{{service}}", "{{service}}"]
base_url: v1/projects/{{project}}/locations/{{location}}/services/{{service}}
import_format: ["projects/{{project}}/locations/{{location}}/services/{{service}}", "{{service}}"]
parameters:
- !ruby/object:Api::Type::String
name: location
Expand Down
3 changes: 1 addition & 2 deletions products/cloudrun/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ overrides: !ruby/object:Overrides::ResourceOverrides
- !ruby/object:Provider::Terraform::Examples
name: "cloud_run_service_basic"
primary_resource_id: "default"
# skipping test until custom import ids are supported
skip_test: true
primary_resource_name: "fmt.Sprintf(\"tftest-cloudrun%s\", context[\"random_suffix\"])"
vars:
cloud_run_service_name: "tftest-cloudrun"
test_env_vars:
Expand Down
12 changes: 9 additions & 3 deletions templates/terraform/examples/base_configs/iam_test_file.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ import (
<% resource_name = product_ns + object.name -%>
<%
individual_url = object.self_link_url
params = extract_identifiers(individual_url.gsub('{{name}}', "{{#{object.name.underscore}}}"))
-%>
<%
tf_product = (@config.legacy_name || product_ns).underscore
resource_ns = object.legacy_name || "google_#{tf_product}_#{object.name.underscore}"
resource_ns_iam = resource_ns + '_iam'
-%>
<% import_url = individual_url.gsub(/({{)(\w+)(}})/, '%s').gsub(object.__product.base_url, '') -%>
<% import_str = Array.new(params.length, '%s').join('/') -%>
<%
if object.iam_policy.import_format
import_format = object.iam_policy.import_format.first
else
import_format = individual_url
end
params = extract_identifiers(import_format.gsub('{{name}}', "{{#{object.name.underscore}}}"))
import_url = import_format.gsub(/({{)(\w+)(}})/, '%s').gsub(object.__product.base_url, '')
-%>
<% import_qualifiers = [] -%>
<% params.each_with_index do |param, i| -%>
<% if param == 'project' -%>
Expand Down
4 changes: 2 additions & 2 deletions templates/terraform/iam_policy.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)
<% resource_name = product_ns + object.name -%>
<%
resource_uri = object.iam_policy.base_url_override || object.self_link_uri
resource_uri = object.iam_policy.base_url || object.self_link_uri
parent_resource_name = object.iam_policy.parent_resource_attribute || object.name.underscore
resource_params = extract_identifiers(resource_uri.gsub('{{name}}', "{{#{parent_resource_name}}}"))
-%>
Expand Down Expand Up @@ -87,7 +87,7 @@ func <%= resource_name -%>IamUpdaterProducer(d *schema.ResourceData, config *Con

<% end # if provider_default_values.include? -%>
<% end # resource_params.each -%>
<% import_format = object.iam_policy.import_format_override || object.import_format -%>
<% import_format = object.iam_policy.import_format || object.import_format -%>

// We may have gotten either a long or short name, so attempt to parse long name if possible
m, err := getImportIdQualifiers([]string{"<%= import_id_formats(import_format, object.identity, object.base_url).map{|s| format2regex s}.map{|s| s.gsub('<name>', "<#{parent_resource_name}>")}.join('","') -%>"}, d, config, d.Get("<%= parent_resource_name -%>").(string))
Expand Down
2 changes: 1 addition & 1 deletion templates/terraform/resource_iam.html.markdown.erb
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ exported:
## Import

For all import syntaxes, the "resource in question" can take any of the following forms:
<% import_format = object.iam_policy.import_format_override || object.import_format -%>
<% import_format = object.iam_policy.import_format || object.import_format -%>

<% import_id_formats(import_format, object.identity, object.base_url).each do |id_format| -%>
* <%= id_format %>
Expand Down
240 changes: 0 additions & 240 deletions third_party/terraform/tests/resource_cloud_run_service_iam_test.go

This file was deleted.

0 comments on commit 78b0410

Please sign in to comment.