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