Skip to content

Commit

Permalink
chore: add template resource trace logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanndickson committed Jul 30, 2024
1 parent 9b0c900 commit c773873
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
30 changes: 30 additions & 0 deletions internal/provider/template_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ func (r *TemplateResource) Create(ctx context.Context, req resource.CreateReques
return
}
if idx == 0 {
tflog.Trace(ctx, "creating template")
templateResp, err = client.CreateTemplate(ctx, orgID, codersdk.CreateTemplateRequest{
Name: data.Name.ValueString(),
DisplayName: data.DisplayName.ValueString(),
Expand All @@ -366,21 +367,31 @@ func (r *TemplateResource) Create(ctx context.Context, req resource.CreateReques
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to create template: %s", err))
return
}
tflog.Trace(ctx, "successfully created template", map[string]any{
"id": templateResp.ID,
})

tflog.Trace(ctx, "updating template ACL")
err = client.UpdateTemplateACL(ctx, templateResp.ID, convertACLToRequest(data.ACL))
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to update template ACL: %s", err))
return
}
tflog.Trace(ctx, "successfully updated template ACL")
}
if version.Active.ValueBool() {
tflog.Trace(ctx, "marking template version as active", map[string]any{
"version_id": versionResp.ID,
"template_id": templateResp.ID,
})
err := client.UpdateActiveTemplateVersion(ctx, templateResp.ID, codersdk.UpdateActiveTemplateVersion{
ID: versionResp.ID,
})
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to set active template version: %s", err))
return
}
tflog.Trace(ctx, "marked template version as active")
}
data.Versions[idx].ID = UUIDValue(versionResp.ID)
data.Versions[idx].Name = types.StringValue(versionResp.Name)
Expand Down Expand Up @@ -478,6 +489,7 @@ func (r *TemplateResource) Update(ctx context.Context, req resource.UpdateReques
client := r.data.Client

if !planState.EqualTemplateMetadata(curState) {
tflog.Trace(ctx, "change in template metadata detected, updating.")
_, err := client.UpdateTemplateMeta(ctx, templateID, codersdk.UpdateTemplateMeta{
Name: planState.Name.ValueString(),
DisplayName: planState.DisplayName.ValueString(),
Expand All @@ -491,11 +503,13 @@ func (r *TemplateResource) Update(ctx context.Context, req resource.UpdateReques
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to update template: %s", err))
return
}
tflog.Trace(ctx, "successfully updated template metadata")
err = client.UpdateTemplateACL(ctx, templateID, convertACLToRequest(planState.ACL))
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to update template ACL: %s", err))
return
}
tflog.Trace(ctx, "successfully updated template ACL")
}

for idx, plannedVersion := range planState.Versions {
Expand All @@ -504,6 +518,7 @@ func (r *TemplateResource) Update(ctx context.Context, req resource.UpdateReques
foundVersion := curState.Versions.ByID(plannedVersion.ID)
// If the version is new, or if the directory hash has changed, create a new version
if foundVersion == nil || foundVersion.DirectoryHash != plannedVersion.DirectoryHash {
tflog.Trace(ctx, "discovered a new or modified template version")
versionResp, err := newVersion(ctx, client, newVersionRequest{
Version: &plannedVersion,
OrganizationID: orgID,
Expand All @@ -524,13 +539,18 @@ func (r *TemplateResource) Update(ctx context.Context, req resource.UpdateReques
return
}
if plannedVersion.Active.ValueBool() {
tflog.Trace(ctx, "marking template version as active", map[string]any{
"version_id": versionResp.ID,
"template_id": templateID,
})
err := client.UpdateActiveTemplateVersion(ctx, templateID, codersdk.UpdateActiveTemplateVersion{
ID: versionResp.ID,
})
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to update active template version: %s", err))
return
}
tflog.Trace(ctx, "marked template version as active")
}
planState.Versions[idx].ID = UUIDValue(versionResp.ID)
}
Expand All @@ -553,6 +573,7 @@ func (r *TemplateResource) Delete(ctx context.Context, req resource.DeleteReques

templateID := data.ID.ValueUUID()

tflog.Trace(ctx, "deleting template")
err := client.DeleteTemplate(ctx, templateID)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete template: %s", err))
Expand Down Expand Up @@ -712,11 +733,14 @@ type newVersionRequest struct {

func newVersion(ctx context.Context, client *codersdk.Client, req newVersionRequest) (*codersdk.TemplateVersion, error) {
directory := req.Version.Directory.ValueString()
tflog.Trace(ctx, "uploading directory")
uploadResp, err := uploadDirectory(ctx, client, slog.Make(newTFLogSink(ctx)), directory)
if err != nil {
return nil, fmt.Errorf("failed to upload directory: %s", err)
}
tflog.Trace(ctx, "successfully uploaded directory")
// TODO(ethanndickson): Uncomment when a released `codersdk` exports template variable parsing
// tflog.Trace(ctx,"discovering and parsing vars files")
// varFiles, err := codersdk.DiscoverVarsFiles(directory)
// if err != nil {
// return nil, fmt.Errorf("failed to discover vars files: %s", err)
Expand All @@ -725,6 +749,9 @@ func newVersion(ctx context.Context, client *codersdk.Client, req newVersionRequ
// if err != nil {
// return nil, fmt.Errorf("failed to parse user variable values: %s", err)
// }
// tflog.Trace(ctx,"discovered and parsed vars files", map[string]any{
// "vars": vars,
// })
vars := make([]codersdk.VariableValue, 0, len(req.Version.TerraformVariables))
for _, variable := range req.Version.TerraformVariables {
vars = append(vars, codersdk.VariableValue{
Expand All @@ -743,14 +770,17 @@ func newVersion(ctx context.Context, client *codersdk.Client, req newVersionRequ
if req.TemplateID != nil {
tmplVerReq.TemplateID = *req.TemplateID
}
tflog.Trace(ctx, "creating template version")
versionResp, err := client.CreateTemplateVersion(ctx, req.OrganizationID, tmplVerReq)
if err != nil {
return nil, fmt.Errorf("failed to create template version: %s", err)
}
tflog.Trace(ctx, "waiting for template version import job.")
err = waitForJob(ctx, client, &versionResp)
if err != nil {
return nil, fmt.Errorf("failed to wait for job: %s", err)
}
tflog.Trace(ctx, "successfully created template version")
return &versionResp, nil
}

Expand Down
5 changes: 4 additions & 1 deletion internal/provider/template_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"
"os"
"regexp"
"slices"
"strings"
Expand All @@ -15,6 +16,9 @@ import (
)

func TestAccTemplateResource(t *testing.T) {
if os.Getenv("TF_ACC") == "" {
t.Skip("Acceptance tests are disabled.")
}
ctx := context.Background()
client := integration.StartCoder(ctx, t, "template_acc", true)
firstUser, err := client.User(ctx, codersdk.Me)
Expand Down Expand Up @@ -84,7 +88,6 @@ func TestAccTemplateResource(t *testing.T) {
cfg6.Versions = slices.Clone(cfg6.Versions[1:])

resource.Test(t, resource.TestCase{
IsUnitTest: true,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
Expand Down

0 comments on commit c773873

Please sign in to comment.