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_network_packet_capture - support for the target_type and scope properties #19385

Merged
merged 15 commits into from
Dec 15, 2022
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
75 changes: 75 additions & 0 deletions internal/services/compute/parse/vmss_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package parse

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

import (
"fmt"
"strings"

"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
)

type VMSSInstanceId struct {
SubscriptionId string
ResourceGroup string
VirtualMachineScaleSetName string
VirtualMachineName string
}

func NewVMSSInstanceID(subscriptionId, resourceGroup, virtualMachineScaleSetName, virtualMachineName string) VMSSInstanceId {
return VMSSInstanceId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
VirtualMachineScaleSetName: virtualMachineScaleSetName,
VirtualMachineName: virtualMachineName,
}
}

func (id VMSSInstanceId) String() string {
segments := []string{
fmt.Sprintf("Virtual Machine Name %q", id.VirtualMachineName),
fmt.Sprintf("Virtual Machine Scale Set Name %q", id.VirtualMachineScaleSetName),
fmt.Sprintf("Resource Group %q", id.ResourceGroup),
}
segmentsStr := strings.Join(segments, " / ")
return fmt.Sprintf("%s: (%s)", "V M S S Instance", segmentsStr)
}

func (id VMSSInstanceId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachineScaleSets/%s/virtualMachines/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.VirtualMachineScaleSetName, id.VirtualMachineName)
}

// VMSSInstanceID parses a VMSSInstance ID into an VMSSInstanceId struct
func VMSSInstanceID(input string) (*VMSSInstanceId, error) {
id, err := resourceids.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

resourceId := VMSSInstanceId{
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.VirtualMachineScaleSetName, err = id.PopSegment("virtualMachineScaleSets"); err != nil {
return nil, err
}
if resourceId.VirtualMachineName, err = id.PopSegment("virtualMachines"); err != nil {
return nil, err
}

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

return &resourceId, nil
}
128 changes: 128 additions & 0 deletions internal/services/compute/parse/vmss_instance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package parse

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

import (
"testing"

"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
)

var _ resourceids.Id = VMSSInstanceId{}

func TestVMSSInstanceIDFormatter(t *testing.T) {
actual := NewVMSSInstanceID("12345678-1234-9876-4563-123456789012", "resGroup1", "vmss1", "vm1").ID()
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/virtualMachineScaleSets/vmss1/virtualMachines/vm1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

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

{
// 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 VirtualMachineScaleSetName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/",
Error: true,
},

{
// missing value for VirtualMachineScaleSetName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/virtualMachineScaleSets/",
Error: true,
},

{
// missing VirtualMachineName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/virtualMachineScaleSets/vmss1/",
Error: true,
},

{
// missing value for VirtualMachineName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/virtualMachineScaleSets/vmss1/virtualMachines/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/virtualMachineScaleSets/vmss1/virtualMachines/vm1",
Expected: &VMSSInstanceId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
VirtualMachineScaleSetName: "vmss1",
VirtualMachineName: "vm1",
},
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.COMPUTE/VIRTUALMACHINESCALESETS/VMSS1/VIRTUALMACHINES/VM1",
Error: true,
},
}

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

actual, err := VMSSInstanceID(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.VirtualMachineScaleSetName != v.Expected.VirtualMachineScaleSetName {
t.Fatalf("Expected %q but got %q for VirtualMachineScaleSetName", v.Expected.VirtualMachineScaleSetName, actual.VirtualMachineScaleSetName)
}
if actual.VirtualMachineName != v.Expected.VirtualMachineName {
t.Fatalf("Expected %q but got %q for VirtualMachineName", v.Expected.VirtualMachineName, actual.VirtualMachineName)
}
}
}
1 change: 1 addition & 0 deletions internal/services/compute/resourceids.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ package compute
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=DataDisk -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Compute/virtualMachines/machine1/dataDisks/disk1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Plan -id=/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.MarketplaceOrdering/agreements/agreement1/offers/offer1/plans/hourly
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=HostGroup -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Compute/hostGroups/hostgroup1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=VMSSInstance -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/virtualMachineScaleSets/vmss1/virtualMachines/vm1
23 changes: 23 additions & 0 deletions internal/services/compute/validate/vmss_instance_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package validate

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

import (
"fmt"

"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/parse"
)

func VMSSInstanceID(input interface{}, key string) (warnings []string, errors []error) {
v, ok := input.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected %q to be a string", key))
return
}

if _, err := parse.VMSSInstanceID(v); err != nil {
errors = append(errors, err)
}

return
}
88 changes: 88 additions & 0 deletions internal/services/compute/validate/vmss_instance_id_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package validate

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

import "testing"

func TestVMSSInstanceID(t *testing.T) {
cases := []struct {
Input string
Valid bool
}{

{
// empty
Input: "",
Valid: false,
},

{
// missing SubscriptionId
Input: "/",
Valid: false,
},

{
// missing value for SubscriptionId
Input: "/subscriptions/",
Valid: false,
},

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

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

{
// missing VirtualMachineScaleSetName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/",
Valid: false,
},

{
// missing value for VirtualMachineScaleSetName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/virtualMachineScaleSets/",
Valid: false,
},

{
// missing VirtualMachineName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/virtualMachineScaleSets/vmss1/",
Valid: false,
},

{
// missing value for VirtualMachineName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/virtualMachineScaleSets/vmss1/virtualMachines/",
Valid: false,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/virtualMachineScaleSets/vmss1/virtualMachines/vm1",
Valid: true,
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.COMPUTE/VIRTUALMACHINESCALESETS/VMSS1/VIRTUALMACHINES/VM1",
Valid: false,
},
}
for _, tc := range cases {
t.Logf("[DEBUG] Testing Value %s", tc.Input)
_, errors := VMSSInstanceID(tc.Input, "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
Expand Up @@ -19,9 +19,11 @@ import (

func resourceNetworkPacketCapture() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceNetworkPacketCaptureCreate,
Read: resourceNetworkPacketCaptureRead,
Delete: resourceNetworkPacketCaptureDelete,
Create: resourceNetworkPacketCaptureCreate,
Read: resourceNetworkPacketCaptureRead,
Delete: resourceNetworkPacketCaptureDelete,
DeprecationMessage: "The \"azurerm_network_packet_capture\" resource is deprecated and will be removed in favour of the `azurerm_virtual_machine_packet_capture` and `azurerm_virtual_machine_scale_set_packet_capture` resources in version 4.0 of the AzureRM Provider.",

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.PacketCaptureID(id)
return err
Expand Down
Loading