Skip to content

chore: add template resource trace logging #44

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/data-sources/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
page_title: "coderd_template Data Source - coderd"
subcategory: ""
description: |-
An existing template on the coder deployment
An existing template on the Coder deployment.
---

# coderd_template (Data Source)

An existing template on the coder deployment
An existing template on the Coder deployment.



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

- `id` (String) The ID of the template to retrieve. This field will be populated if a template name is supplied.
- `name` (String) The name of the template to retrieve. This field will be populated if an ID is supplied.
- `organization_id` (String) ID of the organization the template is associated with.
- `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.

### Read-Only

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