Skip to content

Commit

Permalink
Merge branch 'main' of github.com:shipyard-run/hclconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasjackson committed Mar 16, 2024
2 parents d61496f + 2b9be08 commit e8088c8
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 17 deletions.
14 changes: 8 additions & 6 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ func testSetupConfig(t *testing.T) (*Config, []types.Resource) {
Processed: "bcd",
}

mod1, _ := typs.CreateResource(resources.TypeModule, "module1")
mod1.SetDependsOn([]string{"resource.network.cloud"})
mod1, _ := typs.CreateResource(types.TypeModule, "module1")
mod1.AddDependency("resource.network.cloud")

mod1.Metadata().Checksum = types.Checksum{
Parsed: "345",
Processed: "cde",
Expand All @@ -53,7 +54,7 @@ func testSetupConfig(t *testing.T) (*Config, []types.Resource) {
// depending on a module should return all resources and
// all child resources
con1, _ := typs.CreateResource(structs.TypeContainer, "test_dev")
con1.SetDependsOn([]string{"module.module1"})
con1.AddDependency("module.module1")
con1.Metadata().Checksum = types.Checksum{
Parsed: "678",
Processed: "fgh",
Expand Down Expand Up @@ -82,7 +83,7 @@ func testSetupConfig(t *testing.T) (*Config, []types.Resource) {
// depends on would be added relative as a resource
// when a resource is defined, it has no idea on its
// module
con4.SetDependsOn([]string{"resource.container.test_dev"})
con4.AddDependency("resource.container.test_dev")
con4.Metadata().Checksum = types.Checksum{
Parsed: "90a",
Processed: "ijk",
Expand All @@ -95,8 +96,9 @@ func testSetupConfig(t *testing.T) (*Config, []types.Resource) {
Processed: "jkl",
}

out2, _ := typs.CreateResource(resources.TypeOutput, "out")
out2.SetDependsOn([]string{"resource.network.cloud.id", "resource.container.test_dev"})
out2, _ := typs.CreateResource(types.TypeOutput, "out")
out2.SetDependencies([]string{"resource.network.cloud.id", "resource.container.test_dev"})

out2.Metadata().Checksum = types.Checksum{
Parsed: "abc",
Processed: "klm",
Expand Down
4 changes: 2 additions & 2 deletions convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func GoToCtyValue(val interface{}) (cty.Value, error) {
ctyMap["disabled"] = cty.BoolVal(r.GetDisabled())

// add depends_on to the parent
depTyp, err := gocty.ImpliedType(r.GetDependsOn())
depTyp, err := gocty.ImpliedType(r.GetDependencies())
if err != nil {
return cty.False, err
}

dep, err := gocty.ToCtyValue(r.GetDependsOn(), depTyp)
dep, err := gocty.ToCtyValue(r.GetDependencies(), depTyp)
if err != nil {
return cty.False, fmt.Errorf("unable to convert depends_on to cty: %s", err)
}
Expand Down
7 changes: 5 additions & 2 deletions dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ func doYaLikeDAGs(c *Config) (*dag.AcyclicGraph, error) {

// add links to dependencies
// this is here for now as we might need to process these two lists separately
resource.SetDependsOn(append(resource.GetDependsOn(), resource.Metadata().Links...))
// resource.SetDependsOn(append(resource.GetDependsOn(), resource.Metadata().Links...))
for _, d := range resource.Metadata().Links {
resource.AddDependency(d)
}

for _, d := range resource.GetDependsOn() {
for _, d := range resource.GetDependencies() {
var err error
fqdn, err := resources.ParseFQRN(d)
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,9 @@ func setDisabled(ctx *hcl.EvalContext, r types.Resource, b *hclsyntax.Body, pare
}

func setDependsOn(ctx *hcl.EvalContext, r types.Resource, b *hclsyntax.Body, dependsOn []string) error {
r.SetDependsOn(dependsOn)
for _, d := range dependsOn {
r.AddDependency(d)
}

if attr, ok := b.Attributes["depends_on"]; ok {
dependsOnVal, diags := attr.Expr.Value(ctx)
Expand All @@ -537,7 +539,7 @@ func setDependsOn(ctx *hcl.EvalContext, r types.Resource, b *hclsyntax.Body, dep
return fmt.Errorf("invalid dependency %s, %s", d.AsString(), err)
}

r.SetDependsOn(append(r.GetDependsOn(), d.AsString()))
r.AddDependency(d.AsString())
}
}

Expand Down
27 changes: 23 additions & 4 deletions types/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ type Resource interface {
Metadata() *Meta
GetDisabled() bool
SetDisabled(bool)
GetDependsOn() []string
SetDependsOn([]string)
GetDependencies() []string
SetDependencies([]string)
AddDependency(string)
}

type Meta struct {
Expand Down Expand Up @@ -128,10 +129,28 @@ func (r *ResourceBase) SetDisabled(v bool) {
r.Disabled = v
}

func (r *ResourceBase) GetDependsOn() []string {
func (r *ResourceBase) GetDependencies() []string {
return r.DependsOn
}

func (r *ResourceBase) SetDependsOn(v []string) {
func (r *ResourceBase) SetDependencies(v []string) {
r.DependsOn = v
}

func (r *ResourceBase) AddDependency(v string) {
r.DependsOn = appendIfNotContains(r.DependsOn, v)
}

func appendIfNotContains(list []string, value string) []string {
contains := false
for _, item := range list {
if value == item {
contains = true
}
}

if !contains {
list = append(list, value)
}
return list
}
2 changes: 1 addition & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func castVar(v cty.Value) interface{} {
func generateChecksum(r types.Resource) string {
// first sort the resource links and depends on as these change
// depending on the dag process
sort.Strings(r.GetDependsOn())
sort.Strings(r.GetDependencies())
sort.Strings(r.Metadata().Links)

// first convert the object to json
Expand Down

0 comments on commit e8088c8

Please sign in to comment.