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