-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
terraform: missing provider should add missing aliases [GH-2023] #2475
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
provider "aws" { | ||
alias = "eu" | ||
} | ||
|
||
resource "aws_instance" "foo" { | ||
provider = "aws.eu" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module "child" { | ||
source = "./child" | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
provider "aws" {} | ||
resource "aws_instance" "web" {} | ||
resource "foo_instance" "web" {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do we know the provider has an alias in it here? At first I was worried about what would happen if somebody did There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, we just check for the "." and assume it exists because proper smeantic checks like that should happen earlier. |
||
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}) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was the key change. Before we would just blindly add every supported provider as a missing provider. But now we actually go through, find what providers we need, and add them each in turn. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 I like this more direct approach. |
||
} | ||
|
||
return nil | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole test was testing the opposite of the behavior I added. It doesn't make a lot of sense to me that this should be an error, because it breaks modules completely. I think we just default it like every other provider type.