diff --git a/mmv1/third_party/terraform/tests/resource_eventarc_google_channel_config_test.go.erb b/mmv1/third_party/terraform/tests/resource_eventarc_google_channel_config_test.go.erb new file mode 100644 index 000000000000..11e51b58a60f --- /dev/null +++ b/mmv1/third_party/terraform/tests/resource_eventarc_google_channel_config_test.go.erb @@ -0,0 +1,193 @@ +<% autogen_exception -%> +package google + +import ( + "context" + "fmt" + "strings" + "testing" + + dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl" + eventarc "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/eventarc<%= dcl_version(version) -%>" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +func TestAccEventarcGoogleChannelConfig_basic(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "project_name": getTestProjectFromEnv(), + "region": getTestRegionFromEnv(), + "random_suffix": randString(t, 10), + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckEventarcGoogleChannelConfigDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccEventarcGoogleChannelConfig_basic(context), + }, + { + ResourceName: "google_eventarc_google_channel_config.primary", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccEventarcGoogleChannelConfig_cryptoKeyUpdate(t *testing.T) { + t.Parallel() + + region := getTestRegionFromEnv() + key1 := BootstrapKMSKeyWithPurposeInLocationAndName(t, "ENCRYPT_DECRYPT", region, "tf-bootstrap-key1") + key2 := BootstrapKMSKeyWithPurposeInLocationAndName(t, "ENCRYPT_DECRYPT", region, "tf-bootstrap-key2") + + context := map[string]interface{}{ + "project_name": getTestProjectFromEnv(), + "region": getTestRegionFromEnv(), + "random_suffix": randString(t, 10), + "key_ring": GetResourceNameFromSelfLink(key1.KeyRing.Name), + "key1": GetResourceNameFromSelfLink(key1.CryptoKey.Name), + "key2": GetResourceNameFromSelfLink(key2.CryptoKey.Name), + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckEventarcGoogleChannelConfigDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccEventarcGoogleChannelConfig_setCryptoKey(context), + }, + { + ResourceName: "google_eventarc_google_channel_config.primary", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccEventarcGoogleChannelConfig_cryptoKeyUpdate(context), + }, + { + ResourceName: "google_eventarc_google_channel_config.primary", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccEventarcGoogleChannelConfig_basic(context map[string]interface{}) string { + return Nprintf(` +resource "google_eventarc_google_channel_config" "primary" { + location = "%{region}" + name = "projects/%{project_name}/locations/%{region}/googleChannelConfig" +} + `, context) +} + +func testAccEventarcGoogleChannelConfig_setCryptoKey(context map[string]interface{}) string { + return Nprintf(` +data "google_project" "test_project" { + project_id = "%{project_name}" +} + +data "google_kms_key_ring" "test_key_ring" { + name = "%{key_ring}" + location = "us-central1" +} + +data "google_kms_crypto_key" "key1" { + name = "%{key1}" + key_ring = data.google_kms_key_ring.test_key_ring.id +} + +resource "google_kms_crypto_key_iam_binding" "key1_binding" { + crypto_key_id = data.google_kms_crypto_key.key1.id + role = "roles/cloudkms.cryptoKeyEncrypterDecrypter" + + members = [ + "serviceAccount:service-${data.google_project.test_project.number}@gcp-sa-eventarc.iam.gserviceaccount.com", + ] +} + +resource "google_eventarc_google_channel_config" "primary" { + location = "%{region}" + name = "projects/%{project_name}/locations/%{region}/googleChannelConfig" + crypto_key_name = data.google_kms_crypto_key.key1.id + depends_on =[google_kms_crypto_key_iam_binding.key1_binding] +} + `, context) +} + +func testAccEventarcGoogleChannelConfig_cryptoKeyUpdate(context map[string]interface{}) string { + return Nprintf(` +data "google_project" "test_project" { + project_id = "%{project_name}" +} + +data "google_kms_key_ring" "test_key_ring" { + name = "%{key_ring}" + location = "us-central1" +} + +data "google_kms_crypto_key" "key2" { + name = "%{key2}" + key_ring = data.google_kms_key_ring.test_key_ring.id +} + +resource "google_kms_crypto_key_iam_binding" "key2_binding" { + crypto_key_id = data.google_kms_crypto_key.key2.id + role = "roles/cloudkms.cryptoKeyEncrypterDecrypter" + + members = [ + "serviceAccount:service-${data.google_project.test_project.number}@gcp-sa-eventarc.iam.gserviceaccount.com", + ] +} + +resource "google_eventarc_google_channel_config" "primary" { + location = "%{region}" + name = "projects/%{project_name}/locations/%{region}/googleChannelConfig" + crypto_key_name = data.google_kms_crypto_key.key2.id + depends_on =[google_kms_crypto_key_iam_binding.key2_binding] +} + `, context) +} + +func testAccCheckEventarcGoogleChannelConfigDestroyProducer(t *testing.T) func(s *terraform.State) error { + return func(s *terraform.State) error { + for name, rs := range s.RootModule().Resources { + if rs.Type != "rs.google_eventarc_google_channel_config" { + continue + } + if strings.HasPrefix(name, "data.") { + continue + } + + config := googleProviderConfig(t) + + billingProject := "" + if config.BillingProject != "" { + billingProject = config.BillingProject + } + + obj := &eventarc.GoogleChannelConfig{ + Location: dcl.String(rs.Primary.Attributes["location"]), + Name: dcl.String(rs.Primary.Attributes["name"]), + CryptoKeyName: dcl.String(rs.Primary.Attributes["crypto_key_name"]), + Project: dcl.StringOrNil(rs.Primary.Attributes["project"]), + UpdateTime: dcl.StringOrNil(rs.Primary.Attributes["update_time"]), + } + + client := NewDCLEventarcClient(config, config.userAgent, billingProject, 0) + _, err := client.GetGoogleChannelConfig(context.Background(), obj) + if err == nil { + return fmt.Errorf("google_eventarc_google_channel_config still exists %v", obj) + } + } + return nil + } +} \ No newline at end of file diff --git a/tpgtools/overrides/eventarc/beta/google_channel_config.yaml b/tpgtools/overrides/eventarc/beta/google_channel_config.yaml new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/tpgtools/overrides/eventarc/google_channel_config.yaml b/tpgtools/overrides/eventarc/google_channel_config.yaml new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/tpgtools/overrides/eventarc/samples/googlechannelconfig/basic.tf.tmpl b/tpgtools/overrides/eventarc/samples/googlechannelconfig/basic.tf.tmpl new file mode 100644 index 000000000000..9106bc3c73e6 --- /dev/null +++ b/tpgtools/overrides/eventarc/samples/googlechannelconfig/basic.tf.tmpl @@ -0,0 +1,31 @@ +data "google_project" "test_project" { + project_id = "{{project-id}}" +} + +data "google_kms_key_ring" "test_key_ring" { + name = "{{keyring}}" + location = "{{region}}" +} + +data "google_kms_crypto_key" "key" { + name = "{{key}}" + key_ring = data.google_kms_key_ring.test_key_ring.id +} + +resource "google_kms_crypto_key_iam_binding" "key1_binding" { + crypto_key_id = data.google_kms_crypto_key.key1.id + role = "roles/cloudkms.cryptoKeyEncrypterDecrypter" + + members = [ + "serviceAccount:service-${data.google_project.test_project.number}@gcp-sa-eventarc.iam.gserviceaccount.com", + ] +} + +resource "google_eventarc_google_channel_config" "primary" { + location = "{{region}}" + name = "{{channel}}" + project = "${data.google_project.test_project.project_id}" + crypto_key_name = "${data.google_kms_crypto_key.key1.id}" + third_party_provider = "projects/${data.google_project.test_project.project_id}/locations/{{region}}/providers/datadog" + depends_on = [google_kms_crypto_key_iam_binding.key1_binding] +} \ No newline at end of file diff --git a/tpgtools/overrides/eventarc/samples/googlechannelconfig/basic.yaml b/tpgtools/overrides/eventarc/samples/googlechannelconfig/basic.yaml new file mode 100644 index 000000000000..e6e45bf2f4cd --- /dev/null +++ b/tpgtools/overrides/eventarc/samples/googlechannelconfig/basic.yaml @@ -0,0 +1,11 @@ +variables: + - name: "project-id" + type: "project" + - name: "region" + type: "region" + - name: "keyring" + type: "resource_name" + - name: "key" + type: "resource_name" + - name: "channel" + type: "resource_name" \ No newline at end of file diff --git a/tpgtools/overrides/eventarc/samples/googlechannelconfig/meta.yaml b/tpgtools/overrides/eventarc/samples/googlechannelconfig/meta.yaml new file mode 100644 index 000000000000..b587d36aff29 --- /dev/null +++ b/tpgtools/overrides/eventarc/samples/googlechannelconfig/meta.yaml @@ -0,0 +1,6 @@ +# meta.yaml +# +# We are hiding the terraform template because the test's for an Eventarc GoogleChannelConfig are handwritten in mmv1. However we want to +# generate the docs for a channel on the registry. We make tpgtools do this without it generating a test +test_hide: + - basic.tf.tmpl \ No newline at end of file