hashicorp / hashicorp/terraform-plugin-codegen-framework

Consider mapping for use with associated_external_type

Open
#51 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Go
Stars
57
Forks
29
Avg merge
2d 13h
Merged PRs (30d)
1

Description

Currently, the generation of `To<...>()/From<...>()` methods (refer to [Consider handling of associated_external_type for all types](https://github.com/hashicorp/terraform-plugin-codegen-framework/issues/50#top)) uses a direct mapping from the Terraform Plugin Framework types to the API SDK types.

For example, usage of `associated_external_type` in the following specification results in the generation of the `To<...>()/From<...>()` methods.

```json
{
"datasources": [
{
"name": "example",
"schema": {
"attributes": [
{
"name": "list_nested_attribute",
"list_nested": {
"nested_object": {
"associated_external_type": {
"import": {
"path": "example.com/apisdk"
},
"type": "*apisdk.Type"
},
"attributes": [
{
"name": "int64_attribute",
"int64": {
"computed_optional_required": "optional"
}
}
]
},
"computed_optional_required": "optional"
}
}
]
}
}
],
"provider": {
"name": "example"
}
}
```

```go
func (v ListNestedAttributeValue) ToApisdkType(ctx context.Context) (*apisdk.Type, diag.Diagnostics) {
var diags diag.Diagnostics

if v.IsNull() {
return nil, diags
}

if v.IsUnknown() {
diags.Append(diag.NewErrorDiagnostic(
"ListNestedAttributeValue Value Is Unknown",
`"ListNestedAttributeValue" is unknown.`,
))

return nil, diags
}

return &apisdk.Type{
Int64Attribute: v.Int64Attribute.ValueInt64Pointer(),
}, diags
}

func (v ListNestedAttributeValue) FromApisdkType(ctx context.Context, apiObject *apisdk.Type) (ListNestedAttributeValue, diag.Diagnostics) {
var diags diag.Diagnostics

if apiObject == nil {
return NewListNestedAttributeValueNull(), diags
}

return ListNestedAttributeValue{
Int64Attribute: types.Int64PointerValue(apiObject.Int64Attribute),
state: attr.ValueStateKnown,
}, diags
}
```

The following assumptions are made during the generation of the `To<...>()/From<...>()` methods based on the specified `associated_external_type`:

- The API SDK type is a struct with fields which are named with the camel-cased attribute name (i.e., `int64_attribute` => `Int64Attribute`).
- The values held within the API SDK type are pointer values of the attribute value (i.e., `int64` => `*int64`).

### Proposal

Additional flexibility could be introduced by allowing the specification of "mapping" between the Terraform Plugin Framework types and the types specified by `associated_external_type`. This would increase the usefulness of the generated `To<...>()/From<...>()` methods and accommodate cases where a direct 1-2-1 mapping between the Terraform Plugin Framework types and the API SDK types was not present.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.