rambleraptor commented on code in PR #10: URL: https://github.com/apache/iceberg-terraform/pull/10#discussion_r2801802836
########## internal/provider/resource_table.go: ########## @@ -0,0 +1,564 @@ +// 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" + "strings" + + "github.com/apache/iceberg-go" + "github.com/apache/iceberg-go/catalog" + "github.com/hashicorp/terraform-plugin-framework/attr" + "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/hashicorp/terraform-plugin-framework/resource" + rscschema "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" + "github.com/hashicorp/terraform-plugin-log/tflog" +) + +var ( + _ resource.Resource = &icebergTableResource{} +) + +func NewTableResource() resource.Resource { + return &icebergTableResource{} +} + +type icebergTableResourceModel struct { + ID types.String `tfsdk:"id"` + Namespace types.List `tfsdk:"namespace"` + Name types.String `tfsdk:"name"` + Schema types.Object `tfsdk:"schema"` + UserProperties types.Map `tfsdk:"user_properties"` + ServerProperties types.Map `tfsdk:"server_properties"` +} + +type icebergTableResource struct { + catalog catalog.Catalog + provider *icebergProvider +} + +func (r *icebergTableResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_table" +} + +func (r *icebergTableResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = rscschema.Schema{ + Description: "A resource for managing Iceberg tables.", + Attributes: map[string]rscschema.Attribute{ + "id": rscschema.StringAttribute{ + Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "namespace": rscschema.ListAttribute{ + Description: "The namespace of the table.", + Required: true, + ElementType: types.StringType, + }, + "name": rscschema.StringAttribute{ + Description: "The name of the table.", + Required: true, + }, + "schema": rscschema.SingleNestedAttribute{ + Description: "The schema of the table.", + Required: true, + // TODO: Update schema in place instead of replacement + PlanModifiers: []planmodifier.Object{ + objectplanmodifier.RequiresReplace(), + }, + Attributes: map[string]rscschema.Attribute{ + "id": rscschema.Int64Attribute{ + Description: "The schema ID.", + Optional: true, + Computed: true, + }, + "fields": rscschema.ListNestedAttribute{ + Description: "The fields of the schema.", + Required: true, + NestedObject: rscschema.NestedAttributeObject{ + Attributes: map[string]rscschema.Attribute{ + "id": rscschema.Int64Attribute{ + Description: "The field ID.", + Optional: true, + Computed: true, + }, + "name": rscschema.StringAttribute{ + Description: "The field name.", + Required: true, + }, + "type": rscschema.StringAttribute{ + Description: "The field type.", + Required: true, + }, + "decimal_properties": rscschema.SingleNestedAttribute{ + Description: "Properties for decimal type.", + Optional: true, + Attributes: map[string]rscschema.Attribute{ + "precision": rscschema.Int64Attribute{ + Description: "The precision of the decimal.", + Required: true, + }, + "scale": rscschema.Int64Attribute{ + Description: "The scale of the decimal.", + Required: true, + }, + }, + }, + "fixed_properties": rscschema.SingleNestedAttribute{ + Description: "Properties for fixed type.", + Optional: true, + Attributes: map[string]rscschema.Attribute{ + "length": rscschema.Int64Attribute{ + Description: "The length of the fixed type.", + Required: true, + }, + }, + }, + "list_properties": rscschema.SingleNestedAttribute{ + Description: "Properties for list type.", + Optional: true, + Attributes: map[string]rscschema.Attribute{ + "element_id": rscschema.Int64Attribute{ + Description: "The list element id.", + Required: true, + }, + "element_type": rscschema.StringAttribute{ + Description: "The list element type.", + Required: true, + }, + "element_required": rscschema.BoolAttribute{ + Description: "Whether the list element is required.", + Required: true, + }, + }, + }, + "map_properties": rscschema.SingleNestedAttribute{ + Description: "Properties for map type.", + Optional: true, + Attributes: map[string]rscschema.Attribute{ + "key_id": rscschema.Int64Attribute{ + Description: "The map key id.", + Required: true, + }, + "key_type": rscschema.StringAttribute{ + Description: "The map key type.", + Required: true, + }, + "value_id": rscschema.Int64Attribute{ + Description: "The map value id.", + Required: true, + }, + "value_type": rscschema.StringAttribute{ + Description: "The map value type.", + Required: true, + }, + "value_required": rscschema.BoolAttribute{ + Description: "Whether the map value is required.", + Required: true, + }, + }, + }, + "struct_properties": rscschema.SingleNestedAttribute{ Review Comment: I found a way to use JSON as an intermediary, which simplifies the code greatly. Is that what you had in mind? (Terraform [cannot handle recursive schemas](https://discuss.hashicorp.com/t/recurisve-schema-using-terraform-plugin-framework/46952/2?u=bendbennett), so we have to have some limit to the amount of recursion we can handle. There's some workarounds, but the traditional one is to just pick an arbitrary limit to the amount of recursive layers) -- 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]
