Skip to content

Commit

Permalink
Merge pull request #42 from terraform-providers/b-publicip-idle_timeout
Browse files Browse the repository at this point in the history
azurestack_public_ip: correct import & read idle_timeout_in_minutes
  • Loading branch information
katbyte authored Sep 14, 2018
2 parents e9440ba + ac030b2 commit b2b3164
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 102 deletions.
94 changes: 36 additions & 58 deletions azurestack/resource_arm_public_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

"github.com/Azure/azure-sdk-for-go/profiles/2017-03-09/network/mgmt/network"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"github.com/terraform-providers/terraform-provider-azurestack/azurestack/utils"
)

Expand All @@ -17,6 +19,7 @@ func resourceArmPublicIp() *schema.Resource {
Read: resourceArmPublicIpRead,
Update: resourceArmPublicIpCreate,
Delete: resourceArmPublicIpDelete,

Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
id, err := parseAzureResourceID(d.Id())
Expand All @@ -33,9 +36,10 @@ func resourceArmPublicIp() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
},

"location": locationSchema(),
Expand All @@ -48,22 +52,19 @@ func resourceArmPublicIp() *schema.Resource {
"public_ip_address_allocation": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validatePublicIpAllocation,
StateFunc: ignoreCaseStateFunc,
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validation.StringInSlice([]string{
string(network.Dynamic),
string(network.Static),
}, true),
},

"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
},
Type: schema.TypeInt,
Optional: true,
Default: 4,
ValidateFunc: validation.IntBetween(4, 30),
},

"domain_name_label": {
Expand Down Expand Up @@ -123,17 +124,27 @@ func resourceArmPublicIpCreate(d *schema.ResourceData, meta interface{}) error {
// Not supported for 2017-03-09 profile
// zones := expandZones(d.Get("zones").([]interface{}))

ipAllocationMethod := network.IPAllocationMethod(d.Get("public_ip_address_allocation").(string))

// Not supported for 2017-03-09 profile
// if strings.ToLower(string(sku.Name)) == "standard" {
// if strings.ToLower(string(ipAllocationMethod)) != "static" {
// return fmt.Errorf("Static IP allocation must be used when creating Standard SKU public IP addresses.")
// }
// }

properties := network.PublicIPAddressPropertiesFormat{
PublicIPAllocationMethod: ipAllocationMethod,
ipAllocationMethod := d.Get("public_ip_address_allocation").(string)
idleTimeout := d.Get("idle_timeout_in_minutes").(int)

publicIp := network.PublicIPAddress{
Name: &name,
Location: &location,
PublicIPAddressPropertiesFormat: &network.PublicIPAddressPropertiesFormat{
PublicIPAllocationMethod: network.IPAllocationMethod(ipAllocationMethod),
IdleTimeoutInMinutes: utils.Int32(int32(idleTimeout)),
},
Tags: *expandTags(tags),
// Not supported for 2017-03-09 profile
// Sku: &sku,
// Zones: zones,
}

dnl, hasDnl := d.GetOk("domain_name_label")
Expand All @@ -152,31 +163,15 @@ func resourceArmPublicIpCreate(d *schema.ResourceData, meta interface{}) error {
dnsSettings.DomainNameLabel = &domainNameLabel
}

properties.DNSSettings = &dnsSettings
}

if v, ok := d.GetOk("idle_timeout_in_minutes"); ok {
idleTimeout := int32(v.(int))
properties.IdleTimeoutInMinutes = &idleTimeout
}

publicIp := network.PublicIPAddress{
Name: &name,
Location: &location,
PublicIPAddressPropertiesFormat: &properties,
Tags: *expandTags(tags),
// Not supported for 2017-03-09 profile
// Sku: &sku,
// Zones: zones,
publicIp.PublicIPAddressPropertiesFormat.DNSSettings = &dnsSettings
}

future, err := client.CreateOrUpdate(ctx, resGroup, name, publicIp)
if err != nil {
return fmt.Errorf("Error Creating/Updating Public IP %q (Resource Group %q): %+v", name, resGroup, err)
}

err = future.WaitForCompletionRef(ctx, client.Client)
if err != nil {
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for completion of Public IP %q (Resource Group %q): %+v", name, resGroup, err)
}

Expand Down Expand Up @@ -231,14 +226,11 @@ func resourceArmPublicIpRead(d *schema.ResourceData, meta interface{}) error {
d.Set("public_ip_address_allocation", strings.ToLower(string(props.PublicIPAllocationMethod)))

if settings := props.DNSSettings; settings != nil {
if fqdn := settings.Fqdn; fqdn != nil {
d.Set("fqdn", fqdn)
}
d.Set("fqdn", settings.Fqdn)
}

if ip := props.IPAddress; ip != nil {
d.Set("ip_address", ip)
}
d.Set("ip_address", props.IPAddress)
d.Set("idle_timeout_in_minutes", props.IdleTimeoutInMinutes)
}

flattenAndSetTags(d, &resp.Tags)
Expand All @@ -262,27 +254,13 @@ func resourceArmPublicIpDelete(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error deleting Public IP %q (Resource Group %q): %+v", name, resGroup, err)
}

err = future.WaitForCompletionRef(ctx, client.Client)
if err != nil {
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for deletion of Public IP %q (Resource Group %q): %+v", name, resGroup, err)
}

return nil
}

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 must be an accepted value: Static, Dynamic"))
}
return
}

func validatePublicIpDomainNameLabel(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[a-z0-9-]+$`).MatchString(value) {
Expand Down
40 changes: 2 additions & 38 deletions azurestack/resource_arm_public_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,15 @@ package azurestack
import (
"fmt"
"net/http"
"os"
"regexp"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"os"
"regexp"
)

func TestResourceAzureStackPublicIpAllocation_validation(t *testing.T) {
cases := []struct {
Value string
ErrCount int
}{
{
Value: "Random",
ErrCount: 1,
},
{
Value: "Static",
ErrCount: 0,
},
{
Value: "Dynamic",
ErrCount: 0,
},
{
Value: "STATIC",
ErrCount: 0,
},
{
Value: "static",
ErrCount: 0,
},
}

for _, tc := range cases {
_, errors := validatePublicIpAllocation(tc.Value, "azurestack_public_ip")

if len(errors) != tc.ErrCount {
t.Fatalf("Expected the Azure RM Public IP allocation to trigger a validation error")
}
}
}

func TestResourceAzureStackPublicIpDomainNameLabel_validation(t *testing.T) {
cases := []struct {
Value string
Expand Down
4 changes: 2 additions & 2 deletions azurestack/resource_arm_route_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func resourceArmRouteTableCreateUpdate(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error Creating/Updating Route Table %q (Resource Group %q): %+v", name, resGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for completion of Route Table %q (Resource Group %q): %+v", name, resGroup, err)
}

Expand Down Expand Up @@ -192,7 +192,7 @@ func resourceArmRouteTableDelete(d *schema.ResourceData, meta interface{}) error
}
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for deletion of Route Table %q (Resource Group %q): %+v", name, resGroup, err)
}

Expand Down
4 changes: 2 additions & 2 deletions azurestack/resource_arm_virtual_network_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func resourceArmVirtualNetworkGatewayCreateUpdate(d *schema.ResourceData, meta i
return fmt.Errorf("Error Creating/Updating AzureRM Virtual Network Gateway %q (Resource Group %q): %+v", name, resGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for completion of AzureRM Virtual Network Gateway %q (Resource Group %q): %+v", name, resGroup, err)
}

Expand Down Expand Up @@ -393,7 +393,7 @@ func resourceArmVirtualNetworkGatewayDelete(d *schema.ResourceData, meta interfa
return fmt.Errorf("Error deleting Virtual Network Gateway %q (Resource Group %q): %+v", name, resGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for deletion of Virtual Network Gateway %q (Resource Group %q): %+v", name, resGroup, err)
}

Expand Down
4 changes: 2 additions & 2 deletions azurestack/resource_arm_virtual_network_gateway_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func resourceArmVirtualNetworkGatewayConnectionCreateUpdate(d *schema.ResourceDa
return fmt.Errorf("Error Creating/Updating AzureRM Virtual Network Gateway Connection %q (Resource Group %q): %+v", name, resGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for completion of Virtual Network Gateway Connection %q (Resource Group %q): %+v", name, resGroup, err)
}

Expand Down Expand Up @@ -240,7 +240,7 @@ func resourceArmVirtualNetworkGatewayConnectionDelete(d *schema.ResourceData, me
return fmt.Errorf("Error Deleting Virtual Network Gateway Connection %q (Resource Group %q): %+v", name, resGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for deletion of Virtual Network Gateway Connection %q (Resource Group %q): %+v", name, resGroup, err)
}

Expand Down

0 comments on commit b2b3164

Please sign in to comment.