- Notifications
You must be signed in to change notification settings - Fork1k
chore(provisioner/terraform): extract terraform parsing logic to package tfparse#15230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
176 changes: 4 additions & 172 deletionsprovisioner/terraform/parse.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletionsprovisioner/terraform/tfparse/tfextract.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
package tfparse | ||
import ( | ||
"context" | ||
"encoding/json" | ||
"os" | ||
"slices" | ||
"sort" | ||
"strings" | ||
"github.com/coder/coder/v2/provisionersdk/proto" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hclparse" | ||
"github.com/hashicorp/hcl/v2/hclsyntax" | ||
"github.com/hashicorp/terraform-config-inspect/tfconfig" | ||
"golang.org/x/xerrors" | ||
"cdr.dev/slog" | ||
) | ||
// WorkspaceTags extracts tags from coder_workspace_tags data sources defined in module. | ||
func WorkspaceTags(ctx context.Context, logger slog.Logger, module *tfconfig.Module) (map[string]string, error) { | ||
workspaceTags := map[string]string{} | ||
for _, dataResource := range module.DataResources { | ||
if dataResource.Type != "coder_workspace_tags" { | ||
logger.Debug(ctx, "skip resource as it is not a coder_workspace_tags", "resource_name", dataResource.Name, "resource_type", dataResource.Type) | ||
continue | ||
} | ||
var file *hcl.File | ||
var diags hcl.Diagnostics | ||
parser := hclparse.NewParser() | ||
if !strings.HasSuffix(dataResource.Pos.Filename, ".tf") { | ||
logger.Debug(ctx, "only .tf files can be parsed", "filename", dataResource.Pos.Filename) | ||
continue | ||
} | ||
// We know in which HCL file is the data resource defined. | ||
file, diags = parser.ParseHCLFile(dataResource.Pos.Filename) | ||
if diags.HasErrors() { | ||
return nil, xerrors.Errorf("can't parse the resource file: %s", diags.Error()) | ||
} | ||
// Parse root to find "coder_workspace_tags". | ||
content, _, diags := file.Body.PartialContent(rootTemplateSchema) | ||
if diags.HasErrors() { | ||
return nil, xerrors.Errorf("can't parse the resource file: %s", diags.Error()) | ||
} | ||
// Iterate over blocks to locate the exact "coder_workspace_tags" data resource. | ||
for _, block := range content.Blocks { | ||
if !slices.Equal(block.Labels, []string{"coder_workspace_tags", dataResource.Name}) { | ||
continue | ||
} | ||
// Parse "coder_workspace_tags" to find all key-value tags. | ||
resContent, _, diags := block.Body.PartialContent(coderWorkspaceTagsSchema) | ||
if diags.HasErrors() { | ||
return nil, xerrors.Errorf(`can't parse the resource coder_workspace_tags: %s`, diags.Error()) | ||
} | ||
if resContent == nil { | ||
continue // workspace tags are not present | ||
} | ||
if _, ok := resContent.Attributes["tags"]; !ok { | ||
return nil, xerrors.Errorf(`"tags" attribute is required by coder_workspace_tags`) | ||
} | ||
expr := resContent.Attributes["tags"].Expr | ||
tagsExpr, ok := expr.(*hclsyntax.ObjectConsExpr) | ||
if !ok { | ||
return nil, xerrors.Errorf(`"tags" attribute is expected to be a key-value map`) | ||
} | ||
// Parse key-value entries in "coder_workspace_tags" | ||
for _, tagItem := range tagsExpr.Items { | ||
key, err := previewFileContent(tagItem.KeyExpr.Range()) | ||
if err != nil { | ||
return nil, xerrors.Errorf("can't preview the resource file: %v", err) | ||
} | ||
key = strings.Trim(key, `"`) | ||
value, err := previewFileContent(tagItem.ValueExpr.Range()) | ||
if err != nil { | ||
return nil, xerrors.Errorf("can't preview the resource file: %v", err) | ||
} | ||
logger.Info(ctx, "workspace tag found", "key", key, "value", value) | ||
if _, ok := workspaceTags[key]; ok { | ||
return nil, xerrors.Errorf(`workspace tag %q is defined multiple times`, key) | ||
} | ||
workspaceTags[key] = value | ||
} | ||
} | ||
} | ||
return workspaceTags, nil | ||
} | ||
var rootTemplateSchema = &hcl.BodySchema{ | ||
Blocks: []hcl.BlockHeaderSchema{ | ||
{ | ||
Type: "data", | ||
LabelNames: []string{"type", "name"}, | ||
}, | ||
}, | ||
} | ||
var coderWorkspaceTagsSchema = &hcl.BodySchema{ | ||
Attributes: []hcl.AttributeSchema{ | ||
{ | ||
Name: "tags", | ||
}, | ||
}, | ||
} | ||
func previewFileContent(fileRange hcl.Range) (string, error) { | ||
body, err := os.ReadFile(fileRange.Filename) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(fileRange.SliceBytes(body)), nil | ||
} | ||
// LoadTerraformVariables extracts all Terraform variables from module and converts them | ||
// to template variables. The variables are sorted by source position. | ||
func LoadTerraformVariables(module *tfconfig.Module) ([]*proto.TemplateVariable, error) { | ||
// Sort variables by (filename, line) to make the ordering consistent | ||
variables := make([]*tfconfig.Variable, 0, len(module.Variables)) | ||
for _, v := range module.Variables { | ||
variables = append(variables, v) | ||
} | ||
sort.Slice(variables, func(i, j int) bool { | ||
return compareSourcePos(variables[i].Pos, variables[j].Pos) | ||
}) | ||
var templateVariables []*proto.TemplateVariable | ||
for _, v := range variables { | ||
mv, err := convertTerraformVariable(v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
templateVariables = append(templateVariables, mv) | ||
} | ||
return templateVariables, nil | ||
} | ||
// convertTerraformVariable converts a Terraform variable to a template-wide variable, processed by Coder. | ||
func convertTerraformVariable(variable *tfconfig.Variable) (*proto.TemplateVariable, error) { | ||
var defaultData string | ||
if variable.Default != nil { | ||
var valid bool | ||
defaultData, valid = variable.Default.(string) | ||
if !valid { | ||
defaultDataRaw, err := json.Marshal(variable.Default) | ||
if err != nil { | ||
return nil, xerrors.Errorf("parse variable %q default: %w", variable.Name, err) | ||
} | ||
defaultData = string(defaultDataRaw) | ||
} | ||
} | ||
return &proto.TemplateVariable{ | ||
Name: variable.Name, | ||
Description: variable.Description, | ||
Type: variable.Type, | ||
DefaultValue: defaultData, | ||
// variable.Required is always false. Empty string is a valid default value, so it doesn't enforce required to be "true". | ||
Required: variable.Default == nil, | ||
Sensitive: variable.Sensitive, | ||
}, nil | ||
} | ||
func compareSourcePos(x, y tfconfig.SourcePos) bool { | ||
if x.Filename != y.Filename { | ||
return x.Filename < y.Filename | ||
} | ||
return x.Line < y.Line | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.