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

notificationhubs: switching to use ID Formatters #12845

Merged
merged 8 commits into from
Aug 5, 2021
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package migration

import (
"context"
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/notificationhub/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/pluginsdk"
)

var _ pluginsdk.StateUpgrade = NotificationHubAuthorizationRuleResourceV0ToV1{}

type NotificationHubAuthorizationRuleResourceV0ToV1 struct{}

func (NotificationHubAuthorizationRuleResourceV0ToV1) Schema() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"notification_hub_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"namespace_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"resource_group_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"manage": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},

"send": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},

"listen": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},

"primary_access_key": {
Type: pluginsdk.TypeString,
Computed: true,
},

"secondary_access_key": {
Type: pluginsdk.TypeString,
Computed: true,
},
}
}

func (NotificationHubAuthorizationRuleResourceV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc {
return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
oldIdRaw := rawState["id"].(string)
oldId, err := parse.NotificationHubAuthorizationRuleIDInsensitively(oldIdRaw)
if err != nil {
return rawState, fmt.Errorf("parsing ID %q to upgrade: %+v", oldIdRaw, err)
}

rawState["id"] = oldId.ID()
return rawState, nil
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package migration

import (
"context"
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/notificationhub/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/pluginsdk"
)

var _ pluginsdk.StateUpgrade = NotificationHubNamespaceResourceV0ToV1{}

type NotificationHubNamespaceResourceV0ToV1 struct{}

func (NotificationHubNamespaceResourceV0ToV1) Schema() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"resource_group_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"location": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"sku_name": {
Type: pluginsdk.TypeString,
Required: true,
},

"enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"namespace_type": {
Type: pluginsdk.TypeString,
Required: true,
},

"tags": {
Type: pluginsdk.TypeMap,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

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

func (NotificationHubNamespaceResourceV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc {
return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
oldIdRaw := rawState["id"].(string)
oldId, err := parse.NamespaceIDInsensitively(oldIdRaw)
if err != nil {
return rawState, fmt.Errorf("parsing ID %q to upgrade: %+v", oldIdRaw, err)
}

rawState["id"] = oldId.ID()
return rawState, nil
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package migration

import (
"context"
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/notificationhub/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/pluginsdk"
)

var _ pluginsdk.StateUpgrade = NotificationHubResourceV0ToV1{}

type NotificationHubResourceV0ToV1 struct {
}

func (NotificationHubResourceV0ToV1) Schema() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"namespace_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"resource_group_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"location": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"apns_credential": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
// NOTE: APNS supports two modes, certificate auth (v1) and token auth (v2)
// certificate authentication/v1 is marked for deprecation; as such we're not
// supporting it at this time.
"application_mode": {
Type: pluginsdk.TypeString,
Required: true,
},
"bundle_id": {
Type: pluginsdk.TypeString,
Required: true,
},
"key_id": {
Type: pluginsdk.TypeString,
Required: true,
},
// Team ID (within Apple & the Portal) == "AppID" (within the API)
"team_id": {
Type: pluginsdk.TypeString,
Required: true,
},
"token": {
Type: pluginsdk.TypeString,
Required: true,
Sensitive: true,
},
},
},
},

"gcm_credential": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"api_key": {
Type: pluginsdk.TypeString,
Required: true,
Sensitive: true,
},
},
},
},

"tags": {
Type: pluginsdk.TypeMap,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
}
}

func (NotificationHubResourceV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc {
return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
oldIdRaw := rawState["id"].(string)
oldId, err := parse.NotificationHubIDInsensitively(oldIdRaw)
if err != nil {
return rawState, fmt.Errorf("parsing ID %q to upgrade: %+v", oldIdRaw, err)
}

rawState["id"] = oldId.ID()
return rawState, nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"log"
"time"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/notificationhub/migration"

"github.com/Azure/azure-sdk-for-go/services/notificationhubs/mgmt/2017-04-01/notificationhubs"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
Expand Down Expand Up @@ -35,6 +37,11 @@ func resourceNotificationHubAuthorizationRule() *pluginsdk.Resource {
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},

SchemaVersion: 1,
StateUpgraders: pluginsdk.StateUpgrades(map[int]pluginsdk.StateUpgrade{
0: migration.NotificationHubAuthorizationRuleResourceV0ToV1{},
}),

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Expand Down Expand Up @@ -89,57 +96,44 @@ func resourceNotificationHubAuthorizationRule() *pluginsdk.Resource {

func resourceNotificationHubAuthorizationRuleCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).NotificationHubs.HubsClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

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

manage := d.Get("manage").(bool)
send := d.Get("send").(bool)
listen := d.Get("listen").(bool)

id := parse.NewNotificationHubAuthorizationRuleID(subscriptionId, d.Get("resource_group_name").(string), d.Get("namespace_name").(string), d.Get("notification_hub_name").(string), d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.GetAuthorizationRule(ctx, resourceGroup, namespaceName, notificationHubName, name)
existing, err := client.GetAuthorizationRule(ctx, id.ResourceGroup, id.NamespaceName, id.NotificationHubName, id.AuthorizationRuleName)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Authorization Rule %q (Notification Hub %q / Namespace %q / Resource Group %q): %+v", name, notificationHubName, namespaceName, resourceGroup, err)
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_notification_hub_authorization_rule", *existing.ID)
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_notification_hub_authorization_rule", id.ID())
}
}

locks.ByName(notificationHubName, notificationHubResourceName)
defer locks.UnlockByName(notificationHubName, notificationHubResourceName)
locks.ByName(id.NotificationHubName, notificationHubResourceName)
defer locks.UnlockByName(id.NotificationHubName, notificationHubResourceName)

locks.ByName(namespaceName, notificationHubNamespaceResourceName)
defer locks.UnlockByName(namespaceName, notificationHubNamespaceResourceName)
locks.ByName(id.NamespaceName, notificationHubNamespaceResourceName)
defer locks.UnlockByName(id.NamespaceName, notificationHubNamespaceResourceName)

manage := d.Get("manage").(bool)
send := d.Get("send").(bool)
listen := d.Get("listen").(bool)
parameters := notificationhubs.SharedAccessAuthorizationRuleCreateOrUpdateParameters{
Properties: &notificationhubs.SharedAccessAuthorizationRuleProperties{
Rights: expandNotificationHubAuthorizationRuleRights(manage, send, listen),
},
}

if _, err := client.CreateOrUpdateAuthorizationRule(ctx, resourceGroup, namespaceName, notificationHubName, name, parameters); err != nil {
return fmt.Errorf("Error creating Authorization Rule %q (Notification Hub %q / Namespace %q / Resource Group %q): %+v", name, notificationHubName, namespaceName, resourceGroup, err)
if _, err := client.CreateOrUpdateAuthorizationRule(ctx, id.ResourceGroup, id.NamespaceName, id.NotificationHubName, id.AuthorizationRuleName, parameters); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

read, err := client.GetAuthorizationRule(ctx, resourceGroup, namespaceName, notificationHubName, name)
if err != nil {
return fmt.Errorf("Error retrieving Authorization Rule %q (Notification Hub %q / Namespace %q / Resource Group %q): %+v", name, notificationHubName, namespaceName, resourceGroup, err)
}
if read.ID == nil {
return fmt.Errorf("Cannot read Authorization Rule %q (Notification Hub %q / Namespace %q / Resource Group %q) ID", name, notificationHubName, namespaceName, resourceGroup)
}

d.SetId(*read.ID)

d.SetId(id.ID())
return resourceNotificationHubAuthorizationRuleRead(d, meta)
}

Expand All @@ -156,17 +150,17 @@ func resourceNotificationHubAuthorizationRuleRead(d *pluginsdk.ResourceData, met
resp, err := client.GetAuthorizationRule(ctx, id.ResourceGroup, id.NamespaceName, id.NotificationHubName, id.AuthorizationRuleName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Authorization Rule %q was not found in Notification Hub %q / Namespace %q / Resource Group %q", id.AuthorizationRuleName, id.NotificationHubName, id.NamespaceName, id.ResourceGroup)
log.Printf("[DEBUG] %s was not found - removing from state", *id)
d.SetId("")
return nil
}

return fmt.Errorf("Error making Read request on Authorization Rule %q (Notification Hub %q / Namespace %q / Resource Group %q): %+v", id.AuthorizationRuleName, id.NotificationHubName, id.NamespaceName, id.ResourceGroup, err)
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

keysResp, err := client.ListKeys(ctx, id.ResourceGroup, id.NamespaceName, id.NotificationHubName, id.AuthorizationRuleName)
if err != nil {
return fmt.Errorf("Error Listing Access Keys for Authorization Rule %q (Notification Hub %q / Namespace %q / Resource Group %q): %+v", id.AuthorizationRuleName, id.NotificationHubName, id.NamespaceName, id.ResourceGroup, err)
return fmt.Errorf("listing access keys for %s: %+v", *id, err)
}

d.Set("name", id.AuthorizationRuleName)
Expand Down Expand Up @@ -206,7 +200,7 @@ func resourceNotificationHubAuthorizationRuleDelete(d *pluginsdk.ResourceData, m
resp, err := client.DeleteAuthorizationRule(ctx, id.ResourceGroup, id.NamespaceName, id.NotificationHubName, id.AuthorizationRuleName)
if err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("Error deleting Authorization Rule %q (Notification Hub %q / Namespace %q / Resource Group %q): %+v", id.AuthorizationRuleName, id.NotificationHubName, id.NamespaceName, id.ResourceGroup, err)
return fmt.Errorf("deleting %s: %+v", *id, err)
}
}

Expand Down
Loading