forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Eventarc GoogleChannelConfig Resource support for TPG (GoogleC…
- Loading branch information
Showing
6 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
193 changes: 193 additions & 0 deletions
193
mmv1/third_party/terraform/tests/resource_eventarc_google_channel_config_test.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
Empty file.
Empty file.
31 changes: 31 additions & 0 deletions
31
tpgtools/overrides/eventarc/samples/googlechannelconfig/basic.tf.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
} |
11 changes: 11 additions & 0 deletions
11
tpgtools/overrides/eventarc/samples/googlechannelconfig/basic.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
6 changes: 6 additions & 0 deletions
6
tpgtools/overrides/eventarc/samples/googlechannelconfig/meta.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |