github.com/rhenning/terraform@v0.8.0-beta2/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 loadBalancerID := d.Get("loadbalancer_id").(string) 92 armMutexKV.Lock(loadBalancerID) 93 defer armMutexKV.Unlock(loadBalancerID) 94 95 loadBalancer, exists, err := retrieveLoadBalancerById(loadBalancerID, meta) 96 if err != nil { 97 return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err) 98 } 99 if !exists { 100 d.SetId("") 101 log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string)) 102 return nil 103 } 104 105 newProbe, err := expandAzureRmLoadBalancerProbe(d, loadBalancer) 106 if err != nil { 107 return errwrap.Wrapf("Error Expanding Probe {{err}}", err) 108 } 109 110 probes := append(*loadBalancer.Properties.Probes, *newProbe) 111 112 existingProbe, existingProbeIndex, exists := findLoadBalancerProbeByName(loadBalancer, d.Get("name").(string)) 113 if exists { 114 if d.Id() == *existingProbe.ID { 115 // this probe is being updated remove old copy from the slice 116 probes = append(probes[:existingProbeIndex], probes[existingProbeIndex+1:]...) 117 } else { 118 return fmt.Errorf("A Probe with name %q already exists.", d.Get("name").(string)) 119 } 120 } 121 122 loadBalancer.Properties.Probes = &probes 123 resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string)) 124 if err != nil { 125 return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err) 126 } 127 128 _, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{})) 129 if err != nil { 130 return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err) 131 } 132 133 read, err := lbClient.Get(resGroup, loadBalancerName, "") 134 if err != nil { 135 return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err) 136 } 137 if read.ID == nil { 138 return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup) 139 } 140 141 var createdProbe_id string 142 for _, Probe := range *(*read.Properties).Probes { 143 if *Probe.Name == d.Get("name").(string) { 144 createdProbe_id = *Probe.ID 145 } 146 } 147 148 if createdProbe_id != "" { 149 d.SetId(createdProbe_id) 150 } else { 151 return fmt.Errorf("Cannot find created LoadBalancer Probe ID %q", createdProbe_id) 152 } 153 154 log.Printf("[DEBUG] Waiting for LoadBalancer (%s) to become available", loadBalancerName) 155 stateConf := &resource.StateChangeConf{ 156 Pending: []string{"Accepted", "Updating"}, 157 Target: []string{"Succeeded"}, 158 Refresh: loadbalancerStateRefreshFunc(client, resGroup, loadBalancerName), 159 Timeout: 10 * time.Minute, 160 } 161 if _, err := stateConf.WaitForState(); err != nil { 162 return fmt.Errorf("Error waiting for LoadBalancer (%s) to become available: %s", loadBalancerName, err) 163 } 164 165 return resourceArmLoadBalancerProbeRead(d, meta) 166 } 167 168 func resourceArmLoadBalancerProbeRead(d *schema.ResourceData, meta interface{}) error { 169 loadBalancer, exists, err := retrieveLoadBalancerById(d.Get("loadbalancer_id").(string), meta) 170 if err != nil { 171 return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err) 172 } 173 if !exists { 174 d.SetId("") 175 log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string)) 176 return nil 177 } 178 179 configs := *loadBalancer.Properties.Probes 180 for _, config := range configs { 181 if *config.Name == d.Get("name").(string) { 182 d.Set("name", config.Name) 183 184 d.Set("protocol", config.Properties.Protocol) 185 d.Set("interval_in_seconds", config.Properties.IntervalInSeconds) 186 d.Set("number_of_probes", config.Properties.NumberOfProbes) 187 d.Set("port", config.Properties.Port) 188 d.Set("request_path", config.Properties.RequestPath) 189 190 break 191 } 192 } 193 194 return nil 195 } 196 197 func resourceArmLoadBalancerProbeDelete(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 := findLoadBalancerProbeByName(loadBalancer, d.Get("name").(string)) 215 if !exists { 216 return nil 217 } 218 219 oldProbes := *loadBalancer.Properties.Probes 220 newProbes := append(oldProbes[:index], oldProbes[index+1:]...) 221 loadBalancer.Properties.Probes = &newProbes 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 expandAzureRmLoadBalancerProbe(d *schema.ResourceData, lb *network.LoadBalancer) (*network.Probe, error) { 245 246 properties := network.ProbePropertiesFormat{ 247 NumberOfProbes: azure.Int32(int32(d.Get("number_of_probes").(int))), 248 IntervalInSeconds: azure.Int32(int32(d.Get("interval_in_seconds").(int))), 249 Port: azure.Int32(int32(d.Get("port").(int))), 250 } 251 252 if v, ok := d.GetOk("protocol"); ok { 253 properties.Protocol = network.ProbeProtocol(v.(string)) 254 } 255 256 if v, ok := d.GetOk("request_path"); ok { 257 properties.RequestPath = azure.String(v.(string)) 258 } 259 260 probe := network.Probe{ 261 Name: azure.String(d.Get("name").(string)), 262 Properties: &properties, 263 } 264 265 return &probe, nil 266 }