2016-01-07 02:27:24 +01:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/compute"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2016-06-02 13:59:33 +02:00
|
|
|
"github.com/jen20/riviera/azure"
|
2016-01-07 02:27:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceArmAvailabilitySet() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceArmAvailabilitySetCreate,
|
|
|
|
Read: resourceArmAvailabilitySetRead,
|
|
|
|
Update: resourceArmAvailabilitySetCreate,
|
|
|
|
Delete: resourceArmAvailabilitySetDelete,
|
2016-07-10 23:33:02 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2016-01-07 02:27:24 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-06-01 22:17:21 +02:00
|
|
|
"name": {
|
2016-01-07 02:27:24 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-06-01 22:17:21 +02:00
|
|
|
"resource_group_name": {
|
2016-01-18 17:32:39 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-06-01 22:17:21 +02:00
|
|
|
"location": {
|
2016-01-07 02:27:24 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
StateFunc: azureRMNormalizeLocation,
|
|
|
|
},
|
|
|
|
|
2016-06-01 22:17:21 +02:00
|
|
|
"platform_update_domain_count": {
|
2016-01-07 02:27:24 +01:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 5,
|
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := v.(int)
|
|
|
|
if value > 20 {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"Maximum value for `platform_update_domain_count` is 20"))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2016-06-01 22:17:21 +02:00
|
|
|
"platform_fault_domain_count": {
|
2016-01-07 02:27:24 +01:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 3,
|
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := v.(int)
|
|
|
|
if value > 3 {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
2016-01-12 23:07:26 +01:00
|
|
|
"Maximum value for (%s) is 3", k))
|
2016-01-07 02:27:24 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2016-01-18 17:32:39 +01:00
|
|
|
"tags": tagsSchema(),
|
2016-01-07 02:27:24 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmAvailabilitySetCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient)
|
|
|
|
availSetClient := client.availSetClient
|
|
|
|
|
|
|
|
log.Printf("[INFO] preparing arguments for Azure ARM Availability Set creation.")
|
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
location := d.Get("location").(string)
|
|
|
|
resGroup := d.Get("resource_group_name").(string)
|
2016-06-02 13:59:33 +02:00
|
|
|
updateDomainCount := d.Get("platform_update_domain_count").(int)
|
|
|
|
faultDomainCount := d.Get("platform_fault_domain_count").(int)
|
2016-01-18 17:32:39 +01:00
|
|
|
tags := d.Get("tags").(map[string]interface{})
|
2016-01-07 02:27:24 +01:00
|
|
|
|
|
|
|
availSet := compute.AvailabilitySet{
|
|
|
|
Name: &name,
|
|
|
|
Location: &location,
|
|
|
|
Properties: &compute.AvailabilitySetProperties{
|
2016-06-02 13:59:33 +02:00
|
|
|
PlatformFaultDomainCount: azure.Int32(int32(faultDomainCount)),
|
|
|
|
PlatformUpdateDomainCount: azure.Int32(int32(updateDomainCount)),
|
2016-01-07 02:27:24 +01:00
|
|
|
},
|
2016-01-18 17:32:39 +01:00
|
|
|
Tags: expandTags(tags),
|
2016-01-07 02:27:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := availSetClient.CreateOrUpdate(resGroup, name, availSet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(*resp.ID)
|
|
|
|
|
|
|
|
return resourceArmAvailabilitySetRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmAvailabilitySetRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
availSetClient := meta.(*ArmClient).availSetClient
|
|
|
|
|
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resGroup := id.ResourceGroup
|
|
|
|
name := id.Path["availabilitySets"]
|
|
|
|
|
|
|
|
resp, err := availSetClient.Get(resGroup, 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 {
|
|
|
|
return fmt.Errorf("Error making Read request on Azure Availability Set %s: %s", name, err)
|
|
|
|
}
|
2016-01-07 02:27:24 +01:00
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
availSet := *resp.Properties
|
|
|
|
d.Set("platform_update_domain_count", availSet.PlatformUpdateDomainCount)
|
|
|
|
d.Set("platform_fault_domain_count", availSet.PlatformFaultDomainCount)
|
2016-07-10 23:33:02 +02:00
|
|
|
d.Set("name", resp.Name)
|
|
|
|
d.Set("location", resp.Location)
|
2016-01-07 02:27:24 +01:00
|
|
|
|
2016-01-18 17:32:39 +01:00
|
|
|
flattenAndSetTags(d, resp.Tags)
|
|
|
|
|
2016-01-07 02:27:24 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmAvailabilitySetDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
availSetClient := meta.(*ArmClient).availSetClient
|
|
|
|
|
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resGroup := id.ResourceGroup
|
|
|
|
name := id.Path["availabilitySets"]
|
|
|
|
|
|
|
|
_, err = availSetClient.Delete(resGroup, name)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|