Skip to content

Commit

Permalink
azurerm_virtual_network_gateway - support for new properties (#23220)
Browse files Browse the repository at this point in the history
  • Loading branch information
neil-yechenwei authored Dec 6, 2023
1 parent 12ea051 commit f354f4d
Show file tree
Hide file tree
Showing 10 changed files with 1,100 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

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 VirtualNetworkGatewayPolicyGroupId struct {
SubscriptionId string
ResourceGroup string
VirtualNetworkGatewayName string
Name string
}

func NewVirtualNetworkGatewayPolicyGroupID(subscriptionId, resourceGroup, virtualNetworkGatewayName, name string) VirtualNetworkGatewayPolicyGroupId {
return VirtualNetworkGatewayPolicyGroupId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
VirtualNetworkGatewayName: virtualNetworkGatewayName,
Name: name,
}
}

func (id VirtualNetworkGatewayPolicyGroupId) String() string {
segments := []string{
fmt.Sprintf("Name %q", id.Name),
fmt.Sprintf("Virtual Network Gateway Name %q", id.VirtualNetworkGatewayName),
fmt.Sprintf("Resource Group %q", id.ResourceGroup),
}
segmentsStr := strings.Join(segments, " / ")
return fmt.Sprintf("%s: (%s)", "Virtual Network Gateway Policy Group", segmentsStr)
}

func (id VirtualNetworkGatewayPolicyGroupId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/virtualNetworkGateways/%s/virtualNetworkGatewayPolicyGroups/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.VirtualNetworkGatewayName, id.Name)
}

// VirtualNetworkGatewayPolicyGroupID parses a VirtualNetworkGatewayPolicyGroup ID into an VirtualNetworkGatewayPolicyGroupId struct
func VirtualNetworkGatewayPolicyGroupID(input string) (*VirtualNetworkGatewayPolicyGroupId, error) {
id, err := resourceids.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("parsing %q as an VirtualNetworkGatewayPolicyGroup ID: %+v", input, err)
}

resourceId := VirtualNetworkGatewayPolicyGroupId{
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.VirtualNetworkGatewayName, err = id.PopSegment("virtualNetworkGateways"); err != nil {
return nil, err
}
if resourceId.Name, err = id.PopSegment("virtualNetworkGatewayPolicyGroups"); 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,131 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

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 = VirtualNetworkGatewayPolicyGroupId{}

func TestVirtualNetworkGatewayPolicyGroupIDFormatter(t *testing.T) {
actual := NewVirtualNetworkGatewayPolicyGroupID("12345678-1234-9876-4563-123456789012", "resGroup1", "gw1", "policyGroup1").ID()
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/virtualNetworkGateways/gw1/virtualNetworkGatewayPolicyGroups/policyGroup1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

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

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

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

{
// missing Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/virtualNetworkGateways/gw1/",
Error: true,
},

{
// missing value for Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/virtualNetworkGateways/gw1/virtualNetworkGatewayPolicyGroups/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/virtualNetworkGateways/gw1/virtualNetworkGatewayPolicyGroups/policyGroup1",
Expected: &VirtualNetworkGatewayPolicyGroupId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
VirtualNetworkGatewayName: "gw1",
Name: "policyGroup1",
},
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.NETWORK/VIRTUALNETWORKGATEWAYS/GW1/VIRTUALNETWORKGATEWAYPOLICYGROUPS/POLICYGROUP1",
Error: true,
},
}

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

actual, err := VirtualNetworkGatewayPolicyGroupID(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.VirtualNetworkGatewayName != v.Expected.VirtualNetworkGatewayName {
t.Fatalf("Expected %q but got %q for VirtualNetworkGatewayName", v.Expected.VirtualNetworkGatewayName, actual.VirtualNetworkGatewayName)
}
if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}
}
}
1 change: 1 addition & 0 deletions internal/services/network/resourceids.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ package network
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=VirtualNetworkGateway -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/virtualNetworkGateways/gw1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=VirtualNetworkGatewayIpConfiguration -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/virtualNetworkGateways/gw1/ipConfigurations/cfg1 -rewrite=true
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=VirtualNetworkGatewayNatRule -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/virtualNetworkGateways/gw1/natRules/rule1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=VirtualNetworkGatewayPolicyGroup -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/virtualNetworkGateways/gw1/virtualNetworkGatewayPolicyGroups/policyGroup1

// Express Route Connection
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ExpressRouteCircuit -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/expressRouteCircuits/erCircuit1
Expand Down
23 changes: 23 additions & 0 deletions internal/services/network/validate/policy_group_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package validate

import (
"fmt"

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

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

if m, _ := validate.RegExHelper(i, k, `^[a-zA-Z][a-zA-Z.-_]{0,78}[a-zA-Z_]{0,1}$`); !m {
return nil, []error{fmt.Errorf(`%q must be between 1 and 80 characters in length, must begin with a letter, end with a letter or underscore and contain only letters, periods, underscores and hyphens, got %q`, k, v)}
}

return nil, nil
}
58 changes: 58 additions & 0 deletions internal/services/network/validate/policy_group_name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package validate

import (
"strings"
"testing"
)

func TestPolicyGroupName(t *testing.T) {
cases := []struct {
Input string
ExpectError bool
}{
{
Input: "",
ExpectError: true,
},
{
Input: "a",
ExpectError: false,
},
{
Input: "a_",
ExpectError: false,
},
{
Input: "aaaa.-_a",
ExpectError: false,
},
{
Input: "_",
ExpectError: true,
},
{
Input: strings.Repeat("s", 79),
ExpectError: false,
},
{
Input: strings.Repeat("s", 80),
ExpectError: false,
},
{
Input: strings.Repeat("s", 81),
ExpectError: true,
},
}

for _, tc := range cases {
_, errors := PolicyGroupName(tc.Input, "name")

hasError := len(errors) > 0
if tc.ExpectError && !hasError {
t.Fatalf("Expected the Policy Group Name to trigger a validation error for '%s'", tc.Input)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

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/network/parse"
)

func VirtualNetworkGatewayPolicyGroupID(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.VirtualNetworkGatewayPolicyGroupID(v); err != nil {
errors = append(errors, err)
}

return
}
Loading

0 comments on commit f354f4d

Please sign in to comment.