Skip to content

Commit ecfe3eb

Browse files
authored
Merge pull request #1330 from hashicorp/gs/global-run-tasks-update
Add Global Run Task support
2 parents 899752c + f094806 commit ecfe3eb

14 files changed

+983
-82
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ FEATURES:
44
* `r/tfe_team`: Add attribute `manage_agent_pools` to `organization_access` on `tfe_team` by @emlanctot [#1358](https://github.com/hashicorp/terraform-provider-tfe/pull/1358)
55
* `r/tfe_workspace`: Add an `auto_destroy_activity_duration` attribute for automatic scheduling of auto-destroy runs based off of workspace activity, by @notchairmk [#1377](https://github.com/hashicorp/terraform-provider-tfe/pull/1377)
66
* `d/tfe_workspace`: Add an `auto_destroy_activity_duration`, by @notchairmk [#1377](https://github.com/hashicorp/terraform-provider-tfe/pull/1377)
7+
* `d/tfe_organization_run_task_global_settings`: Add a datasource to retrieve the global settings of Run tasks, by @glennsarti [#1328](https://github.com/hashicorp/terraform-provider-tfe/pull/1330)
8+
* `r/tfe_organization_run_task_global_settings`: Add a resource to manage the global settings of Run tasks, by @glennsarti [#1328](https://github.com/hashicorp/terraform-provider-tfe/pull/1330)
9+
10+
DEPRECATIONS and BREAKING CHANGES:
11+
* `r/_workspace_run_task`: The `stage` attribute has been deprecated in favor of the `stages` attribute, by @glennsarti [#1328](https://github.com/hashicorp/terraform-provider-tfe/pull/1330)
12+
* `d/_workspace_run_task`: The `stage` attribute has been deprecated in favor of the `stages` attribute, by @glennsarti [#1328](https://github.com/hashicorp/terraform-provider-tfe/pull/1330)
713

814
## v0.56.0
915

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package provider
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"github.com/hashicorp/terraform-plugin-framework/datasource"
11+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
12+
"github.com/hashicorp/terraform-plugin-framework/types"
13+
)
14+
15+
var (
16+
_ datasource.DataSource = &dataSourceOrganizationRunTask{}
17+
_ datasource.DataSourceWithConfigure = &dataSourceOrganizationRunTask{}
18+
)
19+
20+
func NewOrganizationRunTaskGlobalSettingsDataSource() datasource.DataSource {
21+
return &dataSourceOrganizationRunTaskGlobalSettings{}
22+
}
23+
24+
type dataSourceOrganizationRunTaskGlobalSettings struct {
25+
config ConfiguredClient
26+
}
27+
28+
func (d *dataSourceOrganizationRunTaskGlobalSettings) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
29+
resp.TypeName = req.ProviderTypeName + "_organization_run_task_global_settings"
30+
}
31+
32+
func (d *dataSourceOrganizationRunTaskGlobalSettings) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
33+
resp.Schema = schema.Schema{
34+
Attributes: map[string]schema.Attribute{
35+
"enabled": schema.BoolAttribute{
36+
Description: "Whether the run task will be applied globally",
37+
Optional: true,
38+
},
39+
"enforcement_level": schema.StringAttribute{
40+
Description: "The enforcement level of the global task.",
41+
Optional: true,
42+
},
43+
"id": schema.StringAttribute{
44+
Computed: true,
45+
Description: "Service-generated identifier for the task settings",
46+
},
47+
"stages": schema.ListAttribute{
48+
ElementType: types.StringType,
49+
Description: "Which stages the task will run in.",
50+
Optional: true,
51+
},
52+
"task_id": schema.StringAttribute{
53+
Description: "The id of the run task.",
54+
Required: true,
55+
},
56+
},
57+
}
58+
}
59+
60+
func (d *dataSourceOrganizationRunTaskGlobalSettings) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
61+
if req.ProviderData == nil {
62+
return
63+
}
64+
65+
client, ok := req.ProviderData.(ConfiguredClient)
66+
if !ok {
67+
resp.Diagnostics.AddError(
68+
"Unexpected Data Source Configure Type",
69+
fmt.Sprintf("Expected tfe.ConfiguredClient, got %T. This is a bug in the tfe provider, so please report it on GitHub.", req.ProviderData),
70+
)
71+
72+
return
73+
}
74+
d.config = client
75+
}
76+
77+
func (d *dataSourceOrganizationRunTaskGlobalSettings) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
78+
var data modelDataTFEOrganizationRunTaskGlobalSettings
79+
80+
// Read Terraform configuration data into the model
81+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
82+
if resp.Diagnostics.HasError() {
83+
return
84+
}
85+
86+
taskID := data.TaskID.ValueString()
87+
88+
task, err := d.config.Client.RunTasks.Read(ctx, taskID)
89+
if err != nil {
90+
resp.Diagnostics.AddError("Error retrieving task",
91+
fmt.Sprintf("Error retrieving task %s: %s", taskID, err.Error()),
92+
)
93+
return
94+
}
95+
96+
if task == nil {
97+
resp.Diagnostics.AddError("Error retrieving task",
98+
fmt.Sprintf("Error retrieving task %s", taskID),
99+
)
100+
return
101+
}
102+
103+
if task.Global == nil {
104+
resp.Diagnostics.AddWarning("Error retrieving task",
105+
fmt.Sprintf("The task %s exists however it does not support global run tasks.", taskID),
106+
)
107+
return
108+
}
109+
110+
result := dataModelFromTFEOrganizationRunTaskGlobalSettings(*task)
111+
112+
// Save updated data into Terraform state
113+
resp.Diagnostics.Append(resp.State.Set(ctx, result)...)
114+
}

internal/provider/data_source_workspace_run_task.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,22 @@ import (
1010
tfe "github.com/hashicorp/go-tfe"
1111
"github.com/hashicorp/terraform-plugin-framework/datasource"
1212
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
13+
"github.com/hashicorp/terraform-plugin-framework/types"
1314
)
1415

15-
// Ensure the implementation satisfies the expected interfaces.
1616
var (
1717
_ datasource.DataSource = &dataSourceWorkspaceRunTask{}
1818
_ datasource.DataSourceWithConfigure = &dataSourceWorkspaceRunTask{}
1919
)
2020

21-
// NewWorkspaceRunTaskDataSource is a helper function to simplify the provider implementation.
2221
func NewWorkspaceRunTaskDataSource() datasource.DataSource {
2322
return &dataSourceWorkspaceRunTask{}
2423
}
2524

26-
// dataSourceWorkspaceRunTask is the data source implementation.
2725
type dataSourceWorkspaceRunTask struct {
2826
config ConfiguredClient
2927
}
3028

31-
// Metadata returns the data source type name.
3229
func (d *dataSourceWorkspaceRunTask) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
3330
resp.TypeName = req.ProviderTypeName + "_workspace_run_task"
3431
}
@@ -53,7 +50,13 @@ func (d *dataSourceWorkspaceRunTask) Schema(_ context.Context, _ datasource.Sche
5350
Computed: true,
5451
},
5552
"stage": schema.StringAttribute{
56-
Description: "Which stage the task will run in.",
53+
DeprecationMessage: "stage is deprecated, please use stages instead",
54+
Description: "Which stage the task will run in.",
55+
Computed: true,
56+
},
57+
"stages": schema.ListAttribute{
58+
ElementType: types.StringType,
59+
Description: "Which stages the task will run in.",
5760
Computed: true,
5861
},
5962
},
@@ -78,9 +81,8 @@ func (d *dataSourceWorkspaceRunTask) Configure(_ context.Context, req datasource
7881
d.config = client
7982
}
8083

81-
// Read refreshes the Terraform state with the latest data.
8284
func (d *dataSourceWorkspaceRunTask) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
83-
var data modelTFEWorkspaceRunTaskV0
85+
var data modelTFEWorkspaceRunTaskV1
8486

8587
// Read Terraform configuration data into the model
8688
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

internal/provider/data_source_workspace_run_task_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func TestAccTFEWorkspaceRunTaskDataSource_basic(t *testing.T) {
3737
resource.TestCheckResourceAttrSet("data.tfe_workspace_run_task.foobar", "id"),
3838
resource.TestCheckResourceAttrSet("data.tfe_workspace_run_task.foobar", "task_id"),
3939
resource.TestCheckResourceAttrSet("data.tfe_workspace_run_task.foobar", "workspace_id"),
40+
resource.TestCheckResourceAttr("data.tfe_workspace_run_task.foobar", "stages.#", "1"),
4041
),
4142
},
4243
},

internal/provider/helper_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ func createBusinessOrganization(t *testing.T, client *tfe.Client) (*tfe.Organiza
6666
return org, orgCleanup
6767
}
6868

69+
func createTrialOrganization(t *testing.T, client *tfe.Client) (*tfe.Organization, func()) {
70+
org, orgCleanup := createOrganization(t, client, tfe.OrganizationCreateOptions{
71+
Name: tfe.String("tst-" + randomString(t)),
72+
Email: tfe.String(fmt.Sprintf("%[email protected]", randomString(t))),
73+
})
74+
75+
newSubscriptionUpdater(org).WithTrialPlan().Update(t)
76+
77+
return org, orgCleanup
78+
}
79+
6980
func createOrganization(t *testing.T, client *tfe.Client, options tfe.OrganizationCreateOptions) (*tfe.Organization, func()) {
7081
ctx := context.Background()
7182
org, err := client.Organizations.Create(ctx, options)

internal/provider/provider_next.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,21 @@ func (p *frameworkProvider) Configure(ctx context.Context, req provider.Configur
123123

124124
func (p *frameworkProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
125125
return []func() datasource.DataSource{
126+
NewNoCodeModuleDataSource,
126127
NewOrganizationRunTaskDataSource,
128+
NewOrganizationRunTaskGlobalSettingsDataSource,
127129
NewRegistryGPGKeyDataSource,
128130
NewRegistryGPGKeysDataSource,
129131
NewRegistryProviderDataSource,
130132
NewRegistryProvidersDataSource,
131-
NewNoCodeModuleDataSource,
132133
NewSAMLSettingsDataSource,
133134
NewWorkspaceRunTaskDataSource,
134135
}
135136
}
136137

137138
func (p *frameworkProvider) Resources(ctx context.Context) []func() resource.Resource {
138139
return []func() resource.Resource{
140+
NewOrganizationRunTaskGlobalSettingsResource,
139141
NewOrganizationRunTaskResource,
140142
NewRegistryGPGKeyResource,
141143
NewRegistryProviderResource,

0 commit comments

Comments
 (0)