Resolve resource provider types in config package

Previously the logic for inferring a provider type from a resource name
was buried a utility function in the 'terraform' package. Instead here we
lift it up into the 'config' package where we can make broader use of it
and where it's easier to discover.
This commit is contained in:
Martin Atkins 2017-04-14 19:07:33 -07:00
parent 7e7d4c70df
commit 0b14c2cdb3
4 changed files with 81 additions and 66 deletions

View File

@ -240,6 +240,33 @@ func (r *Resource) Id() string {
}
}
// ProviderFullName returns the full name of the provider for this resource,
// which may either be specified explicitly using the "provider" meta-argument
// or implied by the prefix on the resource type name.
func (r *Resource) ProviderFullName() string {
return ResourceProviderFullName(r.Type, r.Provider)
}
// ResourceProviderFullName returns the full (dependable) name of the
// provider for a hypothetical resource with the given resource type and
// explicit provider string. If the explicit provider string is empty then
// the provider name is inferred from the resource type name.
func ResourceProviderFullName(resourceType, explicitProvider string) string {
if explicitProvider != "" {
return explicitProvider
}
idx := strings.IndexRune(resourceType, '_')
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 resourceType
}
return resourceType[:idx]
}
// Validate does some basic semantic checking of the configuration.
func (c *Config) Validate() error {
if c == nil {

View File

@ -706,3 +706,53 @@ func TestConfigProviderVersion(t *testing.T) {
t.Fatal("'version' should not exist in raw config")
}
}
func TestResourceProviderFullName(t *testing.T) {
type testCase struct {
ResourceName string
Alias string
Expected string
}
tests := []testCase{
{
// If no alias is provided, the first underscore-separated segment
// is assumed to be the provider name.
ResourceName: "aws_thing",
Alias: "",
Expected: "aws",
},
{
// If we have more than one underscore then it's the first one that we'll use.
ResourceName: "aws_thingy_thing",
Alias: "",
Expected: "aws",
},
{
// A provider can export a resource whose name is just the bare provider name,
// e.g. because the provider only has one resource and so any additional
// parts would be redundant.
ResourceName: "external",
Alias: "",
Expected: "external",
},
{
// Alias always overrides the default extraction of the name
ResourceName: "aws_thing",
Alias: "tls.baz",
Expected: "tls.baz",
},
}
for _, test := range tests {
got := ResourceProviderFullName(test.ResourceName, test.Alias)
if got != test.Expected {
t.Errorf(
"(%q, %q) produced %q; want %q",
test.ResourceName, test.Alias,
got,
test.Expected,
)
}
}
}

View File

@ -2,7 +2,8 @@ package terraform
import (
"sort"
"strings"
"github.com/hashicorp/terraform/config"
)
// Semaphore is a wrapper around a channel to provide
@ -47,21 +48,8 @@ func (s Semaphore) Release() {
}
}
// 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]
func resourceProvider(resourceType, explicitProvider string) string {
return config.ResourceProviderFullName(resourceType, explicitProvider)
}
// strSliceContains checks if a given string is contained in a slice

View File

@ -49,56 +49,6 @@ func TestStrSliceContains(t *testing.T) {
}
}
func TestUtilResourceProvider(t *testing.T) {
type testCase struct {
ResourceName string
Alias string
Expected string
}
tests := []testCase{
{
// If no alias is provided, the first underscore-separated segment
// is assumed to be the provider name.
ResourceName: "aws_thing",
Alias: "",
Expected: "aws",
},
{
// If we have more than one underscore then it's the first one that we'll use.
ResourceName: "aws_thingy_thing",
Alias: "",
Expected: "aws",
},
{
// A provider can export a resource whose name is just the bare provider name,
// e.g. because the provider only has one resource and so any additional
// parts would be redundant.
ResourceName: "external",
Alias: "",
Expected: "external",
},
{
// Alias always overrides the default extraction of the name
ResourceName: "aws_thing",
Alias: "tls.baz",
Expected: "tls.baz",
},
}
for _, test := range tests {
got := resourceProvider(test.ResourceName, test.Alias)
if got != test.Expected {
t.Errorf(
"(%q, %q) produced %q; want %q",
test.ResourceName, test.Alias,
got,
test.Expected,
)
}
}
}
func TestUniqueStrings(t *testing.T) {
cases := []struct {
Input []string