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

azurerm_notification_hub : Support new property browser_credential #27058

Merged
merged 3 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 64 additions & 2 deletions internal/services/notificationhub/notification_hub_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,33 @@ 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": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be marked as Sensitive

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
Sensitive: true,
},
"vapid_public_key": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
},

"gcm_credential": {
Type: pluginsdk.TypeList,
Optional: true,
Expand Down Expand Up @@ -177,8 +204,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{})),
}
Expand Down Expand Up @@ -265,6 +293,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)
Expand Down Expand Up @@ -331,6 +363,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)
Expand Down Expand Up @@ -364,6 +412,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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" {
Expand Down
16 changes: 14 additions & 2 deletions website/docs/r/notification_hub.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -60,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`.

Expand All @@ -74,7 +76,17 @@ A `apns_credential` block contains:

---

A `gcm_credential` block contains:
A `browser_credential` supports the following:

* `subject` - (Required) The subject name of web push.

* `vapid_private_key` - (Required) The Voluntary Application Server Identification (VAPID) private key.

* `vapid_public_key` - (Required) The Voluntary Application Server Identification (VAPID) public key.

---

A `gcm_credential` supports the following:

* `api_key` - (Required) The API Key associated with the Google Cloud Messaging service.

Expand Down
Loading