Skip to content

Commit

Permalink
New resource: azurerm_virtual_wan #3505 (#4089)
Browse files Browse the repository at this point in the history
new PR for #3505
  • Loading branch information
katbyte authored Aug 14, 2019
1 parent 6fb5bf0 commit b6d8918
Show file tree
Hide file tree
Showing 6 changed files with 505 additions and 0 deletions.
5 changes: 5 additions & 0 deletions azurerm/internal/services/network/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Client struct {
VnetGatewayClient *network.VirtualNetworkGatewaysClient
VnetClient *network.VirtualNetworksClient
VnetPeeringsClient *network.VirtualNetworkPeeringsClient
VirtualWanClient *network.VirtualWansClient
WatcherClient *network.WatchersClient
}

Expand Down Expand Up @@ -107,6 +108,9 @@ func BuildClient(o *common.ClientOptions) *Client {
VnetGatewayConnectionsClient := network.NewVirtualNetworkGatewayConnectionsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&VnetGatewayConnectionsClient.Client, o.ResourceManagerAuthorizer)

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

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

Expand Down Expand Up @@ -135,6 +139,7 @@ func BuildClient(o *common.ClientOptions) *Client {
VnetGatewayClient: &VnetGatewayClient,
VnetClient: &VnetClient,
VnetPeeringsClient: &VnetPeeringsClient,
VirtualWanClient: &VirtualWanClient,
WatcherClient: &WatcherClient,
}
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_virtual_network_gateway": resourceArmVirtualNetworkGateway(),
"azurerm_virtual_network_peering": resourceArmVirtualNetworkPeering(),
"azurerm_virtual_network": resourceArmVirtualNetwork(),
"azurerm_virtual_wan": resourceArmVirtualWan(),
}

for _, service := range supportedServices {
Expand Down
214 changes: 214 additions & 0 deletions azurerm/resource_arm_virtual_wan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package azurerm

import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-12-01/network"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmVirtualWan() *schema.Resource {
return &schema.Resource{
Create: resourceArmVirtualWanCreateUpdate,
Read: resourceArmVirtualWanRead,
Update: resourceArmVirtualWanCreateUpdate,
Delete: resourceArmVirtualWanDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
},

"resource_group_name": azure.SchemaResourceGroupName(),

"location": azure.SchemaLocation(),

"disable_vpn_encryption": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},

"security_provider_name": {
Type: schema.TypeString,
Optional: true,
},

"allow_branch_to_branch_traffic": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},

"allow_vnet_to_vnet_traffic": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},

"office365_local_breakout_category": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
string(network.OfficeTrafficCategoryAll),
string(network.OfficeTrafficCategoryNone),
string(network.OfficeTrafficCategoryOptimize),
string(network.OfficeTrafficCategoryOptimizeAndAllow),
}, false),
Default: string(network.OfficeTrafficCategoryNone),
},

"tags": tagsSchema(),
},
}
}

func resourceArmVirtualWanCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).network.VirtualWanClient
ctx := meta.(*ArmClient).StopContext

log.Printf("[INFO] preparing arguments for Virtual WAN creation.")

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
location := azure.NormalizeLocation(d.Get("location").(string))
disableVpnEncryption := d.Get("disable_vpn_encryption").(bool)
securityProviderName := d.Get("security_provider_name").(string)
allowBranchToBranchTraffic := d.Get("allow_branch_to_branch_traffic").(bool)
allowVnetToVnetTraffic := d.Get("allow_vnet_to_vnet_traffic").(bool)
office365LocalBreakoutCategory := d.Get("office365_local_breakout_category").(string)
tags := d.Get("tags").(map[string]interface{})

if requireResourcesToBeImported && d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Virtual WAN %q (Resource Group %q): %+v", name, resourceGroup, err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_virtual_wan", *existing.ID)
}
}

wan := network.VirtualWAN{
Location: utils.String(location),
Tags: expandTags(tags),
VirtualWanProperties: &network.VirtualWanProperties{
DisableVpnEncryption: utils.Bool(disableVpnEncryption),
SecurityProviderName: utils.String(securityProviderName),
AllowBranchToBranchTraffic: utils.Bool(allowBranchToBranchTraffic),
AllowVnetToVnetTraffic: utils.Bool(allowVnetToVnetTraffic),
Office365LocalBreakoutCategory: network.OfficeTrafficCategory(office365LocalBreakoutCategory),
},
}

future, err := client.CreateOrUpdate(ctx, resourceGroup, name, wan)
if err != nil {
return fmt.Errorf("Error creating/updating Virtual WAN %q (Resource Group %q): %+v", name, resourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for creation/update of Virtual WAN %q (Resource Group %q): %+v", name, resourceGroup, err)
}

read, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Error retrieving Virtual WAN %q (Resource Group %q): %+v", name, resourceGroup, err)
}

if read.ID == nil {
return fmt.Errorf("Cannot read Virtual WAN %q (Resource Group %q) ID", name, resourceGroup)
}

d.SetId(*read.ID)

return resourceArmVirtualWanRead(d, meta)
}

func resourceArmVirtualWanRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).network.VirtualWanClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}

resourceGroup := id.ResourceGroup
name := id.Path["virtualWans"]

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Virtual WAN %q (Resource Group %q) was not found - removing from state", name, resourceGroup)
d.SetId("")
return nil
}

return fmt.Errorf("Error making Read request on Virtual WAN %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}

if props := resp.VirtualWanProperties; props != nil {
d.Set("disable_vpn_encryption", props.DisableVpnEncryption)
d.Set("security_provider_name", props.SecurityProviderName)
d.Set("allow_branch_to_branch_traffic", props.AllowBranchToBranchTraffic)
d.Set("allow_vnet_to_vnet_traffic", props.AllowVnetToVnetTraffic)
d.Set("office365_local_breakout_category", props.Office365LocalBreakoutCategory)
}

flattenAndSetTags(d, resp.Tags)

return nil
}

func resourceArmVirtualWanDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).network.VirtualWanClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}

resourceGroup := id.ResourceGroup
name := id.Path["virtualWans"]

future, err := client.Delete(ctx, resourceGroup, name)
if err != nil {
// deleted outside of Terraform
if response.WasNotFound(future.Response()) {
return nil
}

return fmt.Errorf("Error deleting Virtual WAN %q (Resource Group %q): %+v", name, resourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
if !response.WasNotFound(future.Response()) {
return fmt.Errorf("Error waiting for the deletion of Virtual WAN %q (Resource Group %q): %+v", name, resourceGroup, err)
}
}

return nil
}
Loading

0 comments on commit b6d8918

Please sign in to comment.