From 8a08ee621fea310b627f5be349019ff8638e491b Mon Sep 17 00:00:00 2001 From: gary-beautypie <71257526+gary-beautypie@users.noreply.github.com> Date: Fri, 16 Jul 2021 22:45:20 +0100 Subject: [PATCH] feat: Add support for GCP notification integration (#603) --- docs/resources/notification_integration.md | 1 + go.sum | 2 -- pkg/resources/notification_integration.go | 19 +++++++++++++- ...otification_integration_acceptance_test.go | 26 +++++++++++++++++++ .../notification_integration_test.go | 18 ++++++++++++- 5 files changed, 62 insertions(+), 4 deletions(-) diff --git a/docs/resources/notification_integration.md b/docs/resources/notification_integration.md index 5917f1a7a1..eba8af3520 100644 --- a/docs/resources/notification_integration.md +++ b/docs/resources/notification_integration.md @@ -29,6 +29,7 @@ description: |- - **comment** (String) - **direction** (String) Direction of the cloud messaging with respect to Snowflake (required only for error notifications) - **enabled** (Boolean) +- **gcp_pubsub_subscription_name** (String) The subscription id that Snowflake will listen to when using the GCP_PUBSUB provider. - **id** (String) The ID of this resource. - **notification_provider** (String) The third-party cloud message queuing service (e.g. AZURE_STORAGE_QUEUE, AWS_SQS) - **type** (String) A type of integration diff --git a/go.sum b/go.sum index f279d8619b..1a9972678f 100644 --- a/go.sum +++ b/go.sum @@ -1008,8 +1008,6 @@ golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= golang.org/x/tools v0.0.0-20200904185747-39188db58858/go.mod h1:Cj7w3i3Rnn0Xh82ur9kSqwfTHTeVxaDqrfMjpcNT6bE= -golang.org/x/tools v0.1.3 h1:L69ShwSZEyCsLKoAxDKeMvLDZkumEe8gXUZAjab0tX8= -golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.4 h1:cVngSRcfgyZCzys3KYOpCFa+4dqX/Oub9tAq00ttGVs= golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/pkg/resources/notification_integration.go b/pkg/resources/notification_integration.go index 7614f57980..185d841391 100644 --- a/pkg/resources/notification_integration.go +++ b/pkg/resources/notification_integration.go @@ -40,7 +40,7 @@ var notificationIntegrationSchema = map[string]*schema.Schema{ "notification_provider": &schema.Schema{ Type: schema.TypeString, Optional: true, - ValidateFunc: validation.StringInSlice([]string{"AZURE_STORAGE_QUEUE", "AWS_SQS"}, true), + ValidateFunc: validation.StringInSlice([]string{"AZURE_STORAGE_QUEUE", "AWS_SQS", "GCP_PUBSUB"}, true), Description: "The third-party cloud message queuing service (e.g. AZURE_STORAGE_QUEUE, AWS_SQS)", }, "azure_storage_queue_primary_uri": &schema.Schema{ @@ -78,6 +78,11 @@ var notificationIntegrationSchema = map[string]*schema.Schema{ Computed: true, Description: "Date and time when the notification integration was created.", }, + "gcp_pubsub_subscription_name": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Description: "The subscription id that Snowflake will listen to when using the GCP_PUBSUB provider.", + }, } // NotificationIntegration returns a pointer to the resource representing a notification integration @@ -132,6 +137,9 @@ func CreateNotificationIntegration(data *schema.ResourceData, meta interface{}) if v, ok := data.GetOk("aws_sqs_role_arn"); ok { stmt.SetString(`AWS_SQS_ROLE_ARN`, v.(string)) } + if v, ok := data.GetOk("gcp_pubsub_subscription_name"); ok { + stmt.SetString(`GCP_PUBSUB_SUBSCRIPTION_NAME`, v.(string)) + } err := snowflake.Exec(db, stmt.Statement()) if err != nil { @@ -228,6 +236,10 @@ func ReadNotificationIntegration(data *schema.ResourceData, meta interface{}) er if err = data.Set("aws_sqs_external_id", v.(string)); err != nil { return err } + case "GCP_PUBSUB_SUBSCRIPTION_NAME": + if err = data.Set("gcp_pubsub_subscription_name", v.(string)); err != nil { + return err + } default: log.Printf("[WARN] unexpected property %v returned from Snowflake", k) } @@ -292,6 +304,11 @@ func UpdateNotificationIntegration(data *schema.ResourceData, meta interface{}) stmt.SetString("AWS_SQS_ROLE_ARN", data.Get("aws_sqs_role_arn").(string)) } + if data.HasChange("gcp_pubsub_subscription_name") { + runSetStatement = true + stmt.SetString("GCP_PUBSUB_SUBSCRIPTION_NAME", data.Get("gcp_pubsub_subscription_name").(string)) + } + if runSetStatement { if err := snowflake.Exec(db, stmt.Statement()); err != nil { return fmt.Errorf("error updating notification integration: %w", err) diff --git a/pkg/resources/notification_integration_acceptance_test.go b/pkg/resources/notification_integration_acceptance_test.go index 00b6ec92aa..8801d499e4 100644 --- a/pkg/resources/notification_integration_acceptance_test.go +++ b/pkg/resources/notification_integration_acceptance_test.go @@ -32,6 +32,21 @@ func TestAcc_NotificationIntegration(t *testing.T) { }, }, }) + + pubsubName := "projects/project-1234/subscriptions/sub2" + resource.Test(t, resource.TestCase{ + Providers: providers(), + Steps: []resource.TestStep{ + { + Config: gcpNotificationIntegrationConfig(accName, pubsubName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_notification_integration.test", "name", accName), + resource.TestCheckResourceAttr("snowflake_notification_integration.test", "notification_provider", "GCP_PUBSUB"), + resource.TestCheckResourceAttr("snowflake_notification_integration.test", "gcp_pubsub_subscription_name", pubsubName), + ), + }, + }, + }) } func azureNotificationIntegrationConfig(name string, azureStorageQueuePrimaryUri string, azureTenantId string) string { @@ -45,3 +60,14 @@ resource "snowflake_notification_integration" "test" { ` return fmt.Sprintf(s, name, "AZURE_STORAGE_QUEUE", azureStorageQueuePrimaryUri, azureTenantId) } + +func gcpNotificationIntegrationConfig(name string, gcpPubsubSubscriptionName string) string { + s := ` +resource "snowflake_notification_integration" "test" { + name = "%s" + notification_provider = "%s" + gcp_pubsub_subscription_name = "%s" +} +` + return fmt.Sprintf(s, name, "GCP_PUBSUB", gcpPubsubSubscriptionName) +} diff --git a/pkg/resources/notification_integration_test.go b/pkg/resources/notification_integration_test.go index c02edd6bdd..1cf3d18ee4 100644 --- a/pkg/resources/notification_integration_test.go +++ b/pkg/resources/notification_integration_test.go @@ -47,6 +47,16 @@ func TestNotificationIntegrationCreate(t *testing.T) { }, expectSQL: `^CREATE NOTIFICATION INTEGRATION "test_notification_integration" AWS_SQS_ARN='some-sqs-arn' AWS_SQS_ROLE_ARN='some-iam-role-arn' COMMENT='great comment' DIRECTION='OUTBOUND' NOTIFICATION_PROVIDER='AWS_SQS' TYPE='QUEUE' ENABLED=true$`, }, + { + notificationProvider: "GCP_PUBSUB", + raw: map[string]interface{}{ + "name": "test_notification_integration", + "comment": "great comment", + "notification_provider": "GCP_PUBSUB", + "gcp_pubsub_subscription_name": "some-gcp-sub-name", + }, + expectSQL: `^CREATE NOTIFICATION INTEGRATION "test_notification_integration" COMMENT='great comment' GCP_PUBSUB_SUBSCRIPTION_NAME='some-gcp-sub-name' NOTIFICATION_PROVIDER='GCP_PUBSUB' TYPE='QUEUE' ENABLED=true$`, + }, } for _, testCase := range testCases { r := require.New(t) @@ -73,6 +83,9 @@ func TestNotificationIntegrationRead(t *testing.T) { { notificationProvider: "AWS_SQS", }, + { + notificationProvider: "GCP_PUBSUB", + }, } for _, testCase := range testCases { r := require.New(t) @@ -123,7 +136,10 @@ func expectReadNotificationIntegration(mock sqlmock.Sqlmock, notificationProvide AddRow("AWS_SQS_ARN", "String", "some-sqs-arn", nil). AddRow("AWS_SQS_ROLE_ARN", "String", "some-iam-role-arn", nil). AddRow("AWS_SQS_EXTERNAL_ID", "String", "AGreatExternalID", nil) + case "GCP_PUBSUB": + descRows = descRows. + AddRow("NOTIFICATION_PROVIDER", "String", notificationProvider, nil). + AddRow("GCP_PUBSUB_SUBSCRIPTION_NAME", "String", "some-gcp-sub-name", nil) } - mock.ExpectQuery(`DESCRIBE NOTIFICATION INTEGRATION "test_notification_integration"$`).WillReturnRows(descRows) }