zeroshade commented on code in PR #17:
URL: https://github.com/apache/iceberg-terraform/pull/17#discussion_r2926587666
##########
internal/provider/resource_table.go:
##########
@@ -373,34 +355,199 @@ func (r *icebergTableResource) Read(ctx context.Context,
req resource.ReadReques
return
}
- serverProperties, diags := types.MapValueFrom(ctx, types.StringType,
tbl.Properties())
+ r.syncTableToModel(ctx, tbl, &data, &resp.Diagnostics)
+ if resp.Diagnostics.HasError() {
+ return
+ }
+
+ 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
}
- data.ServerProperties = serverProperties
- icebergSchema := tbl.Schema()
- var updatedSchema icebergTableSchema
- if err := updatedSchema.FromIceberg(icebergSchema); err != nil {
- resp.Diagnostics.AddError("failed to convert iceberg schema to
terraform schema", err.Error())
+ var plan, state icebergTableResourceModel
+
+ diags := req.Plan.Get(ctx, &plan)
+ resp.Diagnostics.Append(diags...)
+
+ diags = req.State.Get(ctx, &state)
+ resp.Diagnostics.Append(diags...)
+
+ if resp.Diagnostics.HasError() {
return
}
- data.Schema, diags = types.ObjectValueFrom(ctx,
icebergTableSchema{}.AttrTypes(), updatedSchema)
+
+ var namespaceName []string
+ diags = plan.Namespace.ElementsAs(ctx, &namespaceName, false)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
- diags = resp.State.Set(ctx, &data)
+ tableName := plan.Name.ValueString()
+ tableIdent := append(namespaceName, tableName)
+
+ tbl, err := r.catalog.LoadTable(ctx, tableIdent)
+ if err != nil {
+ resp.Diagnostics.AddError("failed to load table", err.Error())
+ return
+ }
+
+ requirements := []table.Requirement{
+ table.AssertTableUUID(tbl.Metadata().TableUUID()),
+ }
+
+ updates := make([]table.Update, 0)
+
+ updates = append(updates, r.calculatePropertyUpdates(ctx, &plan,
&state, &resp.Diagnostics)...)
+ updates = append(updates, r.calculateSchemaUpdates(ctx, &plan, &state,
&resp.Diagnostics)...)
+
+ if resp.Diagnostics.HasError() {
+ return
+ }
+
+ if len(updates) > 0 {
+ _, _, err = r.catalog.CommitTable(ctx, tableIdent,
requirements, updates)
+ if err != nil {
+ resp.Diagnostics.AddError("failed to commit table
updates", err.Error())
+ return
+ }
+
+ // Reload the table to get the latest state
+ tbl, err = r.catalog.LoadTable(ctx, tableIdent)
Review Comment:
Table has a `Refresh` method that does this for you, maybe use that instead?
--
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]