terraform-provider-stackitp.../stackit/internal/services/scf/organization/datasource.go
Fabian Spottog a8e874699f
feat(scf): Add STACKIT Cloud Foundry (#991)
* onboard STACKIT Cloud Foundry resources/datasource
2025-10-08 09:42:33 +00:00

176 lines
5.8 KiB
Go

package organization
import (
"context"
"fmt"
"net/http"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/stackit-sdk-go/services/scf"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
scfUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/scf/utils"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &scfOrganizationDataSource{}
_ datasource.DataSourceWithConfigure = &scfOrganizationDataSource{}
)
// NewScfOrganizationDataSource creates a new instance of the scfOrganizationDataSource.
func NewScfOrganizationDataSource() datasource.DataSource {
return &scfOrganizationDataSource{}
}
// scfOrganizationDataSource is the datasource implementation.
type scfOrganizationDataSource struct {
client *scf.APIClient
providerData core.ProviderData
}
func (s *scfOrganizationDataSource) Configure(ctx context.Context, request datasource.ConfigureRequest, response *datasource.ConfigureResponse) {
var ok bool
s.providerData, ok = conversion.ParseProviderData(ctx, request.ProviderData, &response.Diagnostics)
if !ok {
return
}
apiClient := scfUtils.ConfigureClient(ctx, &s.providerData, &response.Diagnostics)
if response.Diagnostics.HasError() {
return
}
s.client = apiClient
tflog.Info(ctx, "scf client configured")
}
func (s *scfOrganizationDataSource) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nolint:gocritic // function signature required by Terraform
response.TypeName = request.ProviderTypeName + "_scf_organization"
}
func (s *scfOrganizationDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, response *datasource.SchemaResponse) { // nolint:gocritic // function signature required by Terraform
response.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: descriptions["id"],
Computed: true,
},
"created_at": schema.StringAttribute{
Description: descriptions["created_at"],
Computed: true,
},
"name": schema.StringAttribute{
Description: descriptions["name"],
Computed: true,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 255),
},
},
"platform_id": schema.StringAttribute{
Description: descriptions["platform_id"],
Computed: true,
Validators: []validator.String{
validate.UUID(),
validate.NoSeparator(),
},
},
"project_id": schema.StringAttribute{
Description: descriptions["project_id"],
Required: true,
Validators: []validator.String{
validate.UUID(),
validate.NoSeparator(),
},
},
"org_id": schema.StringAttribute{
Description: descriptions["org_id"],
Required: true,
Validators: []validator.String{
validate.UUID(),
validate.NoSeparator(),
},
},
"quota_id": schema.StringAttribute{
Description: descriptions["quota_id"],
Computed: true,
Validators: []validator.String{
validate.UUID(),
validate.NoSeparator(),
},
},
"region": schema.StringAttribute{
Description: descriptions["region"],
Optional: true,
Computed: true,
},
"status": schema.StringAttribute{
Description: descriptions["status"],
Computed: true,
},
"suspended": schema.BoolAttribute{
Description: descriptions["suspended"],
Computed: true,
},
"updated_at": schema.StringAttribute{
Description: descriptions["updated_at"],
Computed: true,
},
},
Description: "STACKIT Cloud Foundry organization datasource schema. Must have a `region` specified in the provider configuration.",
}
}
func (s *scfOrganizationDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform
// Retrieve the current state of the resource.
var model Model
diags := request.Config.Get(ctx, &model)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}
// Extract the project ID and instance id of the model
projectId := model.ProjectId.ValueString()
orgId := model.OrgId.ValueString()
// Extract the region
region := s.providerData.GetRegionWithOverride(model.Region)
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "org_id", orgId)
ctx = tflog.SetField(ctx, "region", region)
// Read the current scf organization via orgId
scfOrgResponse, err := s.client.GetOrganization(ctx, projectId, region, orgId).Execute()
if err != nil {
utils.LogError(
ctx,
&response.Diagnostics,
err,
"Reading scf organization",
fmt.Sprintf("Organization with ID %q does not exist in project %q.", orgId, projectId),
map[int]string{
http.StatusForbidden: fmt.Sprintf("Organization with ID %q not found or forbidden access", orgId),
},
)
response.State.RemoveResource(ctx)
return
}
err = mapFields(scfOrgResponse, &model)
if err != nil {
core.LogAndAddError(ctx, &response.Diagnostics, "Error reading scf organization", fmt.Sprintf("Processing API response: %v", err))
return
}
// Set the updated state.
diags = response.State.Set(ctx, &model)
response.Diagnostics.Append(diags...)
tflog.Info(ctx, fmt.Sprintf("read scf organization %s", orgId))
}