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

New Resources: Notification Hubs #1589

Merged
merged 21 commits into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
9e83632
Making the messaging highlight consistent
tombuildsstuff Apr 2, 2018
871d2cf
New Resources: `azurerm_notification_hub` & `azurerm_notification_hub…
tombuildsstuff Apr 2, 2018
0078512
Fixing a merge conflict
tombuildsstuff Jul 17, 2018
844ec85
Vendoring v18.0.0 of the Notification Hubs SDK
tombuildsstuff Jul 17, 2018
d8e7d32
Sidebar: making the message highlight consistent
tombuildsstuff Jul 17, 2018
dbd4ce5
Adding placeholders for the documentation
tombuildsstuff Jul 17, 2018
04d2a79
Fixing Notification Hub Namespaces
tombuildsstuff Jul 17, 2018
7c8f0f6
New Data Source: `azurerm_notification_hub_namespace`
tombuildsstuff Jul 17, 2018
660ba6e
New Resource `azurerm_notification_hub`
tombuildsstuff Jul 17, 2018
3df7fe3
New Data Source: `azurerm_notification_hub`
tombuildsstuff Jul 17, 2018
f7ebdd6
Working around a bug where APNS/GCM credentials couldn't be removed
tombuildsstuff Jul 18, 2018
1d38805
Better documenting the Token field
tombuildsstuff Jul 18, 2018
3914a28
New Resource: `azurerm_notification_hub_authorization_rule`
tombuildsstuff Jul 18, 2018
0305fd2
Polling on our own, since the delete Future's broken
tombuildsstuff Jul 18, 2018
af5ab68
Fixing a bug in the tests
tombuildsstuff Jul 18, 2018
85553b5
Adding an acceptance test to ensure the access keys are set
tombuildsstuff Jul 18, 2018
16e09c2
Adding a link to the bug
tombuildsstuff Jul 18, 2018
a3da5bc
Making SKU Name Force-New due to a difference in the API
tombuildsstuff Jul 18, 2018
d2d9a94
repeatidly triggering deletes
tombuildsstuff Jul 18, 2018
54e104b
Removing dead code / adding a comment for why we re-trigger deletion
tombuildsstuff Jul 19, 2018
5343ad5
Moving the APNS Endpoints to Constants
tombuildsstuff Jul 19, 2018
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
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