feat: add cors_behavior support to template resource#294
Conversation
| data.MaxPortShareLevel = types.StringValue(string(mpslResp.MaxPortShareLevel)) | ||
| } | ||
|
|
||
| // Handle cors_behavior - can't be set during create, needs update |
There was a problem hiding this comment.
Good catch - the comment is misleading. cors_behavior CAN be set during create in the Coder API, but I followed the same pattern as max_port_share_level which was implemented as a workaround for backwards compatibility with older Coder versions (see the TODO comments above for max_port_share_level and use_classic_parameter_flow).
I'll update the comment to be more accurate, or alternatively we could add CORSBehavior to toCreateRequest to set it during creation directly. Let me know which approach you'd prefer!
There was a problem hiding this comment.
@Emyrk can decide better here. I think it should be supported on creation, too.
There was a problem hiding this comment.
Done! I've updated the code to set cors_behavior during template creation via toCreateRequest instead of using a post-create update call. This is cleaner and matches the API behavior. The change simplifies the Create function and removes the unnecessary update call.
This adds the ability to configure the CORS behavior for workspace apps via Terraform. The cors_behavior field accepts "simple" (default CORS middleware) or "passthru" (bypass CORS middleware). Changes: - Add cors_behavior field to TemplateResourceModel struct - Add schema attribute with validation - Handle cors_behavior in Create, Read, and Update operations - Add cors_behavior to toUpdateRequest for API calls - Add cors_behavior field to data source as well Fixes #293
Instead of using a post-create update call, cors_behavior is now set directly via the CreateTemplateRequest which is cleaner and matches the API behavior.
When cors_behavior is not specified (unknown/empty), we should not pass an empty string to the API. Instead, we return nil to use the server default.
- Delete custom corsPtr helper; use ptr.Ref (update) and (*codersdk.CORSBehavior)(ValueStringPointer()) (create) to match the UseClassicParameterFlow pattern. - On read, map empty CORSBehavior (old servers) to types.StringNull() instead of storing an invalid empty string. - Add cors_behavior to BasicUsage enterprise subtest (simple → passthru → simple). - Add cors_behavior check to data source test.
Adds TestAccTemplateResourceBackCompat that spins up Coder v2.25.0 (which does not support cors_behavior), creates a template without setting cors_behavior, and verifies the attribute is absent in state.
46a5a98 to
0eee153
Compare
Replace the repeated if/else null-on-empty-string pattern at all 4 CORSBehavior read sites with a single stringValueOrNull() call.
a60f0d6 to
37c5589
Compare
Summary
Adds support for the
cors_behaviorfield in thecoderd_templateresource to allow configuring CORS settings for workspace apps via Terraform.Changes
Template Resource
cors_behaviorfield toTemplateResourceModelstructsimpleorpassthru)cors_behaviorin Create, Read, and Update operationscors_behaviortotoUpdateRequestfor API callscors_behaviortoEqualTemplateMetadatacomparisonTemplate Data Source
cors_behaviorfield to data source struct and schemacors_behaviorin the Read functionUsage
Background
The Coder backend supports a
cors_behaviorfield on templates with two possible values:simple- uses the default CORS middlewarepassthru- bypasses CORS middleware for workspace appsThis setting is available in the Template Settings page in the Coder UI but was not previously configurable via the Terraform provider.
Fixes #293
Created on behalf of @matifali