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