diff --git a/api/resource/iam_policy.rb b/api/resource/iam_policy.rb index 5fc14f7e90a4..606fd27cf4fd 100644 --- a/api/resource/iam_policy.rb +++ b/api/resource/iam_policy.rb @@ -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 @@ -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' diff --git a/products/cloudrun/api.yaml b/products/cloudrun/api.yaml index fb728fe64c0b..e3efd688ccbe 100644 --- a/products/cloudrun/api.yaml +++ b/products/cloudrun/api.yaml @@ -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 diff --git a/products/cloudrun/terraform.yaml b/products/cloudrun/terraform.yaml index 264f6365227c..b17200d5aac8 100644 --- a/products/cloudrun/terraform.yaml +++ b/products/cloudrun/terraform.yaml @@ -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: diff --git a/templates/terraform/examples/base_configs/iam_test_file.go.erb b/templates/terraform/examples/base_configs/iam_test_file.go.erb index bbebb0974463..b4f1fc24fa31 100644 --- a/templates/terraform/examples/base_configs/iam_test_file.go.erb +++ b/templates/terraform/examples/base_configs/iam_test_file.go.erb @@ -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' -%> diff --git a/templates/terraform/iam_policy.go.erb b/templates/terraform/iam_policy.go.erb index e97e0d04dbf4..449849f75546 100644 --- a/templates/terraform/iam_policy.go.erb +++ b/templates/terraform/iam_policy.go.erb @@ -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}}}")) -%> @@ -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('', "<#{parent_resource_name}>")}.join('","') -%>"}, d, config, d.Get("<%= parent_resource_name -%>").(string)) diff --git a/templates/terraform/resource_iam.html.markdown.erb b/templates/terraform/resource_iam.html.markdown.erb index 8daef48fe662..b789e94c433a 100644 --- a/templates/terraform/resource_iam.html.markdown.erb +++ b/templates/terraform/resource_iam.html.markdown.erb @@ -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 %> diff --git a/third_party/terraform/tests/resource_cloud_run_service_iam_test.go b/third_party/terraform/tests/resource_cloud_run_service_iam_test.go deleted file mode 100644 index dcb787455f94..000000000000 --- a/third_party/terraform/tests/resource_cloud_run_service_iam_test.go +++ /dev/null @@ -1,240 +0,0 @@ -package google - -import ( - "fmt" - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" -) - -func TestAccCloudRunServiceIamBinding(t *testing.T) { - t.Parallel() - - context := map[string]interface{}{ - "random_suffix": acctest.RandString(10), - "role": "roles/viewer", - "namespace": getTestProjectFromEnv(), - } - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccCloudRunServiceIamBinding_basic(context), - }, - { - ResourceName: "google_cloud_run_service_iam_binding.foo", - ImportStateId: fmt.Sprintf("projects/%s/locations/%s/services/%s roles/viewer", getTestProjectFromEnv(), getTestRegionFromEnv(), fmt.Sprintf("tftest-cloudrun%s", context["random_suffix"])), - ImportState: true, - ImportStateVerify: true, - }, - { - // Test Iam Binding update - Config: testAccCloudRunServiceIamBinding_update(context), - }, - { - ResourceName: "google_cloud_run_service_iam_binding.foo", - ImportStateId: fmt.Sprintf("projects/%s/locations/%s/services/%s roles/viewer", getTestProjectFromEnv(), getTestRegionFromEnv(), fmt.Sprintf("tftest-cloudrun%s", context["random_suffix"])), - ImportState: true, - ImportStateVerify: true, - }, - }, - }) -} - -func TestAccCloudRunServiceIamMember(t *testing.T) { - t.Parallel() - - context := map[string]interface{}{ - "random_suffix": acctest.RandString(10), - "role": "roles/viewer", - "namespace": getTestProjectFromEnv(), - } - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - // Test Iam Member creation (no update for member, no need to test) - Config: testAccCloudRunServiceIamMember_basic(context), - }, - { - ResourceName: "google_cloud_run_service_iam_member.foo", - ImportStateId: fmt.Sprintf("projects/%s/locations/%s/services/%s roles/viewer user:admin@hashicorptest.com", getTestProjectFromEnv(), getTestRegionFromEnv(), fmt.Sprintf("tftest-cloudrun%s", context["random_suffix"])), - ImportState: true, - ImportStateVerify: true, - }, - }, - }) -} - -func TestAccCloudRunServiceIamPolicy(t *testing.T) { - t.Parallel() - - context := map[string]interface{}{ - "random_suffix": acctest.RandString(10), - "role": "roles/viewer", - "namespace": getTestProjectFromEnv(), - } - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccCloudRunServiceIamPolicy_basic(context), - }, - { - ResourceName: "google_cloud_run_service_iam_policy.foo", - ImportStateId: fmt.Sprintf("projects/%s/locations/%s/services/%s", getTestProjectFromEnv(), getTestRegionFromEnv(), fmt.Sprintf("tftest-cloudrun%s", context["random_suffix"])), - ImportState: true, - ImportStateVerify: true, - }, - }, - }) -} - -func testAccCloudRunServiceIamMember_basic(context map[string]interface{}) string { - return Nprintf(` -resource "google_cloud_run_service" "default" { - name = "tftest-cloudrun%{random_suffix}" - location = "us-central1" - - metadata { - namespace = "%{namespace}" - } - - template { - spec { - containers { - image = "gcr.io/cloudrun/hello" - } - } - } - - traffic { - percent = 100 - latest_revision = true - } -} - -resource "google_cloud_run_service_iam_member" "foo" { - location = "${google_cloud_run_service.default.location}" - project = "${google_cloud_run_service.default.project}" - service = "${google_cloud_run_service.default.name}" - role = "%{role}" - member = "user:admin@hashicorptest.com" -} -`, context) -} - -func testAccCloudRunServiceIamPolicy_basic(context map[string]interface{}) string { - return Nprintf(` -resource "google_cloud_run_service" "default" { - name = "tftest-cloudrun%{random_suffix}" - location = "us-central1" - - metadata { - namespace = "%{namespace}" - } - - template { - spec { - containers { - image = "gcr.io/cloudrun/hello" - } - } - } - - traffic { - percent = 100 - latest_revision = true - } -} - -data "google_iam_policy" "foo" { - binding { - role = "%{role}" - members = ["user:admin@hashicorptest.com"] - } -} - -resource "google_cloud_run_service_iam_policy" "foo" { - location = "${google_cloud_run_service.default.location}" - project = "${google_cloud_run_service.default.project}" - service = "${google_cloud_run_service.default.name}" - policy_data = "${data.google_iam_policy.foo.policy_data}" -} -`, context) -} - -func testAccCloudRunServiceIamBinding_basic(context map[string]interface{}) string { - return Nprintf(` -resource "google_cloud_run_service" "default" { - name = "tftest-cloudrun%{random_suffix}" - location = "us-central1" - - metadata { - namespace = "%{namespace}" - } - - template { - spec { - containers { - image = "gcr.io/cloudrun/hello" - } - } - } - - traffic { - percent = 100 - latest_revision = true - } -} - -resource "google_cloud_run_service_iam_binding" "foo" { - location = "${google_cloud_run_service.default.location}" - project = "${google_cloud_run_service.default.project}" - service = "${google_cloud_run_service.default.name}" - role = "%{role}" - members = ["user:admin@hashicorptest.com"] -} -`, context) -} - -func testAccCloudRunServiceIamBinding_update(context map[string]interface{}) string { - return Nprintf(` -resource "google_cloud_run_service" "default" { - name = "tftest-cloudrun%{random_suffix}" - location = "us-central1" - - metadata { - namespace = "%{namespace}" - } - - template { - spec { - containers { - image = "gcr.io/cloudrun/hello" - } - } - } - - traffic { - percent = 100 - latest_revision = true - } -} - -resource "google_cloud_run_service_iam_binding" "foo" { - location = "${google_cloud_run_service.default.location}" - project = "${google_cloud_run_service.default.project}" - service = "${google_cloud_run_service.default.name}" - role = "%{role}" - members = ["user:admin@hashicorptest.com", "user:paddy@hashicorp.com"] -} -`, context) -}