Skip to content

Commit

Permalink
Refactor - move the ID parsing function of VM, VM extension, VM… (#6405)
Browse files Browse the repository at this point in the history
And azurerm_managed_disk is now using its ID parsing function
  • Loading branch information
ArcturusZhang authored Apr 9, 2020
1 parent 63f07db commit 18e516b
Show file tree
Hide file tree
Showing 31 changed files with 302 additions and 423 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func resourceLinuxVirtualMachine() *schema.Resource {
Update: resourceLinuxVirtualMachineUpdate,
Delete: resourceLinuxVirtualMachineDelete,
Importer: azSchema.ValidateResourceIDPriorToImportThen(func(id string) error {
_, err := ParseVirtualMachineID(id)
_, err := parse.VirtualMachineID(id)
return err
}, importVirtualMachine(compute.Linux, "azurerm_linux_virtual_machine")),

Expand Down Expand Up @@ -471,7 +471,7 @@ func resourceLinuxVirtualMachineRead(d *schema.ResourceData, meta interface{}) e
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := ParseVirtualMachineID(d.Id())
id, err := parse.VirtualMachineID(d.Id())
if err != nil {
return err
}
Expand Down Expand Up @@ -626,7 +626,7 @@ func resourceLinuxVirtualMachineUpdate(d *schema.ResourceData, meta interface{})
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := ParseVirtualMachineID(d.Id())
id, err := parse.VirtualMachineID(d.Id())
if err != nil {
return err
}
Expand Down Expand Up @@ -930,7 +930,7 @@ func resourceLinuxVirtualMachineDelete(d *schema.ResourceData, meta interface{})
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := ParseVirtualMachineID(d.Id())
id, err := parse.VirtualMachineID(d.Id())
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package compute
package parse

import (
"fmt"

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

type VirtualMachineExtensionID struct {
type VirtualMachineExtensionId struct {
ResourceGroup string
Name string
VirtualMachine string
}

func ParseVirtualMachineExtensionID(input string) (*VirtualMachineExtensionID, error) {
func VirtualMachineExtensionID(input string) (*VirtualMachineExtensionId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("[ERROR] Unable to parse App Service ID %q: %+v", input, err)
}

virtualMachineExtension := VirtualMachineExtensionID{
virtualMachineExtension := VirtualMachineExtensionId{
ResourceGroup: id.ResourceGroup,
}

Expand All @@ -32,18 +32,3 @@ func ParseVirtualMachineExtensionID(input string) (*VirtualMachineExtensionID, e

return &virtualMachineExtension, nil
}

func ValidateVirtualMachineExtensionID(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
return
}

if _, err := ParseVirtualMachineExtensionID(v); err != nil {
errors = append(errors, fmt.Errorf("Can not parse %q as a resource id: %v", k, err))
return
}

return warnings, errors
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package compute
package parse

import "testing"

func TestParseVirtualMachineExtensionID(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *VirtualMachineExtensionID
Expected *VirtualMachineExtensionId
}{
{
Name: "Empty",
Expand All @@ -31,7 +31,7 @@ func TestParseVirtualMachineExtensionID(t *testing.T) {
{
Name: "Valid",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myGroup1/providers/Microsoft.Compute/virtualMachines/machine1/extensions/extName",
Expected: &VirtualMachineExtensionID{
Expected: &VirtualMachineExtensionId{
ResourceGroup: "myGroup1",
Name: "extName",
VirtualMachine: "machine1",
Expand All @@ -41,7 +41,7 @@ func TestParseVirtualMachineExtensionID(t *testing.T) {
for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := ParseVirtualMachineExtensionID(v.Input)
actual, err := VirtualMachineExtensionID(v.Input)
if err != nil {
if v.Expected == nil {
continue
Expand All @@ -59,40 +59,3 @@ func TestParseVirtualMachineExtensionID(t *testing.T) {
}
}
}

func TestValidateVirtualMachineExtensionID(t *testing.T) {
cases := []struct {
ID string
Valid bool
}{
{
ID: "",
Valid: false,
},
{
ID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/",
Valid: false,
},
{
ID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myGroup1/providers/microsoft.compute/virtualMachines/machine1/extension/",
Valid: false,
},
{
ID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myGroup1/providers/microsoft.compute/virtualMachines/machine1/Extensions/extName",
Valid: false,
},
{
ID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myGroup1/providers/Microsoft.Compute/virtualMachines/machine1/extensions/extName",
Valid: true,
},
}
for _, tc := range cases {
t.Logf("[DEBUG] Testing Value %s", tc.ID)
_, errors := ValidateVirtualMachineExtensionID(tc.ID, "test")
valid := len(errors) == 0

if tc.Valid != valid {
t.Fatalf("Expected %t but got %t", tc.Valid, valid)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package parse

import (
"fmt"

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

type VirtualMachineScaleSetId struct {
ResourceGroup string
Name string
}

func VirtualMachineScaleSetID(input string) (*VirtualMachineScaleSetId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("[ERROR] Unable to parse Virtual Machine Scale Set ID %q: %+v", input, err)
}

vmScaleSet := VirtualMachineScaleSetId{
ResourceGroup: id.ResourceGroup,
}

if vmScaleSet.Name, err = id.PopSegment("virtualMachineScaleSets"); err != nil {
return nil, err
}

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

return &vmScaleSet, nil
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package compute
package parse

import (
"fmt"

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

type VirtualMachineScaleSetExtensionResourceID struct {
ResourceGroup string
VirtualMachineName string
Name string
type VirtualMachineScaleSetExtensionId struct {
ResourceGroup string
VirtualMachineScaleSetName string
Name string
}

func ParseVirtualMachineScaleSetExtensionID(input string) (*VirtualMachineScaleSetExtensionResourceID, error) {
func VirtualMachineScaleSetExtensionID(input string) (*VirtualMachineScaleSetExtensionId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("[ERROR] Unable to parse Virtual Machine Scale Set Extension ID %q: %+v", input, err)
}

extension := VirtualMachineScaleSetExtensionResourceID{
extension := VirtualMachineScaleSetExtensionId{
ResourceGroup: id.ResourceGroup,
}

if extension.VirtualMachineName, err = id.PopSegment("virtualMachineScaleSets"); err != nil {
if extension.VirtualMachineScaleSetName, err = id.PopSegment("virtualMachineScaleSets"); err != nil {
return nil, err
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package compute
package parse

import "testing"

func TestParseVirtualMachineScaleSetExtensionID(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *VirtualMachineScaleSetExtensionResourceID
Expected *VirtualMachineScaleSetExtensionId
}{
{
Name: "Empty",
Expand Down Expand Up @@ -36,18 +36,18 @@ func TestParseVirtualMachineScaleSetExtensionID(t *testing.T) {
{
Name: "Completed",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/virtualMachineScaleSets/machine1/extensions/extension1",
Expected: &VirtualMachineScaleSetExtensionResourceID{
Name: "extension1",
VirtualMachineName: "machine1",
ResourceGroup: "foo",
Expected: &VirtualMachineScaleSetExtensionId{
Name: "extension1",
VirtualMachineScaleSetName: "machine1",
ResourceGroup: "foo",
},
},
}

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

actual, err := ParseVirtualMachineScaleSetExtensionID(v.Input)
actual, err := VirtualMachineScaleSetExtensionID(v.Input)
if err != nil {
if v.Expected == nil {
continue
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package compute
package parse

import "testing"

func TestParseVirtualMachineScaleSetID(t *testing.T) {
func TestVirtualMachineScaleSetID(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *VirtualMachineScaleSetResourceID
Expected *VirtualMachineScaleSetId
}{
{
Name: "Empty",
Expand All @@ -26,7 +26,7 @@ func TestParseVirtualMachineScaleSetID(t *testing.T) {
{
Name: "Completed",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/virtualMachineScaleSets/example",
Expected: &VirtualMachineScaleSetResourceID{
Expected: &VirtualMachineScaleSetId{
Name: "example",
ResourceGroup: "foo",
},
Expand All @@ -36,7 +36,7 @@ func TestParseVirtualMachineScaleSetID(t *testing.T) {
for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := ParseVirtualMachineScaleSetID(v.Input)
actual, err := VirtualMachineScaleSetID(v.Input)
if err != nil {
if v.Expected == nil {
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ 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"
computeValidate "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/compute/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/compute/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/compute/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/base64"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
Expand All @@ -29,7 +30,7 @@ func resourceArmLinuxVirtualMachineScaleSet() *schema.Resource {
Delete: resourceArmLinuxVirtualMachineScaleSetDelete,

Importer: azSchema.ValidateResourceIDPriorToImportThen(func(id string) error {
_, err := ParseVirtualMachineScaleSetID(id)
_, err := parse.VirtualMachineScaleSetID(id)
return err
}, importVirtualMachineScaleSet(compute.Linux, "azurerm_linux_virtual_machine_scale_set")),

Expand Down Expand Up @@ -180,7 +181,7 @@ func resourceArmLinuxVirtualMachineScaleSet() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: computeValidate.ProximityPlacementGroupID,
ValidateFunc: validate.ProximityPlacementGroupID,
// the Compute API is broken and returns the Resource Group name in UPPERCASE :shrug:
DiffSuppressFunc: suppress.CaseDifference,
},
Expand All @@ -199,9 +200,9 @@ func resourceArmLinuxVirtualMachineScaleSet() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.Any(
computeValidate.ImageID,
computeValidate.SharedImageID,
computeValidate.SharedImageVersionID,
validate.ImageID,
validate.SharedImageID,
validate.SharedImageVersionID,
),
},

Expand Down Expand Up @@ -501,7 +502,7 @@ func resourceArmLinuxVirtualMachineScaleSetUpdate(d *schema.ResourceData, meta i
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := ParseVirtualMachineScaleSetID(d.Id())
id, err := parse.VirtualMachineScaleSetID(d.Id())
if err != nil {
return err
}
Expand Down Expand Up @@ -741,7 +742,7 @@ func resourceArmLinuxVirtualMachineScaleSetRead(d *schema.ResourceData, meta int
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := ParseVirtualMachineScaleSetID(d.Id())
id, err := parse.VirtualMachineScaleSetID(d.Id())
if err != nil {
return err
}
Expand Down Expand Up @@ -914,7 +915,7 @@ func resourceArmLinuxVirtualMachineScaleSetDelete(d *schema.ResourceData, meta i
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := ParseVirtualMachineScaleSetID(d.Id())
id, err := parse.VirtualMachineScaleSetID(d.Id())
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 18e516b

Please sign in to comment.