-
Notifications
You must be signed in to change notification settings - Fork 191
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
chore: Define conversion function for generating API request body from Resource Model in create (non-nested) #3239
Conversation
There was a problem hiding this 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
tagKey = "autogeneration" | ||
tagValOmitJSON = "omitjson" | ||
tagValCreateOnly = "createonly" |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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"` |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!
There was a problem hiding this 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
for i := 0; i < valModel.NumField(); i++ { | ||
attrTypeModel := valModel.Type().Field(i) | ||
tag := attrTypeModel.Tag.Get(tagKey) | ||
if tag == tagValOmitJSON { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
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
andUpdate
. It's not used inRead
,Delete
orImport
.Unmarshal
is used to update the Terraform model from the Atlas response. It's called inCreate
,Update
andRead
. It's not used inDelete
orImport
.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:
Required Checklist:
Further comments