package volume import ( "context" "fmt" "net/http" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils" "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/iaas" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" "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 = &volumeDataSource{} ) // NewVolumeDataSource is a helper function to simplify the provider implementation. func NewVolumeDataSource() datasource.DataSource { return &volumeDataSource{} } // volumeDataSource is the data source implementation. type volumeDataSource struct { client *iaas.APIClient } // Metadata returns the data source type name. func (d *volumeDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = req.ProviderTypeName + "_volume" } func (d *volumeDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) if !ok { return } apiClient := iaasUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics) if resp.Diagnostics.HasError() { return } d.client = apiClient tflog.Info(ctx, "iaas client configured") } // Schema defines the schema for the resource. func (r *volumeDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { description := "Volume resource schema. Must have a `region` specified in the provider configuration." resp.Schema = schema.Schema{ MarkdownDescription: description, Description: description, Attributes: map[string]schema.Attribute{ "id": schema.StringAttribute{ Description: "Terraform's internal resource ID. It is structured as \"`project_id`,`volume_id`\".", Computed: true, }, "project_id": schema.StringAttribute{ Description: "STACKIT project ID to which the volume is associated.", Required: true, Validators: []validator.String{ validate.UUID(), validate.NoSeparator(), }, }, "volume_id": schema.StringAttribute{ Description: "The volume ID.", Required: true, Validators: []validator.String{ validate.UUID(), validate.NoSeparator(), }, }, "server_id": schema.StringAttribute{ Description: "The server ID of the server to which the volume is attached to.", Computed: true, Validators: []validator.String{ validate.UUID(), validate.NoSeparator(), }, }, "name": schema.StringAttribute{ Description: "The name of the volume.", Computed: true, }, "description": schema.StringAttribute{ Description: "The description of the volume.", Computed: true, }, "availability_zone": schema.StringAttribute{ Description: "The availability zone of the volume.", Computed: true, }, "labels": schema.MapAttribute{ Description: "Labels are key-value string pairs which can be attached to a resource container", ElementType: types.StringType, Computed: true, }, "performance_class": schema.StringAttribute{ MarkdownDescription: "The performance class of the volume. Possible values are documented in [Service plans BlockStorage](https://docs.stackit.cloud/stackit/en/service-plans-blockstorage-75137974.html#ServiceplansBlockStorage-CurrentlyavailableServicePlans%28performanceclasses%29)", Computed: true, }, "size": schema.Int64Attribute{ Description: "The size of the volume in GB. It can only be updated to a larger value than the current size", Computed: true, }, "source": schema.SingleNestedAttribute{ Description: "The source of the volume. It can be either a volume, an image, a snapshot or a backup", Computed: true, Attributes: map[string]schema.Attribute{ "type": schema.StringAttribute{ Description: "The type of the source. " + utils.SupportedValuesDocumentation(SupportedSourceTypes), Computed: true, }, "id": schema.StringAttribute{ Description: "The ID of the source, e.g. image ID", Computed: true, }, }, }, }, } } // Read refreshes the Terraform state with the latest data. func (d *volumeDataSource) 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 } projectId := model.ProjectId.ValueString() volumeId := model.VolumeId.ValueString() ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "volume_id", volumeId) volumeResp, err := d.client.GetVolume(ctx, projectId, volumeId).Execute() if err != nil { utils.LogError( ctx, &resp.Diagnostics, err, "Reading volume", fmt.Sprintf("Volume with ID %q does not exist in project %q.", volumeId, projectId), map[int]string{ http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectId), }, ) resp.State.RemoveResource(ctx) return } err = mapFields(ctx, volumeResp, &model) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading volume", fmt.Sprintf("Processing API payload: %v", err)) return } diags = resp.State.Set(ctx, model) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } tflog.Info(ctx, "volume read") }