config: ProviderConfigName on Resource

This commit is contained in:
Mitchell Hashimoto 2014-06-05 12:21:05 -07:00
parent 4397c566a0
commit e2fa7094bd
2 changed files with 33 additions and 0 deletions

View File

@ -82,6 +82,20 @@ func (r *Resource) Id() string {
return fmt.Sprintf("%s.%s", r.Type, r.Name)
}
// ProviderConfigName returns the name of the provider configuration in
// the given mapping that maps to the proper provider configuration
// for this resource.
func (r *Resource) ProviderConfigName(pcs map[string]*ProviderConfig) string {
lk := ""
for k, _ := range pcs {
if strings.HasPrefix(r.Type, k) && len(k) > len(lk) {
lk = k
}
}
return lk
}
// ReplaceVariables replaces the variables in the configuration
// with the given values.
//

View File

@ -64,6 +64,25 @@ func TestNewUserVariable(t *testing.T) {
}
}
func TestResourceProviderConfigName(t *testing.T) {
r := &Resource{
Name: "foo",
Type: "aws_instance",
}
pcs := map[string]*ProviderConfig{
"aw": new(ProviderConfig),
"aws": new(ProviderConfig),
"a": new(ProviderConfig),
"gce_": new(ProviderConfig),
}
n := r.ProviderConfigName(pcs)
if n != "aws" {
t.Fatalf("bad: %s", n)
}
}
func TestResourceReplaceVariables(t *testing.T) {
r := &Resource{
Name: "foo",