Skip to content

Commit

Permalink
new resource: azurerm_vpn_site (#8896)
Browse files Browse the repository at this point in the history
Co-authored-by: kt <[email protected]>
  • Loading branch information
magodo and katbyte authored Oct 27, 2020
1 parent b484991 commit b30d119
Show file tree
Hide file tree
Showing 12 changed files with 1,289 additions and 0 deletions.
5 changes: 5 additions & 0 deletions azurerm/internal/services/network/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Client struct {
VirtualHubClient *network.VirtualHubsClient
VpnGatewaysClient *network.VpnGatewaysClient
VpnServerConfigurationsClient *network.VpnServerConfigurationsClient
VpnSitesClient *network.VpnSitesClient
WatcherClient *network.WatchersClient
WebApplicationFirewallPoliciesClient *network.WebApplicationFirewallPoliciesClient
PrivateDnsZoneGroupClient *network.PrivateDNSZoneGroupsClient
Expand Down Expand Up @@ -178,6 +179,9 @@ func NewClient(o *common.ClientOptions) *Client {
vpnGatewaysClient := network.NewVpnGatewaysClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&vpnGatewaysClient.Client, o.ResourceManagerAuthorizer)

vpnSitesClient := network.NewVpnSitesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&vpnSitesClient.Client, o.ResourceManagerAuthorizer)

WatcherClient := network.NewWatchersClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&WatcherClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -230,6 +234,7 @@ func NewClient(o *common.ClientOptions) *Client {
VirtualHubClient: &VirtualHubClient,
VpnGatewaysClient: &vpnGatewaysClient,
VpnServerConfigurationsClient: &vpnServerConfigurationsClient,
VpnSitesClient: &vpnSitesClient,
WatcherClient: &WatcherClient,
WebApplicationFirewallPoliciesClient: &WebApplicationFirewallPoliciesClient,
PrivateDnsZoneGroupClient: &PrivateDnsZoneGroupClient,
Expand Down
45 changes: 45 additions & 0 deletions azurerm/internal/services/network/parse/virtual_wan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package parse

import (
"fmt"

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

type VirtualWanId struct {
ResourceGroup string
Name string
}

func (id VirtualWanId) ID(subscriptionId string) string {
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/virtualWans/%s",
subscriptionId, id.ResourceGroup, id.Name)
}

func NewVirtualWanID(resourceGroup, name string) VirtualWanId {
return VirtualWanId{
ResourceGroup: resourceGroup,
Name: name,
}
}

func VirtualWanID(input string) (*VirtualWanId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("parsing Virtual Wan ID %q: %+v", input, err)
}

vwanId := VirtualWanId{
ResourceGroup: id.ResourceGroup,
}

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

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

return &vwanId, nil
}
90 changes: 90 additions & 0 deletions azurerm/internal/services/network/parse/virtual_wan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package parse

import (
"testing"

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

var _ resourceid.Formatter = VirtualWanId{}

func TestVirtualWanIDFormatter(t *testing.T) {
subscriptionId := "12345678-1234-5678-1234-123456789012"
actual := NewVirtualWanID("group1", "wan1").ID(subscriptionId)
expected := "/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Network/virtualWans/wan1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

func TestVirtualWanID(t *testing.T) {
testData := []struct {
Name string
Input string
Error bool
Expect *VirtualWanId
}{
{
Name: "Empty",
Input: "",
Error: true,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/11111111-1111-1111-1111-111111111111",
Error: true,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/",
Error: true,
},
{
Name: "Missing leading slash",
Input: "subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/group1",
Error: true,
},
{
Name: "Malformed segments",
Input: "/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/group1/foo/bar",
Error: true,
},
{
Name: "Missing vwan segment",
Input: "subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/group1/providers/Microsoft.Network",
Error: true,
},
{
Name: "Correct",
Input: "/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/group1/providers/Microsoft.Network/virtualWans/wan1",
Expect: &VirtualWanId{
ResourceGroup: "group1",
Name: "wan1",
},
},
}

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

actual, err := VirtualWanID(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")
}

if actual.ResourceGroup != v.Expect.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expect.ResourceGroup, actual.ResourceGroup)
}

if actual.Name != v.Expect.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expect.Name, actual.Name)
}
}
}
89 changes: 89 additions & 0 deletions azurerm/internal/services/network/parse/vpn_site.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package parse

import (
"fmt"

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

type VpnSiteId struct {
ResourceGroup string
Name string
}

func (id VpnSiteId) ID(subscriptionId string) string {
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/vpnSites/%s",
subscriptionId, id.ResourceGroup, id.Name)
}

func NewVpnSiteID(resourceGroup, name string) VpnSiteId {
return VpnSiteId{
ResourceGroup: resourceGroup,
Name: name,
}
}

func VpnSiteID(input string) (*VpnSiteId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("parsing Vpn Site ID %q: %+v", input, err)
}

vpnSiteId := VpnSiteId{
ResourceGroup: id.ResourceGroup,
}

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

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

return &vpnSiteId, nil
}

type VpnSiteLinkId struct {
ResourceGroup string
Site string
Name string
}

func (id VpnSiteLinkId) ID(subscriptionId string) string {
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/vpnSites/%s/vpnSiteLinks/%s",
subscriptionId, id.ResourceGroup, id.Site, id.Name)
}

func NewVpnSiteLinkID(vpnSiteId VpnSiteId, name string) VpnSiteLinkId {
return VpnSiteLinkId{
ResourceGroup: vpnSiteId.ResourceGroup,
Site: vpnSiteId.Name,
Name: name,
}
}

func VpnSiteLinkID(input string) (*VpnSiteLinkId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("parsing Vpn Site Link ID %q: %+v", input, err)
}

vpnSiteLinkId := VpnSiteLinkId{
ResourceGroup: id.ResourceGroup,
}

if vpnSiteLinkId.Site, err = id.PopSegment("vpnSites"); err != nil {
return nil, err
}

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

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

return &vpnSiteLinkId, nil
}
Loading

0 comments on commit b30d119

Please sign in to comment.