github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/azurerm/resource_arm_loadbalancer_nat_pool.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "log" 6 "time" 7 8 "github.com/Azure/azure-sdk-for-go/arm/network" 9 "github.com/hashicorp/errwrap" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/helper/schema" 12 "github.com/jen20/riviera/azure" 13 ) 14 15 func resourceArmLoadBalancerNatPool() *schema.Resource { 16 return &schema.Resource{ 17 Create: resourceArmLoadBalancerNatPoolCreate, 18 Read: resourceArmLoadBalancerNatPoolRead, 19 Update: resourceArmLoadBalancerNatPoolCreate, 20 Delete: resourceArmLoadBalancerNatPoolDelete, 21 22 Schema: map[string]*schema.Schema{ 23 "name": { 24 Type: schema.TypeString, 25 Required: true, 26 ForceNew: true, 27 }, 28 29 "location": locationSchema(), 30 31 "resource_group_name": { 32 Type: schema.TypeString, 33 Required: true, 34 ForceNew: true, 35 }, 36 37 "loadbalancer_id": { 38 Type: schema.TypeString, 39 Required: true, 40 ForceNew: true, 41 }, 42 43 "protocol": { 44 Type: schema.TypeString, 45 Required: true, 46 }, 47 48 "frontend_port_start": { 49 Type: schema.TypeInt, 50 Required: true, 51 }, 52 53 "frontend_port_end": { 54 Type: schema.TypeInt, 55 Required: true, 56 }, 57 58 "backend_port": { 59 Type: schema.TypeInt, 60 Required: true, 61 }, 62 63 "frontend_ip_configuration_name": { 64 Type: schema.TypeString, 65 Required: true, 66 }, 67 68 "frontend_ip_configuration_id": { 69 Type: schema.TypeString, 70 Computed: true, 71 }, 72 }, 73 } 74 } 75 76 func resourceArmLoadBalancerNatPoolCreate(d *schema.ResourceData, meta interface{}) error { 77 client := meta.(*ArmClient) 78 lbClient := client.loadBalancerClient 79 80 loadBalancerID := d.Get("loadbalancer_id").(string) 81 armMutexKV.Lock(loadBalancerID) 82 defer armMutexKV.Unlock(loadBalancerID) 83 84 loadBalancer, exists, err := retrieveLoadBalancerById(loadBalancerID, meta) 85 if err != nil { 86 return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err) 87 } 88 if !exists { 89 d.SetId("") 90 log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string)) 91 return nil 92 } 93 94 newNatPool, err := expandAzureRmLoadBalancerNatPool(d, loadBalancer) 95 if err != nil { 96 return errwrap.Wrapf("Error Expanding NAT Pool {{err}}", err) 97 } 98 99 natPools := append(*loadBalancer.LoadBalancerPropertiesFormat.InboundNatPools, *newNatPool) 100 101 existingNatPool, existingNatPoolIndex, exists := findLoadBalancerNatPoolByName(loadBalancer, d.Get("name").(string)) 102 if exists { 103 if d.Get("name").(string) == *existingNatPool.Name { 104 // this probe is being updated/reapplied remove old copy from the slice 105 natPools = append(natPools[:existingNatPoolIndex], natPools[existingNatPoolIndex+1:]...) 106 } 107 } 108 109 loadBalancer.LoadBalancerPropertiesFormat.InboundNatPools = &natPools 110 resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string)) 111 if err != nil { 112 return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err) 113 } 114 115 _, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{})) 116 if err != nil { 117 return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err) 118 } 119 120 read, err := lbClient.Get(resGroup, loadBalancerName, "") 121 if err != nil { 122 return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err) 123 } 124 if read.ID == nil { 125 return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup) 126 } 127 128 var natPool_id string 129 for _, InboundNatPool := range *(*read.LoadBalancerPropertiesFormat).InboundNatPools { 130 if *InboundNatPool.Name == d.Get("name").(string) { 131 natPool_id = *InboundNatPool.ID 132 } 133 } 134 135 if natPool_id != "" { 136 d.SetId(natPool_id) 137 } else { 138 return fmt.Errorf("Cannot find created LoadBalancer NAT Pool ID %q", natPool_id) 139 } 140 141 log.Printf("[DEBUG] Waiting for LoadBalancer (%s) to become available", loadBalancerName) 142 stateConf := &resource.StateChangeConf{ 143 Pending: []string{"Accepted", "Updating"}, 144 Target: []string{"Succeeded"}, 145 Refresh: loadbalancerStateRefreshFunc(client, resGroup, loadBalancerName), 146 Timeout: 10 * time.Minute, 147 } 148 if _, err := stateConf.WaitForState(); err != nil { 149 return fmt.Errorf("Error waiting for LoadBalancer (%s) to become available: %s", loadBalancerName, err) 150 } 151 152 return resourceArmLoadBalancerNatPoolRead(d, meta) 153 } 154 155 func resourceArmLoadBalancerNatPoolRead(d *schema.ResourceData, meta interface{}) error { 156 loadBalancer, exists, err := retrieveLoadBalancerById(d.Get("loadbalancer_id").(string), meta) 157 if err != nil { 158 return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err) 159 } 160 if !exists { 161 d.SetId("") 162 log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string)) 163 return nil 164 } 165 166 configs := *loadBalancer.LoadBalancerPropertiesFormat.InboundNatPools 167 for _, config := range configs { 168 if *config.Name == d.Get("name").(string) { 169 d.Set("name", config.Name) 170 171 d.Set("protocol", config.InboundNatPoolPropertiesFormat.Protocol) 172 d.Set("frontend_port_start", config.InboundNatPoolPropertiesFormat.FrontendPortRangeStart) 173 d.Set("frontend_port_end", config.InboundNatPoolPropertiesFormat.FrontendPortRangeEnd) 174 d.Set("backend_port", config.InboundNatPoolPropertiesFormat.BackendPort) 175 176 if config.InboundNatPoolPropertiesFormat.FrontendIPConfiguration != nil { 177 d.Set("frontend_ip_configuration_id", config.InboundNatPoolPropertiesFormat.FrontendIPConfiguration.ID) 178 } 179 180 break 181 } 182 } 183 184 return nil 185 } 186 187 func resourceArmLoadBalancerNatPoolDelete(d *schema.ResourceData, meta interface{}) error { 188 client := meta.(*ArmClient) 189 lbClient := client.loadBalancerClient 190 191 loadBalancerID := d.Get("loadbalancer_id").(string) 192 armMutexKV.Lock(loadBalancerID) 193 defer armMutexKV.Unlock(loadBalancerID) 194 195 loadBalancer, exists, err := retrieveLoadBalancerById(loadBalancerID, meta) 196 if err != nil { 197 return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err) 198 } 199 if !exists { 200 d.SetId("") 201 return nil 202 } 203 204 _, index, exists := findLoadBalancerNatPoolByName(loadBalancer, d.Get("name").(string)) 205 if !exists { 206 return nil 207 } 208 209 oldNatPools := *loadBalancer.LoadBalancerPropertiesFormat.InboundNatPools 210 newNatPools := append(oldNatPools[:index], oldNatPools[index+1:]...) 211 loadBalancer.LoadBalancerPropertiesFormat.InboundNatPools = &newNatPools 212 213 resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string)) 214 if err != nil { 215 return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err) 216 } 217 218 _, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{})) 219 if err != nil { 220 return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err) 221 } 222 223 read, err := lbClient.Get(resGroup, loadBalancerName, "") 224 if err != nil { 225 return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err) 226 } 227 if read.ID == nil { 228 return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup) 229 } 230 231 return nil 232 } 233 234 func expandAzureRmLoadBalancerNatPool(d *schema.ResourceData, lb *network.LoadBalancer) (*network.InboundNatPool, error) { 235 236 properties := network.InboundNatPoolPropertiesFormat{ 237 Protocol: network.TransportProtocol(d.Get("protocol").(string)), 238 FrontendPortRangeStart: azure.Int32(int32(d.Get("frontend_port_start").(int))), 239 FrontendPortRangeEnd: azure.Int32(int32(d.Get("frontend_port_end").(int))), 240 BackendPort: azure.Int32(int32(d.Get("backend_port").(int))), 241 } 242 243 if v := d.Get("frontend_ip_configuration_name").(string); v != "" { 244 rule, _, exists := findLoadBalancerFrontEndIpConfigurationByName(lb, v) 245 if !exists { 246 return nil, fmt.Errorf("[ERROR] Cannot find FrontEnd IP Configuration with the name %s", v) 247 } 248 249 feip := network.SubResource{ 250 ID: rule.ID, 251 } 252 253 properties.FrontendIPConfiguration = &feip 254 } 255 256 natPool := network.InboundNatPool{ 257 Name: azure.String(d.Get("name").(string)), 258 InboundNatPoolPropertiesFormat: &properties, 259 } 260 261 return &natPool, nil 262 }