Terraform Framework插件复杂类型与Golang类型高效转换方法问询
Terraform Framework复杂类型资源CRUD优化问题
Terraform Framework官方插件示例仅覆盖简单类型的资源CRUD基础场景,当处理map这类复杂类型时,代码会变得异常冗长。我当前的实现方式如下,但逻辑不够合理:
func (r *FooResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { var data *ResourceModel // Read Terraform prior state data into the model resp.Diagnostics.Append(req.State.Get(ctx, &data)...) if resp.Diagnostics.HasError() { return } // call API to get "real" state foo, err := r.GetFoo(data.Id.ValueString()) if err != nil { resp.Diagnostics.AddError( "Error reading foo", fmt.Sprintf("Error reading foo: %s", err), ) return } // check to see if real data.Bar is actually different from foo.Bar, if so update newBar, barDiag := checkIfMapHasChanged(ctx, data.Bar, foo.Bar) if barDiag != nil { resp.Diagnostics.Append(barDiag...) return } data.Bar = newBar // Save updated data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) } // helper functions func goMapFromTf(ctx context.Context, m types.Map) (map[string]interface{}, diag.Diagnostics) { goObject := make(map[string]interface{}) // read from the terraform data into the map if diags := m.ElementsAs(ctx, &goObject, false); diags != nil { return nil, diags } return goObject, nil } func tfMapFromGo(ctx context.Context, m map[string]interface{}) (types.Map, diag.Diagnostics) { mappedValue, diags := types.MapValueFrom(ctx, types.StringType, m) if diags != nil { return types.MapNull(nil), diags } return mappedValue, nil } func checkIfMapHasChanged(ctx context.Context, tfMap types.Map, comparisonMap map[string]interface{}) (types.Map, diag.Diagnostics) { goTfMap, diags := goMapFromTf(ctx, tfMap) if diags != nil { return types.MapNull(nil), diags } // compare the map to the response from the API and if it's changed update the data object if !reflect.DeepEqual(goTfMap, comparisonMap) { tfFromComparison, err := tfMapFromGo(ctx, comparisonMap) if err != nil { return types.MapNull(nil), err } return tfFromComparison, nil } return tfMap, nil }
我需要准确检查值是否实际发生变更,否则Terraform计划会显示无意义的无变更差异。当前实现对map元素类型做了硬编码假设(比如固定用types.StringType),我认为可以通过泛型解决这个类型限制问题,同时还需要为列表等其他复杂类型编写类似的辅助对比函数。
内容的提问来源于stack exchange,提问作者Kieran
相关产品推荐
相关产品推荐

