Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Eventarc Channel Resource support for TPG #6529

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5dfbdfe
feat: added hand writtent test for eventarc channel acceptance test
daveavi Sep 7, 2022
23be6f1
fix: able to now generate doc's without generating sample
daveavi Sep 8, 2022
be4a174
nit: removed minimal test
daveavi Sep 9, 2022
8a3daa0
fix: updating DCL v.1.20.1
daveavi Sep 9, 2022
9009d25
fix: changed test file to ruby template file to check version, to get…
daveavi Sep 9, 2022
0f258e3
fix: deleted test go file
daveavi Sep 9, 2022
5bd6f75
fix: added the deletion to commit
daveavi Sep 9, 2022
3ee0035
fix: enabling eventarcpublishing before trying to create channel
daveavi Sep 9, 2022
08a8a56
fix: put data infront of google_project
daveavi Sep 9, 2022
abe222f
fix: removing eventarcpublishing dependency in test
daveavi Sep 12, 2022
048bf16
Update mmv1/third_party/terraform/tests/resource_eventarc_channel_tes…
daveavi Sep 13, 2022
919eb75
feat: added comment for why I hid basic.tf.tmpl
daveavi Sep 13, 2022
4ad2b5f
fix: updating handwritten test with suggested changes
daveavi Sep 13, 2022
0c2213b
Merge branch 'add-eventarc-channel-test' of https://github.com/daveav…
daveavi Sep 13, 2022
2d90edd
fix: removed the replacement of local dcl in go.mod
daveavi Sep 14, 2022
62a3dda
nit: made change to crypto_key_name field according to comment made a…
daveavi Sep 14, 2022
8d17c9b
chore: updating dcl version
daveavi Sep 15, 2022
0be2ef5
feat: adding test of creating trigger referencing channel
daveavi Sep 15, 2022
0b72706
fix: changed name of test
daveavi Sep 16, 2022
5117eee
nit: made spacing more consistent for trigger test
daveavi Sep 16, 2022
e24df51
fix: changed documentation in meta.yaml for channel
daveavi Sep 16, 2022
532d89f
fix: ommiting project field
daveavi Sep 16, 2022
5d20438
nit: updating dcl version to v1.22.1
daveavi Sep 27, 2022
48c4d39
Merge branch 'main' into add-eventarc-channel-test
daveavi Nov 7, 2022
27e0d83
fix: update dcl version to v1.26.2
daveavi Nov 7, 2022
f0f3a37
Merge branch 'main' into add-eventarc-channel-test
daveavi Nov 7, 2022
64601b2
Merge branch 'add-eventarc-channel-test' of https://github.com/daveav…
daveavi Nov 7, 2022
adf5112
fix: remove channel field
daveavi Nov 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
211 changes: 211 additions & 0 deletions mmv1/third_party/terraform/tests/resource_eventarc_channel_test.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
<% autogen_exception -%>
package google

import (
"context"
"fmt"
"strings"
"testing"

dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
<% if version == "ga" -%>
eventarc "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/eventarc"
<% else -%>
eventarc "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/eventarc/beta"
<% end %>
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccEventarcChannel_basic(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"region": getTestRegionFromEnv(),
"project_name": getTestProjectFromEnv(),
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckEventarcChannelDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccEventarcChannel_basic(context),
},
{
ResourceName: "google_eventarc_channel.primary",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccEventarcChannel_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{}{
"region": region,
"project_name": getTestProjectFromEnv(),
"key_ring": GetResourceNameFromSelfLink(key1.KeyRing.Name),
"key1": GetResourceNameFromSelfLink(key1.CryptoKey.Name),
"key2": GetResourceNameFromSelfLink(key2.CryptoKey.Name),
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckEventarcChannelDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccEventarcChannel_setCryptoKey(context),
},
{
ResourceName: "google_eventarc_channel.primary",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccEventarcChannel_cryptoKeyUpdate(context),
},
{
ResourceName: "google_eventarc_channel.primary",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccEventarcChannel_basic(context map[string]interface{}) string {
return Nprintf(`
data "google_project" "test_project" {
project_id = "%{project_name}"
}

resource "google_eventarc_channel" "primary" {
location = "%{region}"
name = "tf-test-name%{random_suffix}"
rileykarson marked this conversation as resolved.
Show resolved Hide resolved
third_party_provider = "projects/${data.google_project.test_project.project_id}/locations/%{region}/providers/datadog"
}
`, context)
}

func testAccEventarcChannel_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_channel" "primary" {
rileykarson marked this conversation as resolved.
Show resolved Hide resolved
location = "%{region}"
name = "tf-test-name%{random_suffix}"
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]
}
`, context)
}

func testAccEventarcChannel_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_channel" "primary" {
location = "%{region}"
name = "tf-test-name%{random_suffix}"
crypto_key_name= data.google_kms_crypto_key.key2.id
third_party_provider = "projects/${data.google_project.test_project.project_id}/locations/%{region}/providers/datadog"
depends_on = [google_kms_crypto_key_iam_binding.key2_binding]
}
`, context)
}

func testAccCheckEventarcChannelDestroyProducer(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_channel" {
continue
}
if strings.HasPrefix(name, "data.") {
continue
}

config := googleProviderConfig(t)

billingProject := ""
if config.BillingProject != "" {
billingProject = config.BillingProject
}

obj := &eventarc.Channel{
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"]),
ThirdPartyProvider: dcl.String(rs.Primary.Attributes["third_party_provider"]),
ActivationToken: dcl.StringOrNil(rs.Primary.Attributes["activation_token"]),
CreateTime: dcl.StringOrNil(rs.Primary.Attributes["create_time"]),
PubsubTopic: dcl.StringOrNil(rs.Primary.Attributes["pubsub_topic"]),
State: eventarc.ChannelStateEnumRef(rs.Primary.Attributes["state"]),
Uid: dcl.StringOrNil(rs.Primary.Attributes["uid"]),
UpdateTime: dcl.StringOrNil(rs.Primary.Attributes["update_time"]),
}

client := NewDCLEventarcClient(config, config.userAgent, billingProject, 0)
_, err := client.GetChannel(context.Background(), obj)
if err == nil {
return fmt.Errorf("google_eventarc_channel still exists %v", obj)
}
}
return nil
}
}
175 changes: 175 additions & 0 deletions mmv1/third_party/terraform/tests/resource_eventarc_trigger_test.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<% autogen_exception -%>
package google

import (
"context"
"fmt"
"strings"
"testing"

dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
<% if version == "ga" -%>
eventarc "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/eventarc"
<% else -%>
eventarc "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/eventarc/beta"
<% end %>
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)


func TestAccEventarcTrigger_channel(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{}{
"region": region,
"project_name": getTestProjectFromEnv(),
"service_account": getTestServiceAccountFromEnv(t),
"key_ring": GetResourceNameFromSelfLink(key1.KeyRing.Name),
"key1": GetResourceNameFromSelfLink(key1.CryptoKey.Name),
"key2": GetResourceNameFromSelfLink(key2.CryptoKey.Name),
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckEventarcChannelTriggerDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccEventarcTrigger_createTriggerWithChannelName(context),
},
{
ResourceName: "google_eventarc_trigger.primary",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccEventarcTrigger_createTriggerWithChannelName(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_channel" "test_channel" {
location = "%{region}"
name = "tf-test-channel%{random_suffix}"
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]
}

resource "google_cloud_run_service" "default" {
name = "tf-test-eventarc-service%{random_suffix}"
location = "%{region}"

metadata {
namespace = "%{project_name}"
}

template {
spec {
containers {
image = "gcr.io/cloudrun/hello"
ports {
container_port = 8080
}
}
container_concurrency = 50
timeout_seconds = 100
}
}

traffic {
percent = 100
latest_revision = true
}
}

resource "google_eventarc_trigger" "primary" {
name = "tf-test-trigger%{random_suffix}"
location = "%{region}"
matching_criteria {
attribute = "type"
value = "datadog.v1.alert"
}
destination {
cloud_run_service {
service = google_cloud_run_service.default.name
region = "%{region}"
}
}
service_account= "%{service_account}"

channel = "projects/${data.google_project.test_project.project_id}/locations/%{region}/channels/${google_eventarc_channel.test_channel.name}"
rileykarson marked this conversation as resolved.
Show resolved Hide resolved

depends_on =[google_cloud_run_service.default,google_eventarc_channel.test_channel]
}
`, context)
}

func testAccCheckEventarcChannelTriggerDestroyProducer(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_trigger" {
continue
}
if strings.HasPrefix(name, "data.") {
continue
}

config := googleProviderConfig(t)

billingProject := ""
if config.BillingProject != "" {
billingProject = config.BillingProject
}

obj := &eventarc.Trigger{
Location: dcl.String(rs.Primary.Attributes["location"]),
Name: dcl.String(rs.Primary.Attributes["name"]),
Project: dcl.StringOrNil(rs.Primary.Attributes["project"]),
ServiceAccount: dcl.String(rs.Primary.Attributes["service_account"]),
CreateTime: dcl.StringOrNil(rs.Primary.Attributes["create_time"]),
Etag: dcl.StringOrNil(rs.Primary.Attributes["etag"]),
Uid: dcl.StringOrNil(rs.Primary.Attributes["uid"]),
UpdateTime: dcl.StringOrNil(rs.Primary.Attributes["update_time"]),
Channel: dcl.StringOrNil(rs.Primary.Attributes["channel"]),
}

client := NewDCLEventarcClient(config, config.userAgent, billingProject, 0)
_, err := client.GetTrigger(context.Background(), obj)
if err == nil {
return fmt.Errorf("google_eventarc_trigger still exists %v", obj)
}
}
return nil
}
}
1 change: 1 addition & 0 deletions tpgtools/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func skipResource(r *Resource) bool {
if sFilter != nil && *sFilter != "" && DCLPackageName(*sFilter) != r.ProductMetadata().PackageName {
return true
}

// skip filtered resources
if rFilter != nil && *rFilter != "" && SnakeCaseTerraformResourceName(*rFilter) != r.Name() {
return true
Expand Down
Empty file.