Use powershell for non-localized returns#68826
Open
twangboy wants to merge 3 commits intosaltstack:3006.xfrom
Open
Use powershell for non-localized returns#68826twangboy wants to merge 3 commits intosaltstack:3006.xfrom
twangboy wants to merge 3 commits intosaltstack:3006.xfrom
Conversation
The problem is that the return values for netsh are localized to the language of the system they are run on. This is problematic for any system other than English. PowerShell objects, however, are never localized. Using the ConvertTo-Json cmdlet forces the PowerShell object to be converted to text and is always in English. This also creates a context manager for the powershell session which lets us run multiple commands without launching powershell.exe each time. The session is cleaned up when the context closes.
Replace netsh-based implementations with PowerShell .NET SDK calls (via pythonnet) to eliminate locale-dependent output parsing. Key changes: - win_ip.py: add __virtual__ checks for HAS_CLR and HAS_PWSH_SDK; rename _get_interfaces_netsh to _get_interfaces_legacy_format; add _normalize_gateway_fields to handle PS 5.1 single-element array unwrap bug; add get_interface_new, get_interface_index, set_interface, get_default_gateway; refactor set_static_ip, set_dhcp_ip, set_dhcp_dns, set_dhcp_all, list_interfaces to use PowerShellSession; wrap CIM cmdlet GET calls (Get-NetRoute, Get-NetIPAddress) in try/catch to suppress terminating errors when no objects exist; fix docstrings throughout - win_pwsh.py: fix HAS_CLR flag; add ClearStreams() before each Invoke(); add run_strict() with sentinel-variable approach to distinguish caught errors (script ran to completion) from uncaught errors (script terminated mid-way); add class-level docstring to PowerShellSession; expand __init__ docstring - test_win_ip.py: add functional tests covering set_interface, get_interface_new, set_static_ip, set_dhcp_ip/dns/all, get_default_gateway, protocol binding toggles, and forwarding; add _normalize_gateway_fields and default_static fixtures Fixes: saltstack#58361
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
The problem is that the return values for netsh are localized to the language of the system they are run on. This is problematic for any system other than English. PowerShell objects, however, are never localized. Using the ConvertTo-Json cmdlet forces the PowerShell object to be converted to text and is always in English.
This also creates a context manager for the PowerShell session. This allows us to run multiple commands without launching PowerShell.exe each time. The session is cleaned up when the context closes.
What issues does this PR fix or reference?
Fixes #58361
Previous Behavior
Remove this section if not relevant
New Behavior
Remove this section if not relevant
Merge requirements satisfied?
[NOTICE] Bug fixes or features added to Salt require tests.
Commits signed with GPG?
Yes