2016-01-08 23:50:01 +01:00
|
|
|
package azurerm
|
|
|
|
|
2016-06-01 23:57:13 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/network"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceArmSubnet() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceArmSubnetCreate,
|
|
|
|
Read: resourceArmSubnetRead,
|
|
|
|
Update: resourceArmSubnetCreate,
|
|
|
|
Delete: resourceArmSubnetDelete,
|
2016-10-27 11:20:45 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2016-06-01 23:57:13 +02:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"resource_group_name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"virtual_network_name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"address_prefix": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"network_security_group_id": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"route_table_id": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"ip_configurations": {
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: schema.HashString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmSubnetCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient)
|
|
|
|
subnetClient := client.subnetClient
|
|
|
|
|
|
|
|
log.Printf("[INFO] preparing arguments for Azure ARM Subnet creation.")
|
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
vnetName := d.Get("virtual_network_name").(string)
|
|
|
|
resGroup := d.Get("resource_group_name").(string)
|
|
|
|
addressPrefix := d.Get("address_prefix").(string)
|
|
|
|
|
|
|
|
armMutexKV.Lock(vnetName)
|
|
|
|
defer armMutexKV.Unlock(vnetName)
|
|
|
|
|
|
|
|
properties := network.SubnetPropertiesFormat{
|
|
|
|
AddressPrefix: &addressPrefix,
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("network_security_group_id"); ok {
|
|
|
|
nsgId := v.(string)
|
|
|
|
properties.NetworkSecurityGroup = &network.SecurityGroup{
|
|
|
|
ID: &nsgId,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("route_table_id"); ok {
|
|
|
|
rtId := v.(string)
|
|
|
|
properties.RouteTable = &network.RouteTable{
|
|
|
|
ID: &rtId,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
subnet := network.Subnet{
|
2016-12-06 09:39:47 +01:00
|
|
|
Name: &name,
|
|
|
|
SubnetPropertiesFormat: &properties,
|
2016-06-01 23:57:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := subnetClient.CreateOrUpdate(resGroup, vnetName, name, subnet, make(chan struct{}))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
read, err := subnetClient.Get(resGroup, vnetName, name, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if read.ID == nil {
|
|
|
|
return fmt.Errorf("Cannot read Subnet %s/%s (resource group %s) ID", vnetName, name, resGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(*read.ID)
|
|
|
|
|
|
|
|
return resourceArmSubnetRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmSubnetRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
subnetClient := meta.(*ArmClient).subnetClient
|
|
|
|
|
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resGroup := id.ResourceGroup
|
|
|
|
vnetName := id.Path["virtualNetworks"]
|
|
|
|
name := id.Path["subnets"]
|
|
|
|
|
|
|
|
resp, err := subnetClient.Get(resGroup, vnetName, name, "")
|
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 {
|
2016-09-30 18:57:59 +02:00
|
|
|
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 Subnet %s: %s", name, err)
|
|
|
|
}
|
2016-06-01 23:57:13 +02:00
|
|
|
|
2016-10-27 11:20:45 +02:00
|
|
|
d.Set("name", name)
|
|
|
|
d.Set("resource_group_name", resGroup)
|
|
|
|
d.Set("virtual_network_name", vnetName)
|
2016-12-06 09:39:47 +01:00
|
|
|
d.Set("address_prefix", resp.SubnetPropertiesFormat.AddressPrefix)
|
2016-10-27 11:20:45 +02:00
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
if resp.SubnetPropertiesFormat.NetworkSecurityGroup != nil {
|
|
|
|
d.Set("network_security_group_id", resp.SubnetPropertiesFormat.NetworkSecurityGroup.ID)
|
2016-10-27 11:20:45 +02:00
|
|
|
}
|
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
if resp.SubnetPropertiesFormat.RouteTable != nil {
|
|
|
|
d.Set("route_table_id", resp.SubnetPropertiesFormat.RouteTable.ID)
|
2016-10-27 11:20:45 +02:00
|
|
|
}
|
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
if resp.SubnetPropertiesFormat.IPConfigurations != nil {
|
|
|
|
ips := make([]string, 0, len(*resp.SubnetPropertiesFormat.IPConfigurations))
|
|
|
|
for _, ip := range *resp.SubnetPropertiesFormat.IPConfigurations {
|
2016-06-01 23:57:13 +02:00
|
|
|
ips = append(ips, *ip.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.Set("ip_configurations", ips); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-27 11:20:45 +02:00
|
|
|
} else {
|
|
|
|
d.Set("ip_configurations", []string{})
|
2016-06-01 23:57:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmSubnetDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
subnetClient := meta.(*ArmClient).subnetClient
|
|
|
|
|
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resGroup := id.ResourceGroup
|
|
|
|
name := id.Path["subnets"]
|
|
|
|
vnetName := id.Path["virtualNetworks"]
|
|
|
|
|
|
|
|
armMutexKV.Lock(vnetName)
|
|
|
|
defer armMutexKV.Unlock(vnetName)
|
|
|
|
|
|
|
|
_, err = subnetClient.Delete(resGroup, vnetName, name, make(chan struct{}))
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|