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_site_recovery_network_mapping - refactor to use parser #13277

Merged
merged 4 commits into from
Sep 9, 2021
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"fmt"
"strings"

"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
)

type ReplicationNetworkMappingId struct {
SubscriptionId string
ResourceGroup string
VaultName string
ReplicationFabricName string
ReplicationNetworkName string
Name string
}

func NewReplicationNetworkMappingID(subscriptionId, resourceGroup, vaultName, replicationFabricName, replicationNetworkName, name string) ReplicationNetworkMappingId {
return ReplicationNetworkMappingId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
VaultName: vaultName,
ReplicationFabricName: replicationFabricName,
ReplicationNetworkName: replicationNetworkName,
Name: name,
}
}

func (id ReplicationNetworkMappingId) String() string {
segments := []string{
fmt.Sprintf("Name %q", id.Name),
fmt.Sprintf("Replication Network Name %q", id.ReplicationNetworkName),
fmt.Sprintf("Replication Fabric Name %q", id.ReplicationFabricName),
fmt.Sprintf("Vault Name %q", id.VaultName),
fmt.Sprintf("Resource Group %q", id.ResourceGroup),
}
segmentsStr := strings.Join(segments, " / ")
return fmt.Sprintf("%s: (%s)", "Replication Network Mapping", segmentsStr)
}

func (id ReplicationNetworkMappingId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.RecoveryServices/vaults/%s/replicationFabrics/%s/replicationNetworks/%s/replicationNetworkMappings/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.VaultName, id.ReplicationFabricName, id.ReplicationNetworkName, id.Name)
}

// ReplicationNetworkMappingID parses a ReplicationNetworkMapping ID into an ReplicationNetworkMappingId struct
func ReplicationNetworkMappingID(input string) (*ReplicationNetworkMappingId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

resourceId := ReplicationNetworkMappingId{
SubscriptionId: id.SubscriptionID,
ResourceGroup: id.ResourceGroup,
}

if resourceId.SubscriptionId == "" {
return nil, fmt.Errorf("ID was missing the 'subscriptions' element")
}

if resourceId.ResourceGroup == "" {
return nil, fmt.Errorf("ID was missing the 'resourceGroups' element")
}

if resourceId.VaultName, err = id.PopSegment("vaults"); err != nil {
return nil, err
}
if resourceId.ReplicationFabricName, err = id.PopSegment("replicationFabrics"); err != nil {
return nil, err
}
if resourceId.ReplicationNetworkName, err = id.PopSegment("replicationNetworks"); err != nil {
return nil, err
}
if resourceId.Name, err = id.PopSegment("replicationNetworkMappings"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &resourceId, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/resourceid"
)

var _ resourceid.Formatter = ReplicationNetworkMappingId{}

func TestReplicationNetworkMappingIDFormatter(t *testing.T) {
actual := NewReplicationNetworkMappingID("12345678-1234-9876-4563-123456789012", "group1", "vault1", "fabric1", "network1", "mapping1").ID()
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.RecoveryServices/vaults/vault1/replicationFabrics/fabric1/replicationNetworks/network1/replicationNetworkMappings/mapping1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

func TestReplicationNetworkMappingID(t *testing.T) {
testData := []struct {
Input string
Error bool
Expected *ReplicationNetworkMappingId
}{

{
// empty
Input: "",
Error: true,
},

{
// missing SubscriptionId
Input: "/",
Error: true,
},

{
// missing value for SubscriptionId
Input: "/subscriptions/",
Error: true,
},

{
// missing ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/",
Error: true,
},

{
// missing value for ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/",
Error: true,
},

{
// missing VaultName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.RecoveryServices/",
Error: true,
},

{
// missing value for VaultName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.RecoveryServices/vaults/",
Error: true,
},

{
// missing ReplicationFabricName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.RecoveryServices/vaults/vault1/",
Error: true,
},

{
// missing value for ReplicationFabricName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.RecoveryServices/vaults/vault1/replicationFabrics/",
Error: true,
},

{
// missing ReplicationNetworkName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.RecoveryServices/vaults/vault1/replicationFabrics/fabric1/",
Error: true,
},

{
// missing value for ReplicationNetworkName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.RecoveryServices/vaults/vault1/replicationFabrics/fabric1/replicationNetworks/",
Error: true,
},

{
// missing Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.RecoveryServices/vaults/vault1/replicationFabrics/fabric1/replicationNetworks/network1/",
Error: true,
},

{
// missing value for Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.RecoveryServices/vaults/vault1/replicationFabrics/fabric1/replicationNetworks/network1/replicationNetworkMappings/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.RecoveryServices/vaults/vault1/replicationFabrics/fabric1/replicationNetworks/network1/replicationNetworkMappings/mapping1",
Expected: &ReplicationNetworkMappingId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "group1",
VaultName: "vault1",
ReplicationFabricName: "fabric1",
ReplicationNetworkName: "network1",
Name: "mapping1",
},
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/GROUP1/PROVIDERS/MICROSOFT.RECOVERYSERVICES/VAULTS/VAULT1/REPLICATIONFABRICS/FABRIC1/REPLICATIONNETWORKS/NETWORK1/REPLICATIONNETWORKMAPPINGS/MAPPING1",
Error: true,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Input)

actual, err := ReplicationNetworkMappingID(v.Input)
if err != nil {
if v.Error {
continue
}

t.Fatalf("Expect a value but got an error: %s", err)
}
if v.Error {
t.Fatal("Expect an error but didn't get one")
}

if actual.SubscriptionId != v.Expected.SubscriptionId {
t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId)
}
if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup)
}
if actual.VaultName != v.Expected.VaultName {
t.Fatalf("Expected %q but got %q for VaultName", v.Expected.VaultName, actual.VaultName)
}
if actual.ReplicationFabricName != v.Expected.ReplicationFabricName {
t.Fatalf("Expected %q but got %q for ReplicationFabricName", v.Expected.ReplicationFabricName, actual.ReplicationFabricName)
}
if actual.ReplicationNetworkName != v.Expected.ReplicationNetworkName {
t.Fatalf("Expected %q but got %q for ReplicationNetworkName", v.Expected.ReplicationNetworkName, actual.ReplicationNetworkName)
}
if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}
}
}
3 changes: 3 additions & 0 deletions internal/services/recoveryservices/resourceids.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package recoveryservices

//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ReplicationNetworkMapping -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.RecoveryServices/vaults/vault1/replicationFabrics/fabric1/replicationNetworks/network1/replicationNetworkMappings/mapping1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
networkParse "github.com/hashicorp/terraform-provider-azurerm/internal/services/network/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/recoveryservices/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/recoveryservices/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/suppress"
Expand All @@ -22,8 +24,10 @@ func resourceSiteRecoveryNetworkMapping() *pluginsdk.Resource {
Create: resourceSiteRecoveryNetworkMappingCreate,
Read: resourceSiteRecoveryNetworkMappingRead,
Delete: resourceSiteRecoveryNetworkMappingDelete,
// TODO: replace this with an importer which validates the ID during import
Importer: pluginsdk.DefaultImporter(),
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.ReplicationNetworkMappingID(id)
return err
}),

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Expand Down Expand Up @@ -91,20 +95,13 @@ func resourceSiteRecoveryNetworkMappingCreate(d *pluginsdk.ResourceData, meta in
defer cancel()

// get network name from id
parsedSourceNetworkId, err := azure.ParseAzureResourceID(sourceNetworkId)
parsedSourceNetworkId, err := networkParse.VirtualNetworkID(sourceNetworkId)
if err != nil {
return fmt.Errorf("[ERROR] Unable to parse source_network_id '%s' (network mapping %s): %+v", sourceNetworkId, name, err)
}
sourceNetworkName, hasName := parsedSourceNetworkId.Path["virtualNetworks"]
if !hasName {
sourceNetworkName, hasName = parsedSourceNetworkId.Path["virtualnetworks"] // Handle that different APIs return different ID casings
if !hasName {
return fmt.Errorf("[ERROR] parsed source_network_id '%s' doesn't contain 'virtualnetworks'", parsedSourceNetworkId)
}
}

if d.IsNewResource() {
existing, err := client.Get(ctx, fabricName, sourceNetworkName, name)
existing, err := client.Get(ctx, fabricName, parsedSourceNetworkId.Name, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) &&
// todo this workaround can be removed when this bug is fixed
Expand All @@ -128,15 +125,15 @@ func resourceSiteRecoveryNetworkMappingCreate(d *pluginsdk.ResourceData, meta in
},
},
}
future, err := client.Create(ctx, fabricName, sourceNetworkName, name, parameters)
future, err := client.Create(ctx, fabricName, parsedSourceNetworkId.Name, name, parameters)
if err != nil {
return fmt.Errorf("creating site recovery network mapping %s (vault %s): %+v", name, vaultName, err)
}
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("creating site recovery network mapping %s (vault %s): %+v", name, vaultName, err)
}

resp, err := client.Get(ctx, fabricName, sourceNetworkName, name)
resp, err := client.Get(ctx, fabricName, parsedSourceNetworkId.Name, name)
if err != nil {
return fmt.Errorf("retrieving site recovery network mapping %s (vault %s): %+v", name, vaultName, err)
}
Expand All @@ -147,38 +144,33 @@ func resourceSiteRecoveryNetworkMappingCreate(d *pluginsdk.ResourceData, meta in
}

func resourceSiteRecoveryNetworkMappingRead(d *pluginsdk.ResourceData, meta interface{}) error {
id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.ReplicationNetworkMappingID(d.Id())
if err != nil {
return err
}

resGroup := id.ResourceGroup
vaultName := id.Path["vaults"]
fabricName := id.Path["replicationFabrics"]
networkName := id.Path["replicationNetworks"]
name := id.Path["replicationNetworkMappings"]

client := meta.(*clients.Client).RecoveryServices.NetworkMappingClient(resGroup, vaultName)
client := meta.(*clients.Client).RecoveryServices.NetworkMappingClient(id.ResourceGroup, id.VaultName)
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

resp, err := client.Get(ctx, fabricName, networkName, name)
resp, err := client.Get(ctx, id.ReplicationFabricName, id.ReplicationNetworkName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("making Read request on site recovery network mapping %s (vault %s): %+v", name, vaultName, err)
return fmt.Errorf("making Read request on site recovery network mapping %s (vault %s): %+v", id.Name, id.VaultName, err)
}

d.Set("resource_group_name", resGroup)
d.Set("recovery_vault_name", vaultName)
d.Set("source_recovery_fabric_name", fabricName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("recovery_vault_name", id.VaultName)
d.Set("source_recovery_fabric_name", id.ReplicationFabricName)
d.Set("name", resp.Name)
if props := resp.Properties; props != nil {
d.Set("source_network_id", props.PrimaryNetworkID)
d.Set("target_network_id", props.RecoveryNetworkID)

// TODO update to use parser when fabric parser added
targetFabricId, err := azure.ParseAzureResourceID(handleAzureSdkForGoBug2824(*resp.Properties.RecoveryFabricArmID))
if err != nil {
return err
Expand All @@ -190,28 +182,22 @@ func resourceSiteRecoveryNetworkMappingRead(d *pluginsdk.ResourceData, meta inte
}

func resourceSiteRecoveryNetworkMappingDelete(d *pluginsdk.ResourceData, meta interface{}) error {
id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.ReplicationNetworkMappingID(d.Id())
if err != nil {
return err
}

resGroup := id.ResourceGroup
vaultName := id.Path["vaults"]
fabricName := id.Path["replicationFabrics"]
networkName := id.Path["replicationNetworks"]
name := id.Path["replicationNetworkMappings"]

client := meta.(*clients.Client).RecoveryServices.NetworkMappingClient(resGroup, vaultName)
client := meta.(*clients.Client).RecoveryServices.NetworkMappingClient(id.ResourceGroup, id.VaultName)
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

future, err := client.Delete(ctx, fabricName, networkName, name)
future, err := client.Delete(ctx, id.ReplicationFabricName, id.ReplicationNetworkName, id.Name)
if err != nil {
return fmt.Errorf("deleting site recovery network mapping %s (vault %s): %+v", name, vaultName, err)
return fmt.Errorf("deleting site recovery network mapping %s (vault %s): %+v", id.Name, id.VaultName, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for deletion of site recovery network mapping %s (vault %s): %+v", name, vaultName, err)
return fmt.Errorf("waiting for deletion of site recovery network mapping %s (vault %s): %+v", id.Name, id.VaultName, err)
}

return nil
Expand Down
Loading