Skip to content

Commit

Permalink
Rename certain resource fields to prevent conflicts with user types
Browse files Browse the repository at this point in the history
  • Loading branch information
eveld committed Jan 4, 2024
1 parent 133ecdd commit 1d9c6e6
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 81 deletions.
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,13 @@ func (c *Config) Diff(o *Config) (*ResourceDiff, error) {
}

// check if the resource has changed
if cr.Metadata().Checksum.Parsed != r.Metadata().Checksum.Parsed {
if cr.Metadata().SourceChecksum.Parsed != r.Metadata().SourceChecksum.Parsed {
// resource has changes rebuild
parseChanged = append(parseChanged, r)
continue
}

if cr.Metadata().Checksum.Processed != r.Metadata().Checksum.Processed {
if cr.Metadata().SourceChecksum.Processed != r.Metadata().SourceChecksum.Processed {
// resource has changes rebuild
processChanged = append(processChanged, r)
continue
Expand Down
30 changes: 15 additions & 15 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,34 @@ func testSetupConfig(t *testing.T) (*Config, []types.Resource) {
typs[structs.TypeTemplate] = &structs.Template{}

var1, _ := typs.CreateResource(types.TypeVariable, "var1")
var1.Metadata().Checksum = types.Checksum{
var1.Metadata().SourceChecksum = types.Checksum{
Parsed: "123",
Processed: "abc",
}

net1, _ := typs.CreateResource(structs.TypeNetwork, "cloud")
net1.Metadata().Checksum = types.Checksum{
net1.Metadata().SourceChecksum = types.Checksum{
Parsed: "234",
Processed: "bcd",
}

mod1, _ := typs.CreateResource(types.TypeModule, "module1")
mod1.Metadata().DependsOn = []string{"resource.network.cloud"}
mod1.Metadata().Checksum = types.Checksum{
mod1.Metadata().SourceChecksum = types.Checksum{
Parsed: "345",
Processed: "cde",
}

var2, _ := typs.CreateResource(types.TypeVariable, "var2")
var2.Metadata().Module = "module1"
var2.Metadata().Checksum = types.Checksum{
var2.Metadata().SourceChecksum = types.Checksum{
Parsed: "456",
Processed: "def",
}

mod2, _ := typs.CreateResource(types.TypeModule, "module2")
mod2.Metadata().Module = "module1"
mod2.Metadata().Checksum = types.Checksum{
mod2.Metadata().SourceChecksum = types.Checksum{
Parsed: "567",
Processed: "efg",
}
Expand All @@ -53,23 +53,23 @@ func testSetupConfig(t *testing.T) (*Config, []types.Resource) {
// all child resources
con1, _ := typs.CreateResource(structs.TypeContainer, "test_dev")
con1.Metadata().DependsOn = []string{"module.module1"}
con1.Metadata().Checksum = types.Checksum{
con1.Metadata().SourceChecksum = types.Checksum{
Parsed: "678",
Processed: "fgh",
}

// con2 is embedded in module1
con2, _ := typs.CreateResource(structs.TypeContainer, "test_dev")
con2.Metadata().Module = "module1"
con2.Metadata().Checksum = types.Checksum{
con2.Metadata().SourceChecksum = types.Checksum{
Parsed: "789",
Processed: "ghi",
}

// con3 is loaded from a module inside module2
con3, _ := typs.CreateResource(structs.TypeContainer, "test_dev")
con3.Metadata().Module = "module1.module2"
con3.Metadata().Checksum = types.Checksum{
con3.Metadata().SourceChecksum = types.Checksum{
Parsed: "890",
Processed: "hij",
}
Expand All @@ -82,21 +82,21 @@ func testSetupConfig(t *testing.T) (*Config, []types.Resource) {
// when a resource is defined, it has no idea on its
// module
con4.Metadata().DependsOn = []string{"resource.container.test_dev"}
con4.Metadata().Checksum = types.Checksum{
con4.Metadata().SourceChecksum = types.Checksum{
Parsed: "90a",
Processed: "ijk",
}

out1, _ := typs.CreateResource(types.TypeOutput, "fqdn")
out1.Metadata().Module = "module1.module2"
out1.Metadata().Checksum = types.Checksum{
out1.Metadata().SourceChecksum = types.Checksum{
Parsed: "0ab",
Processed: "jkl",
}

out2, _ := typs.CreateResource(types.TypeOutput, "out")
out2.Metadata().DependsOn = []string{"resource.network.cloud.id", "resource.container.test_dev"}
out2.Metadata().Checksum = types.Checksum{
out2.Metadata().SourceChecksum = types.Checksum{
Parsed: "abc",
Processed: "klm",
}
Expand Down Expand Up @@ -460,7 +460,7 @@ func TestDiffReturnsResourcesAdded(t *testing.T) {

typs := types.DefaultTypes()
var1, _ := typs.CreateResource(types.TypeVariable, "var22")
var1.Metadata().Checksum = types.Checksum{
var1.Metadata().SourceChecksum = types.Checksum{
Parsed: "zzz",
Processed: "111",
}
Expand All @@ -481,10 +481,10 @@ func TestDiffReturnsResourcesUpdated(t *testing.T) {
new := copyConfig(t, c)

var1, _ := new.FindResource("variable.var1")
var1.Metadata().Checksum.Parsed = "zzz"
var1.Metadata().SourceChecksum.Parsed = "zzz"

var2, _ := new.FindResource("module.module1.variable.var2")
var2.Metadata().Checksum.Processed = "zzz"
var2.Metadata().SourceChecksum.Processed = "zzz"

changes, err := c.Diff(new)
require.NoError(t, err)
Expand All @@ -501,7 +501,7 @@ func TestDiffReturnsResourcesRemoved(t *testing.T) {
new := copyConfig(t, c)

var1, _ := new.FindResource("variable.var1")
var1.Metadata().Checksum = types.Checksum{
var1.Metadata().SourceChecksum = types.Checksum{
Parsed: "zzz",
Processed: "111",
}
Expand Down
66 changes: 33 additions & 33 deletions dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ func doYaLikeDAGs(c *Config) (*dag.AcyclicGraph, error) {
fqdn, err := types.ParseFQRN(d)
if err != nil {
pe := errors.ParserError{}
pe.Line = resource.Metadata().Line
pe.Column = resource.Metadata().Column
pe.Filename = resource.Metadata().File
pe.Line = resource.Metadata().SourceLine
pe.Column = resource.Metadata().SourceColumn
pe.Filename = resource.Metadata().SourceFile
pe.Message = fmt.Sprintf("invalid dependency: %s, error: %s", d, err)
pe.Level = errors.ParserErrorLevelError

Expand All @@ -85,9 +85,9 @@ func doYaLikeDAGs(c *Config) (*dag.AcyclicGraph, error) {
deps, err := c.FindModuleResources(relFQDN.String(), true)
if err != nil {
pe := errors.ParserError{}
pe.Line = resource.Metadata().Line
pe.Column = resource.Metadata().Column
pe.Filename = resource.Metadata().File
pe.Line = resource.Metadata().SourceLine
pe.Column = resource.Metadata().SourceColumn
pe.Filename = resource.Metadata().SourceFile
pe.Message = fmt.Sprintf("unable to find resources for module: %s, error: %s", fqdn.Module, err)
pe.Level = errors.ParserErrorLevelError

Expand All @@ -110,9 +110,9 @@ func doYaLikeDAGs(c *Config) (*dag.AcyclicGraph, error) {
dep, err := c.FindResource(relFQDN.String())
if err != nil {
pe := errors.ParserError{}
pe.Line = resource.Metadata().Line
pe.Column = resource.Metadata().Column
pe.Filename = resource.Metadata().File
pe.Line = resource.Metadata().SourceLine
pe.Column = resource.Metadata().SourceColumn
pe.Filename = resource.Metadata().SourceFile
pe.Message = fmt.Sprintf("unable to find dependent resource in module: '%s', error: '%s'", resource.Metadata().Module, err)
pe.Level = errors.ParserErrorLevelError

Expand All @@ -130,9 +130,9 @@ func doYaLikeDAGs(c *Config) (*dag.AcyclicGraph, error) {
d, err := c.FindResource(fqdnString)
if err != nil {
pe := errors.ParserError{}
pe.Line = resource.Metadata().Line
pe.Column = resource.Metadata().Column
pe.Filename = resource.Metadata().File
pe.Line = resource.Metadata().SourceLine
pe.Column = resource.Metadata().SourceColumn
pe.Filename = resource.Metadata().SourceFile
pe.Message = fmt.Sprintf("unable to find resources parent module: '%s, error: %s", fqdnString, err)
pe.Level = errors.ParserErrorLevelError

Expand Down Expand Up @@ -194,9 +194,9 @@ func createCallback(c *Config, wf WalkCallback) func(v dag.Vertex) (diags dag.Di
fqdn, err := types.ParseFQRN(v)
if err != nil {
pe := errors.ParserError{}
pe.Filename = r.Metadata().File
pe.Line = r.Metadata().Line
pe.Column = r.Metadata().Column
pe.Filename = r.Metadata().SourceFile
pe.Line = r.Metadata().SourceLine
pe.Column = r.Metadata().SourceColumn
pe.Message = fmt.Sprintf("error parsing resource link %s", err)
pe.Level = errors.ParserErrorLevelError

Expand All @@ -207,9 +207,9 @@ func createCallback(c *Config, wf WalkCallback) func(v dag.Vertex) (diags dag.Di
l, err := c.FindRelativeResource(v, r.Metadata().Module)
if err != nil {
pe := errors.ParserError{}
pe.Filename = r.Metadata().File
pe.Line = r.Metadata().Line
pe.Column = r.Metadata().Column
pe.Filename = r.Metadata().SourceFile
pe.Line = r.Metadata().SourceLine
pe.Column = r.Metadata().SourceColumn
pe.Message = fmt.Sprintf(`unable to find dependent resource "%s" %s`, v, err)
pe.Level = errors.ParserErrorLevelError

Expand All @@ -233,9 +233,9 @@ func createCallback(c *Config, wf WalkCallback) func(v dag.Vertex) (diags dag.Di

if err != nil {
pe := errors.ParserError{}
pe.Filename = r.Metadata().File
pe.Line = r.Metadata().Line
pe.Column = r.Metadata().Column
pe.Filename = r.Metadata().SourceFile
pe.Line = r.Metadata().SourceLine
pe.Column = r.Metadata().SourceColumn
pe.Message = fmt.Sprintf(`unable to convert reference %s to context variable: %s`, v, err)
pe.Level = errors.ParserErrorLevelError

Expand All @@ -248,9 +248,9 @@ func createCallback(c *Config, wf WalkCallback) func(v dag.Vertex) (diags dag.Di
err = setContextVariableFromPath(ctx, fqdn.String(), ctyRes)
if err != nil {
pe := errors.ParserError{}
pe.Filename = r.Metadata().File
pe.Line = r.Metadata().Line
pe.Column = r.Metadata().Column
pe.Filename = r.Metadata().SourceFile
pe.Line = r.Metadata().SourceLine
pe.Column = r.Metadata().SourceColumn
pe.Message = fmt.Sprintf(`unable to set context variable: %s`, err)
pe.Level = errors.ParserErrorLevelError

Expand All @@ -266,9 +266,9 @@ func createCallback(c *Config, wf WalkCallback) func(v dag.Vertex) (diags dag.Di
diag := gohcl.DecodeBody(bdy, ctx, r)
if diag.HasErrors() {
pe := errors.ParserError{}
pe.Filename = r.Metadata().File
pe.Line = r.Metadata().Line
pe.Column = r.Metadata().Column
pe.Filename = r.Metadata().SourceFile
pe.Line = r.Metadata().SourceLine
pe.Column = r.Metadata().SourceColumn
pe.Message = fmt.Sprintf(`unable to decode body: %s`, diag.Error())
pe.Level = errors.ParserErrorLevelWarning

Expand All @@ -284,9 +284,9 @@ func createCallback(c *Config, wf WalkCallback) func(v dag.Vertex) (diags dag.Di
if err != nil {
// should not be here unless an internal error
pe := errors.ParserError{}
pe.Filename = r.Metadata().File
pe.Line = r.Metadata().Line
pe.Column = r.Metadata().Column
pe.Filename = r.Metadata().SourceFile
pe.Line = r.Metadata().SourceLine
pe.Column = r.Metadata().SourceColumn
pe.Message = fmt.Sprintf(`unable to find disabled module resources "%s", %s"`, r.Metadata().ID, err)
pe.Level = errors.ParserErrorLevelError

Expand All @@ -311,9 +311,9 @@ func createCallback(c *Config, wf WalkCallback) func(v dag.Vertex) (diags dag.Di
err := wf(r)
if err != nil {
pe := errors.ParserError{}
pe.Filename = r.Metadata().File
pe.Line = r.Metadata().Line
pe.Column = r.Metadata().Column
pe.Filename = r.Metadata().SourceFile
pe.Line = r.Metadata().SourceLine
pe.Column = r.Metadata().SourceColumn
pe.Message = fmt.Sprintf(`error calling callback for resource "%s" %s`, r.Metadata().ID, err)
pe.Level = errors.ParserErrorLevelError

Expand Down
2 changes: 1 addition & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {
// this function can be used to execute any external work required for the
// resource.
o.Callback = func(r types.Resource) error {
fmt.Printf(" resource '%s' named '%s' has been parsed from the file: %s\n", r.Metadata().Type, r.Metadata().Name, r.Metadata().File)
fmt.Printf(" resource '%s' named '%s' has been parsed from the file: %s\n", r.Metadata().Type, r.Metadata().Name, r.Metadata().SourceFile)
return nil
}

Expand Down
22 changes: 11 additions & 11 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestParseFileProcessesResources(t *testing.T) {

require.Equal(t, "resource.container.consul", cont.Metadata().ID)
require.Equal(t, "consul", cont.Metadata().Name)
require.Equal(t, absoluteFolderPath, cont.Metadata().File)
require.Equal(t, absoluteFolderPath, cont.Metadata().SourceFile)

require.Equal(t, "consul", cont.Command[0], "consul")
require.Equal(t, "10.6.0.200", cont.Networks[0].IPAddress)
Expand Down Expand Up @@ -774,7 +774,7 @@ func TestParserDeserializesJSONCorrectly(t *testing.T) {
parsed, err := conf.FindResource("resource.container.base")
require.NoError(t, err)

require.Equal(t, orig.Metadata().File, parsed.Metadata().File)
require.Equal(t, orig.Metadata().SourceFile, parsed.Metadata().SourceFile)
require.Equal(t, orig.(*structs.Container).Networks[0].Name, parsed.(*structs.Container).Networks[0].Name)
require.Equal(t, orig.(*structs.Container).Command, parsed.(*structs.Container).Command)
require.Equal(t, orig.(*structs.Container).Resources.CPUPin, parsed.(*structs.Container).Resources.CPUPin)
Expand Down Expand Up @@ -855,15 +855,15 @@ func TestParserGeneratesChecksums(t *testing.T) {

r1, err := c.FindResource("resource.network.onprem")
require.NoError(t, err)
require.NotEmpty(t, r1.Metadata().Checksum.Parsed)
require.NotEmpty(t, r1.Metadata().SourceChecksum.Parsed)

r2, err := c.FindResource("variable.cpu_resources")
require.NoError(t, err)
require.NotEmpty(t, r2.Metadata().Checksum.Parsed)
require.NotEmpty(t, r2.Metadata().SourceChecksum.Parsed)

r3, err := c.FindResource("output.ip_address_1")
require.NoError(t, err)
require.NotEmpty(t, r3.Metadata().Checksum.Parsed)
require.NotEmpty(t, r3.Metadata().SourceChecksum.Parsed)

// parse a second time, the checksums should be equal
p = setupParser(t)
Expand All @@ -873,15 +873,15 @@ func TestParserGeneratesChecksums(t *testing.T) {

c1, err := c.FindResource("resource.network.onprem")
require.NoError(t, err)
require.Equal(t, r1.Metadata().Checksum.Parsed, c1.Metadata().Checksum.Parsed)
require.Equal(t, r1.Metadata().SourceChecksum.Parsed, c1.Metadata().SourceChecksum.Parsed)

c2, err := c.FindResource("variable.cpu_resources")
require.NoError(t, err)
require.Equal(t, r2.Metadata().Checksum.Parsed, c2.Metadata().Checksum.Parsed)
require.Equal(t, r2.Metadata().SourceChecksum.Parsed, c2.Metadata().SourceChecksum.Parsed)

c3, err := c.FindResource("output.ip_address_1")
require.NoError(t, err)
require.Equal(t, r3.Metadata().Checksum.Parsed, c3.Metadata().Checksum.Parsed)
require.Equal(t, r3.Metadata().SourceChecksum.Parsed, c3.Metadata().SourceChecksum.Parsed)
}

func TestParserHandlesCyclicalReference(t *testing.T) {
Expand Down Expand Up @@ -986,8 +986,8 @@ func TestParseFileReturnsConfigErrorWhenResourceProcessError(t *testing.T) {

ce := err.(*errors.ConfigError)
require.Len(t, ce.Errors, 1)
require.False(t,ce.ContainsErrors())

require.False(t, ce.ContainsErrors())

pe := ce.Errors[0].(errors.ParserError)
require.Equal(t, pe.Level, errors.ParserErrorLevelWarning)
Expand All @@ -1006,4 +1006,4 @@ func TestParseFileReturnsConfigErrorWhenInvalidFileFails(t *testing.T) {

ce := err.(*errors.ConfigError)
require.Len(t, ce.Errors, 1)
}
}
Loading

0 comments on commit 1d9c6e6

Please sign in to comment.