Skip to content

Commit

Permalink
New resource: azurerm_virtual_network_gateway_nat_rule (#15720)
Browse files Browse the repository at this point in the history
To resolve the issue #15348, it has to implement Virtual Network Gateway Nat Rule first. Once this PR is merged, I assume it has to submit another PR to implement egressNatRules and ingressNatRules of Virtual Network Gateway Connection (azurerm_virtual_network_gateway_connection).
  • Loading branch information
Neil Ye authored May 18, 2022
1 parent 372fd6e commit 68131fb
Show file tree
Hide file tree
Showing 12 changed files with 1,088 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/services/network/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Client struct {
VirtualHubBgpConnectionClient *network.VirtualHubBgpConnectionClient
VirtualHubIPClient *network.VirtualHubIPConfigurationClient
VnetGatewayConnectionsClient *network.VirtualNetworkGatewayConnectionsClient
VnetGatewayNatRuleClient *network.VirtualNetworkGatewayNatRulesClient
VnetGatewayClient *network.VirtualNetworkGatewaysClient
VnetClient *network.VirtualNetworksClient
VnetPeeringsClient *network.VirtualNetworkPeeringsClient
Expand Down Expand Up @@ -198,6 +199,9 @@ func NewClient(o *common.ClientOptions) *Client {
VnetGatewayConnectionsClient := network.NewVirtualNetworkGatewayConnectionsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&VnetGatewayConnectionsClient.Client, o.ResourceManagerAuthorizer)

VnetGatewayNatRuleClient := network.NewVirtualNetworkGatewayNatRulesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&VnetGatewayNatRuleClient.Client, o.ResourceManagerAuthorizer)

VirtualWanClient := network.NewVirtualWansClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&VirtualWanClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -265,6 +269,7 @@ func NewClient(o *common.ClientOptions) *Client {
VirtualHubBgpConnectionClient: &VirtualHubBgpConnectionClient,
VirtualHubIPClient: &VirtualHubIPClient,
VnetGatewayConnectionsClient: &VnetGatewayConnectionsClient,
VnetGatewayNatRuleClient: &VnetGatewayNatRuleClient,
VnetGatewayClient: &VnetGatewayClient,
VnetClient: &VnetClient,
VnetPeeringsClient: &VnetPeeringsClient,
Expand Down
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 VirtualNetworkGatewayNatRuleId struct {
SubscriptionId string
ResourceGroup string
VirtualNetworkGatewayName string
NatRuleName string
}

func NewVirtualNetworkGatewayNatRuleID(subscriptionId, resourceGroup, virtualNetworkGatewayName, natRuleName string) VirtualNetworkGatewayNatRuleId {
return VirtualNetworkGatewayNatRuleId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
VirtualNetworkGatewayName: virtualNetworkGatewayName,
NatRuleName: natRuleName,
}
}

func (id VirtualNetworkGatewayNatRuleId) String() string {
segments := []string{
fmt.Sprintf("Nat Rule Name %q", id.NatRuleName),
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 Nat Rule", segmentsStr)
}

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

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

resourceId := VirtualNetworkGatewayNatRuleId{
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.NatRuleName, err = id.PopSegment("natRules"); 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,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 = VirtualNetworkGatewayNatRuleId{}

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

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

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

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

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/virtualNetworkGateways/gw1/natRules/rule1",
Expected: &VirtualNetworkGatewayNatRuleId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
VirtualNetworkGatewayName: "gw1",
NatRuleName: "rule1",
},
},

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

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

actual, err := VirtualNetworkGatewayNatRuleID(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.NatRuleName != v.Expected.NatRuleName {
t.Fatalf("Expected %q but got %q for NatRuleName", v.Expected.NatRuleName, actual.NatRuleName)
}
}
}
1 change: 1 addition & 0 deletions internal/services/network/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func (r Registration) SupportedResources() map[string]*pluginsdk.Resource {
"azurerm_virtual_hub_route_table_route": resourceVirtualHubRouteTableRoute(),
"azurerm_virtual_network_dns_servers": resourceVirtualNetworkDnsServers(),
"azurerm_virtual_network_gateway_connection": resourceVirtualNetworkGatewayConnection(),
"azurerm_virtual_network_gateway_nat_rule": resourceVirtualNetworkGatewayNatRule(),
"azurerm_virtual_network_gateway": resourceVirtualNetworkGateway(),
"azurerm_virtual_network_peering": resourceVirtualNetworkPeering(),
"azurerm_virtual_network": resourceVirtualNetwork(),
Expand Down
1 change: 1 addition & 0 deletions internal/services/network/resourceids.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ package network
// Virtual Network Gateway
//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
//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

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

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

return
}
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 TestVirtualNetworkGatewayNatRuleID(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 VirtualNetworkGatewayName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/",
Valid: false,
},

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

{
// missing NatRuleName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/virtualNetworkGateways/gw1/",
Valid: false,
},

{
// missing value for NatRuleName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/virtualNetworkGateways/gw1/natRules/",
Valid: false,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/virtualNetworkGateways/gw1/natRules/rule1",
Valid: true,
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.NETWORK/VIRTUALNETWORKGATEWAYS/GW1/NATRULES/RULE1",
Valid: false,
},
}
for _, tc := range cases {
t.Logf("[DEBUG] Testing Value %s", tc.Input)
_, errors := VirtualNetworkGatewayNatRuleID(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 @@ -76,6 +76,11 @@ func dataSourceVirtualNetworkGateway() *pluginsdk.Resource {
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"id": {
Type: pluginsdk.TypeString,
Computed: true,
},

"name": {
Type: pluginsdk.TypeString,
Computed: true,
Expand Down Expand Up @@ -306,6 +311,10 @@ func flattenVirtualNetworkGatewayDataSourceIPConfigurations(ipConfigs *[]network
props := cfg.VirtualNetworkGatewayIPConfigurationPropertiesFormat
v := make(map[string]interface{})

if id := cfg.ID; id != nil {
v["id"] = *id
}

if name := cfg.Name; name != nil {
v["name"] = *name
}
Expand Down
Loading

0 comments on commit 68131fb

Please sign in to comment.