From fdded8ca148c9333e3e201a5ccc5250de5f6918b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 5 Mar 2015 12:56:31 -0800 Subject: [PATCH] config: allow atlas block --- config/append.go | 6 ++++++ config/append_test.go | 9 +++++++++ config/config.go | 8 ++++++++ config/loader_hcl.go | 23 +++++++++++++++++++++++ config/loader_test.go | 11 +++++++++++ config/merge.go | 7 +++++++ config/merge_test.go | 9 +++++++++ config/test-fixtures/basic.tf | 4 ++++ config/test-fixtures/basic.tf.json | 4 ++++ 9 files changed, 81 insertions(+) diff --git a/config/append.go b/config/append.go index f87e67748..bf13534e7 100644 --- a/config/append.go +++ b/config/append.go @@ -21,6 +21,7 @@ func Append(c1, c2 *Config) (*Config, error) { c.unknownKeys = append(c.unknownKeys, k) } } + for _, k := range c2.unknownKeys { _, present := unknowns[k] if !present { @@ -29,6 +30,11 @@ func Append(c1, c2 *Config) (*Config, error) { } } + c.Atlas = c1.Atlas + if c2.Atlas != nil { + c.Atlas = c2.Atlas + } + if len(c1.Modules) > 0 || len(c2.Modules) > 0 { c.Modules = make( []*Module, 0, len(c1.Modules)+len(c2.Modules)) diff --git a/config/append_test.go b/config/append_test.go index e7aea9d21..adeb7835b 100644 --- a/config/append_test.go +++ b/config/append_test.go @@ -12,6 +12,9 @@ func TestAppend(t *testing.T) { }{ { &Config{ + Atlas: &AtlasConfig{ + Name: "foo", + }, Modules: []*Module{ &Module{Name: "foo"}, }, @@ -32,6 +35,9 @@ func TestAppend(t *testing.T) { }, &Config{ + Atlas: &AtlasConfig{ + Name: "bar", + }, Modules: []*Module{ &Module{Name: "bar"}, }, @@ -52,6 +58,9 @@ func TestAppend(t *testing.T) { }, &Config{ + Atlas: &AtlasConfig{ + Name: "bar", + }, Modules: []*Module{ &Module{Name: "foo"}, &Module{Name: "bar"}, diff --git a/config/config.go b/config/config.go index 8dd9810eb..aabc42260 100644 --- a/config/config.go +++ b/config/config.go @@ -28,6 +28,7 @@ type Config struct { // any meaningful directory. Dir string + Atlas *AtlasConfig Modules []*Module ProviderConfigs []*ProviderConfig Resources []*Resource @@ -39,6 +40,13 @@ type Config struct { unknownKeys []string } +// AtlasConfig is the configuration for building in HashiCorp's Atlas. +type AtlasConfig struct { + Name string + Include []string + Exclude []string +} + // Module is a module used within a configuration. // // This does not represent a module itself, this represents a module diff --git a/config/loader_hcl.go b/config/loader_hcl.go index 6c127ea8b..f75f93df3 100644 --- a/config/loader_hcl.go +++ b/config/loader_hcl.go @@ -17,6 +17,7 @@ type hclConfigurable struct { func (t *hclConfigurable) Config() (*Config, error) { validKeys := map[string]struct{}{ + "atlas": struct{}{}, "module": struct{}{}, "output": struct{}{}, "provider": struct{}{}, @@ -70,6 +71,15 @@ func (t *hclConfigurable) Config() (*Config, error) { } } + // Get Atlas configuration + if atlas := t.Object.Get("atlas", false); atlas != nil { + var err error + config.Atlas, err = loadAtlasHcl(atlas) + if err != nil { + return nil, err + } + } + // Build the modules if modules := t.Object.Get("module", false); modules != nil { var err error @@ -187,6 +197,19 @@ func loadFileHcl(root string) (configurable, []string, error) { return result, nil, nil } +// Given a handle to a HCL object, this transforms it into the Atlas +// configuration. +func loadAtlasHcl(obj *hclobj.Object) (*AtlasConfig, error) { + var config AtlasConfig + if err := hcl.DecodeObject(&config, obj); err != nil { + return nil, fmt.Errorf( + "Error reading atlas config: %s", + err) + } + + return &config, nil +} + // Given a handle to a HCL object, this recurses into the structure // and pulls out a list of modules. // diff --git a/config/loader_test.go b/config/loader_test.go index 39fea0296..d487638e9 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -2,6 +2,7 @@ package config import ( "path/filepath" + "reflect" "strings" "testing" ) @@ -57,6 +58,11 @@ func TestLoadBasic(t *testing.T) { t.Fatalf("bad: %#v", c.Dir) } + expectedAtlas := &AtlasConfig{Name: "mitchellh/foo"} + if !reflect.DeepEqual(c.Atlas, expectedAtlas) { + t.Fatalf("bad: %#v", c.Atlas) + } + actual := variablesStr(c.Variables) if actual != strings.TrimSpace(basicVariablesStr) { t.Fatalf("bad:\n%s", actual) @@ -132,6 +138,11 @@ func TestLoadBasic_json(t *testing.T) { t.Fatalf("bad: %#v", c.Dir) } + expectedAtlas := &AtlasConfig{Name: "mitchellh/foo"} + if !reflect.DeepEqual(c.Atlas, expectedAtlas) { + t.Fatalf("bad: %#v", c.Atlas) + } + actual := variablesStr(c.Variables) if actual != strings.TrimSpace(basicVariablesStr) { t.Fatalf("bad:\n%s", actual) diff --git a/config/merge.go b/config/merge.go index c43f13c04..f72fdfa92 100644 --- a/config/merge.go +++ b/config/merge.go @@ -25,6 +25,13 @@ func Merge(c1, c2 *Config) (*Config, error) { } } + // Merge Atlas configuration. This is a dumb one overrides the other + // sort of merge. + c.Atlas = c1.Atlas + if c2.Atlas != nil { + c.Atlas = c2.Atlas + } + // NOTE: Everything below is pretty gross. Due to the lack of generics // in Go, there is some hoop-jumping involved to make this merging a // little more test-friendly and less repetitive. Ironically, making it diff --git a/config/merge_test.go b/config/merge_test.go index 2dbe5aee9..40144f0c7 100644 --- a/config/merge_test.go +++ b/config/merge_test.go @@ -13,6 +13,9 @@ func TestMerge(t *testing.T) { // Normal good case. { &Config{ + Atlas: &AtlasConfig{ + Name: "foo", + }, Modules: []*Module{ &Module{Name: "foo"}, }, @@ -33,6 +36,9 @@ func TestMerge(t *testing.T) { }, &Config{ + Atlas: &AtlasConfig{ + Name: "bar", + }, Modules: []*Module{ &Module{Name: "bar"}, }, @@ -53,6 +59,9 @@ func TestMerge(t *testing.T) { }, &Config{ + Atlas: &AtlasConfig{ + Name: "bar", + }, Modules: []*Module{ &Module{Name: "foo"}, &Module{Name: "bar"}, diff --git a/config/test-fixtures/basic.tf b/config/test-fixtures/basic.tf index 5751a8583..5afadc779 100644 --- a/config/test-fixtures/basic.tf +++ b/config/test-fixtures/basic.tf @@ -49,3 +49,7 @@ resource "aws_instance" "db" { output "web_ip" { value = "${aws_instance.web.private_ip}" } + +atlas { + name = "mitchellh/foo" +} diff --git a/config/test-fixtures/basic.tf.json b/config/test-fixtures/basic.tf.json index 1b946e22b..1013862b3 100644 --- a/config/test-fixtures/basic.tf.json +++ b/config/test-fixtures/basic.tf.json @@ -63,5 +63,9 @@ "web_ip": { "value": "${aws_instance.web.private_ip}" } + }, + + "atlas": { + "name": "mitchellh/foo" } }