Skip to content

Commit 7148d1e

Browse files
authored
Move Policy CRUD cmdlets to autorest generation: fixes (#23518)
* Set all tests to -LiveOnly Add back some test infrastructure fixes that were lost Improve parameter handling for Remove-AzPolicySetDefinition * Move to supported way of sharing Helpers.psm1 file * Switch to recommended approach for shared functions
1 parent acc4320 commit 7148d1e

File tree

44 files changed

+126
-23203
lines changed

Some content is hidden

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

44 files changed

+126
-23203
lines changed

src/Resources/Policy.Autorest/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ generated
55
internal
66
exports
77
tools
8-
custom/Az.Policy.custom.psm1
8+
custom/*.psm1
99
custom/autogen-model-cmdlets
1010
test/*-TestResults.xml
1111
/*.ps1

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
PowerShellVersion = '5.1'
1111
DotNetFrameworkVersion = '4.7.2'
1212
RequiredAssemblies = './bin/Az.Policy.private.dll'
13-
NestedModules = @('./custom/Helpers')
1413
FormatsToProcess = './Az.Policy.format.ps1xml'
14+
ScriptsToProcess = @('./custom/Helpers.ps1')
1515
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 = @{

src/Resources/Policy.Autorest/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ directive:
378378
hide: true
379379

380380
metadata:
381-
nestedModules:
382-
- ./custom/Helpers
381+
scriptsToProcess:
382+
- ./custom/Helpers.ps1
383383

384384
```

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,6 @@ begin {
209209
Write-Host -ForegroundColor Cyan "begin:Get-AzPolicyAssignment(" $PSBoundParameters ") - (ParameterSet: $($PSCmdlet.ParameterSetName))"
210210
}
211211

212-
# load nested module containing common code
213-
Import-Module ((Get-Module -Name 'Az.Policy').NestedModules | ?{ $_.Name -eq 'Helpers' })
214-
215212
# make mapping table
216213
$mapping = @{
217214
Get = 'Az.Policy.private\Get-AzPolicyAssignment_Get';
@@ -262,7 +259,7 @@ process {
262259
$calledParameterSet = 'List3'
263260

264261
if ($Scope) {
265-
$resolved = Helpers\ResolvePolicyAssignment $null $Scope $null
262+
$resolved = ResolvePolicyAssignment $null $Scope $null
266263
switch ($resolved.ScopeType) {
267264
'mgName' {
268265
if ($IncludeDescendent) {
@@ -328,10 +325,10 @@ process {
328325
Description = $item.Description;
329326
DisplayName = $item.DisplayName;
330327
EnforcementMode = $item.EnforcementMode;
331-
Metadata = (Helpers\ConvertObjectToPSObject $item.Metadata);
332-
NonComplianceMessages = (Helpers\ConvertObjectToPSObject $item.NonComplianceMessage);
333-
NotScopes = (Helpers\ConvertObjectToPSObject $item.NotScope);
334-
Parameters = (Helpers\ConvertObjectToPSObject $item.Parameter);
328+
Metadata = (ConvertObjectToPSObject $item.Metadata);
329+
NonComplianceMessages = (ConvertObjectToPSObject $item.NonComplianceMessage);
330+
NotScopes = (ConvertObjectToPSObject $item.NotScope);
331+
Parameters = (ConvertObjectToPSObject $item.Parameter);
335332
PolicyDefinitionId = $item.PolicyDefinitionId;
336333
Scope = $item.Scope
337334
}
@@ -351,10 +348,10 @@ process {
351348
$item | Add-Member -MemberType NoteProperty -Name 'PolicyAssignmentId' -Value $item.Id
352349
}
353350

354-
$item | Add-Member -MemberType NoteProperty -Name 'Metadata' -Value (Helpers\ConvertObjectToPSObject $item.Metadata) -Force
355-
$item | Add-Member -MemberType NoteProperty -Name 'NonComplianceMessage' -Value (Helpers\ConvertObjectToPSObject $item.NonComplianceMessage) -Force
356-
$item | Add-Member -MemberType NoteProperty -Name 'NotScope' -Value (Helpers\ConvertObjectToPSObject $item.NotScope) -Force
357-
$item | Add-Member -MemberType NoteProperty -Name 'Parameter' -Value (Helpers\ConvertObjectToPSObject $item.Parameter) -Force
351+
$item | Add-Member -MemberType NoteProperty -Name 'Metadata' -Value (ConvertObjectToPSObject $item.Metadata) -Force
352+
$item | Add-Member -MemberType NoteProperty -Name 'NonComplianceMessage' -Value (ConvertObjectToPSObject $item.NonComplianceMessage) -Force
353+
$item | Add-Member -MemberType NoteProperty -Name 'NotScope' -Value (ConvertObjectToPSObject $item.NotScope) -Force
354+
$item | Add-Member -MemberType NoteProperty -Name 'Parameter' -Value (ConvertObjectToPSObject $item.Parameter) -Force
358355
$PSCmdlet.WriteObject($item)
359356
}
360357
}

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,6 @@ begin {
184184
Write-Host -ForegroundColor Cyan "begin:Get-AzPolicyDefinition(" $PSBoundParameters ") - (ParameterSet: $($PSCmdlet.ParameterSetName))"
185185
}
186186

187-
# load nested module containing common code
188-
Import-Module ((Get-Module -Name 'Az.Policy').NestedModules | ?{ $_.Name -eq 'Helpers' })
189-
190187
# mapping table of generated cmdlet parameter sets
191188
$mapping = @{
192189
Get='Az.Policy.private\Get-AzPolicyDefinition_Get';
@@ -224,7 +221,7 @@ process {
224221
$PSBoundParameters.Add('Filter', "policyType eq 'Static'")
225222
}
226223
'Id' {
227-
$parsed = Helpers\ParsePolicyDefinitionId $Id # function is imported from Helpers.psm1
224+
$parsed = ParsePolicyDefinitionId $Id # function is imported from Helpers.psm1
228225
switch ($parsed.ScopeType)
229226
{
230227
'subid' {
@@ -327,10 +324,10 @@ process {
327324
$propertyBag = @{
328325
Description = $item.Description;
329326
DisplayName = $item.DisplayName;
330-
Metadata = Helpers\ConvertObjectToPSObject $item.Metadata;
327+
Metadata = ConvertObjectToPSObject $item.Metadata;
331328
Mode = $item.Mode;
332-
Parameters = Helpers\ConvertObjectToPSObject $item.Parameter;
333-
PolicyRule = Helpers\ConvertObjectToPSObject $item.PolicyRule;
329+
Parameters = ConvertObjectToPSObject $item.Parameter;
330+
PolicyRule = ConvertObjectToPSObject $item.PolicyRule;
334331
PolicyType = $item.PolicyType
335332
}
336333

@@ -342,9 +339,9 @@ process {
342339
}
343340

344341
# use PSCustomObject for JSON properties
345-
$item | Add-Member -MemberType NoteProperty -Name 'Metadata' -Value (Helpers\ConvertObjectToPSObject $item.Metadata) -Force
346-
$item | Add-Member -MemberType NoteProperty -Name 'Parameter' -Value (Helpers\ConvertObjectToPSObject $item.Parameter) -Force
347-
$item | Add-Member -MemberType NoteProperty -Name 'PolicyRule' -Value (Helpers\ConvertObjectToPSObject $item.PolicyRule) -Force
342+
$item | Add-Member -MemberType NoteProperty -Name 'Metadata' -Value (ConvertObjectToPSObject $item.Metadata) -Force
343+
$item | Add-Member -MemberType NoteProperty -Name 'Parameter' -Value (ConvertObjectToPSObject $item.Parameter) -Force
344+
$item | Add-Member -MemberType NoteProperty -Name 'PolicyRule' -Value (ConvertObjectToPSObject $item.PolicyRule) -Force
348345
$PSCmdlet.WriteObject($item)
349346
}
350347
}

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,6 @@ begin {
193193
Write-Host -ForegroundColor Cyan "begin:Get-AzPolicyExemption(" $PSBoundParameters ") - (ParameterSet: $($PSCmdlet.ParameterSetName))"
194194
}
195195

196-
# load nested module containing common code
197-
Import-Module ((Get-Module -Name 'Az.Policy').NestedModules | ?{ $_.Name -eq 'Helpers' })
198-
199196
# make mapping table
200197
$mapping = @{
201198
Get = 'Az.Policy.private\Get-AzPolicyExemption_Get';
@@ -219,7 +216,7 @@ process {
219216
$null = $calledParameters.Remove('SubscriptionId')
220217

221218
if ($Id) {
222-
$parsed = Helpers\ParsePolicyExemptionId $Id
219+
$parsed = ParsePolicyExemptionId $Id
223220

224221
if ($parsed.Name) {
225222
$Name = $parsed.Name
@@ -249,7 +246,7 @@ process {
249246
}
250247

251248
if ($Scope) {
252-
$resolved = Helpers\ResolvePolicyExemption $null $Scope $null
249+
$resolved = ResolvePolicyExemption $null $Scope $null
253250
switch ($resolved.ScopeType) {
254251
'mgName' {
255252
if ($IncludeDescendent) {
@@ -313,8 +310,8 @@ process {
313310
DisplayName = $item.DisplayName;
314311
ExpiresOn = $item.ExpiresOn;
315312
ExemptionCategory = $item.ExemptionCategory;
316-
Metadata = (Helpers\ConvertObjectToPSObject $item.Metadata);
317-
PolicyDefinitionReferenceIds = (Helpers\ConvertObjectToPSObject $item.PolicyDefinitionReferenceId);
313+
Metadata = (ConvertObjectToPSObject $item.Metadata);
314+
PolicyDefinitionReferenceIds = (ConvertObjectToPSObject $item.PolicyDefinitionReferenceId);
318315
PolicyAssignmentId = $item.PolicyAssignmentId
319316
}
320317

@@ -324,8 +321,8 @@ process {
324321
$item | Add-Member -MemberType NoteProperty -Name 'ResourceType' -Value $item.Type
325322
}
326323

327-
$item | Add-Member -MemberType NoteProperty -Name 'Metadata' -Value (Helpers\ConvertObjectToPSObject $item.Metadata) -Force
328-
$item | Add-Member -MemberType NoteProperty -Name 'PolicyDefinitionReferenceId' -Value (Helpers\ConvertObjectToPSObject $item.PolicyDefinitionReferenceId) -Force
324+
$item | Add-Member -MemberType NoteProperty -Name 'Metadata' -Value (ConvertObjectToPSObject $item.Metadata) -Force
325+
$item | Add-Member -MemberType NoteProperty -Name 'PolicyDefinitionReferenceId' -Value (ConvertObjectToPSObject $item.PolicyDefinitionReferenceId) -Force
329326
$PSCmdlet.WriteObject($item)
330327
}
331328
}

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,6 @@ begin {
176176
Write-Host -ForegroundColor Cyan "begin:Get-AzPolicySetDefinition(" $PSBoundParameters ") - (ParameterSet: $($PSCmdlet.ParameterSetName))"
177177
}
178178

179-
# load nested module containing common code
180-
Import-Module ((Get-Module -Name 'Az.Policy').NestedModules | ?{ $_.Name -eq 'Helpers' })
181-
182179
# mapping table of generated cmdlet parameter sets
183180
$mapping = @{
184181
Get = 'Az.Policy.private\Get-AzPolicySetDefinition_Get';
@@ -212,7 +209,7 @@ process {
212209
$PSBoundParameters.Add('Filter', "policyType eq 'Custom'")
213210
}
214211
'Id' {
215-
$parsed = Helpers\ParsePolicySetDefinitionId $Id # function is imported from Helpers.psm1
212+
$parsed = ParsePolicySetDefinitionId $Id # function is imported from Helpers.psm1
216213
switch ($parsed.ScopeType)
217214
{
218215
'subid' {
@@ -316,10 +313,10 @@ process {
316313
$propertyBag = @{
317314
Description = $item.Description;
318315
DisplayName = $item.DisplayName;
319-
Metadata = Helpers\ConvertObjectToPSObject $item.Metadata;
320-
Parameters = Helpers\ConvertObjectToPSObject $item.Parameter;
321-
PolicyDefinitionGroups = Helpers\ConvertObjectToPSObject $item.PolicyDefinitionGroup;
322-
PolicyDefinitions = Helpers\ConvertObjectToPSObject $item.PolicyDefinition;
316+
Metadata = ConvertObjectToPSObject $item.Metadata;
317+
Parameters = ConvertObjectToPSObject $item.Parameter;
318+
PolicyDefinitionGroups = ConvertObjectToPSObject $item.PolicyDefinitionGroup;
319+
PolicyDefinitions = ConvertObjectToPSObject $item.PolicyDefinition;
323320
PolicyType = $item.PolicyType
324321
}
325322

@@ -331,10 +328,10 @@ process {
331328
}
332329

333330
# use PSCustomObject for JSON properties
334-
$item | Add-Member -MemberType NoteProperty -Name 'Metadata' -Value (Helpers\ConvertObjectToPSObject $item.Metadata) -Force
335-
$item | Add-Member -MemberType NoteProperty -Name 'Parameter' -Value (Helpers\ConvertObjectToPSObject $item.Parameter) -Force
336-
$item | Add-Member -MemberType NoteProperty -Name 'PolicyDefinitionGroup' -Value (Helpers\ConvertObjectToPSObject $item.PolicyDefinitionGroup) -Force
337-
$item | Add-Member -MemberType NoteProperty -Name 'PolicyDefinition' -Value (Helpers\ConvertObjectToPSObject $item.PolicyDefinition) -Force
331+
$item | Add-Member -MemberType NoteProperty -Name 'Metadata' -Value (ConvertObjectToPSObject $item.Metadata) -Force
332+
$item | Add-Member -MemberType NoteProperty -Name 'Parameter' -Value (ConvertObjectToPSObject $item.Parameter) -Force
333+
$item | Add-Member -MemberType NoteProperty -Name 'PolicyDefinitionGroup' -Value (ConvertObjectToPSObject $item.PolicyDefinitionGroup) -Force
334+
$item | Add-Member -MemberType NoteProperty -Name 'PolicyDefinition' -Value (ConvertObjectToPSObject $item.PolicyDefinition) -Force
338335
$PSCmdlet.WriteObject($item)
339336
}
340337
}

src/Resources/Policy.Autorest/custom/Helpers.psm1 renamed to src/Resources/Policy.Autorest/custom/Helpers.ps1

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
# split policy ids into usable parts (only used internally in this file)
1818
function parsePolicyId {
19+
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
1920
# the resource Id of a policy definition
2021
param($resourceId, $typeName)
2122

@@ -85,6 +86,7 @@ function parsePolicyId {
8586

8687
# split policy definition resourceId into its parts (used externally)
8788
function ParsePolicyDefinitionId {
89+
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
8890
# the resource Id of a policy definition
8991
param($ResourceId)
9092

@@ -93,6 +95,7 @@ function ParsePolicyDefinitionId {
9395

9496
# split policy set definition resourceId into its parts
9597
function ParsePolicySetDefinitionId {
98+
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
9699
# the resource Id of a policy set definition
97100
param($ResourceId)
98101

@@ -101,6 +104,7 @@ function ParsePolicySetDefinitionId {
101104

102105
# split policy assignment resourceId into its parts
103106
function ParsePolicyAssignmentId {
107+
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
104108
# the resource Id of a policy set definition
105109
param($ResourceId)
106110

@@ -109,6 +113,7 @@ function ParsePolicyAssignmentId {
109113

110114
# split policy assignment resourceId into its parts
111115
function ParsePolicyExemptionId {
116+
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
112117
# the resource Id of a policy set definition
113118
param($resourceId)
114119

@@ -118,6 +123,7 @@ function ParsePolicyExemptionId {
118123
# Convert input parameter value to hashtable type expected by the autorest serializers
119124
function ConvertParameterArray
120125
{
126+
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
121127
param ($InputObject)
122128

123129
if ($InputObject -is [array])
@@ -151,6 +157,7 @@ function ConvertParameterArray
151157
# convert various input formats to policy-formatted hashtable suitable for autorest serializers
152158
function ConvertParameterObject
153159
{
160+
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
154161
param ($InputObject)
155162

156163
if (!$InputObject)
@@ -188,6 +195,7 @@ function ConvertParameterObject
188195

189196
# Convert output hashtable object output by autorest serializers to PSCustomObject format for legacy support
190197
function ConvertObjectToPSObject {
198+
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
191199
param($InputObject)
192200

193201
if ($null -eq $InputObject) {
@@ -210,11 +218,13 @@ function ConvertObjectToPSObject {
210218
ConvertFrom-Json $jsonString -Depth 100
211219
}
212220

213-
function GetPSObjectProperty(
214-
[PSObject]$PropertyObject,
215-
[string]$PropertyPath
216-
)
217-
{
221+
function GetPSObjectProperty {
222+
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
223+
param (
224+
[PSObject]$PropertyObject,
225+
[string]$PropertyPath
226+
)
227+
218228
$propertyNames = $PropertyPath.Split('.')
219229
$tmpObject = $PropertyObject
220230
foreach ($propertyName in $propertyNames)
@@ -237,6 +247,7 @@ function GetPSObjectProperty(
237247

238248
# tests whether the given string is a Uri
239249
function Test-Uri {
250+
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
240251
param([string]$Value)
241252

242253
$uri = ''
@@ -245,6 +256,7 @@ function Test-Uri {
245256

246257
# issues a GET to the given address and returns the contents
247258
function Get-UriContent {
259+
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
248260
param([string]$UriAddress)
249261

250262
$response = Invoke-WebRequest $UriAddress -DisableKeepAlive -Method Get
@@ -256,6 +268,7 @@ function Get-UriContent {
256268
# if the given string is a file path or URI, returns the contents of the file or web page
257269
# otherwise returns the original string
258270
function GetFileUriOrStringParameterValue {
271+
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
259272
param([string]$parameterValue)
260273

261274
if (Test-Path $parameterValue) {
@@ -272,6 +285,7 @@ function GetFileUriOrStringParameterValue {
272285
}
273286

274287
function ResolvePolicyParameter {
288+
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
275289
param(
276290
[string]$ParameterName,
277291
[string]$ParameterValue,
@@ -292,6 +306,7 @@ function ResolvePolicyParameter {
292306
}
293307

294308
function ResolvePolicyMetadataParameter {
309+
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
295310
param(
296311
$MetadataValue,
297312
[bool]$Debug = $false
@@ -325,6 +340,7 @@ function ResolvePolicyMetadataParameter {
325340

326341
# construct the full Id of a resource given the various parts (only used internally in this file)
327342
function resolvePolicyArtifact {
343+
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
328344
param(
329345
[string]$name,
330346
[string]$subscriptionId,
@@ -417,6 +433,7 @@ function resolvePolicyArtifact {
417433
}
418434

419435
function ResolvePolicyDefinition {
436+
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
420437
param(
421438
[string]$Name,
422439
[string]$SubscriptionId,
@@ -428,6 +445,7 @@ function ResolvePolicyDefinition {
428445
}
429446

430447
function ResolvePolicySetDefinition {
448+
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
431449
param(
432450
[string]$Name,
433451
[string]$SubscriptionId,
@@ -439,6 +457,7 @@ function ResolvePolicySetDefinition {
439457
}
440458

441459
function ResolvePolicyAssignment {
460+
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
442461
param(
443462
[string]$Name,
444463
[string]$Scope,
@@ -459,6 +478,7 @@ function ResolvePolicyAssignment {
459478
}
460479

461480
function ResolvePolicyExemption {
481+
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
462482
param(
463483
[string]$Name,
464484
[string]$Scope,

0 commit comments

Comments
 (0)