Skip to content

Commit

Permalink
azurerm_notification_[hub|hub_namespace] - support tags (#6578)
Browse files Browse the repository at this point in the history
Fix the issue #3872
  • Loading branch information
Neil Ye committed Apr 23, 2020
1 parent f6de452 commit 0c33c0a
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 16 deletions.
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -80,9 +81,7 @@ func dataSourceNotificationHub() *schema.Resource {
},
},

// NOTE: skipping tags as there's a bug in the API where the Keys for Tags are returned in lower-case
// Azure Rest API Specs issue: https://github.com/Azure/azure-sdk-for-go/issues/2239
// "tags": tags.SchemaDataSource(),
"tags": tags.SchemaDataSource(),
},
}
}
Expand Down Expand Up @@ -131,7 +130,7 @@ func dataSourceNotificationHubRead(d *schema.ResourceData, meta interface{}) err
}
}

return nil
return tags.FlattenAndSet(d, resp.Tags)
}

func flattenNotificationHubsDataSourceAPNSCredentials(input *notificationhubs.ApnsCredential) []interface{} {
Expand Down
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -53,9 +54,7 @@ func dataSourceNotificationHubNamespace() *schema.Resource {
Computed: true,
},

// NOTE: skipping tags as there's a bug in the API where the Keys for Tags are returned in lower-case
// Azure Rest API Specs issue: https://github.com/Azure/azure-sdk-for-go/issues/2239
// "tags": tags.SchemaDataSource(),
"tags": tags.SchemaDataSource(),

"servicebus_endpoint": {
Type: schema.TypeString,
Expand Down Expand Up @@ -101,7 +100,7 @@ func resourceArmDataSourceNotificationHubNamespaceRead(d *schema.ResourceData, m
d.Set("servicebus_endpoint", props.ServiceBusEndpoint)
}

return nil
return tags.FlattenAndSet(d, resp.Tags)
}

func flattenNotificationHubDataSourceNamespacesSku(input *notificationhubs.Sku) []interface{} {
Expand Down
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -135,9 +136,7 @@ func resourceArmNotificationHub() *schema.Resource {
},
},

// NOTE: skipping tags as there's a bug in the API where the Keys for Tags are returned in lower-case
// Azure Rest API Specs issue: https://github.com/Azure/azure-sdk-for-go/issues/2239
// "tags": tags.Schema(),
"tags": tags.Schema(),
},
}
}
Expand Down Expand Up @@ -171,6 +170,7 @@ func resourceArmNotificationHubCreateUpdate(d *schema.ResourceData, meta interfa
ApnsCredential: expandNotificationHubsAPNSCredentials(d.Get("apns_credential").([]interface{})),
GcmCredential: expandNotificationHubsGCMCredentials(d.Get("gcm_credential").([]interface{})),
},
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

if _, err := client.CreateOrUpdate(ctx, resourceGroup, namespaceName, name, parameters); err != nil {
Expand Down Expand Up @@ -274,7 +274,7 @@ func resourceArmNotificationHubRead(d *schema.ResourceData, meta interface{}) er
}
}

return nil
return tags.FlattenAndSet(d, resp.Tags)
}

func resourceArmNotificationHubDelete(d *schema.ResourceData, meta interface{}) error {
Expand Down
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -78,9 +79,7 @@ func resourceArmNotificationHubNamespace() *schema.Resource {
DiffSuppressFunc: suppress.CaseDifference,
},

// NOTE: skipping tags as there's a bug in the API where the Keys for Tags are returned in lower-case
// Azure Rest API Specs issue: https://github.com/Azure/azure-sdk-for-go/issues/2239
// "tags": tags.Schema(),
"tags": tags.Schema(),

"servicebus_endpoint": {
Type: schema.TypeString,
Expand Down Expand Up @@ -126,6 +125,7 @@ func resourceArmNotificationHubNamespaceCreateUpdate(d *schema.ResourceData, met
NamespaceType: notificationhubs.NamespaceType(namespaceType),
Enabled: utils.Bool(enabled),
},
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}
if _, err := client.CreateOrUpdate(ctx, resourceGroup, name, parameters); err != nil {
return fmt.Errorf("Error creating/updating Notification Hub Namesapce %q (Resource Group %q): %+v", name, resourceGroup, err)
Expand Down Expand Up @@ -206,7 +206,7 @@ func resourceArmNotificationHubNamespaceRead(d *schema.ResourceData, meta interf
d.Set("servicebus_endpoint", props.ServiceBusEndpoint)
}

return nil
return tags.FlattenAndSet(d, resp.Tags)
}

func resourceArmNotificationHubNamespaceDelete(d *schema.ResourceData, meta interface{}) error {
Expand Down
Expand Up @@ -20,6 +20,7 @@ func TestAccDataSourceAzureRMNotificationHubNamespace_free(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(data.ResourceName, "namespace_type", "NotificationHub"),
resource.TestCheckResourceAttr(data.ResourceName, "sku.0.name", "Free"),
resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "1"),
),
},
},
Expand Down
Expand Up @@ -20,6 +20,7 @@ func TestAccDataSourceAzureRMNotificationHub_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(data.ResourceName, "apns_credential.#", "0"),
resource.TestCheckResourceAttr(data.ResourceName, "gcm_credential.#", "0"),
resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "1"),
),
},
},
Expand Down
Expand Up @@ -29,6 +29,41 @@ func TestAccAzureRMNotificationHubNamespace_free(t *testing.T) {
})
}

func TestAccAzureRMNotificationHubNamespace_updateTag(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_notification_hub_namespace", "test")
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMNotificationHubNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMNotificationHubNamespace_free(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMNotificationHubNamespaceExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "1"),
),
},
data.ImportStep(),
{
Config: testAccAzureRMNotificationHubNamespace_withoutTag(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMNotificationHubNamespaceExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "0"),
),
},
data.ImportStep(),
{
Config: testAccAzureRMNotificationHubNamespace_free(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMNotificationHubNamespaceExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "1"),
),
},
data.ImportStep(),
},
})
}

func TestAccAzureRMNotificationHubNamespace_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_notification_hub_namespace", "test")
resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -109,6 +144,32 @@ resource "azurerm_resource_group" "test" {
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"
tags = {
env = "Test"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func testAccAzureRMNotificationHubNamespace_withoutTag(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_notification_hub_namespace" "test" {
name = "acctestnhn-%d"
resource_group_name = azurerm_resource_group.test.name
Expand Down
Expand Up @@ -31,6 +31,41 @@ func TestAccAzureRMNotificationHub_basic(t *testing.T) {
})
}

func TestAccAzureRMNotificationHub_updateTag(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_notification_hub", "test")
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMNotificationHubDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMNotificationHub_basic(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMNotificationHubExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "1"),
),
},
data.ImportStep(),
{
Config: testAccAzureRMNotificationHub_withoutTag(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMNotificationHubExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "0"),
),
},
data.ImportStep(),
{
Config: testAccAzureRMNotificationHub_basic(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMNotificationHubExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "1"),
),
},
data.ImportStep(),
},
})
}

func TestAccAzureRMNotificationHub_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_notification_hub", "test")
resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -124,6 +159,38 @@ resource "azurerm_notification_hub_namespace" "test" {
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
tags = {
env = "Test"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
}

func testAccAzureRMNotificationHub_withoutTag(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
Expand Down
2 changes: 2 additions & 0 deletions website/docs/d/notification_hub.html.markdown
Expand Up @@ -42,6 +42,8 @@ output "id" {

* `gcm_credential` - A `gcm_credential` block as defined below.

* `tags` - A mapping of tags to assign to the resource.

---

A `apns_credential` block exports:
Expand Down
2 changes: 2 additions & 0 deletions website/docs/d/notification_hub_namespace.html.markdown
Expand Up @@ -41,6 +41,8 @@ output "servicebus_endpoint" {

* `enabled` - Is this Notification Hub Namespace enabled?

* `tags` - A mapping of tags to assign to the resource.

---

A `sku` block exports the following:
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/notification_hub.html.markdown
Expand Up @@ -56,6 +56,8 @@ The following arguments are supported:

~> **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.

* `tags` - (Optional) A mapping of tags to assign to the resource.

---

A `apns_credential` block contains:
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/notification_hub_namespace.html.markdown
Expand Up @@ -44,6 +44,8 @@ The following arguments are supported:

* `enabled` - (Optional) Is this Notification Hub Namespace enabled? Defaults to `true`.

* `tags` - (Optional) A mapping of tags to assign to the resource.

## Attributes Reference

The following attributes are exported:
Expand Down

0 comments on commit 0c33c0a

Please sign in to comment.