Skip to content

Commit 42bee92

Browse files
chore: add template resource trace logging (#44)
1 parent 9b0c900 commit 42bee92

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

docs/data-sources/template.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
page_title: "coderd_template Data Source - coderd"
44
subcategory: ""
55
description: |-
6-
An existing template on the coder deployment
6+
An existing template on the Coder deployment.
77
---
88

99
# coderd_template (Data Source)
1010

11-
An existing template on the coder deployment
11+
An existing template on the Coder deployment.
1212

1313

1414

@@ -19,7 +19,7 @@ An existing template on the coder deployment
1919

2020
- `id` (String) The ID of the template to retrieve. This field will be populated if a template name is supplied.
2121
- `name` (String) The name of the template to retrieve. This field will be populated if an ID is supplied.
22-
- `organization_id` (String) ID of the organization the template is associated with.
22+
- `organization_id` (String) ID of the organization the template is associated with. This field will be populated if an ID is supplied. Defaults to the provider default organization ID.
2323

2424
### Read-Only
2525

@@ -38,7 +38,7 @@ An existing template on the coder deployment
3838
- `display_name` (String) Display name of the template.
3939
- `failure_ttl_ms` (Number) Automatic cleanup TTL for failed workspace builds.
4040
- `icon` (String) URL of the template's icon.
41-
- `require_active_version` (Boolean) Whether workspaces created from the template must be up-to-datae on the latest active version.
41+
- `require_active_version` (Boolean) Whether workspaces created from the template must be up-to-date on the latest active version.
4242
- `time_til_dormant_autodelete_ms` (Number) Duration of inactivity after the workspace becomes dormant before a workspace is automatically deleted.
4343
- `time_til_dormant_ms` (Number) Duration of inactivity before a workspace is considered dormant.
4444
- `updated_at` (Number) Unix timestamp of when the template was last updated.

internal/provider/template_resource.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ func (r *TemplateResource) Create(ctx context.Context, req resource.CreateReques
352352
return
353353
}
354354
if idx == 0 {
355+
tflog.Trace(ctx, "creating template")
355356
templateResp, err = client.CreateTemplate(ctx, orgID, codersdk.CreateTemplateRequest{
356357
Name: data.Name.ValueString(),
357358
DisplayName: data.DisplayName.ValueString(),
@@ -366,21 +367,31 @@ func (r *TemplateResource) Create(ctx context.Context, req resource.CreateReques
366367
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to create template: %s", err))
367368
return
368369
}
370+
tflog.Trace(ctx, "successfully created template", map[string]any{
371+
"id": templateResp.ID,
372+
})
369373

374+
tflog.Trace(ctx, "updating template ACL")
370375
err = client.UpdateTemplateACL(ctx, templateResp.ID, convertACLToRequest(data.ACL))
371376
if err != nil {
372377
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to update template ACL: %s", err))
373378
return
374379
}
380+
tflog.Trace(ctx, "successfully updated template ACL")
375381
}
376382
if version.Active.ValueBool() {
383+
tflog.Trace(ctx, "marking template version as active", map[string]any{
384+
"version_id": versionResp.ID,
385+
"template_id": templateResp.ID,
386+
})
377387
err := client.UpdateActiveTemplateVersion(ctx, templateResp.ID, codersdk.UpdateActiveTemplateVersion{
378388
ID: versionResp.ID,
379389
})
380390
if err != nil {
381391
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to set active template version: %s", err))
382392
return
383393
}
394+
tflog.Trace(ctx, "marked template version as active")
384395
}
385396
data.Versions[idx].ID = UUIDValue(versionResp.ID)
386397
data.Versions[idx].Name = types.StringValue(versionResp.Name)
@@ -478,6 +489,7 @@ func (r *TemplateResource) Update(ctx context.Context, req resource.UpdateReques
478489
client := r.data.Client
479490

480491
if !planState.EqualTemplateMetadata(curState) {
492+
tflog.Trace(ctx, "change in template metadata detected, updating.")
481493
_, err := client.UpdateTemplateMeta(ctx, templateID, codersdk.UpdateTemplateMeta{
482494
Name: planState.Name.ValueString(),
483495
DisplayName: planState.DisplayName.ValueString(),
@@ -491,11 +503,13 @@ func (r *TemplateResource) Update(ctx context.Context, req resource.UpdateReques
491503
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to update template: %s", err))
492504
return
493505
}
506+
tflog.Trace(ctx, "successfully updated template metadata")
494507
err = client.UpdateTemplateACL(ctx, templateID, convertACLToRequest(planState.ACL))
495508
if err != nil {
496509
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to update template ACL: %s", err))
497510
return
498511
}
512+
tflog.Trace(ctx, "successfully updated template ACL")
499513
}
500514

501515
for idx, plannedVersion := range planState.Versions {
@@ -504,6 +518,7 @@ func (r *TemplateResource) Update(ctx context.Context, req resource.UpdateReques
504518
foundVersion := curState.Versions.ByID(plannedVersion.ID)
505519
// If the version is new, or if the directory hash has changed, create a new version
506520
if foundVersion == nil || foundVersion.DirectoryHash != plannedVersion.DirectoryHash {
521+
tflog.Trace(ctx, "discovered a new or modified template version")
507522
versionResp, err := newVersion(ctx, client, newVersionRequest{
508523
Version: &plannedVersion,
509524
OrganizationID: orgID,
@@ -524,13 +539,18 @@ func (r *TemplateResource) Update(ctx context.Context, req resource.UpdateReques
524539
return
525540
}
526541
if plannedVersion.Active.ValueBool() {
542+
tflog.Trace(ctx, "marking template version as active", map[string]any{
543+
"version_id": versionResp.ID,
544+
"template_id": templateID,
545+
})
527546
err := client.UpdateActiveTemplateVersion(ctx, templateID, codersdk.UpdateActiveTemplateVersion{
528547
ID: versionResp.ID,
529548
})
530549
if err != nil {
531550
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to update active template version: %s", err))
532551
return
533552
}
553+
tflog.Trace(ctx, "marked template version as active")
534554
}
535555
planState.Versions[idx].ID = UUIDValue(versionResp.ID)
536556
}
@@ -553,6 +573,7 @@ func (r *TemplateResource) Delete(ctx context.Context, req resource.DeleteReques
553573

554574
templateID := data.ID.ValueUUID()
555575

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

713734
func newVersion(ctx context.Context, client *codersdk.Client, req newVersionRequest) (*codersdk.TemplateVersion, error) {
714735
directory := req.Version.Directory.ValueString()
736+
tflog.Trace(ctx, "uploading directory")
715737
uploadResp, err := uploadDirectory(ctx, client, slog.Make(newTFLogSink(ctx)), directory)
716738
if err != nil {
717739
return nil, fmt.Errorf("failed to upload directory: %s", err)
718740
}
741+
tflog.Trace(ctx, "successfully uploaded directory")
719742
// TODO(ethanndickson): Uncomment when a released `codersdk` exports template variable parsing
743+
// tflog.Trace(ctx,"discovering and parsing vars files")
720744
// varFiles, err := codersdk.DiscoverVarsFiles(directory)
721745
// if err != nil {
722746
// return nil, fmt.Errorf("failed to discover vars files: %s", err)
@@ -725,6 +749,9 @@ func newVersion(ctx context.Context, client *codersdk.Client, req newVersionRequ
725749
// if err != nil {
726750
// return nil, fmt.Errorf("failed to parse user variable values: %s", err)
727751
// }
752+
// tflog.Trace(ctx,"discovered and parsed vars files", map[string]any{
753+
// "vars": vars,
754+
// })
728755
vars := make([]codersdk.VariableValue, 0, len(req.Version.TerraformVariables))
729756
for _, variable := range req.Version.TerraformVariables {
730757
vars = append(vars, codersdk.VariableValue{
@@ -743,14 +770,17 @@ func newVersion(ctx context.Context, client *codersdk.Client, req newVersionRequ
743770
if req.TemplateID != nil {
744771
tmplVerReq.TemplateID = *req.TemplateID
745772
}
773+
tflog.Trace(ctx, "creating template version")
746774
versionResp, err := client.CreateTemplateVersion(ctx, req.OrganizationID, tmplVerReq)
747775
if err != nil {
748776
return nil, fmt.Errorf("failed to create template version: %s", err)
749777
}
778+
tflog.Trace(ctx, "waiting for template version import job.")
750779
err = waitForJob(ctx, client, &versionResp)
751780
if err != nil {
752781
return nil, fmt.Errorf("failed to wait for job: %s", err)
753782
}
783+
tflog.Trace(ctx, "successfully created template version")
754784
return &versionResp, nil
755785
}
756786

internal/provider/template_resource_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package provider
22

33
import (
44
"context"
5+
"os"
56
"regexp"
67
"slices"
78
"strings"
@@ -15,6 +16,9 @@ import (
1516
)
1617

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

8690
resource.Test(t, resource.TestCase{
87-
IsUnitTest: true,
8891
PreCheck: func() { testAccPreCheck(t) },
8992
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
9093
Steps: []resource.TestStep{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy