2016-01-08 01:20:49 +01:00
|
|
|
package azurerm
|
|
|
|
|
2016-06-02 00:18:50 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/network"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceArmPublicIp() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceArmPublicIpCreate,
|
|
|
|
Read: resourceArmPublicIpRead,
|
|
|
|
Update: resourceArmPublicIpCreate,
|
|
|
|
Delete: resourceArmPublicIpDelete,
|
2016-07-11 15:32:03 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2016-06-02 00:18:50 +02:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-11-29 16:54:00 +01:00
|
|
|
"location": locationSchema(),
|
2016-06-02 00:18:50 +02:00
|
|
|
|
|
|
|
"resource_group_name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"public_ip_address_allocation": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ValidateFunc: validatePublicIpAllocation,
|
2016-07-11 15:32:03 +02:00
|
|
|
StateFunc: func(val interface{}) string {
|
|
|
|
return strings.ToLower(val.(string))
|
|
|
|
},
|
2016-06-02 00:18:50 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"idle_timeout_in_minutes": {
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := v.(int)
|
|
|
|
if value < 4 || value > 30 {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"The idle timeout must be between 4 and 30 minutes"))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"domain_name_label": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ValidateFunc: validatePublicIpDomainNameLabel,
|
|
|
|
},
|
|
|
|
|
|
|
|
"reverse_fqdn": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"fqdn": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"ip_address": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"tags": tagsSchema(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmPublicIpCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient)
|
|
|
|
publicIPClient := client.publicIPClient
|
|
|
|
|
|
|
|
log.Printf("[INFO] preparing arguments for Azure ARM Public IP creation.")
|
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
location := d.Get("location").(string)
|
|
|
|
resGroup := d.Get("resource_group_name").(string)
|
|
|
|
tags := d.Get("tags").(map[string]interface{})
|
|
|
|
|
|
|
|
properties := network.PublicIPAddressPropertiesFormat{
|
|
|
|
PublicIPAllocationMethod: network.IPAllocationMethod(d.Get("public_ip_address_allocation").(string)),
|
|
|
|
}
|
|
|
|
|
|
|
|
dnl, hasDnl := d.GetOk("domain_name_label")
|
|
|
|
rfqdn, hasRfqdn := d.GetOk("reverse_fqdn")
|
|
|
|
|
|
|
|
if hasDnl || hasRfqdn {
|
|
|
|
dnsSettings := network.PublicIPAddressDNSSettings{}
|
|
|
|
|
|
|
|
if hasRfqdn {
|
|
|
|
reverse_fqdn := rfqdn.(string)
|
|
|
|
dnsSettings.ReverseFqdn = &reverse_fqdn
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasDnl {
|
|
|
|
domain_name_label := dnl.(string)
|
|
|
|
dnsSettings.DomainNameLabel = &domain_name_label
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
properties.DNSSettings = &dnsSettings
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("idle_timeout_in_minutes"); ok {
|
2016-08-18 15:05:14 +02:00
|
|
|
idle_timeout := int32(v.(int))
|
2016-06-02 00:18:50 +02:00
|
|
|
properties.IdleTimeoutInMinutes = &idle_timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
publicIp := network.PublicIPAddress{
|
|
|
|
Name: &name,
|
|
|
|
Location: &location,
|
|
|
|
Properties: &properties,
|
|
|
|
Tags: expandTags(tags),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := publicIPClient.CreateOrUpdate(resGroup, name, publicIp, make(chan struct{}))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
read, err := publicIPClient.Get(resGroup, name, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if read.ID == nil {
|
|
|
|
return fmt.Errorf("Cannot read Public IP %s (resource group %s) ID", name, resGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(*read.ID)
|
|
|
|
|
|
|
|
return resourceArmPublicIpRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmPublicIpRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
publicIPClient := meta.(*ArmClient).publicIPClient
|
|
|
|
|
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resGroup := id.ResourceGroup
|
|
|
|
name := id.Path["publicIPAddresses"]
|
|
|
|
|
|
|
|
resp, err := publicIPClient.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 {
|
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 public ip %s: %s", name, err)
|
|
|
|
}
|
2016-06-02 00:18:50 +02:00
|
|
|
|
2016-09-27 12:00:19 +02:00
|
|
|
d.Set("resource_group_name", resGroup)
|
2016-07-11 15:32:03 +02:00
|
|
|
d.Set("location", resp.Location)
|
|
|
|
d.Set("name", resp.Name)
|
|
|
|
d.Set("public_ip_address_allocation", strings.ToLower(string(resp.Properties.PublicIPAllocationMethod)))
|
|
|
|
|
2016-06-02 00:18:50 +02:00
|
|
|
if resp.Properties.DNSSettings != nil && resp.Properties.DNSSettings.Fqdn != nil && *resp.Properties.DNSSettings.Fqdn != "" {
|
|
|
|
d.Set("fqdn", resp.Properties.DNSSettings.Fqdn)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Properties.IPAddress != nil && *resp.Properties.IPAddress != "" {
|
|
|
|
d.Set("ip_address", resp.Properties.IPAddress)
|
|
|
|
}
|
|
|
|
|
|
|
|
flattenAndSetTags(d, resp.Tags)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmPublicIpDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
publicIPClient := meta.(*ArmClient).publicIPClient
|
|
|
|
|
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resGroup := id.ResourceGroup
|
|
|
|
name := id.Path["publicIPAddresses"]
|
|
|
|
|
|
|
|
_, err = publicIPClient.Delete(resGroup, name, make(chan struct{}))
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func validatePublicIpAllocation(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := strings.ToLower(v.(string))
|
|
|
|
allocations := map[string]bool{
|
|
|
|
"static": true,
|
|
|
|
"dynamic": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if !allocations[value] {
|
|
|
|
errors = append(errors, fmt.Errorf("Public IP Allocation can only be Static of Dynamic"))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func validatePublicIpDomainNameLabel(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := v.(string)
|
|
|
|
if !regexp.MustCompile(`^[a-z0-9-]+$`).MatchString(value) {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"only alphanumeric characters and hyphens allowed in %q: %q",
|
|
|
|
k, value))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(value) > 61 {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q cannot be longer than 61 characters: %q", k, value))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(value) == 0 {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q cannot be an empty string: %q", k, value))
|
|
|
|
}
|
|
|
|
if regexp.MustCompile(`-$`).MatchString(value) {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q cannot end with a hyphen: %q", k, value))
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|