github.com/ggriffiths/terraform@v0.9.0-beta1.0.20170222213024-79c4935604cb/terraform/util.go (about)

     1  package terraform
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  // Semaphore is a wrapper around a channel to provide
     8  // utility methods to clarify that we are treating the
     9  // channel as a semaphore
    10  type Semaphore chan struct{}
    11  
    12  // NewSemaphore creates a semaphore that allows up
    13  // to a given limit of simultaneous acquisitions
    14  func NewSemaphore(n int) Semaphore {
    15  	if n == 0 {
    16  		panic("semaphore with limit 0")
    17  	}
    18  	ch := make(chan struct{}, n)
    19  	return Semaphore(ch)
    20  }
    21  
    22  // Acquire is used to acquire an available slot.
    23  // Blocks until available.
    24  func (s Semaphore) Acquire() {
    25  	s <- struct{}{}
    26  }
    27  
    28  // TryAcquire is used to do a non-blocking acquire.
    29  // Returns a bool indicating success
    30  func (s Semaphore) TryAcquire() bool {
    31  	select {
    32  	case s <- struct{}{}:
    33  		return true
    34  	default:
    35  		return false
    36  	}
    37  }
    38  
    39  // Release is used to return a slot. Acquire must
    40  // be called as a pre-condition.
    41  func (s Semaphore) Release() {
    42  	select {
    43  	case <-s:
    44  	default:
    45  		panic("release without an acquire")
    46  	}
    47  }
    48  
    49  // resourceProvider returns the provider name for the given type.
    50  func resourceProvider(t, alias string) string {
    51  	if alias != "" {
    52  		return alias
    53  	}
    54  
    55  	idx := strings.IndexRune(t, '_')
    56  	if idx == -1 {
    57  		// If no underscores, the resource name is assumed to be
    58  		// also the provider name, e.g. if the provider exposes
    59  		// only a single resource of each type.
    60  		return t
    61  	}
    62  
    63  	return t[:idx]
    64  }
    65  
    66  // strSliceContains checks if a given string is contained in a slice
    67  // When anybody asks why Go needs generics, here you go.
    68  func strSliceContains(haystack []string, needle string) bool {
    69  	for _, s := range haystack {
    70  		if s == needle {
    71  			return true
    72  		}
    73  	}
    74  	return false
    75  }