-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathEdit-AzDataProtectionPolicyRetentionRuleClientObject.ps1
More file actions
114 lines (93 loc) · 6.05 KB
/
Copy pathEdit-AzDataProtectionPolicyRetentionRuleClientObject.ps1
File metadata and controls
114 lines (93 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
function Edit-AzDataProtectionPolicyRetentionRuleClientObject {
[Microsoft.Azure.PowerShell.Cmdlets.DataProtection.ModelCmdletAttribute()]
[OutputType('Microsoft.Azure.PowerShell.Cmdlets.DataProtection.Models.IBackupPolicy')]
[CmdletBinding(PositionalBinding=$false)]
[Microsoft.Azure.PowerShell.Cmdlets.DataProtection.Description('Adds or removes Retention Rule to an existing backup policy. For AzureBlob hybrid policies, OperationalStore lifecycles must use -Name Default_OperationalStore; -Name Default is reserved for VaultStore. Mixing these (or attaching an OperationalStore lifecycle to Weekly/Monthly/Yearly) will throw a validation error.')]
[Microsoft.Azure.PowerShell.Cmdlets.DataProtection.Runtime.PreviewMessage("**********************************************************************************************`n
* This cmdlet will undergo a breaking change in Az v16.0.0, to be released on May 2026. *`n
* At least one change applies to this cmdlet. *`n
* See all possible breaking changes at https://go.microsoft.com/fwlink/?linkid=2333486 *`n
***************************************************************************************************")]
param(
[Parameter(ParameterSetName='AddRetention',Mandatory, HelpMessage='Backup Policy Object')]
[Parameter(ParameterSetName='RemoveRetention',Mandatory, HelpMessage='Backup Policy Object')]
[Microsoft.Azure.PowerShell.Cmdlets.DataProtection.Models.IBackupPolicy]
${Policy},
[Parameter(ParameterSetName='AddRetention',Mandatory, HelpMessage='Retention Rule Name.')]
[Parameter(ParameterSetName='RemoveRetention',Mandatory, HelpMessage='Retention Rule Name. Note: Default retention rules cannot be removed.')]
[Microsoft.Azure.PowerShell.Cmdlets.DataProtection.Support.RetentionRuleName]
${Name},
[Parameter(ParameterSetName='AddRetention',Mandatory, HelpMessage='Specifies if retention rule is default retention rule.')]
[System.Boolean]
${IsDefault},
[Parameter(ParameterSetName='RemoveRetention',Mandatory, HelpMessage='Specifies whether to remove the retention rule.')]
[System.Management.Automation.SwitchParameter]
${RemoveRule},
[Parameter(ParameterSetName='AddRetention',Mandatory=$false, HelpMessage='Specifies whether to modify an existing LifeCycle.')]
[Nullable[System.Boolean]]
${OverwriteLifeCycle},
[Parameter(ParameterSetName='AddRetention',Mandatory, HelpMessage='Life cycles associated with the retention rule.')]
[Microsoft.Azure.PowerShell.Cmdlets.DataProtection.Models.ISourceLifeCycle[]]
${LifeCycles}
)
process {
$parameterSetName = $PsCmdlet.ParameterSetName
if($parameterSetName -eq "RemoveRetention"){
if($Name -eq "Default")
{
throw "Removing Default Retention Rule is not allowed. Please try again with different rule name."
}
$filteredRules = $Policy.PolicyRule | Where-Object { $_.Name -ne $Name }
$Policy.PolicyRule = $filteredRules
return $Policy
}
if($parameterSetName -eq "AddRetention"){
$DatasourceType = GetClientDatasourceType -ServiceDatasourceType $Policy.DatasourceType[0]
$manifest = LoadManifest -DatasourceType $DatasourceType
$defaultRetentionMapping = $null
if($null -ne $manifest.policySettings -and ($manifest.policySettings.PSObject.Properties.Name -contains "defaultRetentionRuleNames")){
$defaultRetentionMapping = $manifest.policySettings.defaultRetentionRuleNames
}
$mappedDefaultNames = @()
if($null -ne $defaultRetentionMapping){
$mappedDefaultNames = ValidateRetentionRuleMatchesMappedStore -Name $Name.ToString() -DefaultRetentionMapping $defaultRetentionMapping -LifeCycles $LifeCycles -DatasourceType $DatasourceType
ValidateExclusiveSourceStoreAssignment -Name $Name.ToString() -Manifest $manifest -DefaultRetentionMapping $defaultRetentionMapping -LifeCycles $LifeCycles -DatasourceType $DatasourceType
}
$retentionPolicyIndex = -1
$policyRuleCount = @($Policy.PolicyRule).Count
Foreach($index in (0..($policyRuleCount - 1))){
if($Policy.PolicyRule[$index].Name -eq $Name){
$retentionPolicyIndex = $index
}
}
if($retentionPolicyIndex -eq -1){
if($manifest.policySettings.disableAddRetentionRule -eq $true)
{
$message = "Adding New Retention Rule is not supported for " + $DatasourceType + " datasource type."
throw $message
}
$isMappedDefault = ($mappedDefaultNames -contains $Name.ToString())
if(($manifest.policySettings.supportedRetentionTags.Contains($Name.ToString()) -eq $false) -and (-not $isMappedDefault))
{
throw "Selected Retention Rule " + $Name + " is not applicable for datasource type " + $DatasourceType
}
$newRetentionRule = [Microsoft.Azure.PowerShell.Cmdlets.DataProtection.Models.AzureRetentionRule]::new()
$newRetentionRule.ObjectType = "AzureRetentionRule"
$newRetentionRule.IsDefault = $IsDefault
$newRetentionRule.Name = $Name
$newRetentionRule.LifeCycle = $LifeCycles
$Policy.PolicyRule += $newRetentionRule
return $Policy
}
if($retentionPolicyIndex -ne -1){
if($OverwriteLifeCycle -eq $false){
throw "Retention rule '$Name' already exists. Use -OverwriteLifeCycle `$true to update it."
}
else {
$Policy.PolicyRule[$retentionPolicyIndex].LifeCycle = $LifeCycles
}
return $Policy
}
}
}
}