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