github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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  	InstancePort     int
    12  	LoadBalancerPort int
    13  	Protocol         string
    14  	SSLCertificateId string
    15  	Bandwidth        int
    16  }
    17  
    18  // Takes the result of flatmap.Expand for an array of listeners and
    19  // returns ELB API compatible objects
    20  func expandListeners(configured []interface{}) ([]*Listener, error) {
    21  	listeners := make([]*Listener, 0, len(configured))
    22  
    23  	// Loop over our configured listeners and create
    24  	// an array of aws-sdk-go compatabile objects
    25  	for _, lRaw := range configured {
    26  		data := lRaw.(map[string]interface{})
    27  
    28  		ip := data["instance_port"].(int)
    29  		lp := data["lb_port"].(int)
    30  		l := &Listener{
    31  			InstancePort:     ip,
    32  			LoadBalancerPort: lp,
    33  			Protocol:         data["lb_protocol"].(string),
    34  			Bandwidth:        data["bandwidth"].(int),
    35  		}
    36  
    37  		if v, ok := data["ssl_certificate_id"]; ok {
    38  			l.SSLCertificateId = v.(string)
    39  		}
    40  
    41  		var valid bool
    42  		if l.SSLCertificateId != "" {
    43  			// validate the protocol is correct
    44  			for _, p := range []string{"https", "ssl"} {
    45  				if strings.ToLower(l.Protocol) == p {
    46  					valid = true
    47  				}
    48  			}
    49  		} else {
    50  			valid = true
    51  		}
    52  
    53  		if valid {
    54  			listeners = append(listeners, l)
    55  		} else {
    56  			return nil, fmt.Errorf("[ERR] SLB Listener: ssl_certificate_id may be set only when protocol is 'https' or 'ssl'")
    57  		}
    58  	}
    59  
    60  	return listeners, nil
    61  }
    62  
    63  func expandBackendServers(list []interface{}) []slb.BackendServerType {
    64  	result := make([]slb.BackendServerType, 0, len(list))
    65  	for _, i := range list {
    66  		if i.(string) != "" {
    67  			result = append(result, slb.BackendServerType{ServerId: i.(string), Weight: 100})
    68  		}
    69  	}
    70  	return result
    71  }