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

New resource: azurerm_virtual_wan #3505

Closed
wants to merge 4 commits into from
Closed
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
5 changes: 5 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ type ArmClient struct {
secGroupClient network.SecurityGroupsClient
secRuleClient network.SecurityRulesClient
subnetClient network.SubnetsClient
virtualWanClient network.VirtualWansClient
vnetGatewayConnectionsClient network.VirtualNetworkGatewayConnectionsClient
vnetGatewayClient network.VirtualNetworkGatewaysClient
vnetClient network.VirtualNetworksClient
Expand Down Expand Up @@ -1202,6 +1203,10 @@ func (c *ArmClient) registerNetworkingClients(endpoint, subscriptionId string, a
c.configureClient(&userAssignedIdentitiesClient.Client, auth)
c.userAssignedIdentitiesClient = userAssignedIdentitiesClient

virtualWansClient := network.NewVirtualWansClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&virtualWansClient.Client, auth)
c.virtualWanClient = virtualWansClient

watchersClient := network.NewWatchersClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&watchersClient.Client, auth)
c.watcherClient = watchersClient
Expand Down
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_virtual_network_gateway": resourceArmVirtualNetworkGateway(),
"azurerm_virtual_network_peering": resourceArmVirtualNetworkPeering(),
"azurerm_virtual_network": resourceArmVirtualNetwork(),
"azurerm_virtual_wan": resourceArmVirtualWan(),
},
}

Expand Down
213 changes: 213 additions & 0 deletions azurerm/resource_arm_virtual_wan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
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/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,
},
joakimhellum marked this conversation as resolved.
Show resolved Hide resolved

"resource_group_name": resourceGroupNameSchema(),

"location": locationSchema(),

"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).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 := azureRMNormalizeLocation(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).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", azureRMNormalizeLocation(*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).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