Skip to content
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

(TER-183) Add Description for Run Tasks #447

Merged
merged 2 commits into from Jul 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,8 @@
* Adds support for new `ExcludeTags` workspace list filter field by @Uk1288 [#438](https://github.com/hashicorp/go-tfe/pull/438)
* [beta] Adds additional filter fields to `RunListOptions` by @mjyocca [#424](https://github.com/hashicorp/go-tfe/pull/424)
* [beta] Renames the optional StateVersion field `ExtState` to `JSONStateOutputs` and changes the purpose and type by @annawinkler [#444](https://github.com/hashicorp/go-tfe/pull/444) and @brandoncroft [#452](https://github.com/hashicorp/go-tfe/pull/452)
* Remove beta messaging for Run Tasks by @glennsarti [#447](https://github.com/hashicorp/go-tfe/pull/447)
* Adds `Description` field to the `RunTask` object by @glennsarti [#447](https://github.com/hashicorp/go-tfe/pull/447)

# v1.3.0

Expand Down
8 changes: 5 additions & 3 deletions helper_test.go
Expand Up @@ -973,10 +973,12 @@ func createRunTask(t *testing.T, client *Client, org *Organization) (*RunTask, f
}

ctx := context.Background()
description := randomString(t)
r, err := client.RunTasks.Create(ctx, org.Name, RunTaskCreateOptions{
Name: "tst-" + randomString(t),
URL: runTaskURL,
Category: "task",
Name: "tst-" + randomString(t),
URL: runTaskURL,
Description: &description,
Category: "task",
})
if err != nil {
t.Fatal(err)
Expand Down
28 changes: 17 additions & 11 deletions run_task.go
Expand Up @@ -11,8 +11,7 @@ var _ RunTasks = (*runTasks)(nil)

// RunTasks represents all the run task related methods in the context of an organization
// that the Terraform Cloud/Enterprise API supports.
// **Note: This API is still in BETA and subject to change.**
// https://www.terraform.io/cloud-docs/api-docs/run-tasks#run-tasks-api
// https://www.terraform.io/cloud-docs/api-docs/run-tasks/run-tasks#run-tasks-api
type RunTasks interface {
// Create a run task for an organization
Create(ctx context.Context, organization string, options RunTaskCreateOptions) (*RunTask, error)
Expand Down Expand Up @@ -43,12 +42,13 @@ type runTasks struct {

// RunTask represents a TFC/E run task
type RunTask struct {
ID string `jsonapi:"primary,tasks"`
Name string `jsonapi:"attr,name"`
URL string `jsonapi:"attr,url"`
Category string `jsonapi:"attr,category"`
HMACKey *string `jsonapi:"attr,hmac-key,omitempty"`
Enabled bool `jsonapi:"attr,enabled"`
ID string `jsonapi:"primary,tasks"`
Name string `jsonapi:"attr,name"`
URL string `jsonapi:"attr,url"`
Description string `jsonapi:"attr,description"`
Category string `jsonapi:"attr,category"`
HMACKey *string `jsonapi:"attr,hmac-key,omitempty"`
Enabled bool `jsonapi:"attr,enabled"`

Organization *Organization `jsonapi:"relation,organization"`
WorkspaceRunTasks []*WorkspaceRunTask `jsonapi:"relation,workspace-tasks"`
Expand All @@ -61,7 +61,7 @@ type RunTaskList struct {
}

// RunTaskIncludeOpt represents the available options for include query params.
// https://www.terraform.io/cloud-docs/api-docs/run-tasks#list-run-tasks
// https://www.terraform.io/cloud-docs/api-docs/run-tasks/run-tasks#list-run-tasks
type RunTaskIncludeOpt string

const (
Expand All @@ -73,14 +73,14 @@ const (
type RunTaskListOptions struct {
ListOptions
// Optional: A list of relations to include with a run task. See available resources:
// https://www.terraform.io/cloud-docs/api-docs/run-tasks#list-run-tasks
// https://www.terraform.io/cloud-docs/api-docs/run-tasks/run-tasks#list-run-tasks
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

Include []RunTaskIncludeOpt `url:"include,omitempty"`
}

// RunTaskReadOptions represents the set of options for reading a run task
type RunTaskReadOptions struct {
// Optional: A list of relations to include with a run task. See available resources:
// https://www.terraform.io/cloud-docs/api-docs/run-tasks#list-run-tasks
// https://www.terraform.io/cloud-docs/api-docs/run-tasks/run-tasks#list-run-tasks
Include []RunTaskIncludeOpt `url:"include,omitempty"`
}

Expand All @@ -98,6 +98,9 @@ type RunTaskCreateOptions struct {
// Required: The URL to send a run task payload
URL string `jsonapi:"attr,url"`

// Optional: Description of the task
Description *string `jsonapi:"attr,description"`

// Required: Must be "task"
Category string `jsonapi:"attr,category"`

Expand All @@ -122,6 +125,9 @@ type RunTaskUpdateOptions struct {
// Optional: The URL to send a run task payload, defaults to previous value
URL *string `jsonapi:"attr,url,omitempty"`

// Optional: An optional description of the task
Description *string `jsonapi:"attr,description,omitempty"`

// Optional: Must be "task", defaults to "task"
Category *string `jsonapi:"attr,category,omitempty"`

Expand Down
55 changes: 45 additions & 10 deletions run_task_integration_test.go
Expand Up @@ -13,7 +13,7 @@ import (
)

func TestRunTasksCreate(t *testing.T) {
skipIfBeta(t)
skipIfFreeOnly(t)

client := testClient(t)
ctx := context.Background()
Expand All @@ -27,20 +27,23 @@ func TestRunTasksCreate(t *testing.T) {
}

runTaskName := "tst-runtask-" + randomString(t)
runTaskDescription := "A Run Task Description"

t.Run("add run task to organization", func(t *testing.T) {
r, err := client.RunTasks.Create(ctx, orgTest.Name, RunTaskCreateOptions{
Name: runTaskName,
URL: runTaskServerURL,
Category: "task",
Enabled: Bool(true),
Name: runTaskName,
URL: runTaskServerURL,
Description: &runTaskDescription,
Category: "task",
Enabled: Bool(true),
})
require.NoError(t, err)

assert.NotEmpty(t, r.ID)
assert.Equal(t, r.Name, runTaskName)
assert.Equal(t, r.URL, runTaskServerURL)
assert.Equal(t, r.Category, "task")
assert.Equal(t, r.Description, runTaskDescription)

t.Run("ensure org is deserialized properly", func(t *testing.T) {
assert.Equal(t, r.Organization.Name, orgTest.Name)
Expand All @@ -49,7 +52,7 @@ func TestRunTasksCreate(t *testing.T) {
}

func TestRunTasksList(t *testing.T) {
skipIfBeta(t)
skipIfFreeOnly(t)

client := testClient(t)
ctx := context.Background()
Expand All @@ -75,7 +78,7 @@ func TestRunTasksList(t *testing.T) {
}

func TestRunTasksRead(t *testing.T) {
skipIfBeta(t)
skipIfFreeOnly(t)

client := testClient(t)
ctx := context.Background()
Expand All @@ -93,6 +96,7 @@ func TestRunTasksRead(t *testing.T) {
assert.Equal(t, runTaskTest.ID, r.ID)
assert.Equal(t, runTaskTest.URL, r.URL)
assert.Equal(t, runTaskTest.Category, r.Category)
assert.Equal(t, runTaskTest.Description, r.Description)
assert.Equal(t, runTaskTest.HMACKey, r.HMACKey)
assert.Equal(t, runTaskTest.Enabled, r.Enabled)
})
Expand Down Expand Up @@ -125,7 +129,7 @@ func TestRunTasksRead(t *testing.T) {
}

func TestRunTasksUpdate(t *testing.T) {
skipIfBeta(t)
skipIfFreeOnly(t)

client := testClient(t)
ctx := context.Background()
Expand All @@ -148,10 +152,36 @@ func TestRunTasksUpdate(t *testing.T) {

assert.Equal(t, rename, r.Name)
})

t.Run("toggle enabled", func(t *testing.T) {
runTaskTest.Enabled = !runTaskTest.Enabled
r, err := client.RunTasks.Update(ctx, runTaskTest.ID, RunTaskUpdateOptions{
Enabled: &runTaskTest.Enabled,
})
require.NoError(t, err)

r, err = client.RunTasks.Read(ctx, r.ID)
require.NoError(t, err)

assert.Equal(t, runTaskTest.Enabled, r.Enabled)
})

t.Run("update description", func(t *testing.T) {
newDescription := "An updated task description"
r, err := client.RunTasks.Update(ctx, runTaskTest.ID, RunTaskUpdateOptions{
Description: &newDescription,
})
require.NoError(t, err)

r, err = client.RunTasks.Read(ctx, r.ID)
require.NoError(t, err)

assert.Equal(t, newDescription, r.Description)
})
}

func TestRunTasksDelete(t *testing.T) {
skipIfBeta(t)
skipIfFreeOnly(t)

client := testClient(t)
ctx := context.Background()
Expand Down Expand Up @@ -181,7 +211,7 @@ func TestRunTasksDelete(t *testing.T) {
}

func TestRunTasksAttachToWorkspace(t *testing.T) {
skipIfBeta(t)
skipIfFreeOnly(t)

client := testClient(t)
ctx := context.Background()
Expand All @@ -197,6 +227,11 @@ func TestRunTasksAttachToWorkspace(t *testing.T) {

t.Run("to a valid workspace", func(t *testing.T) {
wr, err := client.RunTasks.AttachToWorkspace(ctx, wkspaceTest.ID, runTaskTest.ID, Advisory)

defer func() {
client.WorkspaceRunTasks.Delete(ctx, wkspaceTest.ID, wr.ID)
}()

require.NoError(t, err)
require.NotNil(t, wr.ID)
})
Expand Down
17 changes: 10 additions & 7 deletions workspace_run_task_integration_test.go
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestWorkspaceRunTasksCreate(t *testing.T) {
skipIfBeta(t)
skipIfFreeOnly(t)

client := testClient(t)
ctx := context.Background()
Expand All @@ -33,6 +33,10 @@ func TestWorkspaceRunTasksCreate(t *testing.T) {
})

require.NoError(t, err)
defer func() {
client.WorkspaceRunTasks.Delete(ctx, wkspaceTest.ID, wr.ID)
}()

assert.NotEmpty(t, wr.ID)
assert.Equal(t, wr.EnforcementLevel, Mandatory)

Expand All @@ -43,7 +47,7 @@ func TestWorkspaceRunTasksCreate(t *testing.T) {
}

func TestWorkspaceRunTasksList(t *testing.T) {
skipIfBeta(t)
skipIfFreeOnly(t)

client := testClient(t)
ctx := context.Background()
Expand Down Expand Up @@ -77,7 +81,7 @@ func TestWorkspaceRunTasksList(t *testing.T) {
}

func TestWorkspaceRunTasksRead(t *testing.T) {
skipIfBeta(t)
skipIfFreeOnly(t)

client := testClient(t)
ctx := context.Background()
Expand Down Expand Up @@ -112,7 +116,7 @@ func TestWorkspaceRunTasksRead(t *testing.T) {
}

func TestWorkspaceRunTasksUpdate(t *testing.T) {
skipIfBeta(t)
skipIfFreeOnly(t)

client := testClient(t)
ctx := context.Background()
Expand Down Expand Up @@ -143,7 +147,7 @@ func TestWorkspaceRunTasksUpdate(t *testing.T) {
}

func TestWorkspaceRunTasksDelete(t *testing.T) {
skipIfBeta(t)
skipIfFreeOnly(t)

client := testClient(t)
ctx := context.Background()
Expand Down Expand Up @@ -173,8 +177,7 @@ func TestWorkspaceRunTasksDelete(t *testing.T) {
})

t.Run("when the workspace does not exist", func(t *testing.T) {
wkspaceTestCleanup()
err := client.WorkspaceRunTasks.Delete(ctx, wkspaceTest.ID, wrTaskTest.ID)
err := client.WorkspaceRunTasks.Delete(ctx, "does-not-exist", wrTaskTest.ID)
assert.EqualError(t, err, ErrResourceNotFound.Error())
})
}