github.com/mapuri/terraform@v0.7.6-0.20161012203214-7e0408293f97/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": { 30 Type: schema.TypeString, 31 Required: true, 32 ForceNew: true, 33 StateFunc: azureRMNormalizeLocation, 34 }, 35 36 "resource_group_name": { 37 Type: schema.TypeString, 38 Required: true, 39 ForceNew: true, 40 }, 41 42 "loadbalancer_id": { 43 Type: schema.TypeString, 44 Required: true, 45 ForceNew: true, 46 }, 47 48 "protocol": { 49 Type: schema.TypeString, 50 Required: true, 51 }, 52 53 "frontend_port_start": { 54 Type: schema.TypeInt, 55 Required: true, 56 }, 57 58 "frontend_port_end": { 59 Type: schema.TypeInt, 60 Required: true, 61 }, 62 63 "backend_port": { 64 Type: schema.TypeInt, 65 Required: true, 66 }, 67 68 "frontend_ip_configuration_name": { 69 Type: schema.TypeString, 70 Required: true, 71 }, 72 73 "frontend_ip_configuration_id": { 74 Type: schema.TypeString, 75 Computed: true, 76 }, 77 }, 78 } 79 } 80 81 func resourceArmLoadBalancerNatPoolCreate(d *schema.ResourceData, meta interface{}) error { 82 client := meta.(*ArmClient) 83 lbClient := client.loadBalancerClient 84 85 loadBalancer, exists, err := retrieveLoadBalancerById(d.Get("loadbalancer_id").(string), meta) 86 if err != nil { 87 return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err) 88 } 89 if !exists { 90 d.SetId("") 91 log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string)) 92 return nil 93 } 94 95 _, _, exists = findLoadBalancerNatPoolByName(loadBalancer, d.Get("name").(string)) 96 if exists { 97 return fmt.Errorf("A NAT Pool with name %q already exists.", d.Get("name").(string)) 98 } 99 100 newNatPool, err := expandAzureRmLoadBalancerNatPool(d, loadBalancer) 101 if err != nil { 102 return errwrap.Wrapf("Error Expanding NAT Pool {{err}}", err) 103 } 104 105 natPools := append(*loadBalancer.Properties.InboundNatPools, *newNatPool) 106 loadBalancer.Properties.InboundNatPools = &natPools 107 resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string)) 108 if err != nil { 109 return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err) 110 } 111 112 _, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{})) 113 if err != nil { 114 return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err) 115 } 116 117 read, err := lbClient.Get(resGroup, loadBalancerName, "") 118 if err != nil { 119 return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err) 120 } 121 if read.ID == nil { 122 return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup) 123 } 124 125 d.SetId(*read.ID) 126 127 log.Printf("[DEBUG] Waiting for LoadBalancer (%s) to become available", loadBalancerName) 128 stateConf := &resource.StateChangeConf{ 129 Pending: []string{"Accepted", "Updating"}, 130 Target: []string{"Succeeded"}, 131 Refresh: loadbalancerStateRefreshFunc(client, resGroup, loadBalancerName), 132 Timeout: 10 * time.Minute, 133 } 134 if _, err := stateConf.WaitForState(); err != nil { 135 return fmt.Errorf("Error waiting for LoadBalancer (%s) to become available: %s", loadBalancerName, err) 136 } 137 138 return resourceArmLoadBalancerNatPoolRead(d, meta) 139 } 140 141 func resourceArmLoadBalancerNatPoolRead(d *schema.ResourceData, meta interface{}) error { 142 loadBalancer, exists, err := retrieveLoadBalancerById(d.Id(), meta) 143 if err != nil { 144 return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err) 145 } 146 if !exists { 147 d.SetId("") 148 log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string)) 149 return nil 150 } 151 152 configs := *loadBalancer.Properties.InboundNatPools 153 for _, config := range configs { 154 if *config.Name == d.Get("name").(string) { 155 d.Set("name", config.Name) 156 157 d.Set("protocol", config.Properties.Protocol) 158 d.Set("frontend_port_start", config.Properties.FrontendPortRangeStart) 159 d.Set("frontend_port_end", config.Properties.FrontendPortRangeEnd) 160 d.Set("backend_port", config.Properties.BackendPort) 161 162 if config.Properties.FrontendIPConfiguration != nil { 163 d.Set("frontend_ip_configuration_id", config.Properties.FrontendIPConfiguration.ID) 164 } 165 166 break 167 } 168 } 169 170 return nil 171 } 172 173 func resourceArmLoadBalancerNatPoolDelete(d *schema.ResourceData, meta interface{}) error { 174 client := meta.(*ArmClient) 175 lbClient := client.loadBalancerClient 176 177 loadBalancer, exists, err := retrieveLoadBalancerById(d.Get("loadbalancer_id").(string), meta) 178 if err != nil { 179 return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err) 180 } 181 if !exists { 182 d.SetId("") 183 return nil 184 } 185 186 _, index, exists := findLoadBalancerNatPoolByName(loadBalancer, d.Get("name").(string)) 187 if !exists { 188 return nil 189 } 190 191 oldNatPools := *loadBalancer.Properties.InboundNatPools 192 newNatPools := append(oldNatPools[:index], oldNatPools[index+1:]...) 193 loadBalancer.Properties.InboundNatPools = &newNatPools 194 195 resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string)) 196 if err != nil { 197 return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err) 198 } 199 200 _, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{})) 201 if err != nil { 202 return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err) 203 } 204 205 read, err := lbClient.Get(resGroup, loadBalancerName, "") 206 if err != nil { 207 return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err) 208 } 209 if read.ID == nil { 210 return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup) 211 } 212 213 return nil 214 } 215 216 func expandAzureRmLoadBalancerNatPool(d *schema.ResourceData, lb *network.LoadBalancer) (*network.InboundNatPool, error) { 217 218 properties := network.InboundNatPoolPropertiesFormat{ 219 Protocol: network.TransportProtocol(d.Get("protocol").(string)), 220 FrontendPortRangeStart: azure.Int32(int32(d.Get("frontend_port_start").(int))), 221 FrontendPortRangeEnd: azure.Int32(int32(d.Get("frontend_port_end").(int))), 222 BackendPort: azure.Int32(int32(d.Get("backend_port").(int))), 223 } 224 225 if v := d.Get("frontend_ip_configuration_name").(string); v != "" { 226 rule, _, exists := findLoadBalancerFrontEndIpConfigurationByName(lb, v) 227 if !exists { 228 return nil, fmt.Errorf("[ERROR] Cannot find FrontEnd IP Configuration with the name %s", v) 229 } 230 231 feip := network.SubResource{ 232 ID: rule.ID, 233 } 234 235 properties.FrontendIPConfiguration = &feip 236 } 237 238 natPool := network.InboundNatPool{ 239 Name: azure.String(d.Get("name").(string)), 240 Properties: &properties, 241 } 242 243 return &natPool, nil 244 }