Skip to content

Commit

Permalink
provider/azurerm: Changes to Arm LB Resources post review
Browse files Browse the repository at this point in the history
  • Loading branch information
stack72 committed Oct 5, 2016
1 parent f7c8bd0 commit eacaeba
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 132 deletions.
14 changes: 5 additions & 9 deletions builtin/providers/azurerm/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package azurerm

import (
"fmt"
"net/http"
"strings"

"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/resource"
"net/http"
"strings"
)

func resourceGroupAndLBNameFromId(loadBalancerId string) (string, string, error) {
Expand All @@ -25,7 +26,7 @@ func retrieveLoadbalancerById(loadBalancerId string, meta interface{}) (*network

resGroup, name, err := resourceGroupAndLBNameFromId(loadBalancerId)
if err != nil {
return nil, false, errwrap.Wrapf("TODO: error message {{err}}", err)
return nil, false, errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err)
}

resp, err := loadBalancerClient.Get(resGroup, name, "")
Expand Down Expand Up @@ -136,12 +137,7 @@ func loadbalancerStateRefreshFunc(client *ArmClient, resourceGroupName string, l

func validateLoadbalancerPrivateIpAddressAllocation(v interface{}, k string) (ws []string, errors []error) {
value := strings.ToLower(v.(string))
allocations := map[string]bool{
"static": true,
"dynamic": true,
}

if !allocations[value] {
if value != "static" || value != "dynamic" {
errors = append(errors, fmt.Errorf("Loadbalancer Allocations can only be Static or Dynamic"))
}
return
Expand Down
60 changes: 20 additions & 40 deletions builtin/providers/azurerm/resource_arm_loadbalancer.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package azurerm

import (
"bytes"
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/jen20/riviera/azure"
Expand Down Expand Up @@ -41,7 +40,7 @@ func resourceArmLoadbalancer() *schema.Resource {
},

"frontend_ip_configuration": {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
MinItems: 1,
Elem: &schema.Resource{
Expand Down Expand Up @@ -91,7 +90,6 @@ func resourceArmLoadbalancer() *schema.Resource {
},
},
},
Set: resourceArmLoadbalancerFrontEndIpConfigurationHash,
},

"tags": tagsSchema(),
Expand All @@ -103,7 +101,7 @@ func resourceArmLoadbalancerCreate(d *schema.ResourceData, meta interface{}) err
client := meta.(*ArmClient)
loadBalancerClient := client.loadBalancerClient

log.Printf("[INFO] preparing arguments for Azure ARM Loadbalancer creation.")
log.Printf("[INFO] preparing arguments for Azure ARM LoadBalancer creation.")

name := d.Get("name").(string)
location := d.Get("location").(string)
Expand All @@ -114,11 +112,7 @@ func resourceArmLoadbalancerCreate(d *schema.ResourceData, meta interface{}) err
properties := network.LoadBalancerPropertiesFormat{}

if _, ok := d.GetOk("frontend_ip_configuration"); ok {
frontEndConfigs, feIpcErr := expandAzureRmLoadbalancerFrontendIpConfigurations(d)
if feIpcErr != nil {
return fmt.Errorf("Error Building list of Loadbalancer Frontend IP Configurations: %s", feIpcErr)
}
properties.FrontendIPConfigurations = &frontEndConfigs
properties.FrontendIPConfigurations = expandAzureRmLoadbalancerFrontendIpConfigurations(d)
}

loadbalancer := network.LoadBalancer{
Expand All @@ -130,15 +124,15 @@ func resourceArmLoadbalancerCreate(d *schema.ResourceData, meta interface{}) err

_, err := loadBalancerClient.CreateOrUpdate(resGroup, name, loadbalancer, make(chan struct{}))
if err != nil {
return err
return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err)
}

read, err := loadBalancerClient.Get(resGroup, name, "")
if err != nil {
return err
return errwrap.Wrapf("Error Getting LoadBalancer {{err}", err)
}
if read.ID == nil {
return fmt.Errorf("Cannot read Loadbalancer %s (resource group %s) ID", name, resGroup)
return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", name, resGroup)
}

d.SetId(*read.ID)
Expand All @@ -151,7 +145,7 @@ func resourceArmLoadbalancerCreate(d *schema.ResourceData, meta interface{}) err
Timeout: 10 * time.Minute,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("Error waiting for Loadbalancer (%s) to become available: %s", name, err)
return fmt.Errorf("Error waiting for LoadBalancer (%s) to become available: %s", name, err)
}

return resourceArmLoadbalancerRead(d, meta)
Expand All @@ -160,11 +154,11 @@ func resourceArmLoadbalancerCreate(d *schema.ResourceData, meta interface{}) err
func resourceArmLoadbalancerRead(d *schema.ResourceData, meta interface{}) error {
loadBalancer, exists, err := retrieveLoadbalancerById(d.Id(), meta)
if err != nil {
return err
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
}
if !exists {
d.SetId("")
log.Printf("[INFO] Loadbalancer %q not found. Refreshing from state", d.Get("name").(string))
log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string))
return nil
}

Expand All @@ -182,36 +176,22 @@ func resourceArmLoadbalancerDelete(d *schema.ResourceData, meta interface{}) err

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
return errwrap.Wrapf("Error Parsing Azure Resource ID {{err}}", err)
}
resGroup := id.ResourceGroup
name := id.Path["loadBalancers"]

_, err = loadBalancerClient.Delete(resGroup, name, make(chan struct{}))

return err
}

func resourceArmLoadbalancerFrontEndIpConfigurationHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))

if m["private_ip_address"] != nil {
buf.WriteString(fmt.Sprintf("%s-", m["private_ip_address"].(string)))
}
if m["public_ip_address_id"] != nil {
buf.WriteString(fmt.Sprintf("%s-", m["public_ip_address_id"].(string)))
}
if m["subnet_id"] != nil {
buf.WriteString(fmt.Sprintf("%s-", m["subnet_id"].(string)))
if err != nil {
return errwrap.Wrapf("Error Deleting LoadBalancer {{err}}", err)
}

return hashcode.String(buf.String())
d.SetId("")
return nil
}

func expandAzureRmLoadbalancerFrontendIpConfigurations(d *schema.ResourceData) ([]network.FrontendIPConfiguration, error) {
configs := d.Get("frontend_ip_configuration").(*schema.Set).List()
func expandAzureRmLoadbalancerFrontendIpConfigurations(d *schema.ResourceData) *[]network.FrontendIPConfiguration {
configs := d.Get("frontend_ip_configuration").([]interface{})
frontEndConfigs := make([]network.FrontendIPConfiguration, 0, len(configs))

for _, configRaw := range configs {
Expand Down Expand Up @@ -247,11 +227,11 @@ func expandAzureRmLoadbalancerFrontendIpConfigurations(d *schema.ResourceData) (
frontEndConfigs = append(frontEndConfigs, frontEndConfig)
}

return frontEndConfigs, nil
return &frontEndConfigs
}

func flattenLoadBalancerFrontendIpConfiguration(ipConfigs *[]network.FrontendIPConfiguration) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(*ipConfigs))
func flattenLoadBalancerFrontendIpConfiguration(ipConfigs *[]network.FrontendIPConfiguration) []interface{} {
result := make([]interface{}, 0, len(*ipConfigs))
for _, config := range *ipConfigs {
ipConfig := make(map[string]interface{})
ipConfig["name"] = *config.Name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ func resourceArmLoadbalancerBackendAddressPoolCreate(d *schema.ResourceData, met

loadBalancer, exists, err := retrieveLoadbalancerById(d.Get("loadbalancer_id").(string), meta)
if err != nil {
return err
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
}
if !exists {
d.SetId("")
log.Printf("[INFO] Loadbalancer %q not found. Refreshing from state", d.Get("name").(string))
log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string))
return nil
}

Expand All @@ -84,20 +84,20 @@ func resourceArmLoadbalancerBackendAddressPoolCreate(d *schema.ResourceData, met
loadBalancer.Properties.BackendAddressPools = &backendAddressPools
resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string))
if err != nil {
return errwrap.Wrapf("TODO: {{err}}", err)
return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err)
}

_, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{}))
if err != nil {
return err
return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err)
}

read, err := lbClient.Get(resGroup, loadBalancerName, "")
if err != nil {
return err
return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err)
}
if read.ID == nil {
return fmt.Errorf("Cannot read Loadbalancer %s (resource group %s) ID", loadBalancerName, resGroup)
return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup)
}

d.SetId(*read.ID)
Expand All @@ -110,7 +110,7 @@ func resourceArmLoadbalancerBackendAddressPoolCreate(d *schema.ResourceData, met
Timeout: 10 * time.Minute,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("Error waiting for Loadbalancer (%s) to become available: %s", loadBalancerName, err)
return fmt.Errorf("Error waiting for LoadBalancer (%s) to become available: %s", loadBalancerName, err)
}

return resourceArmLoadbalancerBackendAddressPoolRead(d, meta)
Expand All @@ -119,11 +119,11 @@ func resourceArmLoadbalancerBackendAddressPoolCreate(d *schema.ResourceData, met
func resourceArmLoadbalancerBackendAddressPoolRead(d *schema.ResourceData, meta interface{}) error {
loadBalancer, exists, err := retrieveLoadbalancerById(d.Id(), meta)
if err != nil {
return err
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
}
if !exists {
d.SetId("")
log.Printf("[INFO] Loadbalancer %q not found. Refreshing from state", d.Get("name").(string))
log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string))
return nil
}

Expand Down Expand Up @@ -163,7 +163,7 @@ func resourceArmLoadbalancerBackendAddressPoolDelete(d *schema.ResourceData, met

loadBalancer, exists, err := retrieveLoadbalancerById(d.Get("loadbalancer_id").(string), meta)
if err != nil {
return err
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
}
if !exists {
d.SetId("")
Expand All @@ -181,20 +181,20 @@ func resourceArmLoadbalancerBackendAddressPoolDelete(d *schema.ResourceData, met

resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string))
if err != nil {
return errwrap.Wrapf("TODO: {{err}}", err)
return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err)
}

_, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{}))
if err != nil {
return err
return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err)
}

read, err := lbClient.Get(resGroup, loadBalancerName, "")
if err != nil {
return err
return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err)
}
if read.ID == nil {
return fmt.Errorf("Cannot read Loadbalancer %s (resource group %s) ID", loadBalancerName, resGroup)
return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup)
}

return nil
Expand Down
32 changes: 16 additions & 16 deletions builtin/providers/azurerm/resource_arm_loadbalancer_nat_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,42 +84,42 @@ func resourceArmLoadbalancerNatPoolCreate(d *schema.ResourceData, meta interface

loadBalancer, exists, err := retrieveLoadbalancerById(d.Get("loadbalancer_id").(string), meta)
if err != nil {
return err
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
}
if !exists {
d.SetId("")
log.Printf("[INFO] Loadbalancer %q not found. Refreshing from state", d.Get("name").(string))
log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string))
return nil
}

_, _, exists = findLoadBalancerNatPoolByName(loadBalancer, d.Get("name").(string))
if exists {
return fmt.Errorf("A Nat Pool with name %q already exists.", d.Get("name").(string))
return fmt.Errorf("A NAT Pool with name %q already exists.", d.Get("name").(string))
}

newNatPool, err := expandAzureRmLoadbalancerNatPool(d, loadBalancer)
if err != nil {
return err
return errwrap.Wrapf("Error Expanding NAT Pool {{err}}", err)
}

natPools := append(*loadBalancer.Properties.InboundNatPools, *newNatPool)
loadBalancer.Properties.InboundNatPools = &natPools
resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string))
if err != nil {
return errwrap.Wrapf("TODO: {{err}}", err)
return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err)
}

_, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{}))
if err != nil {
return err
return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err)
}

read, err := lbClient.Get(resGroup, loadBalancerName, "")
if err != nil {
return err
return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err)
}
if read.ID == nil {
return fmt.Errorf("Cannot read Loadbalancer %s (resource group %s) ID", loadBalancerName, resGroup)
return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup)
}

d.SetId(*read.ID)
Expand All @@ -132,7 +132,7 @@ func resourceArmLoadbalancerNatPoolCreate(d *schema.ResourceData, meta interface
Timeout: 10 * time.Minute,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("Error waiting for Loadbalancer (%s) to become available: %s", loadBalancerName, err)
return fmt.Errorf("Error waiting for LoadBalancer (%s) to become available: %s", loadBalancerName, err)
}

return resourceArmLoadbalancerNatPoolRead(d, meta)
Expand All @@ -141,11 +141,11 @@ func resourceArmLoadbalancerNatPoolCreate(d *schema.ResourceData, meta interface
func resourceArmLoadbalancerNatPoolRead(d *schema.ResourceData, meta interface{}) error {
loadBalancer, exists, err := retrieveLoadbalancerById(d.Id(), meta)
if err != nil {
return err
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
}
if !exists {
d.SetId("")
log.Printf("[INFO] Loadbalancer %q not found. Refreshing from state", d.Get("name").(string))
log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string))
return nil
}

Expand Down Expand Up @@ -176,7 +176,7 @@ func resourceArmLoadbalancerNatPoolDelete(d *schema.ResourceData, meta interface

loadBalancer, exists, err := retrieveLoadbalancerById(d.Get("loadbalancer_id").(string), meta)
if err != nil {
return err
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
}
if !exists {
d.SetId("")
Expand All @@ -194,20 +194,20 @@ func resourceArmLoadbalancerNatPoolDelete(d *schema.ResourceData, meta interface

resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string))
if err != nil {
return errwrap.Wrapf("TODO: {{err}}", err)
return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err)
}

_, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{}))
if err != nil {
return err
return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err)
}

read, err := lbClient.Get(resGroup, loadBalancerName, "")
if err != nil {
return err
return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err)
}
if read.ID == nil {
return fmt.Errorf("Cannot read Loadbalancer %s (resource group %s) ID", loadBalancerName, resGroup)
return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup)
}

return nil
Expand Down
Loading

0 comments on commit eacaeba

Please sign in to comment.