rambleraptor commented on code in PR #10: URL: https://github.com/apache/iceberg-terraform/pull/10#discussion_r2766458770
########## internal/provider/table_schema.go: ########## @@ -0,0 +1,404 @@ +// 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" + + "github.com/apache/iceberg-go" + "github.com/hashicorp/terraform-plugin-framework/attr" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" +) + +type icebergTableSchema struct { + ID types.Int64 `tfsdk:"id"` + Fields []types.Object `tfsdk:"fields"` +} + +func (icebergTableSchema) AttrTypes() map[string]attr.Type { + return map[string]attr.Type{ + "id": types.Int64Type, + "fields": types.ListType{ + ElemType: types.ObjectType{ + AttrTypes: icebergTableSchemaField{}.AttrTypes(), + }, + }, + } +} + +type icebergTableSchemaField struct { + ID types.Int64 `tfsdk:"id"` + Name types.String `tfsdk:"name"` + Type icebergTableSchemaFieldType `tfsdk:"type"` + Required types.Bool `tfsdk:"required"` + Doc types.String `tfsdk:"doc"` +} + +func (icebergTableSchemaField) AttrTypes() map[string]attr.Type { + return map[string]attr.Type{ + "id": types.Int64Type, + "name": types.StringType, + "type": types.ObjectType{AttrTypes: icebergTableSchemaFieldType{}.AttrTypes()}, + "required": types.BoolType, + "doc": types.StringType, + } +} + +type icebergTableSchemaFieldType struct { + Primitive types.String `tfsdk:"primitive"` + List types.Object `tfsdk:"list"` + Map types.Object `tfsdk:"map"` +} + +func (icebergTableSchemaFieldType) AttrTypes() map[string]attr.Type { + return map[string]attr.Type{ + "primitive": types.StringType, + "list": types.ObjectType{AttrTypes: icebergTableSchemaFieldTypeList{}.AttrTypes()}, + "map": types.ObjectType{AttrTypes: icebergTableSchemaFieldTypeMap{}.AttrTypes()}, + } +} + +type icebergTableSchemaFieldTypeList struct { + ElementID types.Int64 `tfsdk:"element_id"` + ElementType types.String `tfsdk:"element_type"` + ElementRequired types.Bool `tfsdk:"element_required"` +} + +func (icebergTableSchemaFieldTypeList) AttrTypes() map[string]attr.Type { + return map[string]attr.Type{ + "element_id": types.Int64Type, + "element_type": types.StringType, + "element_required": types.BoolType, + } +} + +type icebergTableSchemaFieldTypeMap struct { + KeyID types.Int64 `tfsdk:"key_id"` + KeyType types.String `tfsdk:"key_type"` + ValueID types.Int64 `tfsdk:"value_id"` + ValueType types.String `tfsdk:"value_type"` + ValueRequired types.Bool `tfsdk:"value_required"` +} + +func (icebergTableSchemaFieldTypeMap) AttrTypes() map[string]attr.Type { + return map[string]attr.Type{ + "key_id": types.Int64Type, + "key_type": types.StringType, + "value_id": types.Int64Type, + "value_type": types.StringType, + "value_required": types.BoolType, + } +} + +func terraformTypeToIcebergType(typ icebergTableSchemaFieldType) (iceberg.Type, error) { + if !typ.Primitive.IsNull() { + return stringToType(typ.Primitive.ValueString()) + } + + if !typ.List.IsNull() { + var list icebergTableSchemaFieldTypeList + if err := typ.List.As(context.Background(), &list, basetypes.ObjectAsOptions{}); err.HasError() { + return nil, errors.New("failed to parse list type") + } + + elemType, err := stringToType(list.ElementType.ValueString()) + if err != nil { + return nil, err + } + + return &iceberg.ListType{ + ElementID: int(list.ElementID.ValueInt64()), + Element: elemType, + ElementRequired: list.ElementRequired.ValueBool(), + }, nil + } + + if !typ.Map.IsNull() { + var m icebergTableSchemaFieldTypeMap + if err := typ.Map.As(context.Background(), &m, basetypes.ObjectAsOptions{}); err.HasError() { + return nil, errors.New("failed to parse map type") + } + + keyType, err := stringToType(m.KeyType.ValueString()) + if err != nil { + return nil, err + } + + valueType, err := stringToType(m.ValueType.ValueString()) + if err != nil { + return nil, err + } + + return &iceberg.MapType{ + KeyID: int(m.KeyID.ValueInt64()), + KeyType: keyType, + ValueID: int(m.ValueID.ValueInt64()), + ValueType: valueType, + ValueRequired: m.ValueRequired.ValueBool(), + }, nil + } + + return nil, errors.New("unsupported type") +} + +func stringToType(s string) (iceberg.Type, error) { + switch s { + case "boolean": + return iceberg.BooleanType{}, nil + case "int": + return iceberg.Int32Type{}, nil + case "long": + return iceberg.Int64Type{}, nil + case "float": + return iceberg.Float32Type{}, nil + case "double": + return iceberg.Float64Type{}, nil + case "decimal": + return iceberg.DecimalType{}, nil + case "date": + return iceberg.DateType{}, nil + case "time": + return iceberg.TimeType{}, nil + case "timestamp": + return iceberg.TimestampType{}, nil + case "timestamptz": + return iceberg.TimestampTzType{}, nil Review Comment: Added! ########## internal/provider/resource_table.go: ########## @@ -0,0 +1,419 @@ +// 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/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/planmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier" + "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-framework/attr" + "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"` + FullProperties types.Map `tfsdk:"full_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.", + Required: 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.", + Required: true, + }, + "name": rscschema.StringAttribute{ + Description: "The field name.", + Required: true, + }, + "type": rscschema.SingleNestedAttribute{ + Description: "The field type.", + Required: true, + Attributes: map[string]rscschema.Attribute{ + "primitive": rscschema.StringAttribute{ + Description: "The primitive type.", + Optional: true, + }, + "list": rscschema.SingleNestedAttribute{ Review Comment: Added! -- 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]
