dttung2905 commented on code in PR #41: URL: https://github.com/apache/terraform-provider-iceberg/pull/41#discussion_r3367755077
########## internal/provider/data_source_table.go: ########## @@ -0,0 +1,385 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package provider + +import ( + "context" + "errors" + "fmt" + "strings" + + "github.com/apache/iceberg-go/catalog" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" + "github.com/hashicorp/terraform-plugin-framework/datasource" + dschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/hashicorp/terraform-plugin-framework/schema/validator" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" +) + +var _ datasource.DataSource = &icebergTableDataSource{} + +func NewTableDataSource() datasource.DataSource { + return &icebergTableDataSource{} +} + +type icebergTableDataSourceModel struct { + ID types.String `tfsdk:"id"` + Namespace types.List `tfsdk:"namespace"` + Name types.String `tfsdk:"name"` + Schema types.Object `tfsdk:"schema"` + PartitionSpec types.Object `tfsdk:"partition_spec"` + SortOrder types.Object `tfsdk:"sort_order"` + ServerProperties types.Map `tfsdk:"server_properties"` +} + +type icebergTableDataSource struct { + catalog catalog.Catalog + provider *icebergProvider +} + +func (d *icebergTableDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_table" +} + +func (d *icebergTableDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = dschema.Schema{ + Description: "Reads metadata for an existing Iceberg table from the catalog.", + Attributes: map[string]dschema.Attribute{ + "id": dschema.StringAttribute{ + Description: "Dot-separated full table identifier (namespace segments + name).", + Computed: true, + }, + "namespace": dschema.ListAttribute{ + Description: "The namespace of the table.", + Required: true, + ElementType: types.StringType, + }, + "name": dschema.StringAttribute{ + Description: "The name of the table.", + Required: true, + }, + "schema": dschema.SingleNestedAttribute{ + Description: "The current schema of the table.", + Computed: true, + Attributes: map[string]dschema.Attribute{ + "id": dschema.Int64Attribute{ + Description: "The schema ID.", + Computed: true, + }, + "fields": dschema.ListNestedAttribute{ + Description: "The fields of the schema.", + Computed: true, + NestedObject: dschema.NestedAttributeObject{ + Attributes: dataSourceSchemaFieldAttributes(4), + }, + }, + }, + }, + "partition_spec": dschema.SingleNestedAttribute{ + Description: "The partition spec of the table; null when unpartitioned.", + Computed: true, + Attributes: map[string]dschema.Attribute{ + "spec_id": dschema.Int64Attribute{ + Description: "The partition spec ID.", + Computed: true, + }, + "fields": dschema.ListNestedAttribute{ + Description: "The fields of the partition spec.", + Computed: true, + NestedObject: dschema.NestedAttributeObject{ + Attributes: map[string]dschema.Attribute{ + "source_ids": dschema.ListAttribute{ + Description: "The source field IDs.", + Computed: true, + ElementType: types.Int64Type, + }, + "field_id": dschema.Int64Attribute{ + Description: "The partition field ID.", + Computed: true, + }, + "name": dschema.StringAttribute{ + Description: "The partition field name.", + Computed: true, + }, + "transform": dschema.StringAttribute{ + Description: "The partition transform.", + Computed: true, + }, + }, + }, + }, + }, + }, + "sort_order": dschema.SingleNestedAttribute{ + Description: "The sort order of the table; null when unsorted.", + Computed: true, + Attributes: map[string]dschema.Attribute{ + "order_id": dschema.Int64Attribute{ + Description: "The sort order ID.", + Computed: true, + }, + "fields": dschema.ListNestedAttribute{ + Description: "The fields of the sort order.", + Computed: true, + NestedObject: dschema.NestedAttributeObject{ + Attributes: map[string]dschema.Attribute{ + "source_id": dschema.Int64Attribute{ + Description: "The source field ID.", + Computed: true, + }, + "transform": dschema.StringAttribute{ + Description: "The sort transform.", + Computed: true, + }, + "direction": dschema.StringAttribute{ + Description: "The sort direction (asc or desc).", + Computed: true, + Validators: []validator.String{ + stringvalidator.OneOf("asc", "desc"), + }, + }, + "null_order": dschema.StringAttribute{ + Description: "The null order (nulls-first or nulls-last).", + Computed: true, + Validators: []validator.String{ + stringvalidator.OneOf("nulls-first", "nulls-last"), + }, + }, + }, + }, + }, + }, + }, + "server_properties": dschema.MapAttribute{ + Description: "Properties from table metadata as returned by the catalog.", + Computed: true, + ElementType: types.StringType, + }, + }, + } +} + +func dataSourceSchemaFieldAttributes(depth int) map[string]dschema.Attribute { Review Comment: Thanks for the review. Thats actually a good idea, I have put it into `internal/provider/table_tfschema.go` and updated both the resource and the data source file. Can you help to check again when you have time ? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
