terraform: missing provider should add missing aliases [GH-2023]

This commit is contained in:
Mitchell Hashimoto 2015-06-24 20:58:52 -07:00
parent 4bf0d5e243
commit 29eadb8194
9 changed files with 93 additions and 52 deletions

View File

@ -683,6 +683,10 @@ func (o *Output) mergerMerge(m merger) merger {
return &result return &result
} }
func (c *ProviderConfig) GoString() string {
return fmt.Sprintf("*%#v", *c)
}
func (c *ProviderConfig) FullName() string { func (c *ProviderConfig) FullName() string {
if c.Alias == "" { if c.Alias == "" {
return c.Name return c.Name

View File

@ -697,43 +697,6 @@ func TestContext2Plan_preventDestroy_destroyPlan(t *testing.T) {
} }
} }
func TestContext2Plan_providerAliasMissing(t *testing.T) {
m := testModule(t, "apply-provider-alias-missing")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: rootModulePath,
Resources: map[string]*ResourceState{
"aws_instance.foo": &ResourceState{
Type: "aws_instance",
Provider: "aws.foo",
Primary: &InstanceState{
ID: "bar",
Attributes: map[string]string{
"require_new": "abc",
},
},
},
},
},
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
State: state,
})
if _, err := ctx.Plan(); err == nil {
t.Fatal("should err")
}
}
func TestContext2Plan_computed(t *testing.T) { func TestContext2Plan_computed(t *testing.T) {
m := testModule(t, "plan-computed") m := testModule(t, "plan-computed")
p := testProvider("aws") p := testProvider("aws")
@ -4375,6 +4338,34 @@ func TestContext2Apply_moduleDestroyOrder(t *testing.T) {
} }
} }
func TestContext2Apply_moduleProviderAlias(t *testing.T) {
m := testModule(t, "apply-module-provider-alias")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
if _, err := ctx.Plan(); err != nil {
t.Fatalf("err: %s", err)
}
state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(state.String())
expected := strings.TrimSpace(testTerraformApplyModuleProviderAliasStr)
if actual != expected {
t.Fatalf("bad: \n%s", actual)
}
}
func TestContext2Apply_moduleVarResourceCount(t *testing.T) { func TestContext2Apply_moduleVarResourceCount(t *testing.T) {
m := testModule(t, "apply-module-var-resource-count") m := testModule(t, "apply-module-var-resource-count")
p := testProvider("aws") p := testProvider("aws")

View File

@ -379,6 +379,14 @@ do_instance.foo:
type = do_instance type = do_instance
` `
const testTerraformApplyModuleProviderAliasStr = `
<no state>
module.child:
aws_instance.foo:
ID = foo
provider = aws.eu
`
const testTerraformApplyOutputOrphanStr = ` const testTerraformApplyOutputOrphanStr = `
<no state> <no state>
Outputs: Outputs:

View File

@ -0,0 +1,7 @@
provider "aws" {
alias = "eu"
}
resource "aws_instance" "foo" {
provider = "aws.eu"
}

View File

@ -0,0 +1,3 @@
module "child" {
source = "./child"
}

View File

@ -1,8 +0,0 @@
provider "aws" {
alias = "bar"
}
resource "aws_instance" "bar" {
foo = "bar"
provider = "aws.bar"
}

View File

@ -0,0 +1,3 @@
provider "aws" {}
resource "aws_instance" "web" {}
resource "foo_instance" "web" {}

View File

@ -2,6 +2,7 @@ package terraform
import ( import (
"fmt" "fmt"
"strings"
"github.com/hashicorp/go-multierror" "github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/config"
@ -146,15 +147,44 @@ type MissingProviderTransformer struct {
} }
func (t *MissingProviderTransformer) Transform(g *Graph) error { func (t *MissingProviderTransformer) Transform(g *Graph) error {
// Create a set of our supported providers
supported := make(map[string]struct{}, len(t.Providers))
for _, v := range t.Providers {
supported[v] = struct{}{}
}
// Get the map of providers we already have in our graph
m := providerVertexMap(g) m := providerVertexMap(g)
for _, p := range t.Providers {
// Go through all the provider consumers and make sure we add
// that provider if it is missing.
for _, v := range g.Vertices() {
pv, ok := v.(GraphNodeProviderConsumer)
if !ok {
continue
}
for _, p := range pv.ProvidedBy() {
if _, ok := m[p]; ok { if _, ok := m[p]; ok {
// This provider already exists as a configured node // This provider already exists as a configure node
break
}
// If the provider has an alias in it, we just want the type
ptype := p
if idx := strings.IndexRune(p, '.'); idx != -1 {
ptype = p[:idx]
}
if _, ok := supported[ptype]; !ok {
// If we don't support the provider type, skip it.
// Validation later will catch this as an error.
continue continue
} }
// Add our own missing provider node to the graph // Add our own missing provider node to the graph
g.Add(&graphNodeMissingProvider{ProviderNameValue: p}) m[p] = g.Add(&graphNodeMissingProvider{ProviderNameValue: p})
}
} }
return nil return nil

View File

@ -63,7 +63,7 @@ func TestCloseProviderTransformer(t *testing.T) {
} }
func TestMissingProviderTransformer(t *testing.T) { func TestMissingProviderTransformer(t *testing.T) {
mod := testModule(t, "transform-provider-basic") mod := testModule(t, "transform-provider-missing")
g := Graph{Path: RootModulePath} g := Graph{Path: RootModulePath}
{ {
@ -74,7 +74,7 @@ func TestMissingProviderTransformer(t *testing.T) {
} }
{ {
transform := &MissingProviderTransformer{Providers: []string{"foo"}} transform := &MissingProviderTransformer{Providers: []string{"foo", "bar"}}
if err := transform.Transform(&g); err != nil { if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -275,10 +275,13 @@ provider.aws (close)
const testTransformMissingProviderBasicStr = ` const testTransformMissingProviderBasicStr = `
aws_instance.web aws_instance.web
foo_instance.web
provider.aws provider.aws
provider.aws (close) provider.aws (close)
aws_instance.web aws_instance.web
provider.foo provider.foo
provider.foo (close)
foo_instance.web
` `
const testTransformPruneProviderBasicStr = ` const testTransformPruneProviderBasicStr = `