github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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 _, _, exists = findLoadBalancerProbeByName(loadBalancer, d.Get("name").(string)) 106 if exists { 107 return fmt.Errorf("A Probe with name %q already exists.", d.Get("name").(string)) 108 } 109 110 newProbe, err := expandAzureRmLoadBalancerProbe(d, loadBalancer) 111 if err != nil { 112 return errwrap.Wrapf("Error Expanding Probe {{err}}", err) 113 } 114 115 probes := append(*loadBalancer.Properties.Probes, *newProbe) 116 loadBalancer.Properties.Probes = &probes 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 createdProbe_id string 136 for _, Probe := range *(*read.Properties).Probes { 137 if *Probe.Name == d.Get("name").(string) { 138 createdProbe_id = *Probe.ID 139 } 140 } 141 142 if createdProbe_id != "" { 143 d.SetId(createdProbe_id) 144 } else { 145 return fmt.Errorf("Cannot find created LoadBalancer Probe ID %q", createdProbe_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 resourceArmLoadBalancerProbeRead(d, meta) 160 } 161 162 func resourceArmLoadBalancerProbeRead(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.Probes 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("interval_in_seconds", config.Properties.IntervalInSeconds) 180 d.Set("number_of_probes", config.Properties.NumberOfProbes) 181 d.Set("port", config.Properties.Port) 182 d.Set("request_path", config.Properties.RequestPath) 183 184 break 185 } 186 } 187 188 return nil 189 } 190 191 func resourceArmLoadBalancerProbeDelete(d *schema.ResourceData, meta interface{}) error { 192 client := meta.(*ArmClient) 193 lbClient := client.loadBalancerClient 194 195 loadBalancerID := d.Get("loadbalancer_id").(string) 196 armMutexKV.Lock(loadBalancerID) 197 defer armMutexKV.Unlock(loadBalancerID) 198 199 loadBalancer, exists, err := retrieveLoadBalancerById(loadBalancerID, meta) 200 if err != nil { 201 return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err) 202 } 203 if !exists { 204 d.SetId("") 205 return nil 206 } 207 208 _, index, exists := findLoadBalancerProbeByName(loadBalancer, d.Get("name").(string)) 209 if !exists { 210 return nil 211 } 212 213 oldProbes := *loadBalancer.Properties.Probes 214 newProbes := append(oldProbes[:index], oldProbes[index+1:]...) 215 loadBalancer.Properties.Probes = &newProbes 216 217 resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string)) 218 if err != nil { 219 return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err) 220 } 221 222 _, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{})) 223 if err != nil { 224 return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err) 225 } 226 227 read, err := lbClient.Get(resGroup, loadBalancerName, "") 228 if err != nil { 229 return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err) 230 } 231 if read.ID == nil { 232 return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup) 233 } 234 235 return nil 236 } 237 238 func expandAzureRmLoadBalancerProbe(d *schema.ResourceData, lb *network.LoadBalancer) (*network.Probe, error) { 239 240 properties := network.ProbePropertiesFormat{ 241 NumberOfProbes: azure.Int32(int32(d.Get("number_of_probes").(int))), 242 IntervalInSeconds: azure.Int32(int32(d.Get("interval_in_seconds").(int))), 243 Port: azure.Int32(int32(d.Get("port").(int))), 244 } 245 246 if v, ok := d.GetOk("protocol"); ok { 247 properties.Protocol = network.ProbeProtocol(v.(string)) 248 } 249 250 if v, ok := d.GetOk("request_path"); ok { 251 properties.RequestPath = azure.String(v.(string)) 252 } 253 254 probe := network.Probe{ 255 Name: azure.String(d.Get("name").(string)), 256 Properties: &properties, 257 } 258 259 return &probe, nil 260 }