github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/extension_slb.go (about)

     1  package alicloud
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/denverdino/aliyungo/slb"
     8  )
     9  
    10  type Listener struct {
    11  	slb.HTTPListenerType
    12  
    13  	InstancePort     int
    14  	LoadBalancerPort int
    15  	Protocol         string
    16  	//tcp & udp
    17  	PersistenceTimeout int
    18  
    19  	//https
    20  	SSLCertificateId string
    21  
    22  	//tcp
    23  	HealthCheckType slb.HealthCheckType
    24  
    25  	//api interface: http & https is HealthCheckTimeout, tcp & udp is HealthCheckConnectTimeout
    26  	HealthCheckConnectTimeout int
    27  }
    28  
    29  type ListenerErr struct {
    30  	ErrType string
    31  	Err     error
    32  }
    33  
    34  func (e *ListenerErr) Error() string {
    35  	return e.ErrType + " " + e.Err.Error()
    36  
    37  }
    38  
    39  const (
    40  	HealthCheckErrType   = "healthCheckErrType"
    41  	StickySessionErrType = "stickySessionErrType"
    42  	CookieTimeOutErrType = "cookieTimeoutErrType"
    43  	CookieErrType        = "cookieErrType"
    44  )
    45  
    46  // Takes the result of flatmap.Expand for an array of listeners and
    47  // returns ELB API compatible objects
    48  func expandListeners(configured []interface{}) ([]*Listener, error) {
    49  	listeners := make([]*Listener, 0, len(configured))
    50  
    51  	// Loop over our configured listeners and create
    52  	// an array of aws-sdk-go compatabile objects
    53  	for _, lRaw := range configured {
    54  		data := lRaw.(map[string]interface{})
    55  
    56  		ip := data["instance_port"].(int)
    57  		lp := data["lb_port"].(int)
    58  		l := &Listener{
    59  			InstancePort:     ip,
    60  			LoadBalancerPort: lp,
    61  			Protocol:         data["lb_protocol"].(string),
    62  		}
    63  
    64  		l.Bandwidth = data["bandwidth"].(int)
    65  
    66  		if v, ok := data["scheduler"]; ok {
    67  			l.Scheduler = slb.SchedulerType(v.(string))
    68  		}
    69  
    70  		if v, ok := data["ssl_certificate_id"]; ok {
    71  			l.SSLCertificateId = v.(string)
    72  		}
    73  
    74  		if v, ok := data["sticky_session"]; ok {
    75  			l.StickySession = slb.FlagType(v.(string))
    76  		}
    77  
    78  		if v, ok := data["sticky_session_type"]; ok {
    79  			l.StickySessionType = slb.StickySessionType(v.(string))
    80  		}
    81  
    82  		if v, ok := data["cookie_timeout"]; ok {
    83  			l.CookieTimeout = v.(int)
    84  		}
    85  
    86  		if v, ok := data["cookie"]; ok {
    87  			l.Cookie = v.(string)
    88  		}
    89  
    90  		if v, ok := data["persistence_timeout"]; ok {
    91  			l.PersistenceTimeout = v.(int)
    92  		}
    93  
    94  		if v, ok := data["health_check"]; ok {
    95  			l.HealthCheck = slb.FlagType(v.(string))
    96  		}
    97  
    98  		if v, ok := data["health_check_type"]; ok {
    99  			l.HealthCheckType = slb.HealthCheckType(v.(string))
   100  		}
   101  
   102  		if v, ok := data["health_check_domain"]; ok {
   103  			l.HealthCheckDomain = v.(string)
   104  		}
   105  
   106  		if v, ok := data["health_check_uri"]; ok {
   107  			l.HealthCheckURI = v.(string)
   108  		}
   109  
   110  		if v, ok := data["health_check_connect_port"]; ok {
   111  			l.HealthCheckConnectPort = v.(int)
   112  		}
   113  
   114  		if v, ok := data["healthy_threshold"]; ok {
   115  			l.HealthyThreshold = v.(int)
   116  		}
   117  
   118  		if v, ok := data["unhealthy_threshold"]; ok {
   119  			l.UnhealthyThreshold = v.(int)
   120  		}
   121  
   122  		if v, ok := data["health_check_timeout"]; ok {
   123  			l.HealthCheckTimeout = v.(int)
   124  		}
   125  
   126  		if v, ok := data["health_check_interval"]; ok {
   127  			l.HealthCheckInterval = v.(int)
   128  		}
   129  
   130  		if v, ok := data["health_check_http_code"]; ok {
   131  			l.HealthCheckHttpCode = slb.HealthCheckHttpCodeType(v.(string))
   132  		}
   133  
   134  		var valid bool
   135  		if l.SSLCertificateId != "" {
   136  			// validate the protocol is correct
   137  			for _, p := range []string{"https", "ssl"} {
   138  				if strings.ToLower(l.Protocol) == p {
   139  					valid = true
   140  				}
   141  			}
   142  		} else {
   143  			valid = true
   144  		}
   145  
   146  		if valid {
   147  			listeners = append(listeners, l)
   148  		} else {
   149  			return nil, fmt.Errorf("[ERR] SLB Listener: ssl_certificate_id may be set only when protocol is 'https' or 'ssl'")
   150  		}
   151  	}
   152  
   153  	return listeners, nil
   154  }
   155  
   156  func expandBackendServers(list []interface{}) []slb.BackendServerType {
   157  	result := make([]slb.BackendServerType, 0, len(list))
   158  	for _, i := range list {
   159  		if i.(string) != "" {
   160  			result = append(result, slb.BackendServerType{ServerId: i.(string), Weight: 100})
   161  		}
   162  	}
   163  	return result
   164  }