|
| 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 | +} |
0 commit comments