|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using System; |
| 16 | +using System.Collections.Generic; |
| 17 | +using Microsoft.Azure.Commands.Sql.Backup.Services; |
| 18 | +using Microsoft.Azure.Commands.Sql.Common; |
| 19 | +using System.Management.Automation; |
| 20 | +using Microsoft.Azure.Commands.Common.Authentication.Abstractions; |
| 21 | +using Microsoft.Azure.Commands.Sql.Backup.Model; |
| 22 | +using Microsoft.Azure.Commands.Sql.Database.Model; |
| 23 | +using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; |
| 24 | + |
| 25 | +namespace Microsoft.Azure.Commands.Sql.Backup.Cmdlet |
| 26 | +{ |
| 27 | + public abstract class AzureSqlDatabaseBackupShortTermRetentionPolicyCmdletBase : AzureSqlCmdletBase<IEnumerable<AzureSqlDatabaseBackupShortTermRetentionPolicyModel>, AzureSqlDatabaseBackupAdapter> |
| 28 | + { |
| 29 | + /// <summary> |
| 30 | + /// The expected number of segments in a short term retention policy resource id. |
| 31 | + /// </summary> |
| 32 | + private const int BackupShortTermRetentionPolicyResourceIdSegmentsLength = 12; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Parameter set with ResourceGroup name, Server name and Database name. |
| 36 | + /// </summary> |
| 37 | + protected const string PolicyByResourceServerDatabaseSet = "PolicyByResourceServerDatabaseSet"; |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Parameter set for using a Database Input Object. |
| 41 | + /// </summary> |
| 42 | + private const string PolicyByInputObjectSet = "PolicyByInputObjectSet"; |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Parameter set for using a resource Id. |
| 46 | + /// </summary> |
| 47 | + private const string PolicyByResourceIdSet = "PolicyByResourceIdSet"; |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Gets or sets the Database object to get the policy for. |
| 51 | + /// </summary> |
| 52 | + [Parameter( |
| 53 | + ParameterSetName = PolicyByInputObjectSet, |
| 54 | + Mandatory = true, |
| 55 | + ValueFromPipeline = true, |
| 56 | + HelpMessage = "The database object to get the policy for.")] |
| 57 | + [ValidateNotNullOrEmpty] |
| 58 | + [Alias("AzureSqlDatabase")] |
| 59 | + public AzureSqlDatabaseModel AzureSqlDatabaseObject { get; set; } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Gets or sets the Database object to get the policy for. |
| 63 | + /// </summary> |
| 64 | + [Parameter( |
| 65 | + ParameterSetName = PolicyByResourceIdSet, |
| 66 | + Mandatory = true, |
| 67 | + ValueFromPipelineByPropertyName = true, |
| 68 | + HelpMessage = "The short term retention policy resource Id.")] |
| 69 | + [ValidateNotNullOrEmpty] |
| 70 | + public string ResourceId { get; set; } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Gets or sets the name of the resource group to use. |
| 74 | + /// </summary> |
| 75 | + [Parameter( |
| 76 | + ParameterSetName = PolicyByResourceServerDatabaseSet, |
| 77 | + Mandatory = true, |
| 78 | + ValueFromPipelineByPropertyName = true, |
| 79 | + Position = 0, |
| 80 | + HelpMessage = "The name of the resource group.")] |
| 81 | + [ResourceGroupCompleter] |
| 82 | + [ValidateNotNullOrEmpty] |
| 83 | + public override string ResourceGroupName { get; set; } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Gets or sets the name of the database server to use. |
| 87 | + /// </summary> |
| 88 | + [Parameter(ParameterSetName = PolicyByResourceServerDatabaseSet, |
| 89 | + Mandatory = true, |
| 90 | + ValueFromPipelineByPropertyName = true, |
| 91 | + Position = 1, |
| 92 | + HelpMessage = "The name of the Azure SQL Server the database is in.")] |
| 93 | + [ValidateNotNullOrEmpty] |
| 94 | + public string ServerName { get; set; } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Gets or sets the name of the database to use. |
| 98 | + /// </summary> |
| 99 | + [Parameter( |
| 100 | + ParameterSetName = PolicyByResourceServerDatabaseSet, |
| 101 | + Mandatory = true, |
| 102 | + ValueFromPipelineByPropertyName = true, |
| 103 | + Position = 2, |
| 104 | + HelpMessage = "The name of the Azure SQL Database to use.")] |
| 105 | + [ValidateNotNullOrEmpty] |
| 106 | + public string DatabaseName { get; set; } |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// Initializes the adapter |
| 110 | + /// </summary> |
| 111 | + /// <param name="subscription">The subscription to operate on</param> |
| 112 | + /// <returns></returns> |
| 113 | + protected override AzureSqlDatabaseBackupAdapter InitModelAdapter(IAzureSubscription subscription) |
| 114 | + { |
| 115 | + return new AzureSqlDatabaseBackupAdapter(DefaultProfile.DefaultContext); |
| 116 | + } |
| 117 | + |
| 118 | + public override void ExecuteCmdlet() |
| 119 | + { |
| 120 | + if (AzureSqlDatabaseObject != null) |
| 121 | + { |
| 122 | + this.ResourceGroupName = AzureSqlDatabaseObject.ResourceGroupName; |
| 123 | + this.ServerName = AzureSqlDatabaseObject.ServerName; |
| 124 | + this.DatabaseName = AzureSqlDatabaseObject.DatabaseName; |
| 125 | + } |
| 126 | + else if (!string.IsNullOrEmpty(ResourceId)) |
| 127 | + { |
| 128 | + ParseResourceId(); |
| 129 | + } |
| 130 | + |
| 131 | + base.ExecuteCmdlet(); |
| 132 | + } |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Helper method to parse resourceGroupName, serverName, databaseName from resourceId |
| 136 | + /// Ideally this would utilize Microsoft.Azure.Management.Internal.Resources.Utilities.Models.ResourceIdentifier |
| 137 | + /// However, that class is not setup to handle resources that have an ancestry higher than just parent level. |
| 138 | + /// That class could recursively create a parent ResourceIdentifier on a __get__ ParentResource, |
| 139 | + /// rather than just a string, so a consumer could work all the way to the root resource easily. |
| 140 | + /// Leave as a TODO for now, as many other cmdlets consume that class, and I will work on it in separate change. |
| 141 | + /// </summary> |
| 142 | + private void ParseResourceId() |
| 143 | + { |
| 144 | + string[] tokens = ResourceId.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); |
| 145 | + |
| 146 | + // "/subscriptions/<subId>/resourceGroups/<resourceGroup>/providers/Microsoft.Sql/servers/<server>/databases/<database>/backupShortTermRetentionPolicies/default" |
| 147 | + if (tokens.Length != BackupShortTermRetentionPolicyResourceIdSegmentsLength) |
| 148 | + { |
| 149 | + throw new ArgumentException("Invalid format of the resource identifier.", "ResourceId"); |
| 150 | + } |
| 151 | + |
| 152 | + // Convert tokens into TYPE:NAME key value pairs, ignoring case |
| 153 | + Dictionary<string, string> segments = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase); |
| 154 | + int i = 0; |
| 155 | + while (i < tokens.Length) |
| 156 | + { |
| 157 | + string type = tokens[i++]; |
| 158 | + string name = tokens[i++]; |
| 159 | + segments[type] = name; |
| 160 | + } |
| 161 | + |
| 162 | + try |
| 163 | + { |
| 164 | + this.ResourceGroupName = segments["resourceGroups"]; |
| 165 | + this.ServerName = segments["servers"]; |
| 166 | + this.DatabaseName = segments["databases"]; |
| 167 | + } |
| 168 | + catch (KeyNotFoundException) |
| 169 | + { |
| 170 | + throw new ArgumentException( |
| 171 | + "Invalid format of the resource identifier. ResourceID should follow format /subscriptions/<subscriptionId>/resourceGroups/<resourceGroupName>/providers/Microsoft.Sql/servers/<serverName>/databases/<databaseName>/backupShortTermRetentionPolicies/default"); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments