Skip to content

Commit

Permalink
{wip}
Browse files Browse the repository at this point in the history
  • Loading branch information
glennsarti committed May 3, 2024
1 parent c842c97 commit 3ac88ce
Show file tree
Hide file tree
Showing 9 changed files with 886 additions and 80 deletions.
114 changes: 114 additions & 0 deletions internal/provider/data_source_organization_run_task_global_settings.go
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)...)
}
16 changes: 9 additions & 7 deletions internal/provider/data_source_workspace_run_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,22 @@ import (
tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &dataSourceWorkspaceRunTask{}
_ datasource.DataSourceWithConfigure = &dataSourceWorkspaceRunTask{}
)

// NewWorkspaceRunTaskDataSource is a helper function to simplify the provider implementation.
func NewWorkspaceRunTaskDataSource() datasource.DataSource {
return &dataSourceWorkspaceRunTask{}
}

// dataSourceWorkspaceRunTask is the data source implementation.
type dataSourceWorkspaceRunTask struct {
config ConfiguredClient
}

// Metadata returns the data source type name.
func (d *dataSourceWorkspaceRunTask) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_workspace_run_task"
}
Expand All @@ -53,7 +50,13 @@ func (d *dataSourceWorkspaceRunTask) Schema(_ context.Context, _ datasource.Sche
Computed: true,
},
"stage": schema.StringAttribute{
Description: "Which stage the task will run in.",
DeprecationMessage: "stage is deprecated, please use stages instead",
Description: "Which stage the task will run in.",
Computed: true,
},
"stages": schema.ListAttribute{
ElementType: types.StringType,
Description: "Which stages the task will run in.",
Computed: true,
},
},
Expand All @@ -78,9 +81,8 @@ func (d *dataSourceWorkspaceRunTask) Configure(_ context.Context, req datasource
d.config = client
}

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

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
Expand Down
1 change: 1 addition & 0 deletions internal/provider/data_source_workspace_run_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestAccTFEWorkspaceRunTaskDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrSet("data.tfe_workspace_run_task.foobar", "id"),
resource.TestCheckResourceAttrSet("data.tfe_workspace_run_task.foobar", "task_id"),
resource.TestCheckResourceAttrSet("data.tfe_workspace_run_task.foobar", "workspace_id"),
resource.TestCheckResourceAttr("data.tfe_workspace_run_task.foobar", "stages.#", "1"),
),
},
},
Expand Down
11 changes: 11 additions & 0 deletions internal/provider/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("%s@tfe.local", 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)
Expand Down
4 changes: 3 additions & 1 deletion internal/provider/provider_next.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,21 @@ func (p *frameworkProvider) Configure(ctx context.Context, req provider.Configur

func (p *frameworkProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewNoCodeModuleDataSource,
NewOrganizationRunTaskDataSource,
NewOrganizationRunTaskGlobalSettingsDataSource,
NewRegistryGPGKeyDataSource,
NewRegistryGPGKeysDataSource,
NewRegistryProviderDataSource,
NewRegistryProvidersDataSource,
NewNoCodeModuleDataSource,
NewSAMLSettingsDataSource,
NewWorkspaceRunTaskDataSource,
}
}

func (p *frameworkProvider) Resources(ctx context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewOrganizationRunTaskGlobalSettingsResource,
NewOrganizationRunTaskResource,
NewRegistryGPGKeyResource,
NewRegistryProviderResource,
Expand Down

0 comments on commit 3ac88ce

Please sign in to comment.