Skip to content

Commit

Permalink
feat: early decode orchestrate blocks in deployment configs
Browse files Browse the repository at this point in the history
Their ranges are required for context references that are only available within orchestrate blocks
  • Loading branch information
ansgarm committed Aug 30, 2024
1 parent 4f99bab commit 7a3a629
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 9 deletions.
6 changes: 6 additions & 0 deletions earlydecoder/stacks/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func LoadStack(path string, files map[string]*hcl.File) (*stack.Meta, hcl.Diagno
stores[key] = *store
}

orchestrationRules := make(map[string]stack.OrchestrationRule)
for key, rule := range mod.OrchestrationRules {
orchestrationRules[key] = *rule
}

return &stack.Meta{
Path: path,
Filenames: filenames,
Expand All @@ -98,6 +103,7 @@ func LoadStack(path string, files map[string]*hcl.File) (*stack.Meta, hcl.Diagno
ProviderRequirements: providerRequirements,
Deployments: deployments,
Stores: stores,
OrchestrationRules: orchestrationRules,
}, diags
}

Expand Down
7 changes: 5 additions & 2 deletions earlydecoder/stacks/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestLoadStack(t *testing.T) {
ProviderRequirements: map[string]stack.ProviderRequirement{},
Deployments: map[string]stack.Deployment{},
Stores: map[string]stack.Store{},
OrchestrationRules: map[string]stack.OrchestrationRule{},
},
nil,
},
Expand Down Expand Up @@ -72,6 +73,7 @@ func TestLoadStack(t *testing.T) {
ProviderRequirements: map[string]stack.ProviderRequirement{},
Deployments: map[string]stack.Deployment{},
Stores: map[string]stack.Store{},
OrchestrationRules: map[string]stack.OrchestrationRule{},
},
nil,
},
Expand All @@ -97,8 +99,9 @@ func TestLoadStack(t *testing.T) {
"aws": {Source: tfaddr.MustParseProviderSource("hashicorp/aws"), VersionConstraints: version.MustConstraints(version.NewConstraint("~> 5.7.0"))},
"random": {Source: tfaddr.MustParseProviderSource("hashicorp/random"), VersionConstraints: version.MustConstraints(version.NewConstraint("~> 3.5.1"))},
},
Deployments: map[string]stack.Deployment{},
Stores: map[string]stack.Store{},
Deployments: map[string]stack.Deployment{},
Stores: map[string]stack.Store{},
OrchestrationRules: map[string]stack.OrchestrationRule{},
},
nil,
},
Expand Down
19 changes: 18 additions & 1 deletion earlydecoder/stacks/load_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package earlydecoder
import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/terraform-schema/stack"
"github.com/zclconf/go-cty/cty"
)
Expand Down Expand Up @@ -51,8 +52,24 @@ func loadDeployFromFile(file *hcl.File, ds *decodedStack) hcl.Diagnostics {
ds.Stores[storeName] = &stack.Store{
Type: storeType,
}
}
case "orchestrate":
if len(block.Labels) != 2 || block.Labels[0] == "" || block.Labels[1] == "" {
continue
}

body, ok := block.Body.(*hclsyntax.Body)
if !ok {
continue
}

ruleType := block.Labels[0]
ruleName := block.Labels[1]

ds.OrchestrationRules[ruleName] = &stack.OrchestrationRule{
Type: ruleType,
Range: body.SrcRange,
}
}
}

return diags
Expand Down
6 changes: 4 additions & 2 deletions earlydecoder/stacks/load_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ type decodedStack struct {
Outputs map[string]*stack.Output
ProviderRequirements map[string]*providerRequirement

Deployments map[string]*stack.Deployment
Stores map[string]*stack.Store
Deployments map[string]*stack.Deployment
Stores map[string]*stack.Store
OrchestrationRules map[string]*stack.OrchestrationRule
}

func newDecodedStack() *decodedStack {
Expand All @@ -35,6 +36,7 @@ func newDecodedStack() *decodedStack {
ProviderRequirements: make(map[string]*providerRequirement),
Deployments: make(map[string]*stack.Deployment),
Stores: make(map[string]*stack.Store),
OrchestrationRules: make(map[string]*stack.OrchestrationRule),
}
}

Expand Down
4 changes: 4 additions & 0 deletions earlydecoder/stacks/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ var deploymentRootSchema = &hcl.BodySchema{
Type: "store",
LabelNames: []string{"type", "name"},
},
{
Type: "orchestrate",
LabelNames: []string{"type", "name"},
},
},
}

Expand Down
1 change: 1 addition & 0 deletions internal/schema/refscope/scopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ var (
ComponentScope = lang.ScopeId("component")
IdentityTokenScope = lang.ScopeId("identity_token")
StoreScope = lang.ScopeId("store")
OrchestrateContext = lang.ScopeId("orchestrate_context")
)
5 changes: 3 additions & 2 deletions internal/schema/stacks/1.9/orchestrate_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func orchestrateBlockSchema() *schema.BlockSchema {
Description: lang.PlainText("Rule Type"),
IsDepKey: true,
Completable: true,
// TODO: auto_approve is the only one supported now, but converged, replan, rollout are possible values for the first label on the block
// TODO: auto_approve is the only one supported now, but converged, replan, rollout, deferral_replan are possible values for the first label on the block
// TODO: complete the available labels
},
{
Name: "name",
Expand All @@ -37,7 +38,7 @@ func orchestrateBlockSchema() *schema.BlockSchema {
Attributes: map[string]*schema.AttributeSchema{
"condition": {
Description: lang.Markdown("The condition must evaluate to true or false"),
Constraint: schema.LiteralType{Type: cty.Bool},
Constraint: schema.AnyExpression{OfType: cty.Bool},
},
"reason": {
Description: lang.Markdown("The reason must be a string"),
Expand Down
5 changes: 3 additions & 2 deletions stack/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Meta struct {
Outputs map[string]Output
ProviderRequirements map[string]ProviderRequirement

Deployments map[string]Deployment
Stores map[string]Store
Deployments map[string]Deployment
Stores map[string]Store
OrchestrationRules map[string]OrchestrationRule
}
11 changes: 11 additions & 0 deletions stack/orchestrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package stack

import "github.com/hashicorp/hcl/v2"

type OrchestrationRule struct {
Type string
Range hcl.Range
}

0 comments on commit 7a3a629

Please sign in to comment.