From 94e1bab65d381e09f8ba6bb9ca72cd73f9b04be1 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 1 May 2015 15:28:41 -0700 Subject: [PATCH] terraform: fill in more flat interfaces --- terraform/graph_config_node.go | 67 -------------- terraform/graph_config_node_provider.go | 115 ++++++++++++++++++++++++ terraform/graph_config_node_resource.go | 7 ++ terraform/transform_root.go | 4 + 4 files changed, 126 insertions(+), 67 deletions(-) create mode 100644 terraform/graph_config_node_provider.go diff --git a/terraform/graph_config_node.go b/terraform/graph_config_node.go index 341e1a108..ea4645d72 100644 --- a/terraform/graph_config_node.go +++ b/terraform/graph_config_node.go @@ -1,11 +1,7 @@ package terraform import ( - "fmt" - - "github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/dag" - "github.com/hashicorp/terraform/dot" ) // graphNodeConfig is an interface that all graph nodes for the @@ -43,66 +39,3 @@ type GraphNodeTargetable interface { SetTargets([]ResourceAddress) } - -// GraphNodeConfigProvider represents a configured provider within the -// configuration graph. These are only immediately in the graph when an -// explicit `provider` configuration block is in the configuration. -type GraphNodeConfigProvider struct { - Provider *config.ProviderConfig -} - -func (n *GraphNodeConfigProvider) Name() string { - return fmt.Sprintf("provider.%s", n.ProviderName()) -} - -func (n *GraphNodeConfigProvider) ConfigType() GraphNodeConfigType { - return GraphNodeConfigTypeProvider -} - -func (n *GraphNodeConfigProvider) DependableName() []string { - return []string{n.Name()} -} - -func (n *GraphNodeConfigProvider) DependentOn() []string { - vars := n.Provider.RawConfig.Variables - result := make([]string, 0, len(vars)) - for _, v := range vars { - if vn := varNameForVar(v); vn != "" { - result = append(result, vn) - } - } - - return result -} - -// GraphNodeEvalable impl. -func (n *GraphNodeConfigProvider) EvalTree() EvalNode { - return ProviderEvalTree(n.ProviderName(), n.Provider.RawConfig) -} - -// GraphNodeProvider implementation -func (n *GraphNodeConfigProvider) ProviderName() string { - if n.Provider.Alias == "" { - return n.Provider.Name - } else { - return fmt.Sprintf("%s.%s", n.Provider.Name, n.Provider.Alias) - } -} - -// GraphNodeProvider implementation -func (n *GraphNodeConfigProvider) ProviderConfig() *config.RawConfig { - return n.Provider.RawConfig -} - -// GraphNodeDotter impl. -func (n *GraphNodeConfigProvider) DotNode(name string, opts *GraphDotOpts) *dot.Node { - return dot.NewNode(name, map[string]string{ - "label": n.Name(), - "shape": "diamond", - }) -} - -// GraphNodeDotterOrigin impl. -func (n *GraphNodeConfigProvider) DotOrigin() bool { - return true -} diff --git a/terraform/graph_config_node_provider.go b/terraform/graph_config_node_provider.go new file mode 100644 index 000000000..5b8fb1bcb --- /dev/null +++ b/terraform/graph_config_node_provider.go @@ -0,0 +1,115 @@ +package terraform + +import ( + "fmt" + + "github.com/hashicorp/terraform/config" + "github.com/hashicorp/terraform/dag" + "github.com/hashicorp/terraform/dot" +) + +// GraphNodeConfigProvider represents a configured provider within the +// configuration graph. These are only immediately in the graph when an +// explicit `provider` configuration block is in the configuration. +type GraphNodeConfigProvider struct { + Provider *config.ProviderConfig +} + +func (n *GraphNodeConfigProvider) Name() string { + return fmt.Sprintf("provider.%s", n.ProviderName()) +} + +func (n *GraphNodeConfigProvider) ConfigType() GraphNodeConfigType { + return GraphNodeConfigTypeProvider +} + +func (n *GraphNodeConfigProvider) DependableName() []string { + return []string{n.Name()} +} + +func (n *GraphNodeConfigProvider) DependentOn() []string { + vars := n.Provider.RawConfig.Variables + result := make([]string, 0, len(vars)) + for _, v := range vars { + if vn := varNameForVar(v); vn != "" { + result = append(result, vn) + } + } + + return result +} + +// GraphNodeEvalable impl. +func (n *GraphNodeConfigProvider) EvalTree() EvalNode { + return ProviderEvalTree(n.ProviderName(), n.Provider.RawConfig) +} + +// GraphNodeProvider implementation +func (n *GraphNodeConfigProvider) ProviderName() string { + if n.Provider.Alias == "" { + return n.Provider.Name + } else { + return fmt.Sprintf("%s.%s", n.Provider.Name, n.Provider.Alias) + } +} + +// GraphNodeProvider implementation +func (n *GraphNodeConfigProvider) ProviderConfig() *config.RawConfig { + return n.Provider.RawConfig +} + +// GraphNodeDotter impl. +func (n *GraphNodeConfigProvider) DotNode(name string, opts *GraphDotOpts) *dot.Node { + return dot.NewNode(name, map[string]string{ + "label": n.Name(), + "shape": "diamond", + }) +} + +// GraphNodeDotterOrigin impl. +func (n *GraphNodeConfigProvider) DotOrigin() bool { + return true +} + +// GraphNodeFlattenable impl. +func (n *GraphNodeConfigProvider) Flatten(p []string) (dag.Vertex, error) { + return &GraphNodeConfigProviderFlat{ + GraphNodeConfigProvider: n, + PathValue: p, + }, nil +} + +// Same as GraphNodeConfigProvider, but for flattening +type GraphNodeConfigProviderFlat struct { + *GraphNodeConfigProvider + + PathValue []string +} + +func (n *GraphNodeConfigProviderFlat) Name() string { + return fmt.Sprintf( + "%s.%s", modulePrefixStr(n.PathValue), n.GraphNodeConfigProvider.Name()) +} + +func (n *GraphNodeConfigProviderFlat) Path() []string { + return n.PathValue +} + +func (n *GraphNodeConfigProviderFlat) DependableName() []string { + return modulePrefixList( + n.GraphNodeConfigProvider.DependableName(), + modulePrefixStr(n.PathValue)) +} + +func (n *GraphNodeConfigProviderFlat) DependentOn() []string { + prefix := modulePrefixStr(n.PathValue) + return modulePrefixList( + n.GraphNodeConfigProvider.DependentOn(), + prefix) +} + +func (n *GraphNodeConfigProviderFlat) ProviderName() string { + return fmt.Sprintf( + "%s.%s", modulePrefixStr(n.PathValue), + n.GraphNodeConfigProvider.ProviderName()) +} diff --git a/terraform/graph_config_node_resource.go b/terraform/graph_config_node_resource.go index 071120533..0bc189176 100644 --- a/terraform/graph_config_node_resource.go +++ b/terraform/graph_config_node_resource.go @@ -260,6 +260,13 @@ func (n *GraphNodeConfigResourceFlat) DependentOn() []string { prefix) } +func (n *GraphNodeConfigResourceFlat) ProvidedBy() []string { + prefix := modulePrefixStr(n.PathValue) + return modulePrefixList( + n.GraphNodeConfigResource.ProvidedBy(), + prefix) +} + // graphNodeResourceDestroy represents the logical destruction of a // resource. This node doesn't mean it will be destroyed for sure, but // instead that if a destroy were to happen, it must happen at this point. diff --git a/terraform/transform_root.go b/terraform/transform_root.go index 4d04df9be..15de14754 100644 --- a/terraform/transform_root.go +++ b/terraform/transform_root.go @@ -34,3 +34,7 @@ type graphNodeRoot struct{} func (n graphNodeRoot) Name() string { return "root" } + +func (n graphNodeRoot) Flatten(p []string) (dag.Vertex, error) { + return n, nil +}