Skip to content

chore: Define conversion function for generating API request body from Resource Model in create (non-nested) #3239

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

Merged
merged 14 commits into from
Apr 3, 2025

Conversation

lantoli
Copy link
Member

@lantoli lantoli commented Apr 1, 2025

Description

Define conversion function for generating API request body from Resource Model in create (non-nested).

Marshal is used to create the Atlas request from the Terraform plan model. It's called in operations that need to create an Atlas request payload, i.e. Create and Update. It's not used in Read, Delete or Import.

Unmarshal is used to update the Terraform model from the Atlas response. It's called in Create, Update and Read. It's not used in Delete or Import.

It supports an autogeneration tag in model struct fields that can have:

  • omitjson: Field will never be marshaled into JSON, useful for attributes not needed in Atlas requests like timeouts or computed values.
  • omitjsonupdate: Field that is not marshaled into JSON when it's an update, this field is sent only when the resource is created.

Link to any related issue(s): CLOUDP-310079

Type of change:

  • Bug fix (non-breaking change which fixes an issue). Please, add the "bug" label to the PR.
  • New feature (non-breaking change which adds functionality). Please, add the "enhancement" label to the PR. A migration guide must be created or updated if the new feature will go in a major version.
  • Breaking change (fix or feature that would cause existing functionality to not work as expected). Please, add the "breaking change" label to the PR. A migration guide must be created or updated.
  • This change requires a documentation update
  • Documentation fix/enhancement

Required Checklist:

  • I have signed the MongoDB CLA
  • I have read the contributing guides
  • I have checked that this change does not generate any credentials and that they are NOT accidentally logged anywhere.
  • I have added tests that prove my fix is effective or that my feature works per HashiCorp requirements
  • I have added any necessary documentation (if appropriate)
  • I have run make fmt and formatted my code
  • If changes include deprecations or removals I have added appropriate changelog entries.
  • If changes include removal or addition of 3rd party GitHub actions, I updated our internal document. Reach out to the APIx Integration slack channel to get access to the internal document.

Further comments

@lantoli lantoli marked this pull request as ready for review April 3, 2025 06:32
@lantoli lantoli requested a review from a team as a code owner April 3, 2025 06:32
Copy link
Member

@AgustinBettati AgustinBettati left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, leaving some doubts/suggestions

Comment on lines 15 to 17
tagKey = "autogeneration"
tagValOmitJSON = "omitjson"
tagValCreateOnly = "createonly"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Giving some more thought to the naming, would omitjsonupdate over createonly be more consistent?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, changed here: d6c212b

)

// Marshal gets a Terraform model and marshals it into JSON (e.g. for an Atlas request).
func Marshal(model any, isCreate bool) ([]byte, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would leave an additional comment clarifying when attributes are sent in payload, and how use of tags changes that behaviour

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done here: 065089b

AttrString types.String `tfsdk:"attribute_string"`
// values with tag `omitjson` are not marshaled, and they don't need to be Terraform types
AttrOmit types.String `tfsdk:"attribute_omit" autogeneration:"omitjson"`
AttrOmitNoTerraform string `autogeneration:"omitjson"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason why this is not types.String? Would also define it with a known string value to ensure it is never sent even if it has a value in the config

Copy link
Member Author

@lantoli lantoli Apr 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean AttrOmitNoTerraform? It's to prove that in this case you can use "invalid" types that are not Terraform types in the model, and that's ok because it's not sent in JSON, like explained in the comment above.
In the normal case it would panic if the model has an attribute that is not Terraform, e.g test here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aah makes sense, I see AttrOmit covers the case i was mentioning, thanks!

Copy link
Member

@AgustinBettati AgustinBettati left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for follow ups

@lantoli lantoli merged commit 9d767c4 into CLOUDP-301808-poc-autogeneration Apr 3, 2025
40 checks passed
@lantoli lantoli deleted the CLOUDP-310079_marshal branch April 3, 2025 13:05
for i := 0; i < valModel.NumField(); i++ {
attrTypeModel := valModel.Type().Field(i)
tag := attrTypeModel.Tag.Get(tagKey)
if tag == tagValOmitJSON {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lantoli
Would it be worth supporting multiple tags: "autogeneration:omitjson,{futureTag}"? Probably, it can happen at a later stage

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@EspenAlbert yes, it makes sense, but didn't want to anticipate until we don't need it, this code would be very easy to adapt when neeeded

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

Successfully merging this pull request may close these issues.

4 participants