config: use the RawConfig whereever posible

This commit is contained in:
Mitchell Hashimoto 2014-06-12 17:40:59 -07:00
parent 6420e4bd81
commit 8c50aa6382
4 changed files with 16 additions and 131 deletions

View File

@ -22,8 +22,7 @@ type Config struct {
// For example, Terraform needs to set the AWS access keys for the AWS
// resource provider.
type ProviderConfig struct {
Config map[string]interface{}
Variables map[string]InterpolatedVariable
RawConfig *RawConfig
}
// A resource represents a single Terraform resource in the configuration.
@ -32,8 +31,7 @@ type ProviderConfig struct {
type Resource struct {
Name string
Type string
Config map[string]interface{}
Variables map[string]InterpolatedVariable
RawConfig *RawConfig
}
// Variable is a variable defined within the configuration.
@ -72,20 +70,6 @@ type UserVariable struct {
key string
}
// ReplaceVariables replaces the variables in the configuration
// with the given values.
//
// This replacement is not in place. Instead, this function will
// return a new resource with the variables replaced.
func (r *ProviderConfig) ReplaceVariables(
vs map[string]string) *ProviderConfig {
result := *r
if err := replaceVariables(result.Config, vs); err != nil {
panic(err)
}
return &result
}
// A unique identifier for this resource.
func (r *Resource) Id() string {
return fmt.Sprintf("%s.%s", r.Type, r.Name)
@ -105,19 +89,6 @@ func (r *Resource) ProviderConfigName(pcs map[string]*ProviderConfig) string {
return lk
}
// ReplaceVariables replaces the variables in the configuration
// with the given values.
//
// This replacement is not in place. Instead, this function will
// return a new resource with the variables replaced.
func (r *Resource) ReplaceVariables(vs map[string]string) *Resource {
result := *r
if err := replaceVariables(result.Config, vs); err != nil {
panic(err)
}
return &result
}
// Graph returns a dependency graph of the resources from this
// Terraform configuration.
//
@ -154,9 +125,9 @@ func (c *Config) Graph() *depgraph.Graph {
var vars map[string]InterpolatedVariable
switch n := noun.Meta.(type) {
case *Resource:
vars = n.Variables
vars = n.RawConfig.Variables
case *ProviderConfig:
vars = n.Variables
vars = n.RawConfig.Variables
}
for _, v := range vars {
// Only resource variables impose dependencies

View File

@ -2,7 +2,6 @@ package config
import (
"path/filepath"
"reflect"
"strings"
"testing"
)
@ -76,45 +75,6 @@ func TestNewUserVariable(t *testing.T) {
}
}
func TestProviderConfigReplaceVariables(t *testing.T) {
r := &ProviderConfig{
Config: map[string]interface{}{
"foo": "${var.bar}",
},
}
values := map[string]string{
"var.bar": "value",
}
r2 := r.ReplaceVariables(values)
expected := &ProviderConfig{
Config: map[string]interface{}{
"foo": "value",
},
}
if !reflect.DeepEqual(r2, expected) {
t.Fatalf("bad: %#v", r2)
}
/*
TODO(mitchellh): Eventually, preserve original config...
expectedOriginal := &Resource{
Name: "foo",
Type: "bar",
Config: map[string]interface{}{
"foo": "${var.bar}",
},
}
if !reflect.DeepEqual(r, expectedOriginal) {
t.Fatalf("bad: %#v", r)
}
*/
}
func TestResourceProviderConfigName(t *testing.T) {
r := &Resource{
Name: "foo",
@ -134,49 +94,6 @@ func TestResourceProviderConfigName(t *testing.T) {
}
}
func TestResourceReplaceVariables(t *testing.T) {
r := &Resource{
Name: "foo",
Type: "bar",
Config: map[string]interface{}{
"foo": "${var.bar}",
},
}
values := map[string]string{
"var.bar": "value",
}
r2 := r.ReplaceVariables(values)
expected := &Resource{
Name: "foo",
Type: "bar",
Config: map[string]interface{}{
"foo": "value",
},
}
if !reflect.DeepEqual(r2, expected) {
t.Fatalf("bad: %#v", r2)
}
/*
TODO(mitchellh): Eventually, preserve original config...
expectedOriginal := &Resource{
Name: "foo",
Type: "bar",
Config: map[string]interface{}{
"foo": "${var.bar}",
},
}
if !reflect.DeepEqual(r, expectedOriginal) {
t.Fatalf("bad: %#v", r)
}
*/
}
const resourceGraphValue = `
root: root
openstack_floating_ip.random

View File

@ -5,7 +5,6 @@ import (
"path/filepath"
"github.com/mitchellh/go-libucl"
"github.com/mitchellh/reflectwalk"
)
// Put the parse flags we use for libucl in a constant so we can get
@ -175,8 +174,8 @@ func loadProvidersLibucl(o *libucl.Object) (map[string]*ProviderConfig, error) {
return nil, err
}
walker := new(variableDetectWalker)
if err := reflectwalk.Walk(config, walker); err != nil {
rawConfig, err := NewRawConfig(config)
if err != nil {
return nil, fmt.Errorf(
"Error reading config for provider config %s: %s",
n,
@ -184,8 +183,7 @@ func loadProvidersLibucl(o *libucl.Object) (map[string]*ProviderConfig, error) {
}
result[n] = &ProviderConfig{
Config: config,
Variables: walker.Variables,
RawConfig: rawConfig,
}
}
@ -255,8 +253,8 @@ func loadResourcesLibucl(o *libucl.Object) ([]*Resource, error) {
err)
}
walker := new(variableDetectWalker)
if err := reflectwalk.Walk(config, walker); err != nil {
rawConfig, err := NewRawConfig(config)
if err != nil {
return nil, fmt.Errorf(
"Error reading config for %s[%s]: %s",
t.Key(),
@ -267,8 +265,7 @@ func loadResourcesLibucl(o *libucl.Object) ([]*Resource, error) {
result = append(result, &Resource{
Name: r.Key(),
Type: t.Key(),
Config: config,
Variables: walker.Variables,
RawConfig: rawConfig,
})
}
}

View File

@ -98,13 +98,13 @@ func providerConfigsStr(pcs map[string]*ProviderConfig) string {
for n, pc := range pcs {
result += fmt.Sprintf("%s\n", n)
for k, _ := range pc.Config {
for k, _ := range pc.RawConfig.Raw {
result += fmt.Sprintf(" %s\n", k)
}
if len(pc.Variables) > 0 {
if len(pc.RawConfig.Variables) > 0 {
result += fmt.Sprintf(" vars\n")
for _, rawV := range pc.Variables {
for _, rawV := range pc.RawConfig.Variables {
kind := "unknown"
str := rawV.FullKey()
@ -133,13 +133,13 @@ func resourcesStr(rs []*Resource) string {
r.Type,
r.Name)
for k, _ := range r.Config {
for k, _ := range r.RawConfig.Raw {
result += fmt.Sprintf(" %s\n", k)
}
if len(r.Variables) > 0 {
if len(r.RawConfig.Variables) > 0 {
result += fmt.Sprintf(" vars\n")
for _, rawV := range r.Variables {
for _, rawV := range r.RawConfig.Variables {
kind := "unknown"
str := rawV.FullKey()