Skip to content

Commit

Permalink
Add the environment context to evaluated templates
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewcummings committed Dec 18, 2023
1 parent 98840b0 commit 847f02f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
golang 1.21.0
golang 1.21.5
11 changes: 6 additions & 5 deletions cloudtruth/data_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (

func dataCloudTruthTemplate() *schema.Resource {
return &schema.Resource{
Description: "A CloudTruth template data source",
Description: `A CloudTruth template data source. Use this to reference the rendered values from CloudTruth
Templates in the context of a project and environment.`,
ReadContext: dataCloudTruthTemplateRead,
Schema: map[string]*schema.Schema{
"project": {
Expand Down Expand Up @@ -97,7 +98,7 @@ func dataCloudTruthTemplateRead(ctx context.Context, d *schema.ResourceData, met
body := template.GetBody()
tflog.Debug(ctx, fmt.Sprintf("dataCloudTruthTemplateRead: template body - %s", body))

previewBody, err := renderTemplateBody(ctx, name, body, *projID, meta)
previewBody, err := renderTemplateBody(ctx, name, body, *projID, *envID, meta)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -110,7 +111,7 @@ func dataCloudTruthTemplateRead(ctx context.Context, d *schema.ResourceData, met
return nil
}

func renderTemplateBody(ctx context.Context, name, body, projectID string, meta any) (*string, error) {
func renderTemplateBody(ctx context.Context, name, body, projectID, envID string, meta any) (*string, error) {
tflog.Debug(ctx, "entering renderTemplateBody")
defer tflog.Debug(ctx, "exiting renderTemplateBody")
c := meta.(*cloudTruthClient)
Expand All @@ -121,7 +122,7 @@ func renderTemplateBody(ctx context.Context, name, body, projectID string, meta
// We cannot use the TF Provider SDK's retry functionality because it only works with state change events
// so we employ a simple retry loop instead
for retryCount < loadCacheRetries {
previewCreate, r, err := c.openAPIClient.ProjectsAPI.ProjectsTemplatePreviewCreate(ctx, projectID).
previewCreate, r, err := c.openAPIClient.ProjectsAPI.ProjectsTemplatePreviewCreate(ctx, projectID).Environment(envID).
TemplatePreviewCreateRequest(*templatePrevReq).Execute()
if r.StatusCode >= 500 {
apiError = err
Expand Down Expand Up @@ -218,7 +219,7 @@ func dataCloudTruthTemplatesRead(ctx context.Context, d *schema.ResourceData, me
for _, res := range results {
templateName := res.GetName()
templateBody := res.GetBody()
previewBody, err := renderTemplateBody(ctx, templateName, templateBody, *projID, meta)
previewBody, err := renderTemplateBody(ctx, templateName, templateBody, *projID, *envID, meta)
if err != nil {
return diag.FromErr(fmt.Errorf("dataCloudTruthTemplatesRead: %w", err))
} else {
Expand Down
5 changes: 4 additions & 1 deletion cloudtruth/resource_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (

func resourceTemplate() *schema.Resource {
return &schema.Resource{
Description: "A CloudTruth Template.",
Description: `A CloudTruth Template. Use this to manage Templates in your CloudTruth project
If you want to render a template in the context of a project and environment, use a cloudtruth_template
data source instead. The value for a cloudtruth_template resource is the raw unevaluated template string.
`,
CreateContext: resourceTemplateCreate,
ReadContext: resourceTemplateRead,
UpdateContext: resourceTemplateUpdate,
Expand Down

0 comments on commit 847f02f

Please sign in to comment.