Skip to content

Commit

Permalink
New Resources: Notification Hubs (#1589)
Browse files Browse the repository at this point in the history
* Making the messaging highlight consistent

* New Resources: `azurerm_notification_hub` & `azurerm_notification_hub_namespace`

* Fixing a merge conflict

* Vendoring v18.0.0 of the Notification Hubs SDK

* Sidebar: making the message highlight consistent

* Adding placeholders for the documentation

* Fixing Notification Hub Namespaces

```
$ acctests azurerm TestAccAzureRMNotificationHubNamespace_
=== RUN   TestAccAzureRMNotificationHubNamespace_importFree
--- PASS: TestAccAzureRMNotificationHubNamespace_importFree (134.35s)
=== RUN   TestAccAzureRMNotificationHubNamespace_free
--- PASS: TestAccAzureRMNotificationHubNamespace_free (126.57s)
=== RUN   TestAccAzureRMNotificationHubNamespace_updateSku
--- PASS: TestAccAzureRMNotificationHubNamespace_updateSku (143.16s)
PASS
ok  	github.com/terraform-providers/terraform-provider-azurerm/azurerm	404.111s
```

* New Data Source: `azurerm_notification_hub_namespace`

```
$ acctests azurerm TestAccDataSourceAzureRMNotificationHubNamespace_free
=== RUN   TestAccDataSourceAzureRMNotificationHubNamespace_free
--- PASS: TestAccDataSourceAzureRMNotificationHubNamespace_free (130.03s)
PASS
ok  	github.com/terraform-providers/terraform-provider-azurerm/azurerm	130.081s
```

* New Resource `azurerm_notification_hub`

```
$ acctests azurerm TestAccAzureRMNotificationHub_basic
=== RUN   TestAccAzureRMNotificationHub_basic
--- PASS: TestAccAzureRMNotificationHub_basic (284.22s)
PASS
ok  	github.com/terraform-providers/terraform-provider-azurerm/azurerm	284.273s
```

* New Data Source: `azurerm_notification_hub`

```

```

* Working around a bug where APNS/GCM credentials couldn't be removed

Renaming the fields to better match the portal/terms used in Apple/Google Dev Portals

* Better documenting the Token field

* New Resource: `azurerm_notification_hub_authorization_rule`

Tests pass:
```

```

* Polling on our own, since the delete Future's broken

Tests pass:
```
$ acctests azurerm TestAccAzureRMNotificationHubNamespace_
=== RUN   TestAccAzureRMNotificationHubNamespace_importFree
--- PASS: TestAccAzureRMNotificationHubNamespace_importFree (71.04s)
=== RUN   TestAccAzureRMNotificationHubNamespace_free
--- PASS: TestAccAzureRMNotificationHubNamespace_free (73.17s)
=== RUN   TestAccAzureRMNotificationHubNamespace_updateSku
--- PASS: TestAccAzureRMNotificationHubNamespace_updateSku (86.43s)
PASS
ok  	github.com/terraform-providers/terraform-provider-azurerm/azurerm	230.677s
```

* Fixing a bug in the tests

* Adding an acceptance test to ensure the access keys are set

* Adding a link to the bug

* Making SKU Name Force-New due to a difference in the API

```
azurerm_notification_hub_namespace.test: Error creating/updating Notification Hub Namesapce "acctestnhn-4049058900878420170" (Resource Group "acctestrg-4049058900878420170"): notificationhubs.NamespacesClient#CreateOrUpdate: Failure sending request: StatusCode=409 -- Original Error: autorest/azure: Service returned an error. Status=<nil> Code="Conflict" Message="Namespace acctestnhn-4049058900878420170 is in state Created. Cannot update the property"
```

* repeatidly triggering deletes

* Removing dead code / adding a comment for why we re-trigger deletion

* Moving the APNS Endpoints to Constants
  • Loading branch information
tombuildsstuff authored Jul 19, 2018
1 parent 47dcdfb commit 724bff9
Show file tree
Hide file tree
Showing 43 changed files with 6,722 additions and 35 deletions.
18 changes: 17 additions & 1 deletion azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/monitor/mgmt/2018-03-01/insights"
"github.com/Azure/azure-sdk-for-go/services/mysql/mgmt/2017-12-01/mysql"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-04-01/network"
"github.com/Azure/azure-sdk-for-go/services/notificationhubs/mgmt/2017-04-01/notificationhubs"
"github.com/Azure/azure-sdk-for-go/services/postgresql/mgmt/2017-12-01/postgresql"
"github.com/Azure/azure-sdk-for-go/services/preview/authorization/mgmt/2018-01-01-preview/authorization"
"github.com/Azure/azure-sdk-for-go/services/preview/dns/mgmt/2018-03-01-preview/dns"
Expand Down Expand Up @@ -182,6 +183,10 @@ type ArmClient struct {
vnetPeeringsClient network.VirtualNetworkPeeringsClient
watcherClient network.WatchersClient

// Notification Hubs
notificationHubsClient notificationhubs.Client
notificationNamespacesClient notificationhubs.NamespacesClient

// Recovery Services
recoveryServicesVaultsClient recoveryservices.VaultsClient

Expand Down Expand Up @@ -404,8 +409,10 @@ func getArmClient(c *authentication.Config) (*ArmClient, error) {
client.registerLogicClients(endpoint, c.SubscriptionID, auth, sender)
client.registerMonitorClients(endpoint, c.SubscriptionID, auth, sender)
client.registerNetworkingClients(endpoint, c.SubscriptionID, auth, sender)
client.registerNotificationHubsClient(endpoint, c.SubscriptionID, auth, sender)
client.registerOperationalInsightsClients(endpoint, c.SubscriptionID, auth, sender)
client.registerRecoveryServiceClients(endpoint, c.SubscriptionID, auth)
client.registerPolicyClients(endpoint, c.SubscriptionID, auth)
client.registerRedisClients(endpoint, c.SubscriptionID, auth, sender)
client.registerRelayClients(endpoint, c.SubscriptionID, auth, sender)
client.registerResourcesClients(endpoint, c.SubscriptionID, auth)
Expand All @@ -415,7 +422,6 @@ func getArmClient(c *authentication.Config) (*ArmClient, error) {
client.registerStorageClients(endpoint, c.SubscriptionID, auth)
client.registerTrafficManagerClients(endpoint, c.SubscriptionID, auth)
client.registerWebClients(endpoint, c.SubscriptionID, auth)
client.registerPolicyClients(endpoint, c.SubscriptionID, auth)

return &client, nil
}
Expand Down Expand Up @@ -837,6 +843,16 @@ func (c *ArmClient) registerNetworkingClients(endpoint, subscriptionId string, a
c.watcherClient = watchersClient
}

func (c *ArmClient) registerNotificationHubsClient(endpoint, subscriptionId string, auth *autorest.BearerAuthorizer, sender autorest.Sender) {
namespacesClient := notificationhubs.NewNamespacesClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&namespacesClient.Client, auth)
c.notificationNamespacesClient = namespacesClient

notificationHubsClient := notificationhubs.NewClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&notificationHubsClient.Client, auth)
c.notificationHubsClient = notificationHubsClient
}

func (c *ArmClient) registerOperationalInsightsClients(endpoint, subscriptionId string, auth autorest.Authorizer, sender autorest.Sender) {
opwc := operationalinsights.NewWorkspacesClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&opwc.Client, auth)
Expand Down
176 changes: 176 additions & 0 deletions azurerm/data_source_notification_hub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package azurerm

import (
"fmt"

"github.com/Azure/azure-sdk-for-go/services/notificationhubs/mgmt/2017-04-01/notificationhubs"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceNotificationHub() *schema.Resource {
return &schema.Resource{
Read: dataSourceNotificationHubRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"namespace_name": {
Type: schema.TypeString,
Required: true,
},

"resource_group_name": resourceGroupNameForDataSourceSchema(),

"location": locationForDataSourceSchema(),

"apns_credential": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"application_mode": {
Type: schema.TypeString,
Computed: true,
},
"bundle_id": {
Type: schema.TypeString,
Computed: true,
},
"key_id": {
Type: schema.TypeString,
Computed: true,
},
// Team ID (within Apple & the Portal) == "AppID" (within the API)
"team_id": {
Type: schema.TypeString,
Computed: true,
},
"token": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
},
},

"gcm_credential": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"api_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: 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": tagsForDataSourceSchema(),
},
}
}

func dataSourceNotificationHubRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).notificationHubsClient
ctx := meta.(*ArmClient).StopContext

name := d.Get("name").(string)
namespaceName := d.Get("namespace_name").(string)
resourceGroup := d.Get("resource_group_name").(string)

resp, err := client.Get(ctx, resourceGroup, namespaceName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Notification Hub %q was not found in Namespace %q / Resource Group %q", name, namespaceName, resourceGroup)
}

return fmt.Errorf("Error making Read request on Notification Hub %q (Namespace %q / Resource Group %q): %+v", name, namespaceName, resourceGroup, err)
}

credentials, err := client.GetPnsCredentials(ctx, resourceGroup, namespaceName, name)
if err != nil {
return fmt.Errorf("Error retrieving Credentials for Notification Hub %q (Namespace %q / Resource Group %q): %+v", name, namespaceName, resourceGroup, err)
}

d.SetId(*resp.ID)

d.Set("name", resp.Name)
d.Set("namespace_name", namespaceName)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}

if props := credentials.PnsCredentialsProperties; props != nil {
apns := flattenNotificationHubsDataSourceAPNSCredentials(props.ApnsCredential)
if d.Set("apns_credential", apns); err != nil {
return fmt.Errorf("Error setting `apns_credential`: %+v", err)
}

gcm := flattenNotificationHubsDataSourceGCMCredentials(props.GcmCredential)
if d.Set("gcm_credential", gcm); err != nil {
return fmt.Errorf("Error setting `gcm_credential`: %+v", err)
}
}

return nil
}

func flattenNotificationHubsDataSourceAPNSCredentials(input *notificationhubs.ApnsCredential) []interface{} {
if input == nil {
return make([]interface{}, 0)
}

output := make(map[string]interface{}, 0)

if bundleId := input.AppName; bundleId != nil {
output["bundle_id"] = *bundleId
}

if endpoint := input.Endpoint; endpoint != nil {
applicationEndpoints := map[string]string{
"https://api.push.apple.com:443/3/device": "Production",
"https://api.development.push.apple.com:443/3/device": "Sandbox",
}
applicationMode := applicationEndpoints[*endpoint]
output["application_mode"] = applicationMode
}

if keyId := input.KeyID; keyId != nil {
output["key_id"] = *keyId
}

if teamId := input.AppID; teamId != nil {
output["team_id"] = *teamId
}

if token := input.Token; token != nil {
output["token"] = *token
}

return []interface{}{output}
}

func flattenNotificationHubsDataSourceGCMCredentials(input *notificationhubs.GcmCredential) []interface{} {
if input == nil {
return []interface{}{}
}

output := make(map[string]interface{}, 0)
if props := input.GcmCredentialProperties; props != nil {
if apiKey := props.GoogleAPIKey; apiKey != nil {
output["api_key"] = *apiKey
}
}

return []interface{}{output}
}
109 changes: 109 additions & 0 deletions azurerm/data_source_notification_hub_namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package azurerm

import (
"fmt"

"github.com/Azure/azure-sdk-for-go/services/notificationhubs/mgmt/2017-04-01/notificationhubs"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceNotificationHubNamespace() *schema.Resource {
return &schema.Resource{
Read: resourceArmDataSourceNotificationHubNamespaceRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"resource_group_name": resourceGroupNameForDataSourceSchema(),

"location": locationForDataSourceSchema(),

"sku": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"enabled": {
Type: schema.TypeBool,
Computed: true,
},

"namespace_type": {
Type: schema.TypeString,
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": tagsForDataSourceSchema(),

"servicebus_endpoint": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceArmDataSourceNotificationHubNamespaceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).notificationNamespacesClient
ctx := meta.(*ArmClient).StopContext

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Notification Hub Namespace %q (Resource Group %q) was not found", name, resourceGroup)
}

return fmt.Errorf("Error making Read request on Notification Hub Namespace %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.SetId(*resp.ID)

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}

sku := flattenNotificationHubDataSourceNamespacesSku(resp.Sku)
if err := d.Set("sku", sku); err != nil {
return fmt.Errorf("Error setting `sku`: %+v", err)
}

if props := resp.NamespaceProperties; props != nil {
d.Set("enabled", props.Enabled)
d.Set("namespace_type", props.NamespaceType)
d.Set("servicebus_endpoint", props.ServiceBusEndpoint)
}

return nil
}

func flattenNotificationHubDataSourceNamespacesSku(input *notificationhubs.Sku) []interface{} {
outputs := make([]interface{}, 0)
if input == nil {
return outputs
}

output := map[string]interface{}{
"name": string(input.Name),
}
outputs = append(outputs, output)
return outputs
}
43 changes: 43 additions & 0 deletions azurerm/data_source_notification_hub_namespace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataSourceAzureRMNotificationHubNamespace_free(t *testing.T) {
dataSourceName := "data.azurerm_notification_hub_namespace.test"
rInt := acctest.RandInt()
location := testLocation()
config := testAccDataSourceAzureRMNotificationHubNamespaceFree(rInt, location)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMNotificationHubNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "namespace_type", "NotificationHub"),
resource.TestCheckResourceAttr(dataSourceName, "sku.0.name", "Free"),
),
},
},
})
}

func testAccDataSourceAzureRMNotificationHubNamespaceFree(rInt int, location string) string {
resource := testAzureRMNotificationHubNamespace_free(rInt, location)
return fmt.Sprintf(`
%s
data "azurerm_notification_hub_namespace" "test" {
name = "${azurerm_notification_hub_namespace.test.name}"
resource_group_name = "${azurerm_notification_hub_namespace.test.resource_group_name}"
}
`, resource)
}
Loading

0 comments on commit 724bff9

Please sign in to comment.