From 2f9c4f564672f4320d2713ae714ba937be5e554f Mon Sep 17 00:00:00 2001 From: "Elena Xin (Centific Technologies Inc)" Date: Mon, 5 Aug 2024 15:30:03 +0800 Subject: [PATCH 1/3] support new property --- .../notification_hub_resource.go | 65 ++++++++++++++++++- .../notification_hub_resource_test.go | 52 +++++++++++++++ website/docs/r/notification_hub.html.markdown | 12 ++++ 3 files changed, 127 insertions(+), 2 deletions(-) diff --git a/internal/services/notificationhub/notification_hub_resource.go b/internal/services/notificationhub/notification_hub_resource.go index ece4c2ca2c29..b8791c35afc7 100644 --- a/internal/services/notificationhub/notification_hub_resource.go +++ b/internal/services/notificationhub/notification_hub_resource.go @@ -134,6 +134,32 @@ func resourceNotificationHub() *pluginsdk.Resource { }, }, + "browser_credential": { + Type: pluginsdk.TypeList, + Optional: true, + ForceNew: true, + MaxItems: 1, + Elem: &pluginsdk.Resource{ + Schema: map[string]*pluginsdk.Schema{ + "subject": { + Type: pluginsdk.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + "vapid_private_key": { + Type: pluginsdk.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + "vapid_public_key": { + Type: pluginsdk.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + }, + }, + }, + "gcm_credential": { Type: pluginsdk.TypeList, Optional: true, @@ -177,8 +203,9 @@ func resourceNotificationHubCreateUpdate(d *pluginsdk.ResourceData, meta interfa parameters := hubs.NotificationHubResource{ Location: location.Normalize(d.Get("location").(string)), Properties: &hubs.NotificationHubProperties{ - ApnsCredential: expandNotificationHubsAPNSCredentials(d.Get("apns_credential").([]interface{})), - GcmCredential: expandNotificationHubsGCMCredentials(d.Get("gcm_credential").([]interface{})), + ApnsCredential: expandNotificationHubsAPNSCredentials(d.Get("apns_credential").([]interface{})), + BrowserCredential: expandNotificationHubsBrowserCredentials(d.Get("browser_credential").([]interface{})), + GcmCredential: expandNotificationHubsGCMCredentials(d.Get("gcm_credential").([]interface{})), }, Tags: tags.Expand(d.Get("tags").(map[string]interface{})), } @@ -265,6 +292,10 @@ func resourceNotificationHubRead(d *pluginsdk.ResourceData, meta interface{}) er if setErr := d.Set("apns_credential", apns); setErr != nil { return fmt.Errorf("setting `apns_credential`: %+v", setErr) } + browser := flattenNotificationHubsBrowserCredentials(props.BrowserCredential) + if setErr := d.Set("browser_credential", browser); setErr != nil { + return fmt.Errorf("setting `browser_credential`: %+v", setErr) + } gcm := flattenNotificationHubsGCMCredentials(props.GcmCredential) if setErr := d.Set("gcm_credential", gcm); setErr != nil { return fmt.Errorf("setting `gcm_credential`: %+v", setErr) @@ -331,6 +362,22 @@ func expandNotificationHubsAPNSCredentials(inputs []interface{}) *hubs.ApnsCrede return &credentials } +func expandNotificationHubsBrowserCredentials(inputs []interface{}) *hubs.BrowserCredential { + if len(inputs) == 0 { + return nil + } + + input := inputs[0].(map[string]interface{}) + credentials := hubs.BrowserCredential{ + Properties: hubs.BrowserCredentialProperties{ + Subject: input["subject"].(string), + VapidPrivateKey: input["vapid_private_key"].(string), + VapidPublicKey: input["vapid_public_key"].(string), + }, + } + return &credentials +} + func flattenNotificationHubsAPNSCredentials(input *hubs.ApnsCredential) []interface{} { if input == nil { return make([]interface{}, 0) @@ -364,6 +411,20 @@ func flattenNotificationHubsAPNSCredentials(input *hubs.ApnsCredential) []interf return []interface{}{output} } +func flattenNotificationHubsBrowserCredentials(input *hubs.BrowserCredential) []interface{} { + if input == nil { + return make([]interface{}, 0) + } + + output := make(map[string]interface{}) + + output["subject"] = input.Properties.Subject + output["vapid_private_key"] = input.Properties.VapidPrivateKey + output["vapid_public_key"] = input.Properties.VapidPublicKey + + return []interface{}{output} +} + func expandNotificationHubsGCMCredentials(inputs []interface{}) *hubs.GcmCredential { if len(inputs) == 0 { return nil diff --git a/internal/services/notificationhub/notification_hub_resource_test.go b/internal/services/notificationhub/notification_hub_resource_test.go index 41a038508e53..438c96519ce6 100644 --- a/internal/services/notificationhub/notification_hub_resource_test.go +++ b/internal/services/notificationhub/notification_hub_resource_test.go @@ -34,6 +34,20 @@ func TestAccNotificationHub_basic(t *testing.T) { }) } +func TestAccNotificationHub_browserCredential(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_notification_hub", "test") + r := NotificationHubResource{} + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.browserCredential(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + func TestAccNotificationHub_updateTag(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_notification_hub", "test") r := NotificationHubResource{} @@ -127,6 +141,44 @@ resource "azurerm_notification_hub" "test" { `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger) } +func (NotificationHubResource) browserCredential(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRGpol-%d" + location = "%s" +} + +resource "azurerm_notification_hub_namespace" "test" { + name = "acctestnhn-%d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + namespace_type = "NotificationHub" + sku_name = "Free" +} + +resource "azurerm_notification_hub" "test" { + name = "acctestnh-%d" + namespace_name = azurerm_notification_hub_namespace.test.name + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + + browser_credential { + subject = "testSubject" + vapid_private_key = "X4X_Awjb4HyD70adCrw6FmFgA4wiu_TTWSZFcayBN6U" + vapid_public_key = "BC1XlIUxB6kQ2a214VqTMT4hnX44LRnhWDaiNxEi5bRtkdE5bFkRClX6gunX4_YWIn0UY8TD20gBGqvOg6T-go4" + } + + tags = { + env = "Test" + } +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger) +} + func (NotificationHubResource) withoutTag(data acceptance.TestData) string { return fmt.Sprintf(` provider "azurerm" { diff --git a/website/docs/r/notification_hub.html.markdown b/website/docs/r/notification_hub.html.markdown index 06e3f57418aa..904430276855 100644 --- a/website/docs/r/notification_hub.html.markdown +++ b/website/docs/r/notification_hub.html.markdown @@ -52,6 +52,8 @@ The following arguments are supported: ~> **NOTE:** Removing the `apns_credential` block will currently force a recreation of this resource [due to this bug in the Azure SDK for Go](https://github.com/Azure/azure-sdk-for-go/issues/2246) - we'll remove this limitation when the SDK bug is fixed. +* `browser_credential` - (Optional) A `browser_credential` block as defined below. + * `gcm_credential` - (Optional) A `gcm_credential` block as defined below. ~> **NOTE:** Removing the `gcm_credential` block will currently force a recreation of this resource [due to this bug in the Azure SDK for Go](https://github.com/Azure/azure-sdk-for-go/issues/2246) - we'll remove this limitation when the SDK bug is fixed. @@ -74,6 +76,16 @@ A `apns_credential` block contains: --- +A `browser_credential` block contains: + +* `subject` - (Required) The subject name of web push. + +* `vapid_private_key` - (Required) The VAPID private key. + +* `vapid_public_key` - (Required) The VAPID public key. + +--- + A `gcm_credential` block contains: * `api_key` - (Required) The API Key associated with the Google Cloud Messaging service. From 681e69d0a94c9aa9a9956002ce203c5c8ed1fc8d Mon Sep 17 00:00:00 2001 From: "Elena Xin (Centific Technologies Inc)" Date: Thu, 15 Aug 2024 17:08:51 +0800 Subject: [PATCH 2/3] update doc --- website/docs/r/notification_hub.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/r/notification_hub.html.markdown b/website/docs/r/notification_hub.html.markdown index 904430276855..b9f344441215 100644 --- a/website/docs/r/notification_hub.html.markdown +++ b/website/docs/r/notification_hub.html.markdown @@ -80,9 +80,9 @@ A `browser_credential` block contains: * `subject` - (Required) The subject name of web push. -* `vapid_private_key` - (Required) The VAPID private key. +* `vapid_private_key` - (Required) The Voluntary Application Server Identification (VAPID) private key. -* `vapid_public_key` - (Required) The VAPID public key. +* `vapid_public_key` - (Required) The Voluntary Application Server Identification (VAPID) public key. --- From 227bbb442767011b6d816b72d506242f6c339ebe Mon Sep 17 00:00:00 2001 From: "Elena Xin (Centific Technologies Inc)" Date: Tue, 20 Aug 2024 11:29:50 +0800 Subject: [PATCH 3/3] update code --- .../services/notificationhub/notification_hub_resource.go | 1 + website/docs/r/notification_hub.html.markdown | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/internal/services/notificationhub/notification_hub_resource.go b/internal/services/notificationhub/notification_hub_resource.go index b8791c35afc7..9ddd236a5ec6 100644 --- a/internal/services/notificationhub/notification_hub_resource.go +++ b/internal/services/notificationhub/notification_hub_resource.go @@ -150,6 +150,7 @@ func resourceNotificationHub() *pluginsdk.Resource { Type: pluginsdk.TypeString, Required: true, ValidateFunc: validation.StringIsNotEmpty, + Sensitive: true, }, "vapid_public_key": { Type: pluginsdk.TypeString, diff --git a/website/docs/r/notification_hub.html.markdown b/website/docs/r/notification_hub.html.markdown index b9f344441215..f6a6a080dc04 100644 --- a/website/docs/r/notification_hub.html.markdown +++ b/website/docs/r/notification_hub.html.markdown @@ -62,7 +62,7 @@ The following arguments are supported: --- -A `apns_credential` block contains: +A `apns_credential` supports the following: * `application_mode` - (Required) The Application Mode which defines which server the APNS Messages should be sent to. Possible values are `Production` and `Sandbox`. @@ -76,7 +76,7 @@ A `apns_credential` block contains: --- -A `browser_credential` block contains: +A `browser_credential` supports the following: * `subject` - (Required) The subject name of web push. @@ -86,7 +86,7 @@ A `browser_credential` block contains: --- -A `gcm_credential` block contains: +A `gcm_credential` supports the following: * `api_key` - (Required) The API Key associated with the Google Cloud Messaging service.