Skip to content

feat!: extract provisioner tags from coder_workspace_tags data source #15578

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 20 commits into from
Nov 25, 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
24 changes: 24 additions & 0 deletions cli/templatepush.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -415,6 +416,29 @@ func createValidTemplateVersion(inv *serpent.Invocation, args createValidTemplat
if err != nil {
return nil, err
}
var tagsJSON strings.Builder
if err := json.NewEncoder(&tagsJSON).Encode(version.Job.Tags); err != nil {
// Fall back to the less-pretty string representation.
tagsJSON.Reset()
_, _ = tagsJSON.WriteString(fmt.Sprintf("%v", version.Job.Tags))
}
if version.MatchedProvisioners.Count == 0 {
cliui.Warnf(inv.Stderr, `No provisioners are available to handle the job!
Please contact your deployment administrator for assistance.
Details:
Provisioner job ID : %s
Requested tags : %s
`, version.Job.ID, tagsJSON.String())
} else if version.MatchedProvisioners.Available == 0 {
cliui.Warnf(inv.Stderr, `All available provisioner daemons have been silent for a while.
Your build will proceed once they become available.
If this persists, please contact your deployment administrator for assistance.
Details:
Provisioner job ID : %s
Requested tags : %s
Most recently seen : %s
`, version.Job.ID, strings.TrimSpace(tagsJSON.String()), version.MatchedProvisioners.MostRecentlySeen.Time)
}

err = cliui.ProvisionerJob(inv.Context(), inv.Stdout, cliui.ProvisionerJobOptions{
Fetch: func() (codersdk.ProvisionerJob, error) {
Expand Down
86 changes: 86 additions & 0 deletions cli/templatepush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"runtime"
"strings"
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
Expand All @@ -16,9 +17,12 @@ import (
"github.com/coder/coder/v2/cli/clitest"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/provisioner/echo"
"github.com/coder/coder/v2/provisioner/terraform/tfparse"
"github.com/coder/coder/v2/provisionersdk"
"github.com/coder/coder/v2/provisionersdk/proto"
"github.com/coder/coder/v2/pty/ptytest"
"github.com/coder/coder/v2/testutil"
Expand Down Expand Up @@ -406,6 +410,88 @@ func TestTemplatePush(t *testing.T) {
t.Run("ProvisionerTags", func(t *testing.T) {
t.Parallel()

t.Run("WorkspaceTagsTerraform", func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)

// Start an instance **without** a built-in provisioner.
// We're not actually testing that the Terraform applies.
// What we test is that a provisioner job is created with the expected
// tags based on the __content__ of the Terraform.
store, ps := dbtestutil.NewDB(t)
client := coderdtest.New(t, &coderdtest.Options{
Database: store,
Pubsub: ps,
})

owner := coderdtest.CreateFirstUser(t, client)
templateAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin())

// Create a tar file with some pre-defined content
tarFile := testutil.CreateTar(t, map[string]string{
"main.tf": `
variable "a" {
type = string
default = "1"
}
data "coder_parameter" "b" {
type = string
default = "2"
}
resource "null_resource" "test" {}
data "coder_workspace_tags" "tags" {
tags = {
"foo": "bar",
"a": var.a,
"b": data.coder_parameter.b.value,
}
}`,
})

// Write the tar file to disk.
tempDir := t.TempDir()
err := tfparse.WriteArchive(tarFile, "application/x-tar", tempDir)
require.NoError(t, err)

// Run `coder templates push`
templateName := strings.ReplaceAll(testutil.GetRandomName(t), "_", "-")
var stdout, stderr strings.Builder
inv, root := clitest.New(t, "templates", "push", templateName, "-d", tempDir, "--yes")
inv.Stdout = &stdout
inv.Stderr = &stderr
clitest.SetupConfig(t, templateAdmin, root)

// Don't forget to clean up!
cancelCtx, cancel := context.WithCancel(ctx)
t.Cleanup(cancel)
done := make(chan error)
go func() {
done <- inv.WithContext(cancelCtx).Run()
}()

// Assert that a provisioner job was created with the desired tags.
wantTags := database.StringMap(provisionersdk.MutateTags(uuid.Nil, map[string]string{
"foo": "bar",
"a": "1",
"b": "2",
}))
require.Eventually(t, func() bool {
jobs, err := store.GetProvisionerJobsCreatedAfter(ctx, time.Time{})
if !assert.NoError(t, err) {
return false
}
if len(jobs) == 0 {
return false
}
return assert.EqualValues(t, wantTags, jobs[0].Tags)
}, testutil.WaitShort, testutil.IntervalSlow)

cancel()
<-done

require.Contains(t, stderr.String(), "No provisioners are available to handle the job!")
})

t.Run("ChangeTags", func(t *testing.T) {
t.Parallel()

Expand Down
21 changes: 21 additions & 0 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading