Skip to content

Commit 158da44

Browse files
committed
Update Org and Run Tasks for Private Run Tasks
This commit updates the Org. entitlements for the new Private Run Tasks feature. This commit also updates the Run Task options struct for CRUD operations to pass through the agent pool relationship
1 parent 3b35abc commit 158da44

File tree

5 files changed

+91
-6
lines changed

5 files changed

+91
-6
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Unreleased
22
* Add support for HCP Terraform `/api/v2/workspaces/{external_id}/all-vars` API endpoint to fetch the list of all variables available to a workspace (include inherited variables from varsets) by @debrin-hc [#1105](https://github.com/hashicorp/go-tfe/pull/1105)
33

4+
## Enhancements
5+
6+
* Adds `PrivateRunTasks` field to Entitlements by @glennsarti [#944](https://github.com/hashicorp/go-tfe/pull/944)
7+
* Adds `AgentPool` relationship to options when creating and updating Run Tasks by @glennsarti [#944](https://github.com/hashicorp/go-tfe/pull/944)
8+
49
# v1.82.0
510

611
## Enhancements
@@ -195,6 +200,8 @@ In the last release, Runs interface method `ListForOrganization` included pagina
195200
## Enhancements
196201

197202
* Adds `AllowMemberTokenManagement` permission to `Team` by @juliannatetreault [#922](https://github.com/hashicorp/go-tfe/pull/922)
203+
* Adds `PrivateRunTasks` field to Entitlements by @glennsarti [#944](https://github.com/hashicorp/go-tfe/pull/944)
204+
* Adds `AgentPool` relationship to options when creating and updating Run Tasks by @glennsarti [#944](https://github.com/hashicorp/go-tfe/pull/944)
198205

199206
# v1.61.0
200207

organization.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ type Entitlements struct {
165165
GlobalRunTasks bool `jsonapi:"attr,global-run-tasks"`
166166
Operations bool `jsonapi:"attr,operations"`
167167
PrivateModuleRegistry bool `jsonapi:"attr,private-module-registry"`
168+
PrivateRunTasks bool `jsonapi:"attr,private-run-tasks"`
168169
RunTasks bool `jsonapi:"attr,run-tasks"`
169170
SSO bool `jsonapi:"attr,sso"`
170171
Sentinel bool `jsonapi:"attr,sentinel"`

run_task.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type RunTask struct {
5454
Enabled bool `jsonapi:"attr,enabled"`
5555
Global *GlobalRunTask `jsonapi:"attr,global-configuration,omitempty"`
5656

57+
AgentPool *AgentPool `jsonapi:"relation,agent-pool"`
5758
Organization *Organization `jsonapi:"relation,organization"`
5859
WorkspaceRunTasks []*WorkspaceRunTask `jsonapi:"relation,workspace-tasks"`
5960
}
@@ -130,6 +131,10 @@ type RunTaskCreateOptions struct {
130131

131132
// Optional: Whether the task contains global configuration
132133
Global *GlobalRunTaskOptions `jsonapi:"attr,global-configuration,omitempty"`
134+
135+
// Optional: Whether the task will be executed using an Agent Pool
136+
// Requires the PrivateRunTasks entitlement
137+
AgentPool *AgentPool `jsonapi:"relation,agent-pool,omitempty"`
133138
}
134139

135140
// RunTaskUpdateOptions represents the set of options for updating an organization's run task
@@ -160,6 +165,10 @@ type RunTaskUpdateOptions struct {
160165

161166
// Optional: Whether the task contains global configuration
162167
Global *GlobalRunTaskOptions `jsonapi:"attr,global-configuration,omitempty"`
168+
169+
// Optional: Whether the task will be executed using an Agent Pool
170+
// Requires the PrivateRunTasks entitlement
171+
AgentPool *AgentPool `jsonapi:"relation,agent-pool,omitempty"`
163172
}
164173

165174
// Create is used to create a new run task for an organization

run_task_integration_test.go

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,32 @@ import (
1313
"github.com/stretchr/testify/require"
1414
)
1515

16-
func hasGlobalRunTasks(client *Client, organizationName string) (bool, error) {
16+
func getOrgEntitlements(client *Client, organizationName string) (*Entitlements, error) {
1717
ctx := context.Background()
18-
if orgEntitlements, err := client.Organizations.ReadEntitlements(ctx, organizationName); err != nil {
18+
orgEntitlements, err := client.Organizations.ReadEntitlements(ctx, organizationName)
19+
if err != nil {
20+
return nil, err
21+
}
22+
if orgEntitlements == nil {
23+
return nil, errors.New("The organization entitlements are empty.")
24+
}
25+
return orgEntitlements, nil
26+
}
27+
28+
func hasGlobalRunTasks(client *Client, organizationName string) (bool, error) {
29+
oe, err := getOrgEntitlements(client, organizationName)
30+
if err != nil {
1931
return false, err
20-
} else if orgEntitlements == nil {
21-
return false, errors.New("The organization entitlements are empty.")
22-
} else {
23-
return orgEntitlements.GlobalRunTasks, nil
2432
}
33+
return oe.GlobalRunTasks, nil
34+
}
35+
36+
func hasPrivateRunTasks(client *Client, organizationName string) (bool, error) {
37+
oe, err := getOrgEntitlements(client, organizationName)
38+
if err != nil {
39+
return false, err
40+
}
41+
return oe.PrivateRunTasks, nil
2542
}
2643

2744
func TestRunTasksCreate(t *testing.T) {
@@ -54,6 +71,33 @@ func TestRunTasksCreate(t *testing.T) {
5471
}
5572
globalEnforce := Mandatory
5673

74+
t.Run("with an agent pool", func(t *testing.T) {
75+
// We can only test if the org, supports private run tasks. For now this isn't
76+
// a fatal error and we just skip the test.
77+
if v, err := hasPrivateRunTasks(client, orgTest.Name); err != nil {
78+
t.Fatalf("Could not retrieve the entitlements for the test organization.: %s", err)
79+
} else if !v {
80+
t.Skip("The test organization requires the private-run-tasks entitlement but is not entitled.")
81+
return
82+
}
83+
84+
// Unfortunately when we create a Run Task it automatically verifies that the URL by sending a test payload. But
85+
// this means with an agent pool, we need an agent pool to exist, and an agent created with request forwarding enabled.
86+
// This is too much to create for this one test suite. So instead, we really only need to assert that; when the options include an
87+
// agent pool, then we expect HCP Terraform to process the agent pool. So, if we send it a nonsense agent pool ID, then we
88+
// expect an error to be returned saying that the ID was nonsense.
89+
_, err := client.RunTasks.Create(ctx, orgTest.Name, RunTaskCreateOptions{
90+
Name: runTaskName,
91+
URL: runTaskServerURL,
92+
Description: &runTaskDescription,
93+
Category: "task",
94+
AgentPool: &AgentPool{
95+
ID: "apool-this-pool-id-will-never-exist-so-we-expect-http-error-response",
96+
},
97+
})
98+
require.ErrorContains(t, err, "The provided agent pool does not exist")
99+
})
100+
57101
t.Run("add run task to organization", func(t *testing.T) {
58102
r, err := client.RunTasks.Create(ctx, orgTest.Name, RunTaskCreateOptions{
59103
Name: runTaskName,
@@ -262,6 +306,29 @@ func TestRunTasksUpdate(t *testing.T) {
262306

263307
assert.Equal(t, newDescription, r.Description)
264308
})
309+
310+
t.Run("with an agent pool", func(t *testing.T) {
311+
// We can only test if the org, supports private run tasks. For now this isn't
312+
// a fatal error and we just skip the test.
313+
if v, err := hasPrivateRunTasks(client, orgTest.Name); err != nil {
314+
t.Fatalf("Could not retrieve the entitlements for the test organization.: %s", err)
315+
} else if !v {
316+
t.Skip("The test organization requires the private-run-tasks entitlement but is not entitled.")
317+
return
318+
}
319+
320+
// Unfortunately when we update a Run Task it automatically verifies that the URL by sending a test payload. But
321+
// this means with an agent pool, we need an agent pool to exist, and an agent created with request forwarding enabled.
322+
// This is too much to create for this one test suite. So instead, we really only need to assert that; when the options include an
323+
// agent pool, then we expect HCP Terraform to process the agent pool. So, if we send it a nonsense agent pool ID, then we
324+
// expect an error to be returned saying that the ID was nonsense.
325+
_, err := client.RunTasks.Update(ctx, runTaskTest.ID, RunTaskUpdateOptions{
326+
AgentPool: &AgentPool{
327+
ID: "apool-this-pool-id-will-never-exist-so-we-expect-http-error-response",
328+
},
329+
})
330+
require.ErrorContains(t, err, "The provided agent pool does not exist")
331+
})
265332
}
266333

267334
func TestRunTasksDelete(t *testing.T) {

task_result.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ type TaskResult struct {
6666
TaskURL string `jsonapi:"attr,task-url"`
6767
WorkspaceTaskID string `jsonapi:"attr,workspace-task-id"`
6868
WorkspaceTaskEnforcementLevel TaskEnforcementLevel `jsonapi:"attr,workspace-task-enforcement-level"`
69+
AgentPoolID *string `jsonapi:"attr,agent-pool-id,omitempty"`
6970

7071
// The task stage this result belongs to
7172
TaskStage *TaskStage `jsonapi:"relation,task_stage"`

0 commit comments

Comments
 (0)