From e2fa7094bd2860524d0db3c996811064c4c6530d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 5 Jun 2014 12:21:05 -0700 Subject: [PATCH] config: ProviderConfigName on Resource --- config/config.go | 14 ++++++++++++++ config/config_test.go | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/config/config.go b/config/config.go index 990cd905a..c6df4f619 100644 --- a/config/config.go +++ b/config/config.go @@ -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. // diff --git a/config/config_test.go b/config/config_test.go index 89db2bdee..572a23a80 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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",