这是indexloc提供的服务,不要输入任何密码
Skip to content

Fix saml property on team config resource #347

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 1 commit into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions docs/resources/team_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,9 @@ Optional:
<a id="nestedatt--saml"></a>
### Nested Schema for `saml`

Required:

- `enforced` (Boolean) Indicates if SAML is enforced for the team.

Optional:

- `enforced` (Boolean) Indicates if SAML is enforced for the team.
- `roles` (Attributes Map) Directory groups to role or access group mappings. For each directory group, specify either a role or access group id. (see [below for nested schema](#nestedatt--saml--roles))

<a id="nestedatt--saml--roles"></a>
Expand Down
59 changes: 31 additions & 28 deletions vercel/resource_team_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/mapdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/mapplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
Expand Down Expand Up @@ -122,44 +121,44 @@ func (r *teamConfigResource) Schema(_ context.Context, req resource.SchemaReques
Description: "Hostname that'll be matched with emails on sign-up to automatically join the Team.",
},
"saml": schema.SingleNestedAttribute{
Description: "Configuration for SAML authentication.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Object{objectplanmodifier.UseStateForUnknown()},
Attributes: map[string]schema.Attribute{
"enforced": schema.BoolAttribute{
Description: "Indicates if SAML is enforced for the team.",
Required: true,
Description: "Indicates if SAML is enforced for the team.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{boolplanmodifier.UseStateForUnknown()},
},
"roles": schema.MapNestedAttribute{
Description: "Directory groups to role or access group mappings. For each directory group, specify either a role or access group id.",
Optional: true,
Computed: true,
Description: "Directory groups to role or access group mappings. For each directory group, specify either a role or access group id.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Map{mapplanmodifier.UseStateForUnknown()},
Validators: []validator.Map{validateSamlRoles()},
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"role": schema.StringAttribute{
Description: "The team level role to assign to the user. One of 'MEMBER', 'OWNER', 'VIEWER', 'DEVELOPER', 'BILLING' or 'CONTRIBUTOR'.",
Optional: true,
Description: "The team level role to assign to the user. One of 'MEMBER', 'OWNER', 'VIEWER', 'DEVELOPER', 'BILLING' or 'CONTRIBUTOR'.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
Validators: []validator.String{
stringvalidator.OneOf("MEMBER", "OWNER", "VIEWER", "DEVELOPER", "BILLING", "CONTRIBUTOR"),
},
},
"access_group_id": schema.StringAttribute{
Description: "The access group id to assign to the user.",
Optional: true,
Description: "The access group id to assign to the user.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
},
},
Validators: []validator.Map{validateSamlRoles()},
Default: mapdefault.StaticValue(types.MapValueMust(types.ObjectType{
AttrTypes: map[string]attr.Type{
"role": types.StringType,
"access_group_id": types.StringType,
},
}, map[string]attr.Value{})),
PlanModifiers: []planmodifier.Map{mapplanmodifier.UseStateForUnknown()},
},
},
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Object{objectplanmodifier.UseStateForUnknown()},
Description: "Configuration for SAML authentication.",
},
"invite_code": schema.StringAttribute{
Computed: true,
Expand Down Expand Up @@ -690,7 +689,9 @@ func (r *teamConfigResource) UpgradeState(ctx context.Context) map[int64]resourc
"roles": schema.MapAttribute{
Description: "Directory groups to role or access group mappings.",
Optional: true,
Computed: true,
ElementType: types.StringType,
// PlanModifiers: []planmodifier.Map{mapplanmodifier.UseStateForUnknown()},
Validators: []validator.Map{
// Validate only this attribute or roles is configured.
mapvalidator.ExactlyOneOf(
Expand All @@ -702,6 +703,7 @@ func (r *teamConfigResource) UpgradeState(ctx context.Context) map[int64]resourc
"access_group_id": schema.StringAttribute{
Description: "The ID of the access group to use for the team.",
Optional: true,
Computed: true,
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile("^ag_[A-z0-9_ -]+$"), "Access group ID must be a valid access group"),
// Validate only this attribute or roles is configured.
Expand All @@ -712,10 +714,10 @@ func (r *teamConfigResource) UpgradeState(ctx context.Context) map[int64]resourc
},
},
},
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Object{objectplanmodifier.UseStateForUnknown()},
Description: "Configuration for SAML authentication.",
Optional: true,
Computed: true,
// PlanModifiers: []planmodifier.Object{objectplanmodifier.UseStateForUnknown()},
Description: "Configuration for SAML authentication.",
},
"invite_code": schema.StringAttribute{
Computed: true,
Expand Down Expand Up @@ -776,13 +778,12 @@ func (r *teamConfigResource) UpgradeState(ctx context.Context) map[int64]resourc
},
StateUpgrader: func(ctx context.Context, req resource.UpgradeStateRequest, resp *resource.UpgradeStateResponse) {
var priorStateData TeamConfig

resp.Diagnostics.Append(req.State.Get(ctx, &priorStateData)...)

if resp.Diagnostics.HasError() {
return
}

tflog.Info(ctx, "upgrading state for team_config resource", map[string]any{})
upgradedStateData := TeamConfig{
ID: priorStateData.ID,
Avatar: priorStateData.Avatar,
Expand All @@ -807,6 +808,7 @@ func (r *teamConfigResource) UpgradeState(ctx context.Context) map[int64]resourc
UnhandledUnknownAsEmpty: true,
})
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
}
// samlV0 did not correctly handle access groups, so don't need to upgrade them.
Expand All @@ -822,6 +824,7 @@ func (r *teamConfigResource) UpgradeState(ctx context.Context) map[int64]resourc
Enforced: samlV0.Enforced,
Roles: roles,
})
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}
Expand Down