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

The code generator fails to generate valid code for plan_modifiers #115

Closed
1 task done
TheNilesh opened this issue Jan 17, 2024 · 4 comments
Closed
1 task done
Labels
bug Something isn't working

Comments

@TheNilesh
Copy link

tfplugingen-framework CLI version

tfplugingen-framework module: v0.3.1

Provider Code Spec File

{
  "provider": {
    "name": "hashibug",
    "schema": {
      "attributes": [
        {
          "name": "server",
          "string": {
            "optional_required": "optional",
            "description": "API server"
          }
        }
      ]
    }
  },
  "resources": [
    {
      "name": "myresource",
      "schema": {
        "attributes": [
          {
            "name": "id",
            "string": {
              "computed_optional_required": "computed",
              "description": "The id of the resource.",
              "plan_modifiers": [
                {
                  "use_state_for_unknown": {}
                }
              ]
            }
          }
        ]
      }
    }
  ],
  "datasources": [],
  "version": "0.1"
}

Expected Behavior

For the id field generated code to be similar to this

Imports:

  "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
  "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"

And the generated schema function should be:

func MyresourceSchema(ctx context.Context) schema.Schema {
  return schema.Schema{
    Attributes: map[string]schema.Attribute{
      "id": schema.StringAttribute{
        Computed:            true,
        Description:         "The id of the resource.",
        MarkdownDescription: "The id of the resource.",
        PlanModifiers:       []planmodifier.String{
          stringplanmodifier.UseStateForUnknown(),
        },
      },
    },
  }
}

Actual Behavior

The generated code does not generate import statements. Hence the IDE complains: planmodifier is undefined.

func MyresourceSchema(ctx context.Context) schema.Schema {
  return schema.Schema{
    Attributes: map[string]schema.Attribute{
      "id": schema.StringAttribute{
        Computed:            true,
        Description:         "The id of the resource.",
        MarkdownDescription: "The id of the resource.",
        PlanModifiers:       []planmodifier.String{},
      },
    },
  }
}

Additional Information

The schema is available at https://github.com/hashicorp/terraform-plugin-codegen-spec/blob/main/spec/v0.1/schema.json#L3589

Code of Conduct

  • I agree to follow this project's Code of Conduct
@TheNilesh TheNilesh added the bug Something isn't working label Jan 17, 2024
@TheNilesh
Copy link
Author

@bendbennett Is this under development?

@bendbennett
Copy link
Contributor

bendbennett commented Jan 17, 2024

Hi @TheNilesh 👋

Sorry you ran into trouble here.

The processing of plan modifiers specified with requires_replace, or use_state_for_unknown is currently under development/review. In the meanwhile, you should be able to achieve the desired outcome by modifying your spec file,

Replace the following:

              "plan_modifiers": [
                {
                  "use_state_for_unknown": {}
                }
              ]

Replace with:

              "plan_modifiers": [
                {
                  "custom": {
                    "imports": [
                      {
                        "path": "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
                      }
                    ],
                    "schema_definition": "stringplanmodifier.UseStateForUnknown()"
                  }
                }
              ]

We will discuss handling of requires_replace, or use_state_for_unknown within the team and provide further feedback.

@bendbennett
Copy link
Contributor

References:

A PR has been opened to remove Framework-specific implementation details from the codegen-spec schema.

Issues have been opened on codegen-spec, and codegen-framework to consider inclusion, and handling of Terraform-specific behaviour properties, respectively.

Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants