2016-10-07 20:14:26 +02:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/network"
|
|
|
|
"github.com/hashicorp/errwrap"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/jen20/riviera/azure"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceArmLoadBalancerNatPool() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceArmLoadBalancerNatPoolCreate,
|
|
|
|
Read: resourceArmLoadBalancerNatPoolRead,
|
|
|
|
Update: resourceArmLoadBalancerNatPoolCreate,
|
|
|
|
Delete: resourceArmLoadBalancerNatPoolDelete,
|
2017-02-01 18:17:51 +01:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: loadBalancerSubResourceStateImporter,
|
|
|
|
},
|
2016-10-07 20:14:26 +02:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2017-02-01 18:17:51 +01:00
|
|
|
"location": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
ForceNew: true,
|
|
|
|
Optional: true,
|
|
|
|
StateFunc: azureRMNormalizeLocation,
|
|
|
|
DiffSuppressFunc: azureRMSuppressLocationDiff,
|
|
|
|
Deprecated: "location is no longer used",
|
|
|
|
},
|
2016-10-07 20:14:26 +02:00
|
|
|
|
|
|
|
"resource_group_name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"loadbalancer_id": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"protocol": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"frontend_port_start": {
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"frontend_port_end": {
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"backend_port": {
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"frontend_ip_configuration_name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"frontend_ip_configuration_id": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmLoadBalancerNatPoolCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient)
|
|
|
|
lbClient := client.loadBalancerClient
|
|
|
|
|
2016-10-19 14:51:47 +02:00
|
|
|
loadBalancerID := d.Get("loadbalancer_id").(string)
|
|
|
|
armMutexKV.Lock(loadBalancerID)
|
|
|
|
defer armMutexKV.Unlock(loadBalancerID)
|
|
|
|
|
|
|
|
loadBalancer, exists, err := retrieveLoadBalancerById(loadBalancerID, meta)
|
2016-10-07 20:14:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
d.SetId("")
|
|
|
|
log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
newNatPool, err := expandAzureRmLoadBalancerNatPool(d, loadBalancer)
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Expanding NAT Pool {{err}}", err)
|
|
|
|
}
|
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
natPools := append(*loadBalancer.LoadBalancerPropertiesFormat.InboundNatPools, *newNatPool)
|
2016-11-11 12:09:00 +01:00
|
|
|
|
|
|
|
existingNatPool, existingNatPoolIndex, exists := findLoadBalancerNatPoolByName(loadBalancer, d.Get("name").(string))
|
|
|
|
if exists {
|
2017-01-12 14:57:07 +01:00
|
|
|
if d.Get("name").(string) == *existingNatPool.Name {
|
|
|
|
// this probe is being updated/reapplied remove old copy from the slice
|
2016-11-11 12:09:00 +01:00
|
|
|
natPools = append(natPools[:existingNatPoolIndex], natPools[existingNatPoolIndex+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
loadBalancer.LoadBalancerPropertiesFormat.InboundNatPools = &natPools
|
2016-10-07 20:14:26 +02:00
|
|
|
resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string))
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{}))
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
read, err := lbClient.Get(resGroup, loadBalancerName, "")
|
|
|
|
if err != nil {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-10-17 19:21:30 +02:00
|
|
|
var natPool_id string
|
2016-12-06 09:39:47 +01:00
|
|
|
for _, InboundNatPool := range *(*read.LoadBalancerPropertiesFormat).InboundNatPools {
|
2016-10-18 11:44:15 +02:00
|
|
|
if *InboundNatPool.Name == d.Get("name").(string) {
|
|
|
|
natPool_id = *InboundNatPool.ID
|
2016-10-17 19:21:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if natPool_id != "" {
|
|
|
|
d.SetId(natPool_id)
|
|
|
|
} else {
|
2016-10-18 15:00:38 +02:00
|
|
|
return fmt.Errorf("Cannot find created LoadBalancer NAT Pool ID %q", natPool_id)
|
2016-10-17 19:21:30 +02:00
|
|
|
}
|
2016-10-07 20:14:26 +02:00
|
|
|
|
|
|
|
log.Printf("[DEBUG] Waiting for LoadBalancer (%s) to become available", loadBalancerName)
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"Accepted", "Updating"},
|
|
|
|
Target: []string{"Succeeded"},
|
|
|
|
Refresh: loadbalancerStateRefreshFunc(client, resGroup, loadBalancerName),
|
|
|
|
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 resourceArmLoadBalancerNatPoolRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmLoadBalancerNatPoolRead(d *schema.ResourceData, meta interface{}) error {
|
2017-02-01 18:17:51 +01:00
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
name := id.Path["inboundNatPools"]
|
|
|
|
|
2016-10-17 19:21:30 +02:00
|
|
|
loadBalancer, exists, err := retrieveLoadBalancerById(d.Get("loadbalancer_id").(string), meta)
|
2016-10-07 20:14:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
d.SetId("")
|
2017-02-01 18:17:51 +01:00
|
|
|
log.Printf("[INFO] LoadBalancer %q not found. Removing from state", name)
|
2016-10-07 20:14:26 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-01 18:17:51 +01:00
|
|
|
config, _, exists := findLoadBalancerNatPoolByName(loadBalancer, name)
|
2017-01-31 16:55:17 +01:00
|
|
|
if !exists {
|
|
|
|
d.SetId("")
|
2017-02-01 18:17:51 +01:00
|
|
|
log.Printf("[INFO] LoadBalancer Nat Pool %q not found. Removing from state", name)
|
2017-01-31 16:55:17 +01:00
|
|
|
return nil
|
|
|
|
}
|
2016-10-07 20:14:26 +02:00
|
|
|
|
2017-01-31 16:55:17 +01:00
|
|
|
d.Set("name", config.Name)
|
2017-02-01 18:17:51 +01:00
|
|
|
d.Set("resource_group_name", id.ResourceGroup)
|
2017-01-31 16:55:17 +01:00
|
|
|
d.Set("protocol", config.InboundNatPoolPropertiesFormat.Protocol)
|
|
|
|
d.Set("frontend_port_start", config.InboundNatPoolPropertiesFormat.FrontendPortRangeStart)
|
|
|
|
d.Set("frontend_port_end", config.InboundNatPoolPropertiesFormat.FrontendPortRangeEnd)
|
|
|
|
d.Set("backend_port", config.InboundNatPoolPropertiesFormat.BackendPort)
|
2016-10-07 20:14:26 +02:00
|
|
|
|
2017-01-31 16:55:17 +01:00
|
|
|
if config.InboundNatPoolPropertiesFormat.FrontendIPConfiguration != nil {
|
2017-02-01 18:17:51 +01:00
|
|
|
fipID, err := parseAzureResourceID(*config.InboundNatPoolPropertiesFormat.FrontendIPConfiguration.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("frontend_ip_configuration_name", fipID.Path["frontendIPConfigurations"])
|
2017-01-31 16:55:17 +01:00
|
|
|
d.Set("frontend_ip_configuration_id", config.InboundNatPoolPropertiesFormat.FrontendIPConfiguration.ID)
|
2016-10-07 20:14:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmLoadBalancerNatPoolDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient)
|
|
|
|
lbClient := client.loadBalancerClient
|
|
|
|
|
2016-10-19 14:51:47 +02:00
|
|
|
loadBalancerID := d.Get("loadbalancer_id").(string)
|
|
|
|
armMutexKV.Lock(loadBalancerID)
|
|
|
|
defer armMutexKV.Unlock(loadBalancerID)
|
|
|
|
|
|
|
|
loadBalancer, exists, err := retrieveLoadBalancerById(loadBalancerID, meta)
|
2016-10-07 20:14:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, index, exists := findLoadBalancerNatPoolByName(loadBalancer, d.Get("name").(string))
|
|
|
|
if !exists {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
oldNatPools := *loadBalancer.LoadBalancerPropertiesFormat.InboundNatPools
|
2016-10-07 20:14:26 +02:00
|
|
|
newNatPools := append(oldNatPools[:index], oldNatPools[index+1:]...)
|
2016-12-06 09:39:47 +01:00
|
|
|
loadBalancer.LoadBalancerPropertiesFormat.InboundNatPools = &newNatPools
|
2016-10-07 20:14:26 +02:00
|
|
|
|
|
|
|
resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string))
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{}))
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
read, err := lbClient.Get(resGroup, loadBalancerName, "")
|
|
|
|
if err != nil {
|
|
|
|
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 nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func expandAzureRmLoadBalancerNatPool(d *schema.ResourceData, lb *network.LoadBalancer) (*network.InboundNatPool, error) {
|
|
|
|
|
|
|
|
properties := network.InboundNatPoolPropertiesFormat{
|
|
|
|
Protocol: network.TransportProtocol(d.Get("protocol").(string)),
|
|
|
|
FrontendPortRangeStart: azure.Int32(int32(d.Get("frontend_port_start").(int))),
|
|
|
|
FrontendPortRangeEnd: azure.Int32(int32(d.Get("frontend_port_end").(int))),
|
|
|
|
BackendPort: azure.Int32(int32(d.Get("backend_port").(int))),
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := d.Get("frontend_ip_configuration_name").(string); v != "" {
|
|
|
|
rule, _, exists := findLoadBalancerFrontEndIpConfigurationByName(lb, v)
|
|
|
|
if !exists {
|
|
|
|
return nil, fmt.Errorf("[ERROR] Cannot find FrontEnd IP Configuration with the name %s", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
feip := network.SubResource{
|
|
|
|
ID: rule.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
properties.FrontendIPConfiguration = &feip
|
|
|
|
}
|
|
|
|
|
|
|
|
natPool := network.InboundNatPool{
|
2016-12-06 09:39:47 +01:00
|
|
|
Name: azure.String(d.Get("name").(string)),
|
|
|
|
InboundNatPoolPropertiesFormat: &properties,
|
2016-10-07 20:14:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &natPool, nil
|
|
|
|
}
|