terraform: missing provider should add missing aliases [GH-2023]
This commit is contained in:
parent
4bf0d5e243
commit
29eadb8194
|
@ -683,6 +683,10 @@ func (o *Output) mergerMerge(m merger) merger {
|
|||
return &result
|
||||
}
|
||||
|
||||
func (c *ProviderConfig) GoString() string {
|
||||
return fmt.Sprintf("*%#v", *c)
|
||||
}
|
||||
|
||||
func (c *ProviderConfig) FullName() string {
|
||||
if c.Alias == "" {
|
||||
return c.Name
|
||||
|
|
|
@ -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) {
|
||||
m := testModule(t, "plan-computed")
|
||||
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) {
|
||||
m := testModule(t, "apply-module-var-resource-count")
|
||||
p := testProvider("aws")
|
||||
|
|
|
@ -379,6 +379,14 @@ do_instance.foo:
|
|||
type = do_instance
|
||||
`
|
||||
|
||||
const testTerraformApplyModuleProviderAliasStr = `
|
||||
<no state>
|
||||
module.child:
|
||||
aws_instance.foo:
|
||||
ID = foo
|
||||
provider = aws.eu
|
||||
`
|
||||
|
||||
const testTerraformApplyOutputOrphanStr = `
|
||||
<no state>
|
||||
Outputs:
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
provider "aws" {
|
||||
alias = "eu"
|
||||
}
|
||||
|
||||
resource "aws_instance" "foo" {
|
||||
provider = "aws.eu"
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
module "child" {
|
||||
source = "./child"
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
provider "aws" {
|
||||
alias = "bar"
|
||||
}
|
||||
|
||||
resource "aws_instance" "bar" {
|
||||
foo = "bar"
|
||||
provider = "aws.bar"
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
provider "aws" {}
|
||||
resource "aws_instance" "web" {}
|
||||
resource "foo_instance" "web" {}
|
|
@ -2,6 +2,7 @@ package terraform
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/go-multierror"
|
||||
"github.com/hashicorp/terraform/config"
|
||||
|
@ -146,15 +147,44 @@ type MissingProviderTransformer struct {
|
|||
}
|
||||
|
||||
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)
|
||||
for _, p := range t.Providers {
|
||||
if _, ok := m[p]; ok {
|
||||
// This provider already exists as a configured node
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// Add our own missing provider node to the graph
|
||||
g.Add(&graphNodeMissingProvider{ProviderNameValue: p})
|
||||
for _, p := range pv.ProvidedBy() {
|
||||
if _, ok := m[p]; ok {
|
||||
// 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
|
||||
}
|
||||
|
||||
// Add our own missing provider node to the graph
|
||||
m[p] = g.Add(&graphNodeMissingProvider{ProviderNameValue: p})
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -63,7 +63,7 @@ func TestCloseProviderTransformer(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestMissingProviderTransformer(t *testing.T) {
|
||||
mod := testModule(t, "transform-provider-basic")
|
||||
mod := testModule(t, "transform-provider-missing")
|
||||
|
||||
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 {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
@ -275,10 +275,13 @@ provider.aws (close)
|
|||
|
||||
const testTransformMissingProviderBasicStr = `
|
||||
aws_instance.web
|
||||
foo_instance.web
|
||||
provider.aws
|
||||
provider.aws (close)
|
||||
aws_instance.web
|
||||
provider.foo
|
||||
provider.foo (close)
|
||||
foo_instance.web
|
||||
`
|
||||
|
||||
const testTransformPruneProviderBasicStr = `
|
||||
|
|
Loading…
Reference in New Issue