Skip to content

Commit 5ae5ed0

Browse files
authored
Replace Get-AzContext with Utils\Get-SubscriptionIdTestSafe (#24540)
* Update to autorest 4.x Update to Azure Policy 2023-04-01 Move to non-versioned interface types Complete full test suite for existing cmdlets Few bug fixes Add completer for Location parameter Fix record/playback, remove -LiveOnly tags Make test names repeatable to work with record/playback Implement resource group functions that became inaccessible Fix test failures due to default parameter injection * Clean up some transforms in README Remove unsupported (for now) parameters Streamline common test code Move common test code to utils.ps1 Add serialization of test variables to env.json * Remove extra parameters from Get-AzPolicyAssignment Rerecord the tests * Fix $testFilesFolder calculation to work on macOS/linux. * Hide generated parameter sets for New-* Correct definitions, validation, and tests for New-AzPolicyAssignment parameters Re-record all tests * Replace Get-AzContext with Utils\Get-SubscriptionIdTestSafe Move to autorest 4.0.690 Update managed identity properties to match generation changes Remove several parameter transforms no longer needed Rerecord all tests 91 tests still failing on -Playback with the same signature * Remove location completer from exports/docs/examples/help * Improve location completer tests * Remove transforms for Scope and Id parameters no longer needed Update cmdlets to use Scope and Id directly Rerecord all tests All tests pass in -Record mode Failures in -Playback mode unchanged
1 parent 0b48f4e commit 5ae5ed0

File tree

101 files changed

+12129
-12416
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+12129
-12416
lines changed

src/Resources/Policy.Autorest/Az.Policy.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RequiredAssemblies = './bin/Az.Policy.private.dll'
1313
FormatsToProcess = './Az.Policy.format.ps1xml'
1414
ScriptsToProcess = @('./custom/Helpers.ps1')
15-
FunctionsToExport = 'Get-AzPolicyAssignment', 'Get-AzPolicyDefinition', 'Get-AzPolicyExemption', 'Get-AzPolicySetDefinition', 'LocationCompleter', 'New-AzPolicyAssignment', 'New-AzPolicyDefinition', 'New-AzPolicyExemption', 'New-AzPolicySetDefinition', 'Remove-AzPolicyAssignment', 'Remove-AzPolicyDefinition', 'Remove-AzPolicyExemption', 'Remove-AzPolicySetDefinition', 'Update-AzPolicyAssignment', 'Update-AzPolicyDefinition', 'Update-AzPolicyExemption', 'Update-AzPolicySetDefinition'
15+
FunctionsToExport = 'Get-AzPolicyAssignment', 'Get-AzPolicyDefinition', 'Get-AzPolicyExemption', 'Get-AzPolicySetDefinition', 'New-AzPolicyAssignment', 'New-AzPolicyDefinition', 'New-AzPolicyExemption', 'New-AzPolicySetDefinition', 'Remove-AzPolicyAssignment', 'Remove-AzPolicyDefinition', 'Remove-AzPolicyExemption', 'Remove-AzPolicySetDefinition', 'Update-AzPolicyAssignment', 'Update-AzPolicyDefinition', 'Update-AzPolicyExemption', 'Update-AzPolicySetDefinition'
1616
AliasesToExport = 'Set-AzPolicyAssignment', 'Set-AzPolicyDefinition', 'Set-AzPolicyExemption', 'Set-AzPolicySetDefinition'
1717
PrivateData = @{
1818
PSData = @{

src/Resources/Policy.Autorest/README.md

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
This directory contains the PowerShell module for the Policy service.
44

55
---
6-
## Status
7-
[![Az.Policy](https://img.shields.io/powershellgallery/v/Az.Policy.svg?style=flat-square&label=Az.Policy "Az.Policy")](https://www.powershellgallery.com/packages/Az.Policy/)
8-
96
## Info
107
- Modifiable: yes
118
- Generated: all
@@ -133,106 +130,6 @@ directive:
133130
script: '{ "" }'
134131
hide: true
135132

136-
# Rename and hide Name and SubscriptionId parameters on Get-AzPolicySetDefinition
137-
# to allow customizing control over parameter bindings to match current SDK cmdlets
138-
- where:
139-
verb: Get
140-
subject: PolicyDefinition|PolicyDefinitionVersion|PolicySetDefinition|PolicySetDefinitionVersion
141-
parameter-name: Name
142-
set:
143-
parameter-name: NameInternal
144-
default:
145-
name: DefaultName
146-
description: Need a placeholder to keep autorest happy
147-
script: '"PlaceholderName"'
148-
hide: true
149-
clear-alias: true
150-
- where:
151-
verb: Get
152-
subject: PolicyDefinition|PolicyDefinitionVersion|PolicySetDefinition|PolicySetDefinitionVersion
153-
parameter-name: SubscriptionId
154-
set:
155-
parameter-name: SubscriptionIdInternal
156-
default:
157-
name: DefaultSubscriptionId
158-
description: Need a placeholder to keep autorest happy
159-
script: '(Get-AzContext).Subscription.Id'
160-
hide: true
161-
162-
# Rename and hide identity parameters on *-AzPolicyAssignment. This is required
163-
# because autorest generation handles these paramters specially and applies their
164-
# own parameter set bindings. We need to customize control over parameter bindings
165-
# to match the behavior of the current SDK cmdlets.
166-
- where:
167-
subject: PolicyAssignment
168-
parameter-name: Name
169-
set:
170-
parameter-name: NameInternal
171-
default:
172-
name: DefaultName
173-
description: Need a placeholder to keep autorest happy
174-
script: '"PlaceholderName"'
175-
hide: true
176-
clear-alias: true
177-
- where:
178-
subject: PolicyAssignment
179-
parameter-name: Scope
180-
set:
181-
parameter-name: ScopeInternal
182-
default:
183-
name: DefaultScope
184-
description: Need a placeholder to keep autorest happy
185-
script: "/subscriptions/((Get-AzContext).Subscription.Id)"
186-
hide: true
187-
- where:
188-
subject: PolicyAssignment
189-
parameter-name: Id
190-
set:
191-
parameter-name: IdInternal
192-
default:
193-
name: DefaultId
194-
description: Need a placeholder to keep autorest happy
195-
script: '"PlaceholderId"'
196-
hide: true
197-
clear-alias: true
198-
199-
# Rename and hide identity parameters on *-AzPolicyExemption. This is required
200-
# because autorest generation handles these paramters specially and applies their
201-
# own parameter set bindings. We need to customize control over parameter bindings
202-
# to match the behavior of the current SDK cmdlets.
203-
- where:
204-
subject: PolicyExemption
205-
parameter-name: Name
206-
set:
207-
parameter-name: NameInternal
208-
default:
209-
name: DefaultName
210-
description: Need a placeholder to keep autorest happy
211-
script: '"PlaceholderName"'
212-
hide: true
213-
clear-alias: true
214-
- where:
215-
subject: PolicyExemption
216-
parameter-name: Scope
217-
set:
218-
parameter-name: ScopeInternal
219-
default:
220-
name: DefaultScope
221-
description: Need a placeholder to keep autorest happy
222-
script: "/subscriptions/((Get-AzContext).Subscription.Id)"
223-
hide: true
224-
- where:
225-
subject: PolicyExemption
226-
parameter-name: Id
227-
set:
228-
parameter-name: IdInternal
229-
default:
230-
name: DefaultId
231-
description: Need a placeholder to keep autorest happy
232-
script: '"PlaceholderId"'
233-
hide: true
234-
clear-alias: true
235-
236133
# Need to change ExpiresOn to a nullable DateTime (not supported by autorest)
237134
- where:
238135
verb: New

src/Resources/Policy.Autorest/custom/Get-AzPolicyAssignment.ps1

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,17 @@ process {
177177

178178
if ($Name) {
179179
$calledParameterSet = 'Get'
180-
$calledParameters.NameInternal = $Name
180+
$calledParameters.Name = $Name
181181

182182
if (!$Scope) {
183-
$Scope = "/subscriptions/$($(Get-AzContext).Subscription.Id)"
183+
$Scope = "/subscriptions/$($(Get-SubscriptionId))"
184184
}
185185

186-
$calledParameters.ScopeInternal = $Scope
186+
$calledParameters.Scope = $Scope
187187
}
188188
elseif ($Id) {
189189
$calledParameterSet = 'Get1'
190-
$calledParameters.IdInternal = $Id
190+
$calledParameters.Id = $Id
191191
}
192192
else {
193193
# set up filter values for list case
@@ -234,18 +234,17 @@ process {
234234
'none' {
235235
# This will fail, but return a helpful message
236236
$calledParameterSet = 'Get1'
237-
$calledParameters = @{ IdInternal = $resolved.Scope }
237+
$calledParameters = @{ Id = $resolved.Scope }
238238
}
239239
}
240240
}
241241
else {
242-
$calledParameters.SubscriptionId = @((Get-AzContext).Subscription.Id)
242+
$calledParameters.SubscriptionId = @(Get-SubscriptionId)
243243
}
244+
245+
$null = $calledParameters.Remove('Scope')
244246
}
245247

246-
$null = $calledParameters.Remove('Name')
247-
$null = $calledParameters.Remove('Scope')
248-
$null = $calledParameters.Remove('Id')
249248
$null = $calledParameters.Remove('PolicyDefinitionId')
250249
$null = $calledParameters.Remove('IncludeDescendent')
251250
$null = $calledParameters.Remove('BackwardCompatible')

src/Resources/Policy.Autorest/custom/Get-AzPolicyDefinition.ps1

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,6 @@ process {
240240
else {
241241
$calledParameterSet = 'List';
242242
}
243-
244-
# rename subscription parameter to internal version
245-
$PSBoundParameters['SubscriptionIdInternal'] = $PSBoundParameters['SubscriptionId']
246-
247243
}
248244
elseif ($PSBoundParameters['ManagementGroupName']) {
249245
$PSBoundParameters['ManagementGroupId'] = $PSBoundParameters['ManagementGroupName']
@@ -255,7 +251,7 @@ process {
255251
}
256252
}
257253
elseif ($parameterSet -ne 'Id') {
258-
$PSBoundParameters['SubscriptionIdInternal'] = (Get-AzContext).Subscription.Id
254+
$PSBoundParameters['SubscriptionId'] = (Get-SubscriptionId)
259255
if ($PSBoundParameters['Name']) {
260256
$calledParameterSet = 'Get'
261257
}
@@ -264,7 +260,6 @@ process {
264260

265261
# remove parameters not used by generated cmdlets
266262
$null = $PSBoundParameters.Remove('BackwardCompatible')
267-
$null = $PSBoundParameters.Remove('SubscriptionId')
268263
$null = $PSBoundParameters.Remove('ManagementGroupName')
269264
$null = $PSBoundParameters.Remove('Id')
270265
$null = $PSBoundParameters.Remove('BuiltIn')
@@ -285,12 +280,12 @@ process {
285280
$output = Invoke-Command -ScriptBlock $scriptCmd
286281
}
287282
catch {
288-
if (($_.Exception.Message -like '*PolicyDefinitionNotFound*') -and $PSBoundParameters.Name -and $PSBoundParameters.SubscriptionIdInternal) {
283+
if (($_.Exception.Message -like '*PolicyDefinitionNotFound*') -and $PSBoundParameters.Name -and $PSBoundParameters.SubscriptionId) {
289284

290285
# failed by name at subscription level, try builtins
291286
$PSBoundParameters.PolicyDefinitionName = $PSBoundParameters.Name
292287
$null = $PSBoundParameters.Remove('Name')
293-
$null = $PSBoundParameters.Remove('SubscriptionIdInternal')
288+
$null = $PSBoundParameters.Remove('SubscriptionId')
294289
$cmdInfo = Get-Command -Name $mapping['BuiltInGet']
295290
[Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.MessageAttributeHelper]::ProcessCustomAttributesAtRuntime($cmdInfo, $MyInvocation, $calledParameterSet, $PSCmdlet)
296291
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand(($mapping['BuiltInGet']), [System.Management.Automation.CommandTypes]::Cmdlet)

src/Resources/Policy.Autorest/custom/Get-AzPolicyExemption.ps1

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -83,45 +83,6 @@ param(
8383
# To construct, see NOTES section for INPUTOBJECT properties and create a hash table.
8484
${InputObject},
8585

86-
[Parameter(DontShow)]
87-
[Microsoft.Azure.PowerShell.Cmdlets.Policy.Category('Path')]
88-
[System.String[]]
89-
# The ID of the target subscription.
90-
${SubscriptionId},
91-
92-
[Parameter(DontShow)]
93-
[Microsoft.Azure.PowerShell.Cmdlets.Policy.Category('Path')]
94-
[System.String]
95-
# The name of the resource group containing the resource.
96-
${ResourceGroupName},
97-
98-
[Parameter(DontShow)]
99-
[Microsoft.Azure.PowerShell.Cmdlets.Policy.Category('Path')]
100-
[System.String]
101-
# The parent resource path.
102-
# Use empty string if there is none.
103-
${ParentResourcePath},
104-
105-
[Parameter(DontShow)]
106-
[Microsoft.Azure.PowerShell.Cmdlets.Policy.Category('Path')]
107-
[System.String]
108-
# The name of the resource.
109-
${ResourceName},
110-
111-
[Parameter(DontShow)]
112-
[Microsoft.Azure.PowerShell.Cmdlets.Policy.Category('Path')]
113-
[System.String]
114-
# The namespace of the resource provider.
115-
# For example, the namespace of a virtual machine is Microsoft.Compute (from Microsoft.Compute/virtualMachines)
116-
${ResourceProviderNamespace},
117-
118-
[Parameter(DontShow)]
119-
[Microsoft.Azure.PowerShell.Cmdlets.Policy.Category('Path')]
120-
[System.String]
121-
# The resource type name.
122-
# For example the type name of a web app is 'sites' (from Microsoft.Web/sites).
123-
${ResourceType},
124-
12586
[Parameter(DontShow)]
12687
[Microsoft.Azure.PowerShell.Cmdlets.Policy.Category('Query')]
12788
[System.String]
@@ -211,10 +172,6 @@ process {
211172

212173
$calledParameters = $PSBoundParameters
213174

214-
# SubscriptionId is autorest-generated and mandatory so gets populated with default value.
215-
# This customized cmdlet doesn't allow/use it, so we must remove it, otherwise the parameter set is broken.
216-
$null = $calledParameters.Remove('SubscriptionId')
217-
218175
if ($Id) {
219176
$parsed = ParsePolicyExemptionId $Id
220177

@@ -228,13 +185,13 @@ process {
228185
}
229186

230187
if (!$Scope) {
231-
$Scope = "/subscriptions/$($(Get-AzContext).Subscription.Id)"
188+
$Scope = "/subscriptions/$($(Get-SubscriptionId))"
232189
}
233190

234191
if ($Name) {
235192
$calledParameterSet = 'Get'
236-
$calledParameters.NameInternal = $Name
237-
$calledParameters.ScopeInternal = $Scope
193+
$calledParameters.Name = $Name
194+
$calledParameters.Scope = $Scope
238195
}
239196
else {
240197
# set up filter values for list case
@@ -282,12 +239,11 @@ process {
282239
}
283240
}
284241
}
242+
243+
$null = $calledParameters.Remove('Scope')
285244
}
286245

287-
$null = $calledParameters.Remove('Name')
288-
$null = $calledParameters.Remove('Scope')
289246
$null = $calledParameters.Remove('Id')
290-
291247
$null = $calledParameters.Remove('PolicyAssignmentIdFilter')
292248
$null = $calledParameters.Remove('IncludeDescendent')
293249
$null = $calledParameters.Remove('BackwardCompatible')

src/Resources/Policy.Autorest/custom/Get-AzPolicySetDefinition.ps1

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,6 @@ process {
230230
else {
231231
$calledParameterSet = 'List';
232232
}
233-
234-
# rename subscription parameter to internal version
235-
$PSBoundParameters['SubscriptionIdInternal'] = $PSBoundParameters['SubscriptionId']
236-
237233
}
238234
elseif ($PSBoundParameters['ManagementGroupName']) {
239235
$PSBoundParameters['ManagementGroupId'] = $PSBoundParameters['ManagementGroupName']
@@ -245,7 +241,7 @@ process {
245241
}
246242
}
247243
elseif ($parameterSet -ne 'Id') {
248-
$PSBoundParameters['SubscriptionIdInternal'] = (Get-AzContext).Subscription.Id
244+
$PSBoundParameters['SubscriptionId'] = (Get-SubscriptionId)
249245
if ($PSBoundParameters['Name']) {
250246
$calledParameterSet = 'Get'
251247
}
@@ -254,7 +250,6 @@ process {
254250

255251
# remove parameters not used by generated cmdlets
256252
$null = $PSBoundParameters.Remove('BackwardCompatible')
257-
$null = $PSBoundParameters.Remove('SubscriptionId')
258253
$null = $PSBoundParameters.Remove('ManagementGroupName')
259254
$null = $PSBoundParameters.Remove('Id')
260255
$null = $PSBoundParameters.Remove('BuiltIn')
@@ -274,12 +269,12 @@ process {
274269
$output = Invoke-Command -ScriptBlock $scriptCmd
275270
}
276271
catch {
277-
if (($_.Exception.Message -like '*PolicySetDefinitionNotFound*') -and $PSBoundParameters.Name -and $PSBoundParameters.SubscriptionIdInternal) {
272+
if (($_.Exception.Message -like '*PolicySetDefinitionNotFound*') -and $PSBoundParameters.Name -and $PSBoundParameters.SubscriptionId) {
278273

279274
# failed by name at subscription level, try builtins
280275
$PSBoundParameters.PolicySetDefinitionName = $PSBoundParameters.Name
281276
$null = $PSBoundParameters.Remove('Name')
282-
$null = $PSBoundParameters.Remove('SubscriptionIdInternal')
277+
$null = $PSBoundParameters.Remove('SubscriptionId')
283278

284279
$cmdInfo = Get-Command -Name $mapping['BuiltInGet']
285280
[Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.MessageAttributeHelper]::ProcessCustomAttributesAtRuntime($cmdInfo, $MyInvocation, $calledParameterSet, $PSCmdlet)

0 commit comments

Comments
 (0)