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