179 lines
6.5 KiB
Go
179 lines
6.5 KiB
Go
package folder
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"regexp"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework-validators/mapvalidator"
|
|
"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-framework/types"
|
|
"github.com/hashicorp/terraform-plugin-log/tflog"
|
|
"github.com/stackitcloud/stackit-sdk-go/services/resourcemanager"
|
|
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
|
|
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
|
resourcemanagerUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/resourcemanager/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 = &folderDataSource{}
|
|
_ datasource.DataSourceWithConfigure = &folderDataSource{}
|
|
)
|
|
|
|
// NewFolderDataSource is a helper function to simplify the provider implementation.
|
|
func NewFolderDataSource() datasource.DataSource {
|
|
return &folderDataSource{}
|
|
}
|
|
|
|
// folderDataSource is the data source implementation.
|
|
type folderDataSource struct {
|
|
client *resourcemanager.APIClient
|
|
}
|
|
|
|
// Metadata returns the data source type name.
|
|
func (d *folderDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
|
resp.TypeName = req.ProviderTypeName + "_resourcemanager_folder"
|
|
}
|
|
|
|
func (d *folderDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
|
providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
apiClient := resourcemanagerUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
d.client = apiClient
|
|
tflog.Info(ctx, "Resource Manager client configured")
|
|
}
|
|
|
|
// Schema defines the schema for the data source.
|
|
func (d *folderDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
|
descriptions := map[string]string{
|
|
"main": "Resource Manager folder data source schema. To identify the folder, you need to provide the container_id.",
|
|
"id": "Terraform's internal resource ID. It is structured as \"`container_id`\".",
|
|
"container_id": "Folder container ID. Globally unique, user-friendly identifier.",
|
|
"folder_id": "Folder UUID identifier. Globally unique folder identifier",
|
|
"parent_container_id": "Parent resource identifier. Both container ID (user-friendly) and UUID are supported.",
|
|
"name": "The name of the folder.",
|
|
"labels": "Labels are key-value string pairs which can be attached to a resource container. A label key must match the regex [A-ZÄÜÖa-zäüöß0-9_-]{1,64}. A label value must match the regex ^$|[A-ZÄÜÖa-zäüöß0-9_-]{1,64}.",
|
|
"owner_email": "Email address of the owner of the folder. This value is only considered during creation. Changing it afterwards will have no effect.",
|
|
"creation_time": "Date-time at which the folder was created.",
|
|
"update_time": "Date-time at which the folder was last modified.",
|
|
}
|
|
|
|
resp.Schema = schema.Schema{
|
|
Description: descriptions["main"],
|
|
Attributes: map[string]schema.Attribute{
|
|
"id": schema.StringAttribute{
|
|
Description: descriptions["id"],
|
|
Computed: true,
|
|
},
|
|
"container_id": schema.StringAttribute{
|
|
Description: descriptions["container_id"],
|
|
Validators: []validator.String{
|
|
validate.NoSeparator(),
|
|
},
|
|
Required: true,
|
|
},
|
|
"folder_id": schema.StringAttribute{
|
|
Description: descriptions["folder_id"],
|
|
Computed: true,
|
|
Validators: []validator.String{
|
|
validate.UUID(),
|
|
},
|
|
},
|
|
"parent_container_id": schema.StringAttribute{
|
|
Description: descriptions["parent_container_id"],
|
|
Computed: true,
|
|
Validators: []validator.String{
|
|
validate.NoSeparator(),
|
|
},
|
|
},
|
|
"name": schema.StringAttribute{
|
|
Description: descriptions["name"],
|
|
Computed: true,
|
|
Validators: []validator.String{
|
|
stringvalidator.LengthAtLeast(1),
|
|
stringvalidator.LengthAtMost(63),
|
|
},
|
|
},
|
|
"labels": schema.MapAttribute{
|
|
Description: descriptions["labels"],
|
|
ElementType: types.StringType,
|
|
Computed: true,
|
|
Validators: []validator.Map{
|
|
mapvalidator.KeysAre(
|
|
stringvalidator.RegexMatches(
|
|
regexp.MustCompile(`[A-ZÄÜÖa-zäüöß0-9_-]{1,64}`),
|
|
"must match expression"),
|
|
),
|
|
mapvalidator.ValueStringsAre(
|
|
stringvalidator.RegexMatches(
|
|
regexp.MustCompile(`[A-ZÄÜÖa-zäüöß0-9_-]{1,64}`),
|
|
"must match expression"),
|
|
),
|
|
},
|
|
},
|
|
"creation_time": schema.StringAttribute{
|
|
Description: descriptions["creation_time"],
|
|
Computed: true,
|
|
},
|
|
"update_time": schema.StringAttribute{
|
|
Description: descriptions["update_time"],
|
|
Computed: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// Read refreshes the Terraform state with the latest data.
|
|
func (d *folderDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform
|
|
var model Model
|
|
diags := req.Config.Get(ctx, &model)
|
|
resp.Diagnostics.Append(diags...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
containerId := model.ContainerId.ValueString()
|
|
ctx = tflog.SetField(ctx, "container_id", containerId)
|
|
|
|
folderResp, err := d.client.GetFolderDetails(ctx, containerId).Execute()
|
|
if err != nil {
|
|
utils.LogError(
|
|
ctx,
|
|
&resp.Diagnostics,
|
|
err,
|
|
"Reading folder",
|
|
fmt.Sprintf("folder with ID %q does not exist.", containerId),
|
|
map[int]string{
|
|
http.StatusForbidden: fmt.Sprintf("folder with ID %q not found or forbidden access", containerId),
|
|
},
|
|
)
|
|
resp.State.RemoveResource(ctx)
|
|
return
|
|
}
|
|
|
|
err = mapFolderFields(ctx, folderResp, &model, &resp.State)
|
|
if err != nil {
|
|
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading folder", fmt.Sprintf("Processing API response: %v", err))
|
|
return
|
|
}
|
|
|
|
diags = resp.State.Set(ctx, &model)
|
|
resp.Diagnostics.Append(diags...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
tflog.Info(ctx, "Resource Manager folder read")
|
|
}
|