github.com/nevins-b/terraform@v0.3.8-0.20170215184714-bbae22007d5a/builtin/providers/azurerm/resource_arm_loadbalancer_nat_rule.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 resourceArmLoadBalancerNatRule() *schema.Resource { 16 return &schema.Resource{ 17 Create: resourceArmLoadBalancerNatRuleCreate, 18 Read: resourceArmLoadBalancerNatRuleRead, 19 Update: resourceArmLoadBalancerNatRuleCreate, 20 Delete: resourceArmLoadBalancerNatRuleDelete, 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": { 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 "backend_ip_configuration_id": { 79 Type: schema.TypeString, 80 Computed: true, 81 }, 82 }, 83 } 84 } 85 86 func resourceArmLoadBalancerNatRuleCreate(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 newNatRule, err := expandAzureRmLoadBalancerNatRule(d, loadBalancer) 105 if err != nil { 106 return errwrap.Wrapf("Error Expanding NAT Rule {{err}}", err) 107 } 108 109 natRules := append(*loadBalancer.LoadBalancerPropertiesFormat.InboundNatRules, *newNatRule) 110 111 existingNatRule, existingNatRuleIndex, exists := findLoadBalancerNatRuleByName(loadBalancer, d.Get("name").(string)) 112 if exists { 113 if d.Get("name").(string) == *existingNatRule.Name { 114 // this probe is being updated/reapplied remove old copy from the slice 115 natRules = append(natRules[:existingNatRuleIndex], natRules[existingNatRuleIndex+1:]...) 116 } 117 } 118 119 loadBalancer.LoadBalancerPropertiesFormat.InboundNatRules = &natRules 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 natRule_id string 139 for _, InboundNatRule := range *(*read.LoadBalancerPropertiesFormat).InboundNatRules { 140 if *InboundNatRule.Name == d.Get("name").(string) { 141 natRule_id = *InboundNatRule.ID 142 } 143 } 144 145 if natRule_id != "" { 146 d.SetId(natRule_id) 147 } else { 148 return fmt.Errorf("Cannot find created LoadBalancer NAT Rule ID %q", natRule_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 resourceArmLoadBalancerNatRuleRead(d, meta) 163 } 164 165 func resourceArmLoadBalancerNatRuleRead(d *schema.ResourceData, meta interface{}) error { 166 id, err := parseAzureResourceID(d.Id()) 167 if err != nil { 168 return err 169 } 170 name := id.Path["inboundNatRules"] 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 := findLoadBalancerNatRuleByName(loadBalancer, name) 183 if !exists { 184 d.SetId("") 185 log.Printf("[INFO] LoadBalancer Nat Rule %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.InboundNatRulePropertiesFormat.Protocol) 192 d.Set("frontend_port", config.InboundNatRulePropertiesFormat.FrontendPort) 193 d.Set("backend_port", config.InboundNatRulePropertiesFormat.BackendPort) 194 195 if config.InboundNatRulePropertiesFormat.FrontendIPConfiguration != nil { 196 fipID, err := parseAzureResourceID(*config.InboundNatRulePropertiesFormat.FrontendIPConfiguration.ID) 197 if err != nil { 198 return err 199 } 200 201 d.Set("frontend_ip_configuration_name", fipID.Path["frontendIPConfigurations"]) 202 d.Set("frontend_ip_configuration_id", config.InboundNatRulePropertiesFormat.FrontendIPConfiguration.ID) 203 } 204 205 if config.InboundNatRulePropertiesFormat.BackendIPConfiguration != nil { 206 d.Set("backend_ip_configuration_id", config.InboundNatRulePropertiesFormat.BackendIPConfiguration.ID) 207 } 208 209 return nil 210 } 211 212 func resourceArmLoadBalancerNatRuleDelete(d *schema.ResourceData, meta interface{}) error { 213 client := meta.(*ArmClient) 214 lbClient := client.loadBalancerClient 215 216 loadBalancerID := d.Get("loadbalancer_id").(string) 217 armMutexKV.Lock(loadBalancerID) 218 defer armMutexKV.Unlock(loadBalancerID) 219 220 loadBalancer, exists, err := retrieveLoadBalancerById(loadBalancerID, meta) 221 if err != nil { 222 return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err) 223 } 224 if !exists { 225 d.SetId("") 226 return nil 227 } 228 229 _, index, exists := findLoadBalancerNatRuleByName(loadBalancer, d.Get("name").(string)) 230 if !exists { 231 return nil 232 } 233 234 oldNatRules := *loadBalancer.LoadBalancerPropertiesFormat.InboundNatRules 235 newNatRules := append(oldNatRules[:index], oldNatRules[index+1:]...) 236 loadBalancer.LoadBalancerPropertiesFormat.InboundNatRules = &newNatRules 237 238 resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string)) 239 if err != nil { 240 return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err) 241 } 242 243 _, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{})) 244 if err != nil { 245 return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err) 246 } 247 248 read, err := lbClient.Get(resGroup, loadBalancerName, "") 249 if err != nil { 250 return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err) 251 } 252 if read.ID == nil { 253 return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup) 254 } 255 256 return nil 257 } 258 259 func expandAzureRmLoadBalancerNatRule(d *schema.ResourceData, lb *network.LoadBalancer) (*network.InboundNatRule, error) { 260 261 properties := network.InboundNatRulePropertiesFormat{ 262 Protocol: network.TransportProtocol(d.Get("protocol").(string)), 263 FrontendPort: azure.Int32(int32(d.Get("frontend_port").(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 natRule := network.InboundNatRule{ 281 Name: azure.String(d.Get("name").(string)), 282 InboundNatRulePropertiesFormat: &properties, 283 } 284 285 return &natRule, nil 286 }