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