github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/digitalocean/loadbalancer.go (about)

     1  package digitalocean
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/digitalocean/godo"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  )
    10  
    11  func loadbalancerStateRefreshFunc(client *godo.Client, loadbalancerId string) resource.StateRefreshFunc {
    12  	return func() (interface{}, string, error) {
    13  		lb, _, err := client.LoadBalancers.Get(context.Background(), loadbalancerId)
    14  		if err != nil {
    15  			return nil, "", fmt.Errorf("Error issuing read request in LoadbalancerStateRefreshFunc to DigitalOcean for Load Balancer '%s': %s", loadbalancerId, err)
    16  		}
    17  
    18  		return lb, lb.Status, nil
    19  	}
    20  }
    21  
    22  func expandStickySessions(config []interface{}) *godo.StickySessions {
    23  	stickysessionConfig := config[0].(map[string]interface{})
    24  
    25  	stickySession := &godo.StickySessions{
    26  		Type: stickysessionConfig["type"].(string),
    27  	}
    28  
    29  	if v, ok := stickysessionConfig["cookie_name"]; ok {
    30  		stickySession.CookieName = v.(string)
    31  	}
    32  
    33  	if v, ok := stickysessionConfig["cookie_ttl_seconds"]; ok {
    34  		stickySession.CookieTtlSeconds = v.(int)
    35  	}
    36  
    37  	return stickySession
    38  }
    39  
    40  func expandHealthCheck(config []interface{}) *godo.HealthCheck {
    41  	healthcheckConfig := config[0].(map[string]interface{})
    42  
    43  	healthcheck := &godo.HealthCheck{
    44  		Protocol:               healthcheckConfig["protocol"].(string),
    45  		Port:                   healthcheckConfig["port"].(int),
    46  		CheckIntervalSeconds:   healthcheckConfig["check_interval_seconds"].(int),
    47  		ResponseTimeoutSeconds: healthcheckConfig["response_timeout_seconds"].(int),
    48  		UnhealthyThreshold:     healthcheckConfig["unhealthy_threshold"].(int),
    49  		HealthyThreshold:       healthcheckConfig["healthy_threshold"].(int),
    50  	}
    51  
    52  	if v, ok := healthcheckConfig["path"]; ok {
    53  		healthcheck.Path = v.(string)
    54  	}
    55  
    56  	return healthcheck
    57  }
    58  
    59  func expandForwardingRules(config []interface{}) []godo.ForwardingRule {
    60  	forwardingRules := make([]godo.ForwardingRule, 0, len(config))
    61  
    62  	for _, rawRule := range config {
    63  		rule := rawRule.(map[string]interface{})
    64  
    65  		r := godo.ForwardingRule{
    66  			EntryPort:      rule["entry_port"].(int),
    67  			EntryProtocol:  rule["entry_protocol"].(string),
    68  			TargetPort:     rule["target_port"].(int),
    69  			TargetProtocol: rule["target_protocol"].(string),
    70  			TlsPassthrough: rule["tls_passthrough"].(bool),
    71  		}
    72  
    73  		if v, ok := rule["certificate_id"]; ok {
    74  			r.CertificateID = v.(string)
    75  		}
    76  
    77  		forwardingRules = append(forwardingRules, r)
    78  
    79  	}
    80  
    81  	return forwardingRules
    82  }
    83  
    84  func flattenDropletIds(list []int) []interface{} {
    85  	vs := make([]interface{}, 0, len(list))
    86  	for _, v := range list {
    87  		vs = append(vs, v)
    88  	}
    89  	return vs
    90  }
    91  
    92  func flattenHealthChecks(health *godo.HealthCheck) []map[string]interface{} {
    93  	result := make([]map[string]interface{}, 0, 1)
    94  
    95  	if health != nil {
    96  
    97  		r := make(map[string]interface{})
    98  		r["protocol"] = (*health).Protocol
    99  		r["port"] = (*health).Port
   100  		r["path"] = (*health).Path
   101  		r["check_interval_seconds"] = (*health).CheckIntervalSeconds
   102  		r["response_timeout_seconds"] = (*health).ResponseTimeoutSeconds
   103  		r["unhealthy_threshold"] = (*health).UnhealthyThreshold
   104  		r["healthy_threshold"] = (*health).HealthyThreshold
   105  
   106  		result = append(result, r)
   107  	}
   108  
   109  	return result
   110  }
   111  
   112  func flattenStickySessions(session *godo.StickySessions) []map[string]interface{} {
   113  	result := make([]map[string]interface{}, 0, 1)
   114  
   115  	if session != nil {
   116  
   117  		r := make(map[string]interface{})
   118  		r["type"] = (*session).Type
   119  		r["cookie_name"] = (*session).CookieName
   120  		r["cookie_ttl_seconds"] = (*session).CookieTtlSeconds
   121  
   122  		result = append(result, r)
   123  	}
   124  
   125  	return result
   126  }
   127  
   128  func flattenForwardingRules(rules []godo.ForwardingRule) []map[string]interface{} {
   129  	result := make([]map[string]interface{}, 0, 1)
   130  
   131  	if rules != nil {
   132  		for _, rule := range rules {
   133  			r := make(map[string]interface{})
   134  			r["entry_protocol"] = rule.EntryProtocol
   135  			r["entry_port"] = rule.EntryPort
   136  			r["target_protocol"] = rule.TargetProtocol
   137  			r["target_port"] = rule.TargetPort
   138  			r["certificate_id"] = rule.CertificateID
   139  			r["tls_passthrough"] = rule.TlsPassthrough
   140  
   141  			result = append(result, r)
   142  		}
   143  	}
   144  
   145  	return result
   146  }