Skip to content

Commit 5ce883c

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 de2e5b8 commit 5ce883c

File tree

5 files changed

+85
-3
lines changed

5 files changed

+85
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# UNRELEASED
2+
3+
## Enhancements
4+
25
* Adds `AllowMemberTokenManagement` permission to `Team` by @juliannatetreault [#922](https://github.com/hashicorp/go-tfe/pull/922)
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)
38

49
# v1.61.0
510

organization.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ type Entitlements struct {
164164
GlobalRunTasks bool `jsonapi:"attr,global-run-tasks"`
165165
Operations bool `jsonapi:"attr,operations"`
166166
PrivateModuleRegistry bool `jsonapi:"attr,private-module-registry"`
167+
PrivateRunTasks bool `jsonapi:"attr,private-run-tasks"`
167168
RunTasks bool `jsonapi:"attr,run-tasks"`
168169
SSO bool `jsonapi:"attr,sso"`
169170
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: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,33 @@ 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()
1818
if orgEntitlements, err := client.Organizations.ReadEntitlements(ctx, organizationName); err != nil {
19-
return false, err
19+
return nil, err
2020
} else if orgEntitlements == nil {
21-
return false, errors.New("The organization entitlements are empty.")
21+
return nil, errors.New("The organization entitlements are empty.")
22+
} else {
23+
return orgEntitlements, nil
24+
}
25+
}
26+
27+
func hasGlobalRunTasks(client *Client, organizationName string) (bool, error) {
28+
if orgEntitlements, err := getOrgEntitlements(client, organizationName); err != nil {
29+
return false, err
2230
} else {
2331
return orgEntitlements.GlobalRunTasks, nil
2432
}
2533
}
2634

35+
func hasPrivateRunTasks(client *Client, organizationName string) (bool, error) {
36+
if orgEntitlements, err := getOrgEntitlements(client, organizationName); err != nil {
37+
return false, err
38+
} else {
39+
return orgEntitlements.PrivateRunTasks, nil
40+
}
41+
}
42+
2743
func TestRunTasksCreate(t *testing.T) {
2844
client := testClient(t)
2945
ctx := context.Background()
@@ -54,6 +70,33 @@ func TestRunTasksCreate(t *testing.T) {
5470
}
5571
globalEnforce := Mandatory
5672

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

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

267333
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)