github.com/aspring/terraform@v0.8.2-0.20161216122603-6a8619a5db2e/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.Id() == *existingNatPool.ID { 104 // this probe is being updated remove old copy from the slice 105 natPools = append(natPools[:existingNatPoolIndex], natPools[existingNatPoolIndex+1:]...) 106 } else { 107 return fmt.Errorf("A NAT Pool with name %q already exists.", d.Get("name").(string)) 108 } 109 } 110 111 loadBalancer.LoadBalancerPropertiesFormat.InboundNatPools = &natPools 112 resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string)) 113 if err != nil { 114 return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err) 115 } 116 117 _, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{})) 118 if err != nil { 119 return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err) 120 } 121 122 read, err := lbClient.Get(resGroup, loadBalancerName, "") 123 if err != nil { 124 return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err) 125 } 126 if read.ID == nil { 127 return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup) 128 } 129 130 var natPool_id string 131 for _, InboundNatPool := range *(*read.LoadBalancerPropertiesFormat).InboundNatPools { 132 if *InboundNatPool.Name == d.Get("name").(string) { 133 natPool_id = *InboundNatPool.ID 134 } 135 } 136 137 if natPool_id != "" { 138 d.SetId(natPool_id) 139 } else { 140 return fmt.Errorf("Cannot find created LoadBalancer NAT Pool ID %q", natPool_id) 141 } 142 143 log.Printf("[DEBUG] Waiting for LoadBalancer (%s) to become available", loadBalancerName) 144 stateConf := &resource.StateChangeConf{ 145 Pending: []string{"Accepted", "Updating"}, 146 Target: []string{"Succeeded"}, 147 Refresh: loadbalancerStateRefreshFunc(client, resGroup, loadBalancerName), 148 Timeout: 10 * time.Minute, 149 } 150 if _, err := stateConf.WaitForState(); err != nil { 151 return fmt.Errorf("Error waiting for LoadBalancer (%s) to become available: %s", loadBalancerName, err) 152 } 153 154 return resourceArmLoadBalancerNatPoolRead(d, meta) 155 } 156 157 func resourceArmLoadBalancerNatPoolRead(d *schema.ResourceData, meta interface{}) error { 158 loadBalancer, exists, err := retrieveLoadBalancerById(d.Get("loadbalancer_id").(string), meta) 159 if err != nil { 160 return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err) 161 } 162 if !exists { 163 d.SetId("") 164 log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string)) 165 return nil 166 } 167 168 configs := *loadBalancer.LoadBalancerPropertiesFormat.InboundNatPools 169 for _, config := range configs { 170 if *config.Name == d.Get("name").(string) { 171 d.Set("name", config.Name) 172 173 d.Set("protocol", config.InboundNatPoolPropertiesFormat.Protocol) 174 d.Set("frontend_port_start", config.InboundNatPoolPropertiesFormat.FrontendPortRangeStart) 175 d.Set("frontend_port_end", config.InboundNatPoolPropertiesFormat.FrontendPortRangeEnd) 176 d.Set("backend_port", config.InboundNatPoolPropertiesFormat.BackendPort) 177 178 if config.InboundNatPoolPropertiesFormat.FrontendIPConfiguration != nil { 179 d.Set("frontend_ip_configuration_id", config.InboundNatPoolPropertiesFormat.FrontendIPConfiguration.ID) 180 } 181 182 break 183 } 184 } 185 186 return nil 187 } 188 189 func resourceArmLoadBalancerNatPoolDelete(d *schema.ResourceData, meta interface{}) error { 190 client := meta.(*ArmClient) 191 lbClient := client.loadBalancerClient 192 193 loadBalancerID := d.Get("loadbalancer_id").(string) 194 armMutexKV.Lock(loadBalancerID) 195 defer armMutexKV.Unlock(loadBalancerID) 196 197 loadBalancer, exists, err := retrieveLoadBalancerById(loadBalancerID, meta) 198 if err != nil { 199 return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err) 200 } 201 if !exists { 202 d.SetId("") 203 return nil 204 } 205 206 _, index, exists := findLoadBalancerNatPoolByName(loadBalancer, d.Get("name").(string)) 207 if !exists { 208 return nil 209 } 210 211 oldNatPools := *loadBalancer.LoadBalancerPropertiesFormat.InboundNatPools 212 newNatPools := append(oldNatPools[:index], oldNatPools[index+1:]...) 213 loadBalancer.LoadBalancerPropertiesFormat.InboundNatPools = &newNatPools 214 215 resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string)) 216 if err != nil { 217 return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err) 218 } 219 220 _, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{})) 221 if err != nil { 222 return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err) 223 } 224 225 read, err := lbClient.Get(resGroup, loadBalancerName, "") 226 if err != nil { 227 return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err) 228 } 229 if read.ID == nil { 230 return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup) 231 } 232 233 return nil 234 } 235 236 func expandAzureRmLoadBalancerNatPool(d *schema.ResourceData, lb *network.LoadBalancer) (*network.InboundNatPool, error) { 237 238 properties := network.InboundNatPoolPropertiesFormat{ 239 Protocol: network.TransportProtocol(d.Get("protocol").(string)), 240 FrontendPortRangeStart: azure.Int32(int32(d.Get("frontend_port_start").(int))), 241 FrontendPortRangeEnd: azure.Int32(int32(d.Get("frontend_port_end").(int))), 242 BackendPort: azure.Int32(int32(d.Get("backend_port").(int))), 243 } 244 245 if v := d.Get("frontend_ip_configuration_name").(string); v != "" { 246 rule, _, exists := findLoadBalancerFrontEndIpConfigurationByName(lb, v) 247 if !exists { 248 return nil, fmt.Errorf("[ERROR] Cannot find FrontEnd IP Configuration with the name %s", v) 249 } 250 251 feip := network.SubResource{ 252 ID: rule.ID, 253 } 254 255 properties.FrontendIPConfiguration = &feip 256 } 257 258 natPool := network.InboundNatPool{ 259 Name: azure.String(d.Get("name").(string)), 260 InboundNatPoolPropertiesFormat: &properties, 261 } 262 263 return &natPool, nil 264 }