rambleraptor commented on code in PR #8: URL: https://github.com/apache/iceberg-terraform/pull/8#discussion_r2683869860
########## internal/provider/resource_namespace.go: ########## @@ -0,0 +1,289 @@ +// 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/resource" + "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/stringplanmodifier" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" +) + +var ( + _ resource.Resource = &icebergNamespaceResource{} +) + +func NewNamespaceResource() resource.Resource { + return &icebergNamespaceResource{} +} + +type icebergNamespaceResourceModel struct { + ID types.String `tfsdk:"id"` + Name types.List `tfsdk:"name"` + Description types.String `tfsdk:"description"` + FullProperties types.Map `tfsdk:"full_properties"` +} + +type icebergNamespaceResource struct { + catalog catalog.Catalog +} + +func (r *icebergNamespaceResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_namespace" +} + +func (r *icebergNamespaceResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = schema.Schema{ + Description: "A resource for managing Iceberg namespaces.", + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "name": schema.ListAttribute{ + Description: "The name of the namespace.", + Required: true, + ElementType: types.StringType, + }, + "description": schema.StringAttribute{ + Description: "A description for the namespace.", + Optional: true, + }, + // full_properties includes properties on the namespace set by the IRC. + // User-settable properties should be individually added to the list of Attributes above. Review Comment: I'd love to get some thoughts on this. Sorry for the long explanation! Terraform has a reconciliation process where it looks at the response from the server and diffs it against what the user specified. If there's any differences, it attempts to update the server so that the server will return exactly what the user specified. If that fails, it errors out. Most properties are set by the user, but the server is able to set some of its own and return those. Terraform will get confused by these server-set properties and error out, since the user never specified them. (This is a common pattern! Terraform's schema makes it really easy to specify which fields are server-set.) My solution to this problem was to make every server-settable property listed individually in the Terraform schema with a "server-set" `fullProperties` property. That way, any server-set properties are just ignored and we get full types + validation. A couple other choices we could make: 1. We could list out all the possible server-set properties (this would be a long list!) 2. We just ignore every property that the user didn't set and assume that it's server-set. This has two caveats: we lose the pre run-time validation and any out-of-band changes (changes made to the IRC by not-Terraform) will not be overridden by Terraform. -- 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]
