zeroshade commented on code in PR #10: URL: https://github.com/apache/iceberg-terraform/pull/10#discussion_r2765740887
########## dev/docker-compose.yml: ########## @@ -0,0 +1,52 @@ +// 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. + +version: "3" +services: + rest: + image: tabulario/iceberg-rest:0.10.0 Review Comment: we should use `apache/iceberg-rest-fixture` instead, see https://github.com/apache/iceberg-go/blob/main/internal/recipe/docker-compose.yml#L40 ########## examples/main.tf: ########## @@ -0,0 +1,62 @@ +// 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. + +provider "iceberg" { + catalog_uri = "http://localhost:8181" +} Review Comment: same as my comment on #8, should we allow a "type" parameter so that we can easily add non-rest catalog handling in the future? ########## 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"` Review Comment: what about user-defined input properties? ########## 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{ Review Comment: instead of ``` type = { primitive = "long" } ``` can we instead be able to do ``` type = "long" ``` That seems like it would be much better/easier than requiring an object and having "primitive" like this ########## 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: struct type? ########## 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 Review Comment: more information is needed to use decimal which doesn't seem to be available in the current configuration. Decimal needs to be parsed as `decimal(prec, scale)` or otherwise needs a user to be able to define them. We cannot accept "decimal" without specifying the precision and scale. ########## 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{ + Description: "The 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": rscschema.SingleNestedAttribute{ + Description: "The 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, + }, + }, + }, + }, + }, + "required": rscschema.BoolAttribute{ + Description: "Whether the field is required.", + Required: true, + }, + "doc": rscschema.StringAttribute{ + Description: "The field documentation.", + Optional: true, + }, + }, + }, + }, + }, + }, + "full_properties": rscschema.MapAttribute{ + Description: "Full properties returned by IRC for the table. Cannot be set by users.", + Computed: true, + ElementType: types.StringType, + }, + }, + } +} + +func (r *icebergTableResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + provider, ok := req.ProviderData.(*icebergProvider) + if !ok { + resp.Diagnostics.AddError( + "Unexpected Resource Configure Type", + "Expected *icebergProvider, got: %T. Please report this issue to the provider developers.", + ) + return + } + + r.provider = provider +} + +func (r *icebergTableResource) ConfigureCatalog(ctx context.Context, diags *diag.Diagnostics) { + if r.catalog != nil { + return + } + + if r.provider == nil { + diags.AddError( + "Provider not configured", + "The provider hasn't been configured before this operation", + ) + return + } + + catalog, err := r.provider.NewCatalog(ctx) + if err != nil { + diags.AddError( + "Failed to create catalog", + "Failed to create catalog: "+err.Error(), + ) + return + } + r.catalog = catalog +} + +func (r *icebergTableResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + r.ConfigureCatalog(ctx, &resp.Diagnostics) + if resp.Diagnostics.HasError() { + return + } + + var data icebergTableResourceModel + + diags := req.Plan.Get(ctx, &data) + resp.Diagnostics.Append(diags...) + + if resp.Diagnostics.HasError() { + return + } + + var namespaceName []string + diags = data.Namespace.ElementsAs(ctx, &namespaceName, false) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tableName := data.Name.ValueString() + tableIdent := catalog.ToIdentifier(append(namespaceName, tableName)...) Review Comment: isn't this equivalent to `catalog.ToIdentiifer(namespaceName, tableName)`? We don't need the `append` here ########## 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{ + Description: "The 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": rscschema.SingleNestedAttribute{ + Description: "The 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, + }, + }, + }, + }, + }, + "required": rscschema.BoolAttribute{ + Description: "Whether the field is required.", + Required: true, + }, + "doc": rscschema.StringAttribute{ + Description: "The field documentation.", + Optional: true, + }, + }, + }, + }, + }, + }, + "full_properties": rscschema.MapAttribute{ + Description: "Full properties returned by IRC for the table. Cannot be set by users.", + Computed: true, + ElementType: types.StringType, + }, + }, + } +} + +func (r *icebergTableResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + provider, ok := req.ProviderData.(*icebergProvider) + if !ok { + resp.Diagnostics.AddError( + "Unexpected Resource Configure Type", + "Expected *icebergProvider, got: %T. Please report this issue to the provider developers.", + ) + return + } + + r.provider = provider +} + +func (r *icebergTableResource) ConfigureCatalog(ctx context.Context, diags *diag.Diagnostics) { + if r.catalog != nil { + return + } + + if r.provider == nil { + diags.AddError( + "Provider not configured", + "The provider hasn't been configured before this operation", + ) + return + } + + catalog, err := r.provider.NewCatalog(ctx) + if err != nil { + diags.AddError( + "Failed to create catalog", + "Failed to create catalog: "+err.Error(), + ) + return + } + r.catalog = catalog +} + +func (r *icebergTableResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + r.ConfigureCatalog(ctx, &resp.Diagnostics) + if resp.Diagnostics.HasError() { + return + } + + var data icebergTableResourceModel + + diags := req.Plan.Get(ctx, &data) + resp.Diagnostics.Append(diags...) + + if resp.Diagnostics.HasError() { + return + } + + var namespaceName []string + diags = data.Namespace.ElementsAs(ctx, &namespaceName, false) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tableName := data.Name.ValueString() + tableIdent := catalog.ToIdentifier(append(namespaceName, tableName)...) + + var schema icebergTableSchema + diags = data.Schema.As(ctx, &schema, basetypes.ObjectAsOptions{}) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + fields := make([]iceberg.NestedField, len(schema.Fields)) + for i, fieldObj := range schema.Fields { + var field icebergTableSchemaField + diags = fieldObj.As(ctx, &field, basetypes.ObjectAsOptions{}) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + typ, err := terraformTypeToIcebergType(field.Type) + if err != nil { + resp.Diagnostics.AddError("invalid field type", err.Error()) + return + } + + fields[i] = iceberg.NestedField{ + ID: int(field.ID.ValueInt64()), + Name: field.Name.ValueString(), + Type: typ, + Required: field.Required.ValueBool(), + Doc: field.Doc.ValueString(), + } + } + + tblSchema := iceberg.NewSchema(int(schema.ID.ValueInt64()), fields...) + + // TODO: Add PartitionSpec support + tbl, err := r.catalog.CreateTable(ctx, tableIdent, tblSchema) + if err != nil { + resp.Diagnostics.AddError("failed to create table", err.Error()) + return + } + + data.ID = types.StringValue(strings.Join(tableIdent, ".")) + + loadedProperties, diags := types.MapValueFrom(ctx, types.StringType, tbl.Properties()) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + data.FullProperties = loadedProperties + + diags = resp.State.Set(ctx, &data) + resp.Diagnostics.Append(diags...) +} + +func (r *icebergTableResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + r.ConfigureCatalog(ctx, &resp.Diagnostics) + if resp.Diagnostics.HasError() { + return + } + + var data icebergTableResourceModel + + tflog.Info(ctx, "Reading table resource") + diags := req.State.Get(ctx, &data) + resp.Diagnostics.Append(diags...) + + if resp.Diagnostics.HasError() { + return + } + + var namespaceName []string + diags = data.Namespace.ElementsAs(ctx, &namespaceName, false) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tableName := data.Name.ValueString() + tableIdent := catalog.ToIdentifier(append(namespaceName, tableName)...) + + tbl, err := r.catalog.LoadTable(ctx, tableIdent) + if err != nil { + if errors.Is(err, catalog.ErrNoSuchTable) { + resp.State.RemoveResource(ctx) + return + } + resp.Diagnostics.AddError("failed to load table", err.Error()) + return + } + + fullProperties, diags := types.MapValueFrom(ctx, types.StringType, tbl.Properties()) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + data.FullProperties = fullProperties + + icebergSchema := tbl.Schema() + fields := make([]attr.Value, len(icebergSchema.Fields())) + for i, field := range icebergSchema.Fields() { + terraformType, err := icebergTypeToTerraformType(field.Type) + if err != nil { + resp.Diagnostics.AddError("failed to convert iceberg type to terraform type", err.Error()) + return + } + + fields[i] = types.ObjectValueMust( + icebergTableSchemaField{}.AttrTypes(), + map[string]attr.Value{ + "id": types.Int64Value(int64(field.ID)), + "name": types.StringValue(field.Name), + "type": terraformType, + "required": types.BoolValue(field.Required), + "doc": types.StringValue(field.Doc), + }, + ) + } + data.Schema = types.ObjectValueMust( + icebergTableSchema{}.AttrTypes(), + map[string]attr.Value{ + "id": types.Int64Value(int64(icebergSchema.ID)), + "fields": types.ListValueMust(types.ObjectType{AttrTypes: icebergTableSchemaField{}.AttrTypes()}, fields), + }, + ) + + diags = resp.State.Set(ctx, &data) + resp.Diagnostics.Append(diags...) +} + +func (r *icebergTableResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + r.ConfigureCatalog(ctx, &resp.Diagnostics) + if resp.Diagnostics.HasError() { + return + } + // Not implemented yet Review Comment: how do we tell terraform this isn't implemented yet? ########## examples/main.tf: ########## @@ -0,0 +1,62 @@ +// 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. + +provider "iceberg" { + catalog_uri = "http://localhost:8181" +} + +resource "iceberg_namespace" "example" { + name = ["example_namespace"] + description = "An example namespace" +} + +resource "iceberg_table" "example" { + namespace = iceberg_namespace.example.name + name = "example_table" + + schema = { + id = 0 Review Comment: can the ids be optional and then we let it auto assign IDs? ########## 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 + case "string": + return iceberg.StringType{}, nil + case "uuid": + return iceberg.UUIDType{}, nil + case "fixed": + return iceberg.FixedType{}, nil + case "binary": + return iceberg.BinaryType{}, nil + } + + return nil, errors.New("unsupported type: " + s) +} + +func typeToString(t iceberg.Type) (string, error) { + switch t.(type) { + case iceberg.BooleanType: + return "boolean", nil + case iceberg.Int32Type: + return "int", nil + case iceberg.Int64Type: + return "long", nil + case iceberg.Float32Type: + return "float", nil + case iceberg.Float64Type: + return "double", nil + case iceberg.DecimalType: + return "decimal", nil Review Comment: this should just be `return t.String()` shouldn't it? ########## 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{ + Description: "The 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": rscschema.SingleNestedAttribute{ + Description: "The 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, + }, + }, + }, + }, + }, + "required": rscschema.BoolAttribute{ + Description: "Whether the field is required.", + Required: true, + }, + "doc": rscschema.StringAttribute{ + Description: "The field documentation.", + Optional: true, + }, + }, + }, + }, + }, + }, Review Comment: missing user-defined properties. properties can be set on the namespace and can be set separately on tables, etc. ########## 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"` Review Comment: Struct? ########## 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: `timestamp_ns` and `timestamptz_ns`? ########## go.mod: ########## @@ -13,525 +13,184 @@ // See the License for the specific language governing permissions and // limitations under the License. -module github.com/apache/iceberg-terraform +module github.com/apache/terraform-provider-iceberg Review Comment: why this change? Are we going to change the URL of this repo appropriately? ########## 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 + case "string": + return iceberg.StringType{}, nil + case "uuid": + return iceberg.UUIDType{}, nil + case "fixed": + return iceberg.FixedType{}, nil Review Comment: the `fixed` type requires a length which should be provided either by parsing `fixed(L)` or in some other way. ########## 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 + case "string": + return iceberg.StringType{}, nil + case "uuid": + return iceberg.UUIDType{}, nil + case "fixed": + return iceberg.FixedType{}, nil + case "binary": + return iceberg.BinaryType{}, nil + } + + return nil, errors.New("unsupported type: " + s) +} + +func typeToString(t iceberg.Type) (string, error) { + switch t.(type) { + case iceberg.BooleanType: + return "boolean", nil + case iceberg.Int32Type: + return "int", nil + case iceberg.Int64Type: + return "long", nil + case iceberg.Float32Type: + return "float", nil + case iceberg.Float64Type: + return "double", nil + case iceberg.DecimalType: + return "decimal", nil + case iceberg.DateType: + return "date", nil + case iceberg.TimeType: + return "time", nil + case iceberg.TimestampType: + return "timestamp", nil + case iceberg.TimestampTzType: + return "timestamptz", nil + case iceberg.StringType: + return "string", nil + case iceberg.UUIDType: + return "uuid", nil + case iceberg.FixedType: + return "fixed", nil + case iceberg.BinaryType: + return "binary", nil + } + return "", errors.New("unsupported type or nested type inside list/map not supported by this provider version") +} + +func icebergTypeToTerraformType(t iceberg.Type) (attr.Value, error) { + switch typ := t.(type) { + case iceberg.BooleanType: + return types.ObjectValueMust( + icebergTableSchemaFieldType{}.AttrTypes(), + map[string]attr.Value{ + "primitive": types.StringValue("boolean"), + "list": types.ObjectNull(icebergTableSchemaFieldTypeList{}.AttrTypes()), + "map": types.ObjectNull(icebergTableSchemaFieldTypeMap{}.AttrTypes()), + }, + ), nil Review Comment: Could we just do ```go switch typ := t.(type) { case iceberg.PrimitiveType: return types.ObjectValueMust( icebergTableSchemaFieldType{}.AttrTypes(), map[string]attr.Value{ "primitive": types.StringValue(typ.String()), "list": types.ObjectNull(icebergTableSchemaFieldTypeList{}.AttrTypes()), "map": types.ObjectNull(icebergTableSchemaFieldTypeList{}.AttrTypes()), }, ), nil case *iceberg.ListType: // .... case *iceberg.MapType: // ... case *iceberg.StructType: // .... } ``` -- 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]
