Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider handling of associated_external_type for all types #50

Open
bendbennett opened this issue Sep 21, 2023 · 0 comments
Open

Consider handling of associated_external_type for all types #50

bendbennett opened this issue Sep 21, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@bendbennett
Copy link
Contributor

bendbennett commented Sep 21, 2023

Within a specification defined by the schema within the terraform-plugin-codegen-spec repo, associated_external_type is property that can be optionally defined for

  • nested_object within list_nested, map_nested, and set_nested attributes
  • single_nested attributes
  • nested_object within list_nested, and set_nested blocks
  • single_nested blocks

The associated_external_type field is used during code generation to signal that methods which transform to and from Terraform Plugin Framework types to API SDK types for use with API calls be autogenerated.

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

{
  "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"
  }
}
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
}

Proposal

Extension of the handling of associated_external_type for other types (e.g., bool attributes, list attributes) should be considered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant