Skip to content
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

Move Policy CRUD cmdlets to autorest generation: fixes #23518

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
2 changes: 1 addition & 1 deletion src/Resources/Policy.Autorest/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ generated
internal
exports
tools
custom/Az.Policy.custom.psm1
custom/*.psm1
custom/autogen-model-cmdlets
test/*-TestResults.xml
/*.ps1
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/Policy.Autorest/Az.Policy.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
PowerShellVersion = '5.1'
DotNetFrameworkVersion = '4.7.2'
RequiredAssemblies = './bin/Az.Policy.private.dll'
NestedModules = @('./custom/Helpers')
FormatsToProcess = './Az.Policy.format.ps1xml'
ScriptsToProcess = @('./custom/Helpers.ps1')
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', '*'
AliasesToExport = 'Set-AzPolicyAssignment', 'Set-AzPolicyDefinition', 'Set-AzPolicyExemption', 'Set-AzPolicySetDefinition', '*'
PrivateData = @{
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/Policy.Autorest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ directive:
hide: true

metadata:
nestedModules:
- ./custom/Helpers
scriptsToProcess:
- ./custom/Helpers.ps1

```
21 changes: 9 additions & 12 deletions src/Resources/Policy.Autorest/custom/Get-AzPolicyAssignment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,6 @@ begin {
Write-Host -ForegroundColor Cyan "begin:Get-AzPolicyAssignment(" $PSBoundParameters ") - (ParameterSet: $($PSCmdlet.ParameterSetName))"
}

# load nested module containing common code
Import-Module ((Get-Module -Name 'Az.Policy').NestedModules | ?{ $_.Name -eq 'Helpers' })

# make mapping table
$mapping = @{
Get = 'Az.Policy.private\Get-AzPolicyAssignment_Get';
Expand Down Expand Up @@ -262,7 +259,7 @@ process {
$calledParameterSet = 'List3'

if ($Scope) {
$resolved = Helpers\ResolvePolicyAssignment $null $Scope $null
$resolved = ResolvePolicyAssignment $null $Scope $null
switch ($resolved.ScopeType) {
'mgName' {
if ($IncludeDescendent) {
Expand Down Expand Up @@ -328,10 +325,10 @@ process {
Description = $item.Description;
DisplayName = $item.DisplayName;
EnforcementMode = $item.EnforcementMode;
Metadata = (Helpers\ConvertObjectToPSObject $item.Metadata);
NonComplianceMessages = (Helpers\ConvertObjectToPSObject $item.NonComplianceMessage);
NotScopes = (Helpers\ConvertObjectToPSObject $item.NotScope);
Parameters = (Helpers\ConvertObjectToPSObject $item.Parameter);
Metadata = (ConvertObjectToPSObject $item.Metadata);
NonComplianceMessages = (ConvertObjectToPSObject $item.NonComplianceMessage);
NotScopes = (ConvertObjectToPSObject $item.NotScope);
Parameters = (ConvertObjectToPSObject $item.Parameter);
PolicyDefinitionId = $item.PolicyDefinitionId;
Scope = $item.Scope
}
Expand All @@ -351,10 +348,10 @@ process {
$item | Add-Member -MemberType NoteProperty -Name 'PolicyAssignmentId' -Value $item.Id
}

$item | Add-Member -MemberType NoteProperty -Name 'Metadata' -Value (Helpers\ConvertObjectToPSObject $item.Metadata) -Force
$item | Add-Member -MemberType NoteProperty -Name 'NonComplianceMessage' -Value (Helpers\ConvertObjectToPSObject $item.NonComplianceMessage) -Force
$item | Add-Member -MemberType NoteProperty -Name 'NotScope' -Value (Helpers\ConvertObjectToPSObject $item.NotScope) -Force
$item | Add-Member -MemberType NoteProperty -Name 'Parameter' -Value (Helpers\ConvertObjectToPSObject $item.Parameter) -Force
$item | Add-Member -MemberType NoteProperty -Name 'Metadata' -Value (ConvertObjectToPSObject $item.Metadata) -Force
$item | Add-Member -MemberType NoteProperty -Name 'NonComplianceMessage' -Value (ConvertObjectToPSObject $item.NonComplianceMessage) -Force
$item | Add-Member -MemberType NoteProperty -Name 'NotScope' -Value (ConvertObjectToPSObject $item.NotScope) -Force
$item | Add-Member -MemberType NoteProperty -Name 'Parameter' -Value (ConvertObjectToPSObject $item.Parameter) -Force
$PSCmdlet.WriteObject($item)
}
}
Expand Down
17 changes: 7 additions & 10 deletions src/Resources/Policy.Autorest/custom/Get-AzPolicyDefinition.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,6 @@ begin {
Write-Host -ForegroundColor Cyan "begin:Get-AzPolicyDefinition(" $PSBoundParameters ") - (ParameterSet: $($PSCmdlet.ParameterSetName))"
}

# load nested module containing common code
Import-Module ((Get-Module -Name 'Az.Policy').NestedModules | ?{ $_.Name -eq 'Helpers' })

# mapping table of generated cmdlet parameter sets
$mapping = @{
Get='Az.Policy.private\Get-AzPolicyDefinition_Get';
Expand Down Expand Up @@ -224,7 +221,7 @@ process {
$PSBoundParameters.Add('Filter', "policyType eq 'Static'")
}
'Id' {
$parsed = Helpers\ParsePolicyDefinitionId $Id # function is imported from Helpers.psm1
$parsed = ParsePolicyDefinitionId $Id # function is imported from Helpers.psm1
switch ($parsed.ScopeType)
{
'subid' {
Expand Down Expand Up @@ -327,10 +324,10 @@ process {
$propertyBag = @{
Description = $item.Description;
DisplayName = $item.DisplayName;
Metadata = Helpers\ConvertObjectToPSObject $item.Metadata;
Metadata = ConvertObjectToPSObject $item.Metadata;
Mode = $item.Mode;
Parameters = Helpers\ConvertObjectToPSObject $item.Parameter;
PolicyRule = Helpers\ConvertObjectToPSObject $item.PolicyRule;
Parameters = ConvertObjectToPSObject $item.Parameter;
PolicyRule = ConvertObjectToPSObject $item.PolicyRule;
PolicyType = $item.PolicyType
}

Expand All @@ -342,9 +339,9 @@ process {
}

# use PSCustomObject for JSON properties
$item | Add-Member -MemberType NoteProperty -Name 'Metadata' -Value (Helpers\ConvertObjectToPSObject $item.Metadata) -Force
$item | Add-Member -MemberType NoteProperty -Name 'Parameter' -Value (Helpers\ConvertObjectToPSObject $item.Parameter) -Force
$item | Add-Member -MemberType NoteProperty -Name 'PolicyRule' -Value (Helpers\ConvertObjectToPSObject $item.PolicyRule) -Force
$item | Add-Member -MemberType NoteProperty -Name 'Metadata' -Value (ConvertObjectToPSObject $item.Metadata) -Force
$item | Add-Member -MemberType NoteProperty -Name 'Parameter' -Value (ConvertObjectToPSObject $item.Parameter) -Force
$item | Add-Member -MemberType NoteProperty -Name 'PolicyRule' -Value (ConvertObjectToPSObject $item.PolicyRule) -Force
$PSCmdlet.WriteObject($item)
}
}
Expand Down
15 changes: 6 additions & 9 deletions src/Resources/Policy.Autorest/custom/Get-AzPolicyExemption.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,6 @@ begin {
Write-Host -ForegroundColor Cyan "begin:Get-AzPolicyExemption(" $PSBoundParameters ") - (ParameterSet: $($PSCmdlet.ParameterSetName))"
}

# load nested module containing common code
Import-Module ((Get-Module -Name 'Az.Policy').NestedModules | ?{ $_.Name -eq 'Helpers' })

# make mapping table
$mapping = @{
Get = 'Az.Policy.private\Get-AzPolicyExemption_Get';
Expand All @@ -219,7 +216,7 @@ process {
$null = $calledParameters.Remove('SubscriptionId')

if ($Id) {
$parsed = Helpers\ParsePolicyExemptionId $Id
$parsed = ParsePolicyExemptionId $Id

if ($parsed.Name) {
$Name = $parsed.Name
Expand Down Expand Up @@ -249,7 +246,7 @@ process {
}

if ($Scope) {
$resolved = Helpers\ResolvePolicyExemption $null $Scope $null
$resolved = ResolvePolicyExemption $null $Scope $null
switch ($resolved.ScopeType) {
'mgName' {
if ($IncludeDescendent) {
Expand Down Expand Up @@ -313,8 +310,8 @@ process {
DisplayName = $item.DisplayName;
ExpiresOn = $item.ExpiresOn;
ExemptionCategory = $item.ExemptionCategory;
Metadata = (Helpers\ConvertObjectToPSObject $item.Metadata);
PolicyDefinitionReferenceIds = (Helpers\ConvertObjectToPSObject $item.PolicyDefinitionReferenceId);
Metadata = (ConvertObjectToPSObject $item.Metadata);
PolicyDefinitionReferenceIds = (ConvertObjectToPSObject $item.PolicyDefinitionReferenceId);
PolicyAssignmentId = $item.PolicyAssignmentId
}

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

$item | Add-Member -MemberType NoteProperty -Name 'Metadata' -Value (Helpers\ConvertObjectToPSObject $item.Metadata) -Force
$item | Add-Member -MemberType NoteProperty -Name 'PolicyDefinitionReferenceId' -Value (Helpers\ConvertObjectToPSObject $item.PolicyDefinitionReferenceId) -Force
$item | Add-Member -MemberType NoteProperty -Name 'Metadata' -Value (ConvertObjectToPSObject $item.Metadata) -Force
$item | Add-Member -MemberType NoteProperty -Name 'PolicyDefinitionReferenceId' -Value (ConvertObjectToPSObject $item.PolicyDefinitionReferenceId) -Force
$PSCmdlet.WriteObject($item)
}
}
Expand Down
21 changes: 9 additions & 12 deletions src/Resources/Policy.Autorest/custom/Get-AzPolicySetDefinition.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,6 @@ begin {
Write-Host -ForegroundColor Cyan "begin:Get-AzPolicySetDefinition(" $PSBoundParameters ") - (ParameterSet: $($PSCmdlet.ParameterSetName))"
}

# load nested module containing common code
Import-Module ((Get-Module -Name 'Az.Policy').NestedModules | ?{ $_.Name -eq 'Helpers' })

# mapping table of generated cmdlet parameter sets
$mapping = @{
Get = 'Az.Policy.private\Get-AzPolicySetDefinition_Get';
Expand Down Expand Up @@ -212,7 +209,7 @@ process {
$PSBoundParameters.Add('Filter', "policyType eq 'Custom'")
}
'Id' {
$parsed = Helpers\ParsePolicySetDefinitionId $Id # function is imported from Helpers.psm1
$parsed = ParsePolicySetDefinitionId $Id # function is imported from Helpers.psm1
switch ($parsed.ScopeType)
{
'subid' {
Expand Down Expand Up @@ -316,10 +313,10 @@ process {
$propertyBag = @{
Description = $item.Description;
DisplayName = $item.DisplayName;
Metadata = Helpers\ConvertObjectToPSObject $item.Metadata;
Parameters = Helpers\ConvertObjectToPSObject $item.Parameter;
PolicyDefinitionGroups = Helpers\ConvertObjectToPSObject $item.PolicyDefinitionGroup;
PolicyDefinitions = Helpers\ConvertObjectToPSObject $item.PolicyDefinition;
Metadata = ConvertObjectToPSObject $item.Metadata;
Parameters = ConvertObjectToPSObject $item.Parameter;
PolicyDefinitionGroups = ConvertObjectToPSObject $item.PolicyDefinitionGroup;
PolicyDefinitions = ConvertObjectToPSObject $item.PolicyDefinition;
PolicyType = $item.PolicyType
}

Expand All @@ -331,10 +328,10 @@ process {
}

# use PSCustomObject for JSON properties
$item | Add-Member -MemberType NoteProperty -Name 'Metadata' -Value (Helpers\ConvertObjectToPSObject $item.Metadata) -Force
$item | Add-Member -MemberType NoteProperty -Name 'Parameter' -Value (Helpers\ConvertObjectToPSObject $item.Parameter) -Force
$item | Add-Member -MemberType NoteProperty -Name 'PolicyDefinitionGroup' -Value (Helpers\ConvertObjectToPSObject $item.PolicyDefinitionGroup) -Force
$item | Add-Member -MemberType NoteProperty -Name 'PolicyDefinition' -Value (Helpers\ConvertObjectToPSObject $item.PolicyDefinition) -Force
$item | Add-Member -MemberType NoteProperty -Name 'Metadata' -Value (ConvertObjectToPSObject $item.Metadata) -Force
$item | Add-Member -MemberType NoteProperty -Name 'Parameter' -Value (ConvertObjectToPSObject $item.Parameter) -Force
$item | Add-Member -MemberType NoteProperty -Name 'PolicyDefinitionGroup' -Value (ConvertObjectToPSObject $item.PolicyDefinitionGroup) -Force
$item | Add-Member -MemberType NoteProperty -Name 'PolicyDefinition' -Value (ConvertObjectToPSObject $item.PolicyDefinition) -Force
$PSCmdlet.WriteObject($item)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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

Expand Down Expand Up @@ -85,6 +86,7 @@ function parsePolicyId {

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

Expand All @@ -93,6 +95,7 @@ function ParsePolicyDefinitionId {

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

Expand All @@ -101,6 +104,7 @@ function ParsePolicySetDefinitionId {

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

Expand All @@ -109,6 +113,7 @@ function ParsePolicyAssignmentId {

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

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

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

if (!$InputObject)
Expand Down Expand Up @@ -188,6 +195,7 @@ function ConvertParameterObject

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

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

function GetPSObjectProperty(
[PSObject]$PropertyObject,
[string]$PropertyPath
)
{
function GetPSObjectProperty {
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
param (
[PSObject]$PropertyObject,
[string]$PropertyPath
)

$propertyNames = $PropertyPath.Split('.')
$tmpObject = $PropertyObject
foreach ($propertyName in $propertyNames)
Expand All @@ -237,6 +247,7 @@ function GetPSObjectProperty(

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

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

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

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

if (Test-Path $parameterValue) {
Expand All @@ -272,6 +285,7 @@ function GetFileUriOrStringParameterValue {
}

function ResolvePolicyParameter {
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
param(
[string]$ParameterName,
[string]$ParameterValue,
Expand All @@ -292,6 +306,7 @@ function ResolvePolicyParameter {
}

function ResolvePolicyMetadataParameter {
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
param(
$MetadataValue,
[bool]$Debug = $false
Expand Down Expand Up @@ -325,6 +340,7 @@ function ResolvePolicyMetadataParameter {

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

function ResolvePolicyDefinition {
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
param(
[string]$Name,
[string]$SubscriptionId,
Expand All @@ -428,6 +445,7 @@ function ResolvePolicyDefinition {
}

function ResolvePolicySetDefinition {
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
param(
[string]$Name,
[string]$SubscriptionId,
Expand All @@ -439,6 +457,7 @@ function ResolvePolicySetDefinition {
}

function ResolvePolicyAssignment {
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
param(
[string]$Name,
[string]$Scope,
Expand All @@ -459,6 +478,7 @@ function ResolvePolicyAssignment {
}

function ResolvePolicyExemption {
[Microsoft.Azure.PowerShell.Cmdlets.Policy.DoNotExportAttribute()]
param(
[string]$Name,
[string]$Scope,
Expand Down
Loading
Loading