-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1330 from hashicorp/gs/global-run-tasks-update
Add Global Run Task support
- Loading branch information
Showing
14 changed files
with
983 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
internal/provider/data_source_organization_run_task_global_settings.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
var ( | ||
_ datasource.DataSource = &dataSourceOrganizationRunTask{} | ||
_ datasource.DataSourceWithConfigure = &dataSourceOrganizationRunTask{} | ||
) | ||
|
||
func NewOrganizationRunTaskGlobalSettingsDataSource() datasource.DataSource { | ||
return &dataSourceOrganizationRunTaskGlobalSettings{} | ||
} | ||
|
||
type dataSourceOrganizationRunTaskGlobalSettings struct { | ||
config ConfiguredClient | ||
} | ||
|
||
func (d *dataSourceOrganizationRunTaskGlobalSettings) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_organization_run_task_global_settings" | ||
} | ||
|
||
func (d *dataSourceOrganizationRunTaskGlobalSettings) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"enabled": schema.BoolAttribute{ | ||
Description: "Whether the run task will be applied globally", | ||
Optional: true, | ||
}, | ||
"enforcement_level": schema.StringAttribute{ | ||
Description: "The enforcement level of the global task.", | ||
Optional: true, | ||
}, | ||
"id": schema.StringAttribute{ | ||
Computed: true, | ||
Description: "Service-generated identifier for the task settings", | ||
}, | ||
"stages": schema.ListAttribute{ | ||
ElementType: types.StringType, | ||
Description: "Which stages the task will run in.", | ||
Optional: true, | ||
}, | ||
"task_id": schema.StringAttribute{ | ||
Description: "The id of the run task.", | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *dataSourceOrganizationRunTaskGlobalSettings) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
client, ok := req.ProviderData.(ConfiguredClient) | ||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Data Source Configure Type", | ||
fmt.Sprintf("Expected tfe.ConfiguredClient, got %T. This is a bug in the tfe provider, so please report it on GitHub.", req.ProviderData), | ||
) | ||
|
||
return | ||
} | ||
d.config = client | ||
} | ||
|
||
func (d *dataSourceOrganizationRunTaskGlobalSettings) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var data modelDataTFEOrganizationRunTaskGlobalSettings | ||
|
||
// Read Terraform configuration data into the model | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
taskID := data.TaskID.ValueString() | ||
|
||
task, err := d.config.Client.RunTasks.Read(ctx, taskID) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Error retrieving task", | ||
fmt.Sprintf("Error retrieving task %s: %s", taskID, err.Error()), | ||
) | ||
return | ||
} | ||
|
||
if task == nil { | ||
resp.Diagnostics.AddError("Error retrieving task", | ||
fmt.Sprintf("Error retrieving task %s", taskID), | ||
) | ||
return | ||
} | ||
|
||
if task.Global == nil { | ||
resp.Diagnostics.AddWarning("Error retrieving task", | ||
fmt.Sprintf("The task %s exists however it does not support global run tasks.", taskID), | ||
) | ||
return | ||
} | ||
|
||
result := dataModelFromTFEOrganizationRunTaskGlobalSettings(*task) | ||
|
||
// Save updated data into Terraform state | ||
resp.Diagnostics.Append(resp.State.Set(ctx, result)...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,17 @@ func createBusinessOrganization(t *testing.T, client *tfe.Client) (*tfe.Organiza | |
return org, orgCleanup | ||
} | ||
|
||
func createTrialOrganization(t *testing.T, client *tfe.Client) (*tfe.Organization, func()) { | ||
org, orgCleanup := createOrganization(t, client, tfe.OrganizationCreateOptions{ | ||
Name: tfe.String("tst-" + randomString(t)), | ||
Email: tfe.String(fmt.Sprintf("%[email protected]", randomString(t))), | ||
}) | ||
|
||
newSubscriptionUpdater(org).WithTrialPlan().Update(t) | ||
|
||
return org, orgCleanup | ||
} | ||
|
||
func createOrganization(t *testing.T, client *tfe.Client, options tfe.OrganizationCreateOptions) (*tfe.Organization, func()) { | ||
ctx := context.Background() | ||
org, err := client.Organizations.Create(ctx, options) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.