|
| 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.Management.Automation; |
| 16 | +using Microsoft.Azure.Commands.Sql.Location_Capabilities.Model; |
| 17 | +using Microsoft.Azure.Commands.Sql.Location_Capabilities.Services; |
| 18 | +using Microsoft.WindowsAzure.Commands.Utilities.Common; |
| 19 | +using System.Linq; |
| 20 | + |
| 21 | +namespace Microsoft.Azure.Commands.Sql.Location_Capabilities.Cmdlet |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Defines the Get-AzureSqlLocationCapability cmdlet |
| 25 | + /// </summary> |
| 26 | + [Cmdlet(VerbsCommon.Get, "AzureSqlLocationCapability", |
| 27 | + ConfirmImpact = ConfirmImpact.None, |
| 28 | + DefaultParameterSetName = _filtered)] |
| 29 | + public class GetAzureSqlLocationCapability : AzurePSCmdlet |
| 30 | + { |
| 31 | + /// <summary> |
| 32 | + /// Parameter set name for when the cmdlet is invoked without specifying -Default |
| 33 | + /// </summary> |
| 34 | + private const string _filtered = "FilterResults"; |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Parameter set name for when the cmdlet is invoked with the -Default switch specified |
| 38 | + /// </summary> |
| 39 | + private const string _default = "DefaultResults"; |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Gets or sets the name of the Location |
| 43 | + /// </summary> |
| 44 | + [Parameter(Mandatory = true, |
| 45 | + ValueFromPipelineByPropertyName = true, |
| 46 | + Position = 0, |
| 47 | + HelpMessage = "The name of the Location for which to get the capabilities")] |
| 48 | + [ValidateNotNullOrEmpty] |
| 49 | + public string LocationName { get; set; } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Gets or sets the name of the server version |
| 53 | + /// </summary> |
| 54 | + [Parameter(Mandatory = false, |
| 55 | + ValueFromPipelineByPropertyName = true, |
| 56 | + HelpMessage = "The name of the server version for which to get the capabilities", |
| 57 | + ParameterSetName = _filtered)] |
| 58 | + [ValidateNotNullOrEmpty] |
| 59 | + public string ServerVersionName { get; set; } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Gets or sets the name of the database edition |
| 63 | + /// </summary> |
| 64 | + [Parameter(Mandatory = false, |
| 65 | + ValueFromPipelineByPropertyName = true, |
| 66 | + HelpMessage = "The name of the database edition for which to get the capabilities", |
| 67 | + ParameterSetName = _filtered)] |
| 68 | + [ValidateNotNullOrEmpty] |
| 69 | + public string EditionName { get; set; } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Gets or sets the name of the edition service level objective |
| 73 | + /// </summary> |
| 74 | + [Parameter(Mandatory = false, |
| 75 | + ValueFromPipelineByPropertyName = true, |
| 76 | + HelpMessage = "The name of the edition Service Objective for which to get the capabilities", |
| 77 | + ParameterSetName = _filtered)] |
| 78 | + [ValidateNotNullOrEmpty] |
| 79 | + public string ServiceObjectiveName { get; set; } |
| 80 | + |
| 81 | + [Parameter(Mandatory = false, |
| 82 | + HelpMessage = "Indicates that the results should be filtered such that only defaults are shown", |
| 83 | + ParameterSetName = _default)] |
| 84 | + public SwitchParameter Defaults { get; set; } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Executes the cmdlet |
| 88 | + /// </summary> |
| 89 | + public override void ExecuteCmdlet() |
| 90 | + { |
| 91 | + AzureSqlLocationCapabilitiesAdapter adapter = new AzureSqlLocationCapabilitiesAdapter(Profile, Profile.Context.Subscription); |
| 92 | + LocationCapabilityModel model = adapter.GetLocationCapabilities(LocationName); |
| 93 | + |
| 94 | + switch(ParameterSetName) |
| 95 | + { |
| 96 | + case _default: |
| 97 | + { |
| 98 | + FilterByDefaults(model); |
| 99 | + } |
| 100 | + break; |
| 101 | + case _filtered: |
| 102 | + { |
| 103 | + if (this.MyInvocation.BoundParameters.ContainsKey("ServerVersionName")) |
| 104 | + { |
| 105 | + FilterByServerVersion(model); |
| 106 | + } |
| 107 | + if (this.MyInvocation.BoundParameters.ContainsKey("EditionName")) |
| 108 | + { |
| 109 | + FilterByEditionName(model); |
| 110 | + } |
| 111 | + if (this.MyInvocation.BoundParameters.ContainsKey("ServiceObjectiveName")) |
| 112 | + { |
| 113 | + FilterByServiceObjectiveName(model); |
| 114 | + } |
| 115 | + } |
| 116 | + break; |
| 117 | + } |
| 118 | + |
| 119 | + this.WriteObject(model); |
| 120 | + } |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Filter the results based on what is marked as status |
| 124 | + /// </summary> |
| 125 | + /// <param name="model">The model to filter</param> |
| 126 | + private void FilterByDefaults(LocationCapabilityModel model) |
| 127 | + { |
| 128 | + model.SupportedServerVersions = model.SupportedServerVersions.Where(v => { return v.Status == "Default"; }).ToList(); |
| 129 | + |
| 130 | + // Get all defaults |
| 131 | + var defaultVersion = model.SupportedServerVersions; |
| 132 | + defaultVersion[0].SupportedEditions = defaultVersion[0].SupportedEditions.Where(v => { return v.Status == "Default"; }).ToList(); |
| 133 | + var defaultEdition = defaultVersion[0].SupportedEditions; |
| 134 | + defaultEdition[0].SupportedServiceLevelObjectives = defaultEdition[0].SupportedServiceLevelObjectives.Where(v => { return v.Status == "Default"; }).ToList(); |
| 135 | + var defaultServiceObjective = defaultEdition[0].SupportedServiceLevelObjectives; |
| 136 | + |
| 137 | + // Assign defaults back to model. |
| 138 | + defaultServiceObjective[0].SupportedMaxSizes = defaultServiceObjective[0].SupportedMaxSizes.Where(v => { return v.Status == "Default"; }).ToList(); |
| 139 | + defaultEdition[0].SupportedServiceLevelObjectives = defaultServiceObjective; |
| 140 | + defaultVersion[0].SupportedEditions = defaultEdition; |
| 141 | + model.SupportedServerVersions = defaultVersion; |
| 142 | + } |
| 143 | + |
| 144 | + /// <summary> |
| 145 | + /// Filter the results based on the Service Objective Name |
| 146 | + /// </summary> |
| 147 | + /// <param name="model">The model to filter</param> |
| 148 | + private void FilterByServiceObjectiveName(LocationCapabilityModel model) |
| 149 | + { |
| 150 | + foreach (var version in model.SupportedServerVersions) |
| 151 | + { |
| 152 | + foreach (var edition in version.SupportedEditions) |
| 153 | + { |
| 154 | + // Remove all service objectives with a name that does not match the desired value |
| 155 | + edition.SupportedServiceLevelObjectives = |
| 156 | + edition.SupportedServiceLevelObjectives |
| 157 | + .Where(slo => { return slo.ServiceObjectiveName == this.ServiceObjectiveName; }) |
| 158 | + .ToList(); |
| 159 | + } |
| 160 | + |
| 161 | + // Remove editions that have no supported service objectives after filtering |
| 162 | + version.SupportedEditions = version.SupportedEditions.Where(e => e.SupportedServiceLevelObjectives.Count > 0).ToList(); |
| 163 | + } |
| 164 | + |
| 165 | + // Remove server versions that have no supported editions after filtering |
| 166 | + model.SupportedServerVersions = model.SupportedServerVersions.Where(v => v.SupportedEditions.Count > 0).ToList(); |
| 167 | + } |
| 168 | + |
| 169 | + /// <summary> |
| 170 | + /// Filter the results based on the Edition Name |
| 171 | + /// </summary> |
| 172 | + /// <param name="model">The model to filter</param> |
| 173 | + private void FilterByEditionName(LocationCapabilityModel model) |
| 174 | + { |
| 175 | + foreach(var version in model.SupportedServerVersions) |
| 176 | + { |
| 177 | + // Remove all editions that do not match the desired edition name |
| 178 | + version.SupportedEditions = |
| 179 | + version.SupportedEditions |
| 180 | + .Where(e => { return e.EditionName == this.EditionName; }) |
| 181 | + .ToList(); |
| 182 | + } |
| 183 | + |
| 184 | + // Remove server versions that have no supported editions after filtering |
| 185 | + model.SupportedServerVersions = model.SupportedServerVersions.Where(v => v.SupportedEditions.Count > 0).ToList(); |
| 186 | + } |
| 187 | + |
| 188 | + /// <summary> |
| 189 | + /// Filter the results based on the server version name |
| 190 | + /// </summary> |
| 191 | + /// <param name="model">The model to filter</param> |
| 192 | + private void FilterByServerVersion(LocationCapabilityModel model) |
| 193 | + { |
| 194 | + // Remove all server versions that don't match the desired name |
| 195 | + model.SupportedServerVersions = |
| 196 | + model.SupportedServerVersions |
| 197 | + .Where(obj => { return obj.ServerVersionName == this.ServerVersionName; }) |
| 198 | + .ToList(); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments