opentofu/terraform/util.go
Martin Atkins e772b45970 "external" data source, for integrating with external programs (#8768)
* "external" provider for gluing in external logic

This provider will become a bit of glue to help people interface external
programs with Terraform without writing a full Terraform provider.

It will be nowhere near as capable as a first-class provider, but is
intended as a light-touch way to integrate some pre-existing or custom
system into Terraform.

* Unit test for the "resourceProvider" utility function

This small function determines the dependable name of a provider for
a given resource name and optional provider alias. It's simple but it's
a key part of how resource nodes get connected to provider nodes so
worth specifying the intended behavior in the form of a test.

* Allow a provider to export a resource with the provider's name

If a provider only implements one resource of each type (managed vs. data)
then it can be reasonable for the resource names to exactly match the
provider name, if the provider name is descriptive enough for the
purpose of the each resource to be obvious.

* provider/external: data source

A data source that executes a child process, expecting it to support a
particular gateway protocol, and exports its result. This can be used as
a straightforward way to retrieve data from sources that Terraform
doesn't natively support..

* website: documentation for the "external" provider
2016-12-05 17:24:57 +00:00

76 lines
1.6 KiB
Go

package terraform
import (
"strings"
)
// Semaphore is a wrapper around a channel to provide
// utility methods to clarify that we are treating the
// channel as a semaphore
type Semaphore chan struct{}
// NewSemaphore creates a semaphore that allows up
// to a given limit of simultaneous acquisitions
func NewSemaphore(n int) Semaphore {
if n == 0 {
panic("semaphore with limit 0")
}
ch := make(chan struct{}, n)
return Semaphore(ch)
}
// Acquire is used to acquire an available slot.
// Blocks until available.
func (s Semaphore) Acquire() {
s <- struct{}{}
}
// TryAcquire is used to do a non-blocking acquire.
// Returns a bool indicating success
func (s Semaphore) TryAcquire() bool {
select {
case s <- struct{}{}:
return true
default:
return false
}
}
// Release is used to return a slot. Acquire must
// be called as a pre-condition.
func (s Semaphore) Release() {
select {
case <-s:
default:
panic("release without an acquire")
}
}
// resourceProvider returns the provider name for the given type.
func resourceProvider(t, alias string) string {
if alias != "" {
return alias
}
idx := strings.IndexRune(t, '_')
if idx == -1 {
// If no underscores, the resource name is assumed to be
// also the provider name, e.g. if the provider exposes
// only a single resource of each type.
return t
}
return t[:idx]
}
// strSliceContains checks if a given string is contained in a slice
// When anybody asks why Go needs generics, here you go.
func strSliceContains(haystack []string, needle string) bool {
for _, s := range haystack {
if s == needle {
return true
}
}
return false
}