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

azurerm_local_network_gateway - support for BGP Settings #611

Merged
merged 3 commits into from
Dec 8, 2017
Merged
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
22 changes: 22 additions & 0 deletions azurerm/import_arm_local_network_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,25 @@ func TestAccAzureRMLocalNetworkGateway_importBasic(t *testing.T) {
},
})
}

func TestAccAzureRMLocalNetworkGateway_importBGPSettingsComplete(t *testing.T) {
resourceName := "azurerm_local_network_gateway.test"
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLocalNetworkGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMLocalNetworkGatewayConfig_bgpSettingsComplete(rInt, testLocation()),
},

{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
137 changes: 104 additions & 33 deletions azurerm/resource_arm_local_network_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package azurerm

import (
"fmt"
"net/http"

"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -42,94 +41,124 @@ func resourceArmLocalNetworkGateway() *schema.Resource {
Type: schema.TypeString,
},
},

"bgp_settings": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"asn": {
Type: schema.TypeInt,
Required: true,
},

"bgp_peering_address": {
Type: schema.TypeString,
Required: true,
},

"peer_weight": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
},
},
},
},
}
}

func resourceArmLocalNetworkGatewayCreate(d *schema.ResourceData, meta interface{}) error {
lnetClient := meta.(*ArmClient).localNetConnClient
client := meta.(*ArmClient).localNetConnClient

name := d.Get("name").(string)
location := d.Get("location").(string)
resGroup := d.Get("resource_group_name").(string)
ipAddress := d.Get("gateway_address").(string)

// fetch the 'address_space_prefixes:
prefixes := []string{}
for _, pref := range d.Get("address_space").([]interface{}) {
prefixes = append(prefixes, pref.(string))
addressSpaces := expandLocalNetworkGatewayAddressSpaces(d)

bgpSettings, err := expandLocalNetworkGatewayBGPSettings(d)
if err != nil {
return err
}

gateway := network.LocalNetworkGateway{
Name: &name,
Location: &location,
LocalNetworkGatewayPropertiesFormat: &network.LocalNetworkGatewayPropertiesFormat{
LocalNetworkAddressSpace: &network.AddressSpace{
AddressPrefixes: &prefixes,
AddressPrefixes: &addressSpaces,
},
GatewayIPAddress: &ipAddress,
BgpSettings: bgpSettings,
},
}

_, error := lnetClient.CreateOrUpdate(resGroup, name, gateway, make(chan struct{}))
err := <-error
_, createError := client.CreateOrUpdate(resGroup, name, gateway, make(chan struct{}))
err = <-createError
if err != nil {
return fmt.Errorf("Error creating Azure ARM Local Network Gateway '%s': %s", name, err)
return fmt.Errorf("Error creating Local Network Gateway %q: %+v", name, err)
}

read, err := lnetClient.Get(resGroup, name)
read, err := client.Get(resGroup, name)
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read Virtual Network %s (resource group %s) ID", name, resGroup)
return fmt.Errorf("Cannot read Local Network Gateway ID %q (resource group %q) ID", name, resGroup)
}

d.SetId(*read.ID)

return resourceArmLocalNetworkGatewayRead(d, meta)
}

// resourceArmLocalNetworkGatewayRead goes ahead and reads the state of the corresponding ARM local network gateway.
func resourceArmLocalNetworkGatewayRead(d *schema.ResourceData, meta interface{}) error {
lnetClient := meta.(*ArmClient).localNetConnClient
client := meta.(*ArmClient).localNetConnClient

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
name := id.Path["localNetworkGateways"]
if name == "" {
return fmt.Errorf("Cannot find 'localNetworkGateways' in '%s', make sure it is specified in the ID parameter", d.Id())
}
resGroup := id.ResourceGroup

resp, err := lnetClient.Get(resGroup, name)
resp, err := client.Get(resGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error reading the state of Azure ARM local network gateway '%s': %s", name, err)

return fmt.Errorf("Error reading the state of Local Network Gateway %q (Resource Group %q): %+v", name, resGroup, err)
}

d.Set("resource_group_name", resGroup)
d.Set("name", resp.Name)
d.Set("resource_group_name", resGroup)
d.Set("location", azureRMNormalizeLocation(*resp.Location))
d.Set("gateway_address", resp.LocalNetworkGatewayPropertiesFormat.GatewayIPAddress)

prefs := []string{}
if ps := *resp.LocalNetworkGatewayPropertiesFormat.LocalNetworkAddressSpace.AddressPrefixes; ps != nil {
prefs = ps
if props := resp.LocalNetworkGatewayPropertiesFormat; props != nil {
d.Set("gateway_address", props.GatewayIPAddress)

if lnas := props.LocalNetworkAddressSpace; lnas != nil {
if prefixes := lnas.AddressPrefixes; prefixes != nil {
d.Set("address_space", *prefixes)
}
}
flattenedSettings := flattenLocalNetworkGatewayBGPSettings(props.BgpSettings)
if err := d.Set("bgp_settings", flattenedSettings); err != nil {
return err
}
}
d.Set("address_space", prefs)

return nil
}

// resourceArmLocalNetworkGatewayDelete deletes the specified ARM local network gateway.
func resourceArmLocalNetworkGatewayDelete(d *schema.ResourceData, meta interface{}) error {
lnetClient := meta.(*ArmClient).localNetConnClient
client := meta.(*ArmClient).localNetConnClient

id, err := parseAzureResourceID(d.Id())
if err != nil {
Expand All @@ -138,17 +167,59 @@ func resourceArmLocalNetworkGatewayDelete(d *schema.ResourceData, meta interface
name := id.Path["localNetworkGateways"]
resGroup := id.ResourceGroup

deleteResp, error := lnetClient.Delete(resGroup, name, make(chan struct{}))
deleteResp, error := client.Delete(resGroup, name, make(chan struct{}))
resp := <-deleteResp
err = <-error

if resp.StatusCode == http.StatusNotFound {
return nil
}

if err != nil {
return fmt.Errorf("Error issuing Azure ARM delete request of local network gateway '%s': %s", name, err)
if utils.ResponseWasNotFound(resp) {
return nil
}

return fmt.Errorf("Error issuing delete request for local network gateway %q: %+v", name, err)
}

return nil
}

func expandLocalNetworkGatewayBGPSettings(d *schema.ResourceData) (*network.BgpSettings, error) {
v, exists := d.GetOk("bgp_settings")
if !exists {
return nil, nil
}

settings := v.([]interface{})
setting := settings[0].(map[string]interface{})

bgpSettings := network.BgpSettings{
Asn: utils.Int64(int64(setting["asn"].(int))),
BgpPeeringAddress: utils.String(setting["bgp_peering_address"].(string)),
PeerWeight: utils.Int32(int32(setting["peer_weight"].(int))),
}

return &bgpSettings, nil
}

func expandLocalNetworkGatewayAddressSpaces(d *schema.ResourceData) []string {
prefixes := make([]string, 0)

for _, pref := range d.Get("address_space").([]interface{}) {
prefixes = append(prefixes, pref.(string))
}

return prefixes
}

func flattenLocalNetworkGatewayBGPSettings(input *network.BgpSettings) []interface{} {
output := make(map[string]interface{}, 0)

if input == nil {
return []interface{}{}
}

output["asn"] = int(*input.Asn)
output["bgp_peering_address"] = *input.BgpPeeringAddress
output["peer_weight"] = int(*input.PeerWeight)

return []interface{}{output}
}
Loading