Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit c62370f

Browse files
author
Sander van Harmelen
committed
Add a function to load JSON directly
Without this 12 line function it’s impossible to use any of the Terraform code without the need for having the files on disk. As more and more people are using (parts of) Terraform in other software, this seems to be a very welcome addition. It has no negative impact on Terraform itself whatsoever (the function is never called), but it opens up a lot of other use cases. Next to the single new function, I renamed the existing function (and related tests) to better reflect what the function does. So now there is a `LoadDir` function which calls `LoadFile` for each file, which kind of made sense to me, especially when now adding a `LoadJSON` function as well. But of course if the rename is a problem, I can revert that part as it’s not related to the added `LoadJSON` function. Thanks!
1 parent ce8baea commit c62370f

4 files changed

Lines changed: 95 additions & 27 deletions

File tree

config/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ func TestVariableDefaultsMap(t *testing.T) {
450450
}
451451

452452
func testConfig(t *testing.T, name string) *Config {
453-
c, err := Load(filepath.Join(fixtureDir, name, "main.tf"))
453+
c, err := LoadFile(filepath.Join(fixtureDir, name, "main.tf"))
454454
if err != nil {
455455
t.Fatalf("file: %s\n\nerr: %s", name, err)
456456
}

config/loader.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,41 @@
11
package config
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"io"
67
"os"
78
"path/filepath"
89
"sort"
910
"strings"
11+
12+
"github.com/hashicorp/hcl"
1013
)
1114

12-
// Load loads the Terraform configuration from a given file.
15+
// LoadJSON loads a single Terraform configuration from a given JSON document.
16+
//
17+
// The document must be a complete Terraform configuration. This function will
18+
// NOT try to load any additional modules so only the given document is loaded.
19+
func LoadJSON(raw json.RawMessage) (*Config, error) {
20+
obj, err := hcl.Parse(string(raw))
21+
if err != nil {
22+
return nil, fmt.Errorf(
23+
"Error parsing JSON document as HCL: %s", err)
24+
}
25+
26+
// Start building the result
27+
hclConfig := &hclConfigurable{
28+
Object: obj,
29+
}
30+
31+
return hclConfig.Config()
32+
}
33+
34+
// LoadFile loads the Terraform configuration from a given file.
1335
//
1436
// This file can be any format that Terraform recognizes, and import any
1537
// other format that Terraform recognizes.
16-
func Load(path string) (*Config, error) {
38+
func LoadFile(path string) (*Config, error) {
1739
importTree, err := loadTree(path)
1840
if err != nil {
1941
return nil, err
@@ -66,7 +88,7 @@ func LoadDir(root string) (*Config, error) {
6688

6789
// Load all the regular files, append them to each other.
6890
for _, f := range files {
69-
c, err := Load(f)
91+
c, err := LoadFile(f)
7092
if err != nil {
7193
return nil, err
7294
}
@@ -83,7 +105,7 @@ func LoadDir(root string) (*Config, error) {
83105

84106
// Load all the overrides, and merge them into the config
85107
for _, f := range overrides {
86-
c, err := Load(f)
108+
c, err := LoadFile(f)
87109
if err != nil {
88110
return nil, err
89111
}

config/loader_test.go

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"io/ioutil"
45
"path/filepath"
56
"reflect"
67
"strings"
@@ -37,15 +38,15 @@ func TestIsEmptyDir_noConfigs(t *testing.T) {
3738
}
3839
}
3940

40-
func TestLoad_badType(t *testing.T) {
41-
_, err := Load(filepath.Join(fixtureDir, "bad_type.tf.nope"))
41+
func TestLoadFile_badType(t *testing.T) {
42+
_, err := LoadFile(filepath.Join(fixtureDir, "bad_type.tf.nope"))
4243
if err == nil {
4344
t.Fatal("should have error")
4445
}
4546
}
4647

47-
func TestLoadBasic(t *testing.T) {
48-
c, err := Load(filepath.Join(fixtureDir, "basic.tf"))
48+
func TestLoadFileBasic(t *testing.T) {
49+
c, err := LoadFile(filepath.Join(fixtureDir, "basic.tf"))
4950
if err != nil {
5051
t.Fatalf("err: %s", err)
5152
}
@@ -84,8 +85,8 @@ func TestLoadBasic(t *testing.T) {
8485
}
8586
}
8687

87-
func TestLoadBasic_empty(t *testing.T) {
88-
c, err := Load(filepath.Join(fixtureDir, "empty.tf"))
88+
func TestLoadFileBasic_empty(t *testing.T) {
89+
c, err := LoadFile(filepath.Join(fixtureDir, "empty.tf"))
8990
if err != nil {
9091
t.Fatalf("err: %s", err)
9192
}
@@ -95,11 +96,11 @@ func TestLoadBasic_empty(t *testing.T) {
9596
}
9697
}
9798

98-
func TestLoadBasic_import(t *testing.T) {
99+
func TestLoadFileBasic_import(t *testing.T) {
99100
// Skip because we disabled importing
100101
t.Skip()
101102

102-
c, err := Load(filepath.Join(fixtureDir, "import.tf"))
103+
c, err := LoadFile(filepath.Join(fixtureDir, "import.tf"))
103104
if err != nil {
104105
t.Fatalf("err: %s", err)
105106
}
@@ -124,8 +125,8 @@ func TestLoadBasic_import(t *testing.T) {
124125
}
125126
}
126127

127-
func TestLoadBasic_json(t *testing.T) {
128-
c, err := Load(filepath.Join(fixtureDir, "basic.tf.json"))
128+
func TestLoadFileBasic_json(t *testing.T) {
129+
c, err := LoadFile(filepath.Join(fixtureDir, "basic.tf.json"))
129130
if err != nil {
130131
t.Fatalf("err: %s", err)
131132
}
@@ -164,8 +165,8 @@ func TestLoadBasic_json(t *testing.T) {
164165
}
165166
}
166167

167-
func TestLoadBasic_modules(t *testing.T) {
168-
c, err := Load(filepath.Join(fixtureDir, "modules.tf"))
168+
func TestLoadFileBasic_modules(t *testing.T) {
169+
c, err := LoadFile(filepath.Join(fixtureDir, "modules.tf"))
169170
if err != nil {
170171
t.Fatalf("err: %s", err)
171172
}
@@ -184,8 +185,53 @@ func TestLoadBasic_modules(t *testing.T) {
184185
}
185186
}
186187

187-
func TestLoad_variables(t *testing.T) {
188-
c, err := Load(filepath.Join(fixtureDir, "variables.tf"))
188+
func TestLoadJSONBasic(t *testing.T) {
189+
raw, err := ioutil.ReadFile(filepath.Join(fixtureDir, "basic.tf.json"))
190+
if err != nil {
191+
t.Fatalf("err: %s", err)
192+
}
193+
194+
c, err := LoadJSON(raw)
195+
if err != nil {
196+
t.Fatalf("err: %s", err)
197+
}
198+
199+
if c == nil {
200+
t.Fatal("config should not be nil")
201+
}
202+
203+
if c.Dir != "" {
204+
t.Fatalf("bad: %#v", c.Dir)
205+
}
206+
207+
expectedAtlas := &AtlasConfig{Name: "mitchellh/foo"}
208+
if !reflect.DeepEqual(c.Atlas, expectedAtlas) {
209+
t.Fatalf("bad: %#v", c.Atlas)
210+
}
211+
212+
actual := variablesStr(c.Variables)
213+
if actual != strings.TrimSpace(basicVariablesStr) {
214+
t.Fatalf("bad:\n%s", actual)
215+
}
216+
217+
actual = providerConfigsStr(c.ProviderConfigs)
218+
if actual != strings.TrimSpace(basicProvidersStr) {
219+
t.Fatalf("bad:\n%s", actual)
220+
}
221+
222+
actual = resourcesStr(c.Resources)
223+
if actual != strings.TrimSpace(basicResourcesStr) {
224+
t.Fatalf("bad:\n%s", actual)
225+
}
226+
227+
actual = outputsStr(c.Outputs)
228+
if actual != strings.TrimSpace(basicOutputsStr) {
229+
t.Fatalf("bad:\n%s", actual)
230+
}
231+
}
232+
233+
func TestLoadFile_variables(t *testing.T) {
234+
c, err := LoadFile(filepath.Join(fixtureDir, "variables.tf"))
189235
if err != nil {
190236
t.Fatalf("err: %s", err)
191237
}
@@ -303,8 +349,8 @@ func TestLoadDir_override(t *testing.T) {
303349
}
304350
}
305351

306-
func TestLoad_provisioners(t *testing.T) {
307-
c, err := Load(filepath.Join(fixtureDir, "provisioners.tf"))
352+
func TestLoadFile_provisioners(t *testing.T) {
353+
c, err := LoadFile(filepath.Join(fixtureDir, "provisioners.tf"))
308354
if err != nil {
309355
t.Fatalf("err: %s", err)
310356
}
@@ -319,8 +365,8 @@ func TestLoad_provisioners(t *testing.T) {
319365
}
320366
}
321367

322-
func TestLoad_connections(t *testing.T) {
323-
c, err := Load(filepath.Join(fixtureDir, "connection.tf"))
368+
func TestLoadFile_connections(t *testing.T) {
369+
c, err := LoadFile(filepath.Join(fixtureDir, "connection.tf"))
324370
if err != nil {
325371
t.Fatalf("err: %s", err)
326372
}
@@ -357,8 +403,8 @@ func TestLoad_connections(t *testing.T) {
357403
}
358404
}
359405

360-
func TestLoad_createBeforeDestroy(t *testing.T) {
361-
c, err := Load(filepath.Join(fixtureDir, "create-before-destroy.tf"))
406+
func TestLoadFile_createBeforeDestroy(t *testing.T) {
407+
c, err := LoadFile(filepath.Join(fixtureDir, "create-before-destroy.tf"))
362408
if err != nil {
363409
t.Fatalf("err: %s", err)
364410
}
@@ -394,7 +440,7 @@ func TestLoad_createBeforeDestroy(t *testing.T) {
394440
}
395441
}
396442

397-
func TestLoad_temporary_files(t *testing.T) {
443+
func TestLoadDir_temporary_files(t *testing.T) {
398444
_, err := LoadDir(filepath.Join(fixtureDir, "dir-temporary-files"))
399445
if err == nil {
400446
t.Fatalf("Expected to see an error stating no config files found")

terraform/terraform_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func tempEnv(t *testing.T, k string, v string) string {
5656
}
5757

5858
func testConfig(t *testing.T, name string) *config.Config {
59-
c, err := config.Load(filepath.Join(fixtureDir, name, "main.tf"))
59+
c, err := config.LoadFile(filepath.Join(fixtureDir, name, "main.tf"))
6060
if err != nil {
6161
t.Fatalf("err: %s", err)
6262
}

0 commit comments

Comments
 (0)