Skip to content

Commit 2b82646

Browse files
Merge pull request #17 from jumppad-labs/restructure
Restructure to remove sdk dependencies on cty
2 parents 849dbcf + cc6b8c9 commit 2b82646

20 files changed

+202
-144
lines changed

config.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/hashicorp/hcl/v2"
1515
"github.com/hashicorp/hcl/v2/hclsyntax"
1616
"github.com/jumppad-labs/hclconfig/errors"
17+
"github.com/jumppad-labs/hclconfig/resources"
1718
"github.com/jumppad-labs/hclconfig/types"
1819
"github.com/silas/dag"
1920
)
@@ -82,7 +83,7 @@ func (c *Config) FindResource(path string) (types.Resource, error) {
8283

8384
// local version of FindResource that does not lock the config
8485
func (c *Config) findResource(path string) (types.Resource, error) {
85-
fqdn, err := types.ParseFQRN(path)
86+
fqdn, err := resources.ParseFQRN(path)
8687
if err != nil {
8788
return nil, err
8889
}
@@ -108,7 +109,7 @@ func (c *Config) FindRelativeResource(path string, parentModule string) (types.R
108109
c.sync.Lock()
109110
defer c.sync.Unlock()
110111

111-
fqdn, err := types.ParseFQRN(path)
112+
fqdn, err := resources.ParseFQRN(path)
112113
if err != nil {
113114
return nil, err
114115
}
@@ -157,12 +158,12 @@ func (c *Config) FindModuleResources(module string, includeSubModules bool) ([]t
157158
c.sync.Lock()
158159
defer c.sync.Unlock()
159160

160-
fqdn, err := types.ParseFQRN(module)
161+
fqdn, err := resources.ParseFQRN(module)
161162
if err != nil {
162163
return nil, err
163164
}
164165

165-
if fqdn.Type != types.TypeModule {
166+
if fqdn.Type != resources.TypeModule {
166167
return nil, fmt.Errorf("resource %s is not a module reference", module)
167168
}
168169

@@ -206,7 +207,7 @@ func (c *Config) AppendResourcesFromConfig(new *Config) error {
206207
defer c.sync.Unlock()
207208

208209
for _, r := range new.Resources {
209-
fqdn := types.FQDNFromResource(r).String()
210+
fqdn := resources.FQRNFromResource(r).String()
210211

211212
// does the resource already exist?
212213
if _, err := c.findResource(fqdn); err == nil {
@@ -420,7 +421,7 @@ func (c *Config) Walk(wf WalkCallback, reverse bool) error {
420421
}
421422

422423
// if this is the root module or is disabled skip
423-
if (r.Metadata().Type == types.TypeRoot || r.Metadata().Type == types.TypeModule) || r.GetDisabled() {
424+
if (r.Metadata().Type == resources.TypeRoot || r.Metadata().Type == resources.TypeModule) || r.GetDisabled() {
424425
return nil
425426
}
426427

@@ -493,7 +494,7 @@ func (c *Config) walk(wf dag.WalkFunc, reverse bool) []error {
493494
}
494495

495496
func (c *Config) addResource(r types.Resource, ctx *hcl.EvalContext, b *hclsyntax.Body) error {
496-
fqdn := types.FQDNFromResource(r)
497+
fqdn := resources.FQRNFromResource(r)
497498

498499
// set the ID
499500
r.Metadata().ID = fqdn.String()

config_test.go

+16-15
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ import (
55
"sync"
66
"testing"
77

8+
"github.com/jumppad-labs/hclconfig/resources"
89
"github.com/jumppad-labs/hclconfig/test_fixtures/structs"
910
"github.com/jumppad-labs/hclconfig/types"
1011
"github.com/stretchr/testify/require"
1112
)
1213

1314
func testSetupConfig(t *testing.T) (*Config, []types.Resource) {
14-
typs := types.DefaultTypes()
15+
typs := resources.DefaultResources()
1516
typs[structs.TypeNetwork] = &structs.Network{}
1617
typs[structs.TypeContainer] = &structs.Container{}
1718
typs[structs.TypeTemplate] = &structs.Template{}
1819

19-
var1, _ := typs.CreateResource(types.TypeVariable, "var1")
20+
var1, _ := typs.CreateResource(resources.TypeVariable, "var1")
2021
var1.Metadata().Checksum = types.Checksum{
2122
Parsed: "123",
2223
Processed: "abc",
@@ -28,21 +29,21 @@ func testSetupConfig(t *testing.T) (*Config, []types.Resource) {
2829
Processed: "bcd",
2930
}
3031

31-
mod1, _ := typs.CreateResource(types.TypeModule, "module1")
32+
mod1, _ := typs.CreateResource(resources.TypeModule, "module1")
3233
mod1.SetDependsOn([]string{"resource.network.cloud"})
3334
mod1.Metadata().Checksum = types.Checksum{
3435
Parsed: "345",
3536
Processed: "cde",
3637
}
3738

38-
var2, _ := typs.CreateResource(types.TypeVariable, "var2")
39+
var2, _ := typs.CreateResource(resources.TypeVariable, "var2")
3940
var2.Metadata().Module = "module1"
4041
var2.Metadata().Checksum = types.Checksum{
4142
Parsed: "456",
4243
Processed: "def",
4344
}
4445

45-
mod2, _ := typs.CreateResource(types.TypeModule, "module2")
46+
mod2, _ := typs.CreateResource(resources.TypeModule, "module2")
4647
mod2.Metadata().Module = "module1"
4748
mod2.Metadata().Checksum = types.Checksum{
4849
Parsed: "567",
@@ -87,14 +88,14 @@ func testSetupConfig(t *testing.T) (*Config, []types.Resource) {
8788
Processed: "ijk",
8889
}
8990

90-
out1, _ := typs.CreateResource(types.TypeOutput, "fqdn")
91+
out1, _ := typs.CreateResource(resources.TypeOutput, "fqdn")
9192
out1.Metadata().Module = "module1.module2"
9293
out1.Metadata().Checksum = types.Checksum{
9394
Parsed: "0ab",
9495
Processed: "jkl",
9596
}
9697

97-
out2, _ := typs.CreateResource(types.TypeOutput, "out")
98+
out2, _ := typs.CreateResource(resources.TypeOutput, "out")
9899
out2.SetDependsOn([]string{"resource.network.cloud.id", "resource.container.test_dev"})
99100
out2.Metadata().Checksum = types.Checksum{
100101
Parsed: "abc",
@@ -293,7 +294,7 @@ func TestRemoveResourceRemoves(t *testing.T) {
293294
}
294295

295296
func TestRemoveResourceNotFoundReturnsError(t *testing.T) {
296-
typs := types.DefaultTypes()
297+
typs := resources.DefaultResources()
297298
typs[structs.TypeNetwork] = &structs.Network{}
298299

299300
c, _ := testSetupConfig(t)
@@ -315,7 +316,7 @@ func TestToJSONSerializesJSON(t *testing.T) {
315316
}
316317

317318
func TestAppendResourcesMerges(t *testing.T) {
318-
typs := types.DefaultTypes()
319+
typs := resources.DefaultResources()
319320
typs[structs.TypeNetwork] = &structs.Network{}
320321

321322
c, _ := testSetupConfig(t)
@@ -334,7 +335,7 @@ func TestAppendResourcesMerges(t *testing.T) {
334335
}
335336

336337
func TestAppendResourcesWhenExistsReturnsError(t *testing.T) {
337-
typs := types.DefaultTypes()
338+
typs := resources.DefaultResources()
338339
typs[structs.TypeNetwork] = &structs.Network{}
339340

340341
c, _ := testSetupConfig(t)
@@ -357,7 +358,7 @@ func TestProcessForwardExecutesCallbacksInCorrectOrder(t *testing.T) {
357358
func(r types.Resource) error {
358359
callSync.Lock()
359360

360-
calls = append(calls, types.ResourceFQRN{
361+
calls = append(calls, resources.FQRN{
361362
Module: r.Metadata().Module,
362363
Resource: r.Metadata().Name,
363364
Type: r.Metadata().Type,
@@ -389,7 +390,7 @@ func TestProcessReverseExecutesCallbacksInCorrectOrder(t *testing.T) {
389390
func(r types.Resource) error {
390391
callSync.Lock()
391392

392-
calls = append(calls, types.ResourceFQRN{
393+
calls = append(calls, resources.FQRN{
393394
Module: r.Metadata().Module,
394395
Resource: r.Metadata().Name,
395396
Type: r.Metadata().Type,
@@ -418,7 +419,7 @@ func TestProcessCallbackErrorHaltsExecution(t *testing.T) {
418419
err := c.Walk(
419420
func(r types.Resource) error {
420421
callSync.Lock()
421-
calls = append(calls, types.ResourceFQRN{
422+
calls = append(calls, resources.FQRN{
422423
Module: r.Metadata().Module,
423424
Resource: r.Metadata().Name,
424425
Type: r.Metadata().Type,
@@ -458,8 +459,8 @@ func TestDiffReturnsResourcesAdded(t *testing.T) {
458459
c, _ := testSetupConfig(t)
459460
new := copyConfig(t, c)
460461

461-
typs := types.DefaultTypes()
462-
var1, _ := typs.CreateResource(types.TypeVariable, "var22")
462+
typs := resources.DefaultResources()
463+
var1, _ := typs.CreateResource(resources.TypeVariable, "var22")
463464
var1.Metadata().Checksum = types.Checksum{
464465
Parsed: "zzz",
465466
Processed: "111",

dag.go

+21-20
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/jumppad-labs/hclconfig/convert"
1010
"github.com/jumppad-labs/hclconfig/errors"
11+
"github.com/jumppad-labs/hclconfig/resources"
1112
"github.com/jumppad-labs/hclconfig/types"
1213
"github.com/silas/dag"
1314
"github.com/zclconf/go-cty/cty"
@@ -21,13 +22,13 @@ func doYaLikeDAGs(c *Config) (*dag.AcyclicGraph, error) {
2122
graph := &dag.AcyclicGraph{}
2223

2324
// add a root node for the graph
24-
root, _ := types.DefaultTypes().CreateResource(types.TypeRoot, "root")
25+
root, _ := resources.DefaultResources().CreateResource(resources.TypeRoot, "root")
2526
graph.Add(root)
2627

2728
// Loop over all resources and add to graph
2829
for _, resource := range c.Resources {
2930
// ignore variables
30-
if resource.Metadata().Type != types.TypeVariable {
31+
if resource.Metadata().Type != resources.TypeVariable {
3132
graph.Add(resource)
3233
}
3334
}
@@ -37,7 +38,7 @@ func doYaLikeDAGs(c *Config) (*dag.AcyclicGraph, error) {
3738
hasDeps := false
3839

3940
// do nothing with variables
40-
if resource.Metadata().Type == types.TypeVariable {
41+
if resource.Metadata().Type == resources.TypeVariable {
4142
continue
4243
}
4344

@@ -62,7 +63,7 @@ func doYaLikeDAGs(c *Config) (*dag.AcyclicGraph, error) {
6263

6364
for _, d := range resource.GetDependsOn() {
6465
var err error
65-
fqdn, err := types.ParseFQRN(d)
66+
fqdn, err := resources.ParseFQRN(d)
6667
if err != nil {
6768
pe := errors.ParserError{}
6869
pe.Line = resource.Metadata().Line
@@ -75,7 +76,7 @@ func doYaLikeDAGs(c *Config) (*dag.AcyclicGraph, error) {
7576
}
7677

7778
// when the dependency is a module, depend on all resources in the module
78-
if fqdn.Type == types.TypeModule {
79+
if fqdn.Type == resources.TypeModule {
7980
// assume that all dependencies references have been written with no
8081
// knowledge of their parent module. Therefore if the parent module is
8182
// "module1" and the reference is "module.module2.resource.container.mine.id"
@@ -100,7 +101,7 @@ func doYaLikeDAGs(c *Config) (*dag.AcyclicGraph, error) {
100101
}
101102

102103
// when the dependency is a resource, depend on the resource
103-
if fqdn.Type != types.TypeModule {
104+
if fqdn.Type != resources.TypeModule {
104105
// assume that all dependencies references have been written with no
105106
// knowledge of their parent module. Therefore if the parent module is
106107
// "module1" and the reference is "module.module2.resource.container.mine.id"
@@ -173,7 +174,7 @@ func createCallback(c *Config, wf WalkCallback) func(v dag.Vertex) (diags dag.Di
173174
}
174175

175176
// if this is the root module or is disabled skip or is a variable
176-
if (r.Metadata().Type == types.TypeRoot) || r.GetDisabled() || r.Metadata().Type == types.TypeVariable {
177+
if (r.Metadata().Type == resources.TypeRoot) || r.GetDisabled() || r.Metadata().Type == resources.TypeVariable {
177178
return nil
178179
}
179180

@@ -191,7 +192,7 @@ func createCallback(c *Config, wf WalkCallback) func(v dag.Vertex) (diags dag.Di
191192
// all linked values should now have been processed as the graph
192193
// will have handled them first
193194
for _, v := range r.Metadata().Links {
194-
fqrn, err := types.ParseFQRN(v)
195+
fqrn, err := resources.ParseFQRN(v)
195196
if err != nil {
196197
pe := errors.ParserError{}
197198
pe.Filename = r.Metadata().File
@@ -221,11 +222,11 @@ func createCallback(c *Config, wf WalkCallback) func(v dag.Vertex) (diags dag.Di
221222
// once we have found a resource convert it to a cty type and then
222223
// set it on the context
223224
switch l.Metadata().Type {
224-
case types.TypeLocal:
225-
loc := l.(*types.Local)
225+
case resources.TypeLocal:
226+
loc := l.(*resources.Local)
226227
ctyRes = loc.CtyValue
227-
case types.TypeOutput:
228-
out := l.(*types.Output)
228+
case resources.TypeOutput:
229+
out := l.(*resources.Output)
229230
ctyRes = out.CtyValue
230231
default:
231232
ctyRes, err = convert.GoToCtyValue(l)
@@ -278,7 +279,7 @@ func createCallback(c *Config, wf WalkCallback) func(v dag.Vertex) (diags dag.Di
278279
// if the type is a module the potentially we only just found out that we should be
279280
// disabled
280281
// as an additional check, set all module resources to disabled if the module is disabled
281-
if r.GetDisabled() && r.Metadata().Type == types.TypeModule {
282+
if r.GetDisabled() && r.Metadata().Type == resources.TypeModule {
282283
// find all dependent resources
283284
dr, err := c.FindModuleResources(r.Metadata().ID, true)
284285
if err != nil {
@@ -304,7 +305,7 @@ func createCallback(c *Config, wf WalkCallback) func(v dag.Vertex) (diags dag.Di
304305
//
305306
// if disabled was set through interpolation, the value has only been set here
306307
// we need to handle an additional check
307-
if !r.GetDisabled() && r.Metadata().Type != types.TypeModule {
308+
if !r.GetDisabled() && r.Metadata().Type != resources.TypeModule {
308309

309310
// call the callbacks
310311
if wf != nil {
@@ -324,8 +325,8 @@ func createCallback(c *Config, wf WalkCallback) func(v dag.Vertex) (diags dag.Di
324325

325326
// if the type is a module we need to add the variables to the
326327
// context
327-
if r.Metadata().Type == types.TypeModule {
328-
mod := r.(*types.Module)
328+
if r.Metadata().Type == resources.TypeModule {
329+
mod := r.(*resources.Module)
329330

330331
var mapVars map[string]cty.Value
331332
if att, ok := mod.Variables.(*hcl.Attribute); ok {
@@ -340,16 +341,16 @@ func createCallback(c *Config, wf WalkCallback) func(v dag.Vertex) (diags dag.Di
340341

341342
// if this is an output or local we need to convert the value into
342343
// a go type
343-
if r.Metadata().Type == types.TypeOutput {
344-
o := r.(*types.Output)
344+
if r.Metadata().Type == resources.TypeOutput {
345+
o := r.(*resources.Output)
345346

346347
if !o.CtyValue.IsNull() {
347348
o.Value = castVar(o.CtyValue)
348349
}
349350
}
350351

351-
if r.Metadata().Type == types.TypeLocal {
352-
o := r.(*types.Local)
352+
if r.Metadata().Type == resources.TypeLocal {
353+
o := r.(*resources.Local)
353354

354355
if !o.CtyValue.IsNull() {
355356
o.Value = castVar(o.CtyValue)

example/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88

99
"github.com/jumppad-labs/hclconfig"
10+
"github.com/jumppad-labs/hclconfig/resources"
1011
"github.com/jumppad-labs/hclconfig/types"
1112
)
1213

@@ -89,7 +90,7 @@ func printConfig(c *hclconfig.Config) {
8990
fmt.Println(printPostgres(t, 2))
9091

9192
case "output":
92-
t := r.(*types.Output)
93+
t := r.(*resources.Output)
9394
fmt.Printf(" Postgres %s\n", t.Meta.Name)
9495
fmt.Printf(" Module %s\n", t.Meta.Module)
9596
fmt.Printf(" --- Value: %s\n", t.Value)

0 commit comments

Comments
 (0)