From 0b14c2cdb3c9591ff35e9900b146b6972197ffdb Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 14 Apr 2017 19:07:33 -0700 Subject: [PATCH] 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. --- config/config.go | 27 +++++++++++++++++++++++ config/config_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++ terraform/util.go | 20 ++++------------- terraform/util_test.go | 50 ------------------------------------------ 4 files changed, 81 insertions(+), 66 deletions(-) diff --git a/config/config.go b/config/config.go index 34ce8cf2d..0648d6917 100644 --- a/config/config.go +++ b/config/config.go @@ -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 { diff --git a/config/config_test.go b/config/config_test.go index dea2ab87e..db62e1c54 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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, + ) + } + } +} diff --git a/terraform/util.go b/terraform/util.go index f41f0d7d6..752241af1 100644 --- a/terraform/util.go +++ b/terraform/util.go @@ -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 diff --git a/terraform/util_test.go b/terraform/util_test.go index 9c5712cfa..8b3907e23 100644 --- a/terraform/util_test.go +++ b/terraform/util_test.go @@ -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