Skip to content

Conversation

@mawasile
Copy link
Contributor

This pull request updates the managed environment resource logic to improve handling of environments that are part of an Environment Group and adds more robust checks for certain string settings. It also introduces a new test fixture for extended environment responses. The key changes are grouped below:

Governance and Environment Group Handling:

  • Added checks in the Read, Update, and Delete methods of resource_managed_environment.go to detect if an environment is part of an Environment Group (ParentEnvironmentGroup). If so, a warning is issued and the Manage Environment Settings are not applied, preventing unintended configuration changes. [1] [2] [3]

String Setting Robustness:

  • Improved logic when setting LimitSharingMode and SolutionCheckerMode by only updating these fields if their string length is greater than 1, avoiding potential errors with empty or single-character values. This change is applied in both state population and update flows. [1] [2]

Test Coverage:

  • Added a new test fixture file get_environment_create_response_extended_29.json with a comprehensive sample environment response, including extended governance settings and capacity details, to support validation of the new logic.

@mawasile mawasile marked this pull request as ready for review October 27, 2025 14:37
@mawasile mawasile requested a review from a team as a code owner October 27, 2025 14:37
Copilot AI review requested due to automatic review settings October 27, 2025 14:37
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request adds validation and handling for environments that belong to an Environment Group in the managed environment resource. When an environment is part of an Environment Group, managed environment settings cannot be applied, so the resource now detects this condition and issues a warning instead of attempting (and failing) to apply settings.

Key changes:

  • Added ParentEnvironmentGroup validation checks in Read, Update, and Delete operations
  • Enhanced string field validation for LimitSharingMode and SolutionCheckerMode to prevent processing empty/single-character values
  • Added comprehensive test fixture with extended environment response including governance configuration

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
internal/services/managed_environment/resource_managed_environment.go Added ParentEnvironmentGroup checks and improved string field validation
templates/resources/managed_environment.md.tmpl Added documentation template with Known Limitations section
docs/resources/managed_environment.md Generated documentation with Environment Groups limitation
internal/services/managed_environment/tests/resource/Validate_Create_And_Update/get_environment_create_response_extended_29.json New test fixture with comprehensive environment response
.changes/unreleased/fixed-20251022-124634.yaml Changelog entry for the fix

Comment on lines +238 to +242
if len(env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.LimitSharingMode) > 1 {
state.LimitSharingMode = types.StringValue(strings.ToUpper(env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.LimitSharingMode[:1]) + env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.LimitSharingMode[1:])
}
if len(env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.SolutionCheckerMode) > 1 {
state.SolutionCheckerMode = types.StringValue(strings.ToUpper(env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.SolutionCheckerMode[:1]) + env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.SolutionCheckerMode[1:])
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

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

The length check > 1 allows single-character strings to pass through unchecked. If the API returns a single-character value, the slice operation [:1] and [1:] will cause a panic on the second part [1:] since there's no character at index 1. Consider changing the condition to >= 2 or len(...) > 0 with additional validation, or handle the single-character case explicitly.

Suggested change
if len(env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.LimitSharingMode) > 1 {
state.LimitSharingMode = types.StringValue(strings.ToUpper(env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.LimitSharingMode[:1]) + env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.LimitSharingMode[1:])
}
if len(env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.SolutionCheckerMode) > 1 {
state.SolutionCheckerMode = types.StringValue(strings.ToUpper(env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.SolutionCheckerMode[:1]) + env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.SolutionCheckerMode[1:])
limitSharingMode := env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.LimitSharingMode
if len(limitSharingMode) > 0 {
if len(limitSharingMode) == 1 {
state.LimitSharingMode = types.StringValue(strings.ToUpper(limitSharingMode))
} else {
state.LimitSharingMode = types.StringValue(strings.ToUpper(limitSharingMode[:1]) + limitSharingMode[1:])
}
}
solutionCheckerMode := env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.SolutionCheckerMode
if len(solutionCheckerMode) > 0 {
if len(solutionCheckerMode) == 1 {
state.SolutionCheckerMode = types.StringValue(strings.ToUpper(solutionCheckerMode))
} else {
state.SolutionCheckerMode = types.StringValue(strings.ToUpper(solutionCheckerMode[:1]) + solutionCheckerMode[1:])
}

Copilot uses AI. Check for mistakes.
Comment on lines +417 to +420
if len(env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.LimitSharingMode) > 1 {
plan.LimitSharingMode = types.StringValue(strings.ToUpper(env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.LimitSharingMode[:1]) + env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.LimitSharingMode[1:])
}
if len(env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.SolutionCheckerMode) > 1 {
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

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

Same issue as in the Read method: the length check > 1 allows single-character strings which will cause a slice bounds panic on [1:]. Consider changing to >= 2 or len(...) > 0 with proper handling of the single-character case.

Suggested change
if len(env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.LimitSharingMode) > 1 {
plan.LimitSharingMode = types.StringValue(strings.ToUpper(env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.LimitSharingMode[:1]) + env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.LimitSharingMode[1:])
}
if len(env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.SolutionCheckerMode) > 1 {
if len(env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.LimitSharingMode) > 0 {
plan.LimitSharingMode = types.StringValue(strings.ToUpper(env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.LimitSharingMode[:1]) + env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.LimitSharingMode[1:])
}
if len(env.Properties.GovernanceConfiguration.Settings.ExtendedSettings.SolutionCheckerMode) > 0 {

Copilot uses AI. Check for mistakes.
}

if env.Properties.ParentEnvironmentGroup != nil && env.Properties.ParentEnvironmentGroup.Id != "" {
resp.Diagnostics.AddWarning(fmt.Sprintf("Environment '%s' is included in Environment Group '%s'. The Manage Environment Settings will not be applied.", state.EnvironmentId.ValueString(), env.Properties.ParentEnvironmentGroup.Id), "")
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

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

The second parameter to AddWarning is an empty string. Consider providing a more detailed diagnostic message in this parameter to explain why this limitation exists or what actions the user should take, such as 'To manage this environment's settings, remove it from the Environment Group first.'

Suggested change
resp.Diagnostics.AddWarning(fmt.Sprintf("Environment '%s' is included in Environment Group '%s'. The Manage Environment Settings will not be applied.", state.EnvironmentId.ValueString(), env.Properties.ParentEnvironmentGroup.Id), "")
resp.Diagnostics.AddWarning(
fmt.Sprintf("Environment '%s' is included in Environment Group '%s'. The Manage Environment Settings will not be applied.", state.EnvironmentId.ValueString(), env.Properties.ParentEnvironmentGroup.Id),
"To manage this environment's settings, remove it from the Environment Group first. This limitation exists because settings cannot be applied to environments that are part of an Environment Group."
)

Copilot uses AI. Check for mistakes.
if err != nil {
resp.Diagnostics.AddError(fmt.Sprintf("Client error when reading environment %s", r.FullTypeName()), err.Error())
if env.Properties.ParentEnvironmentGroup != nil && env.Properties.ParentEnvironmentGroup.Id != "" {
resp.Diagnostics.AddWarning(fmt.Sprintf("Environment '%s' is included in Environment Group '%s'. The Manage Environment Settings will not be applied.", plan.EnvironmentId.ValueString(), env.Properties.ParentEnvironmentGroup.Id), "")
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

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

The second parameter to AddWarning is an empty string. Provide a detailed diagnostic message explaining the limitation or recommended user actions.

Suggested change
resp.Diagnostics.AddWarning(fmt.Sprintf("Environment '%s' is included in Environment Group '%s'. The Manage Environment Settings will not be applied.", plan.EnvironmentId.ValueString(), env.Properties.ParentEnvironmentGroup.Id), "")
resp.Diagnostics.AddWarning(
fmt.Sprintf("Environment '%s' is included in Environment Group '%s'. The Manage Environment Settings will not be applied.", plan.EnvironmentId.ValueString(), env.Properties.ParentEnvironmentGroup.Id),
"Managed Environment settings cannot be applied to environments that are part of an Environment Group. " +
"To manage settings for this environment, remove it from the group or apply settings at the group level if supported."
)

Copilot uses AI. Check for mistakes.
}

if env.Properties.ParentEnvironmentGroup != nil && env.Properties.ParentEnvironmentGroup.Id != "" {
resp.Diagnostics.AddWarning(fmt.Sprintf("Environment '%s' is included in Environment Group '%s'. The Manage Environment Settings will not be applied.", state.EnvironmentId.ValueString(), env.Properties.ParentEnvironmentGroup.Id), "")
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

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

The second parameter to AddWarning is an empty string. Provide a detailed diagnostic message explaining the limitation or recommended user actions.

Copilot uses AI. Check for mistakes.
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.

Enabling environment group causes error on powerplatform_managed_environment

2 participants