github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 StateFunc: ignoreCaseStateFunc, 57 DiffSuppressFunc: ignoreCaseDiffSuppressFunc, 58 }, 59 60 "frontend_port": { 61 Type: schema.TypeInt, 62 Required: true, 63 }, 64 65 "backend_port": { 66 Type: schema.TypeInt, 67 Required: true, 68 }, 69 70 "frontend_ip_configuration_name": { 71 Type: schema.TypeString, 72 Required: true, 73 }, 74 75 "frontend_ip_configuration_id": { 76 Type: schema.TypeString, 77 Computed: true, 78 }, 79 80 "backend_ip_configuration_id": { 81 Type: schema.TypeString, 82 Computed: true, 83 }, 84 }, 85 } 86 } 87 88 func resourceArmLoadBalancerNatRuleCreate(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 newNatRule, err := expandAzureRmLoadBalancerNatRule(d, loadBalancer) 107 if err != nil { 108 return errwrap.Wrapf("Error Expanding NAT Rule {{err}}", err) 109 } 110 111 natRules := append(*loadBalancer.LoadBalancerPropertiesFormat.InboundNatRules, *newNatRule) 112 113 existingNatRule, existingNatRuleIndex, exists := findLoadBalancerNatRuleByName(loadBalancer, d.Get("name").(string)) 114 if exists { 115 if d.Get("name").(string) == *existingNatRule.Name { 116 // this probe is being updated/reapplied remove old copy from the slice 117 natRules = append(natRules[:existingNatRuleIndex], natRules[existingNatRuleIndex+1:]...) 118 } 119 } 120 121 loadBalancer.LoadBalancerPropertiesFormat.InboundNatRules = &natRules 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 natRule_id string 141 for _, InboundNatRule := range *(*read.LoadBalancerPropertiesFormat).InboundNatRules { 142 if *InboundNatRule.Name == d.Get("name").(string) { 143 natRule_id = *InboundNatRule.ID 144 } 145 } 146 147 if natRule_id != "" { 148 d.SetId(natRule_id) 149 } else { 150 return fmt.Errorf("Cannot find created LoadBalancer NAT Rule ID %q", natRule_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 resourceArmLoadBalancerNatRuleRead(d, meta) 165 } 166 167 func resourceArmLoadBalancerNatRuleRead(d *schema.ResourceData, meta interface{}) error { 168 id, err := parseAzureResourceID(d.Id()) 169 if err != nil { 170 return err 171 } 172 name := id.Path["inboundNatRules"] 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 := findLoadBalancerNatRuleByName(loadBalancer, name) 185 if !exists { 186 d.SetId("") 187 log.Printf("[INFO] LoadBalancer Nat Rule %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.InboundNatRulePropertiesFormat.Protocol) 194 d.Set("frontend_port", config.InboundNatRulePropertiesFormat.FrontendPort) 195 d.Set("backend_port", config.InboundNatRulePropertiesFormat.BackendPort) 196 197 if config.InboundNatRulePropertiesFormat.FrontendIPConfiguration != nil { 198 fipID, err := parseAzureResourceID(*config.InboundNatRulePropertiesFormat.FrontendIPConfiguration.ID) 199 if err != nil { 200 return err 201 } 202 203 d.Set("frontend_ip_configuration_name", fipID.Path["frontendIPConfigurations"]) 204 d.Set("frontend_ip_configuration_id", config.InboundNatRulePropertiesFormat.FrontendIPConfiguration.ID) 205 } 206 207 if config.InboundNatRulePropertiesFormat.BackendIPConfiguration != nil { 208 d.Set("backend_ip_configuration_id", config.InboundNatRulePropertiesFormat.BackendIPConfiguration.ID) 209 } 210 211 return nil 212 } 213 214 func resourceArmLoadBalancerNatRuleDelete(d *schema.ResourceData, meta interface{}) error { 215 client := meta.(*ArmClient) 216 lbClient := client.loadBalancerClient 217 218 loadBalancerID := d.Get("loadbalancer_id").(string) 219 armMutexKV.Lock(loadBalancerID) 220 defer armMutexKV.Unlock(loadBalancerID) 221 222 loadBalancer, exists, err := retrieveLoadBalancerById(loadBalancerID, meta) 223 if err != nil { 224 return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err) 225 } 226 if !exists { 227 d.SetId("") 228 return nil 229 } 230 231 _, index, exists := findLoadBalancerNatRuleByName(loadBalancer, d.Get("name").(string)) 232 if !exists { 233 return nil 234 } 235 236 oldNatRules := *loadBalancer.LoadBalancerPropertiesFormat.InboundNatRules 237 newNatRules := append(oldNatRules[:index], oldNatRules[index+1:]...) 238 loadBalancer.LoadBalancerPropertiesFormat.InboundNatRules = &newNatRules 239 240 resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string)) 241 if err != nil { 242 return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err) 243 } 244 245 _, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{})) 246 if err != nil { 247 return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err) 248 } 249 250 read, err := lbClient.Get(resGroup, loadBalancerName, "") 251 if err != nil { 252 return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err) 253 } 254 if read.ID == nil { 255 return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup) 256 } 257 258 return nil 259 } 260 261 func expandAzureRmLoadBalancerNatRule(d *schema.ResourceData, lb *network.LoadBalancer) (*network.InboundNatRule, error) { 262 263 properties := network.InboundNatRulePropertiesFormat{ 264 Protocol: network.TransportProtocol(d.Get("protocol").(string)), 265 FrontendPort: azure.Int32(int32(d.Get("frontend_port").(int))), 266 BackendPort: azure.Int32(int32(d.Get("backend_port").(int))), 267 } 268 269 if v := d.Get("frontend_ip_configuration_name").(string); v != "" { 270 rule, _, exists := findLoadBalancerFrontEndIpConfigurationByName(lb, v) 271 if !exists { 272 return nil, fmt.Errorf("[ERROR] Cannot find FrontEnd IP Configuration with the name %s", v) 273 } 274 275 feip := network.SubResource{ 276 ID: rule.ID, 277 } 278 279 properties.FrontendIPConfiguration = &feip 280 } 281 282 natRule := network.InboundNatRule{ 283 Name: azure.String(d.Get("name").(string)), 284 InboundNatRulePropertiesFormat: &properties, 285 } 286 287 return &natRule, nil 288 }