terraform/builtin/providers/azurerm/resource_arm_route.go

162 lines
3.8 KiB
Go
Raw Normal View History

2016-01-10 16:02:48 +01:00
package azurerm
import (
"fmt"
"net/http"
2016-11-28 18:35:55 +01:00
"strings"
"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceArmRoute() *schema.Resource {
return &schema.Resource{
Create: resourceArmRouteCreate,
Read: resourceArmRouteRead,
Update: resourceArmRouteCreate,
Delete: resourceArmRouteDelete,
2016-11-28 18:35:55 +01:00
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"resource_group_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"route_table_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"address_prefix": {
Type: schema.TypeString,
Required: true,
},
"next_hop_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateRouteTableNextHopType,
2016-11-28 18:35:55 +01:00
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return strings.ToLower(old) == strings.ToLower(new)
},
},
"next_hop_in_ip_address": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func resourceArmRouteCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
routesClient := client.routesClient
name := d.Get("name").(string)
rtName := d.Get("route_table_name").(string)
resGroup := d.Get("resource_group_name").(string)
addressPrefix := d.Get("address_prefix").(string)
nextHopType := d.Get("next_hop_type").(string)
armMutexKV.Lock(rtName)
defer armMutexKV.Unlock(rtName)
properties := network.RoutePropertiesFormat{
AddressPrefix: &addressPrefix,
NextHopType: network.RouteNextHopType(nextHopType),
}
if v, ok := d.GetOk("next_hop_in_ip_address"); ok {
nextHopInIpAddress := v.(string)
properties.NextHopIPAddress = &nextHopInIpAddress
}
route := network.Route{
Name: &name,
RoutePropertiesFormat: &properties,
}
_, err := routesClient.CreateOrUpdate(resGroup, rtName, name, route, make(chan struct{}))
if err != nil {
return err
}
read, err := routesClient.Get(resGroup, rtName, name)
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read Route %s/%s (resource group %s) ID", rtName, name, resGroup)
}
d.SetId(*read.ID)
return resourceArmRouteRead(d, meta)
}
func resourceArmRouteRead(d *schema.ResourceData, meta interface{}) error {
routesClient := meta.(*ArmClient).routesClient
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
rtName := id.Path["routeTables"]
routeName := id.Path["routes"]
resp, err := routesClient.Get(resGroup, rtName, routeName)
provider/azurerm: Reordering the checks after an Azure API Get We are receiving suggestions of a panic as follows: ``` 2016/09/01 07:21:55 [DEBUG] plugin: terraform: panic: runtime error: invalid memory address or nil pointer dereference 2016/09/01 07:21:55 [DEBUG] plugin: terraform: [signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0xa3170f] 2016/09/01 07:21:55 [DEBUG] plugin: terraform: 2016/09/01 07:21:55 [DEBUG] plugin: terraform: goroutine 114 [running]: 2016/09/01 07:21:55 [DEBUG] plugin: terraform: panic(0x27f4e60, 0xc4200100e0) 2016/09/01 07:21:55 [DEBUG] plugin: terraform: /opt/go/src/runtime/panic.go:500 +0x1a1 2016/09/01 07:21:55 [DEBUG] plugin: terraform: github.com/hashicorp/terraform/builtin/providers/azurerm.resourceArmVirtualMachineRead(0xc4206d8060, 0x2995620, 0xc4204d0000, 0x0, 0x17) 2016/09/01 07:21:55 [DEBUG] plugin: terraform: /opt/gopath/src/github.com/hashicorp/terraform/builtin/providers/azurerm/resource_arm_virtual_machine.go:488 +0x1ff 2016/09/01 07:21:55 [DEBUG] plugin: terraform: github.com/hashicorp/terraform/helper/schema.(*Resource).Refresh(0xc420017a40, 0xc42040c780, 0x2995620, 0xc4204d0000, 0xc42019c990, 0x1, 0x0) ``` This is because the code is as follows: ``` resp, err := client.Get(resGroup, vnetName, name) if resp.StatusCode == http.StatusNotFound { d.SetId("") return nil } if err != nil { return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err) } ``` When a request throws an error, the response object isn't valid. Therefore, we need to flip that code to check the error first ``` resp, err := client.Get(resGroup, vnetName, name) if err != nil { return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err) } if resp.StatusCode == http.StatusNotFound { d.SetId("") return nil } ```
2016-09-01 16:31:42 +02:00
if err != nil {
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
provider/azurerm: Reordering the checks after an Azure API Get We are receiving suggestions of a panic as follows: ``` 2016/09/01 07:21:55 [DEBUG] plugin: terraform: panic: runtime error: invalid memory address or nil pointer dereference 2016/09/01 07:21:55 [DEBUG] plugin: terraform: [signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0xa3170f] 2016/09/01 07:21:55 [DEBUG] plugin: terraform: 2016/09/01 07:21:55 [DEBUG] plugin: terraform: goroutine 114 [running]: 2016/09/01 07:21:55 [DEBUG] plugin: terraform: panic(0x27f4e60, 0xc4200100e0) 2016/09/01 07:21:55 [DEBUG] plugin: terraform: /opt/go/src/runtime/panic.go:500 +0x1a1 2016/09/01 07:21:55 [DEBUG] plugin: terraform: github.com/hashicorp/terraform/builtin/providers/azurerm.resourceArmVirtualMachineRead(0xc4206d8060, 0x2995620, 0xc4204d0000, 0x0, 0x17) 2016/09/01 07:21:55 [DEBUG] plugin: terraform: /opt/gopath/src/github.com/hashicorp/terraform/builtin/providers/azurerm/resource_arm_virtual_machine.go:488 +0x1ff 2016/09/01 07:21:55 [DEBUG] plugin: terraform: github.com/hashicorp/terraform/helper/schema.(*Resource).Refresh(0xc420017a40, 0xc42040c780, 0x2995620, 0xc4204d0000, 0xc42019c990, 0x1, 0x0) ``` This is because the code is as follows: ``` resp, err := client.Get(resGroup, vnetName, name) if resp.StatusCode == http.StatusNotFound { d.SetId("") return nil } if err != nil { return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err) } ``` When a request throws an error, the response object isn't valid. Therefore, we need to flip that code to check the error first ``` resp, err := client.Get(resGroup, vnetName, name) if err != nil { return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err) } if resp.StatusCode == http.StatusNotFound { d.SetId("") return nil } ```
2016-09-01 16:31:42 +02:00
return fmt.Errorf("Error making Read request on Azure Route %s: %s", routeName, err)
}
2016-11-28 18:35:55 +01:00
d.Set("name", routeName)
d.Set("resource_group_name", resGroup)
d.Set("route_table_name", rtName)
d.Set("address_prefix", resp.RoutePropertiesFormat.AddressPrefix)
d.Set("next_hop_type", string(resp.RoutePropertiesFormat.NextHopType))
2016-11-28 18:35:55 +01:00
if resp.RoutePropertiesFormat.NextHopIPAddress != nil {
d.Set("next_hop_in_ip_address", resp.RoutePropertiesFormat.NextHopIPAddress)
2016-11-28 18:35:55 +01:00
}
return nil
}
func resourceArmRouteDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
routesClient := client.routesClient
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
rtName := id.Path["routeTables"]
routeName := id.Path["routes"]
armMutexKV.Lock(rtName)
defer armMutexKV.Unlock(rtName)
_, err = routesClient.Delete(resGroup, rtName, routeName, make(chan struct{}))
return err
}