github.com/mapuri/terraform@v0.7.6-0.20161012203214-7e0408293f97/builtin/providers/azurerm/resource_arm_loadbalancer_probe.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 resourceArmLoadBalancerProbe() *schema.Resource { 16 return &schema.Resource{ 17 Create: resourceArmLoadBalancerProbeCreate, 18 Read: resourceArmLoadBalancerProbeRead, 19 Update: resourceArmLoadBalancerProbeCreate, 20 Delete: resourceArmLoadBalancerProbeDelete, 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 Computed: true, 51 Optional: true, 52 }, 53 54 "port": { 55 Type: schema.TypeInt, 56 Required: true, 57 }, 58 59 "request_path": { 60 Type: schema.TypeString, 61 Optional: true, 62 Computed: true, 63 }, 64 65 "interval_in_seconds": { 66 Type: schema.TypeInt, 67 Optional: true, 68 Default: 15, 69 }, 70 71 "number_of_probes": { 72 Type: schema.TypeInt, 73 Optional: true, 74 Default: 2, 75 }, 76 77 "load_balance_rules": { 78 Type: schema.TypeSet, 79 Computed: true, 80 Elem: &schema.Schema{Type: schema.TypeString}, 81 Set: schema.HashString, 82 }, 83 }, 84 } 85 } 86 87 func resourceArmLoadBalancerProbeCreate(d *schema.ResourceData, meta interface{}) error { 88 client := meta.(*ArmClient) 89 lbClient := client.loadBalancerClient 90 91 loadBalancer, exists, err := retrieveLoadBalancerById(d.Get("loadbalancer_id").(string), meta) 92 if err != nil { 93 return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err) 94 } 95 if !exists { 96 d.SetId("") 97 log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string)) 98 return nil 99 } 100 101 _, _, exists = findLoadBalancerProbeByName(loadBalancer, d.Get("name").(string)) 102 if exists { 103 return fmt.Errorf("A Probe with name %q already exists.", d.Get("name").(string)) 104 } 105 106 newProbe, err := expandAzureRmLoadBalancerProbe(d, loadBalancer) 107 if err != nil { 108 return errwrap.Wrapf("Error Expanding Probe {{err}}", err) 109 } 110 111 probes := append(*loadBalancer.Properties.Probes, *newProbe) 112 loadBalancer.Properties.Probes = &probes 113 resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string)) 114 if err != nil { 115 return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err) 116 } 117 118 _, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{})) 119 if err != nil { 120 return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err) 121 } 122 123 read, err := lbClient.Get(resGroup, loadBalancerName, "") 124 if err != nil { 125 return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err) 126 } 127 if read.ID == nil { 128 return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup) 129 } 130 131 d.SetId(*read.ID) 132 133 log.Printf("[DEBUG] Waiting for LoadBalancer (%s) to become available", loadBalancerName) 134 stateConf := &resource.StateChangeConf{ 135 Pending: []string{"Accepted", "Updating"}, 136 Target: []string{"Succeeded"}, 137 Refresh: loadbalancerStateRefreshFunc(client, resGroup, loadBalancerName), 138 Timeout: 10 * time.Minute, 139 } 140 if _, err := stateConf.WaitForState(); err != nil { 141 return fmt.Errorf("Error waiting for LoadBalancer (%s) to become available: %s", loadBalancerName, err) 142 } 143 144 return resourceArmLoadBalancerProbeRead(d, meta) 145 } 146 147 func resourceArmLoadBalancerProbeRead(d *schema.ResourceData, meta interface{}) error { 148 loadBalancer, exists, err := retrieveLoadBalancerById(d.Id(), meta) 149 if err != nil { 150 return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err) 151 } 152 if !exists { 153 d.SetId("") 154 log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string)) 155 return nil 156 } 157 158 configs := *loadBalancer.Properties.Probes 159 for _, config := range configs { 160 if *config.Name == d.Get("name").(string) { 161 d.Set("name", config.Name) 162 163 d.Set("protocol", config.Properties.Protocol) 164 d.Set("interval_in_seconds", config.Properties.IntervalInSeconds) 165 d.Set("number_of_probes", config.Properties.NumberOfProbes) 166 d.Set("port", config.Properties.Port) 167 d.Set("request_path", config.Properties.RequestPath) 168 169 break 170 } 171 } 172 173 return nil 174 } 175 176 func resourceArmLoadBalancerProbeDelete(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 := findLoadBalancerProbeByName(loadBalancer, d.Get("name").(string)) 190 if !exists { 191 return nil 192 } 193 194 oldProbes := *loadBalancer.Properties.Probes 195 newProbes := append(oldProbes[:index], oldProbes[index+1:]...) 196 loadBalancer.Properties.Probes = &newProbes 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 expandAzureRmLoadBalancerProbe(d *schema.ResourceData, lb *network.LoadBalancer) (*network.Probe, error) { 220 221 properties := network.ProbePropertiesFormat{ 222 NumberOfProbes: azure.Int32(int32(d.Get("number_of_probes").(int))), 223 IntervalInSeconds: azure.Int32(int32(d.Get("interval_in_seconds").(int))), 224 Port: azure.Int32(int32(d.Get("port").(int))), 225 } 226 227 if v, ok := d.GetOk("protocol"); ok { 228 properties.Protocol = network.ProbeProtocol(v.(string)) 229 } 230 231 if v, ok := d.GetOk("request_path"); ok { 232 properties.RequestPath = azure.String(v.(string)) 233 } 234 235 probe := network.Probe{ 236 Name: azure.String(d.Get("name").(string)), 237 Properties: &properties, 238 } 239 240 return &probe, nil 241 }