/*******************************************************************************
* Copyright 2012-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file.
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
* *****************************************************************************
*
* AWS Tools for Windows (TM) PowerShell (TM)
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using Amazon.Runtime;
namespace Amazon.PowerShell.Common
{
///
///
/// Sets a default AWS region into the shell environment, accessible as $StoredAWSRegion.
///
///
///
/// Note: The regions available for tab completion to the -Region parameter were those known
/// at the time this module was built. Regions launched subsequent to the build will not be listed for
/// tab completion but can still be used by simply entering the region system name.
///
///
[Cmdlet("Set", "DefaultAWSRegion")]
[AWSCmdlet("Sets a default AWS region system name (e.g. us-west-2, eu-west-1 etc) into the shell variable $StoredAWSRegion. "
+ "AWS cmdlets will use the value of this variable to satisfy their -Region parameter if the parameter is not specified.")]
[OutputType("None")]
[AWSCmdletOutput("None", "This cmdlet does not generate any output.")]
public class SetDefaultRegionCmdlet : AWSRegionArgumentsCmdlet
{
///
///
/// This parameter allows to specify the scope of the region configuration to set.
/// For details about variables scopes see https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes.
///
///
[Parameter(ValueFromPipelineByPropertyName = true, Mandatory = false)]
public VariableScope Scope { get; set; }
protected override void ProcessRecord()
{
var region = this.GetRegion(true, SessionState);
string scope = MyInvocation.BoundParameters.ContainsKey("Scope") ? Scope.ToString() + ":" : "";
this.SessionState.PSVariable.Set(scope + SessionKeys.AWSRegionVariableName, region.SystemName);
}
}
///
/// Clears any default AWS region set in the shell variable $StoredAWSRegion.
///
[Cmdlet("Clear", "DefaultAWSRegion")]
[AWSCmdlet("Clears any default AWS region set in the shell variable $StoredAWSRegion.")]
[OutputType("None")]
[AWSCmdletOutput("None", "This cmdlet does not generate any output.")]
public class ClearDefaultRegionCmdlet : PSCmdlet
{
///
///
/// This parameter allows to specify the scope of the region configuration to clear.
/// For details about variables scopes see https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes.
///
///
[Parameter(ValueFromPipelineByPropertyName = true, Mandatory =false)]
public VariableScope Scope { get; set; }
protected override void ProcessRecord()
{
string scope = MyInvocation.BoundParameters.ContainsKey("Scope") ? Scope.ToString() + ":" : "";
this.SessionState.PSVariable.Set(scope + SessionKeys.AWSRegionVariableName, null);
}
}
///
/// Returns the current default AWS region for this shell, if any, as held in the shell variable $StoredAWSRegion.
///
[Cmdlet("Get", "DefaultAWSRegion")]
[OutputType(typeof(AWSRegion))]
[AWSCmdlet("Returns the current default AWS region for this shell, if any, as held in the shell variable $StoredAWSRegion.")]
[AWSCmdletOutput("AWSRegion", "AWSRegion instance mapping to the default AWS region stored in $StoredAWSRegion.")]
public class GetDefaultRegionCmdlet : PSCmdlet
{
protected override void ProcessRecord()
{
var region = this.SessionState.PSVariable.Get(SessionKeys.AWSRegionVariableName);
if (region != null && region.Value != null)
{
var rep = RegionEndpoint.GetBySystemName(region.Value.ToString());
WriteObject(new AWSRegion(rep, this.SessionState));
}
}
}
///
///
/// Returns the set of available AWS regions.
///
///
///
/// Note: The regions listed as output for this cmdlet are those known
/// at the time this module was built. Regions launched subsequent to the build
/// will not be listed in the output. The new regions can still be used with the
/// -Region parameter for cmdlets in this module by simply entering the region
/// system name (eg us-west-2, eu-west-3 etc).
///
///
[Cmdlet("Get", "AWSRegion", DefaultParameterSetName = DefaultParameterSet)]
[OutputType(typeof(AWSRegion))]
[AWSCmdlet("Returns the set of available AWS regions.")]
[AWSCmdletOutput("AWSRegion", "AWSRegion instance for each available region.")]
public class GetRegionCmdlet : PSCmdlet
{
private const string DefaultParameterSet = "PublicOnly";
private const string PublicAndGovCloudParameterSet = "PublicAndGovCloud";
private const string GovCloudOnlyParameterSet = "GovCloudOnly";
#region Parameter SystemName
///
///
/// If set returns an AWSRegion instance corresponding to the specified system name (e.g. us-west-2).
///
///
/// This parameter can also be used to return AWSRegion instances for the US GovCloud and China (Beijing)
/// regions by specifying the relevant system name.
///
///
[Parameter(Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = DefaultParameterSet)]
[Parameter(ParameterSetName = PublicAndGovCloudParameterSet, ValueFromPipelineByPropertyName = true)]
public string SystemName { get; set; }
#endregion
#region Parameter IncludeChina
///
///
/// Include the China (Beijing) region in the returned collection of AWSRegion instances.
/// Note that use of this region requires an alternate set of credentials.
///
///
/// This switch is ignored if the SystemName parameter is used to request a specific
/// AWSRegion instance. To return the specific China (Beijing) region, specify a
/// value of 'cn-north-1' for the SystemName parameter.
///
/// Default: off.
///
[Parameter(ParameterSetName = DefaultParameterSet, ValueFromPipelineByPropertyName = true)]
[Parameter(ParameterSetName = PublicAndGovCloudParameterSet, ValueFromPipelineByPropertyName = true)]
public SwitchParameter IncludeChina { get; set; }
#endregion
#region Parameter IncludeGovCloud
///
/// If set the returned collection includes 'Gov Cloud' region(s).
/// Default: off.
///
[Parameter(ParameterSetName = PublicAndGovCloudParameterSet, ValueFromPipelineByPropertyName = true)]
public SwitchParameter IncludeGovCloud { get; set; }
#endregion
#region Parameter GovCloudOnly
///
/// If set the returned collection contains only the 'Gov Cloud' region(s).
/// Default: off.
///
[Parameter(ParameterSetName = GovCloudOnlyParameterSet, ValueFromPipelineByPropertyName = true)]
public SwitchParameter GovCloudOnly { get; set; }
#endregion
protected override void ProcessRecord()
{
if (!string.IsNullOrEmpty(SystemName))
{
try
{
WriteObject(new AWSRegion(RegionEndpoint.GetBySystemName(SystemName), this.SessionState));
}
catch (Exception e)
{
this.ThrowTerminatingError(new ErrorRecord(new ArgumentException("Unrecognized region system name", e),
"ArgumentException",
ErrorCategory.InvalidArgument,
this.SystemName));
}
}
else
{
foreach (var region in RegionEndpoint.EnumerableAllRegions)
{
// this is the only way we have to test at the moment
var isGovCloudRegion = region.SystemName.StartsWith("us-gov-", StringComparison.OrdinalIgnoreCase);
if (isGovCloudRegion)
{
if (this.IncludeGovCloud || this.GovCloudOnly)
WriteObject(new AWSRegion(region, this.SessionState));
}
else
{
if (this.GovCloudOnly)
continue;
if (region.SystemName.StartsWith("cn-", StringComparison.OrdinalIgnoreCase) && !this.IncludeChina)
continue;
WriteObject(new AWSRegion(region, this.SessionState));
}
}
}
}
}
///
/// Information about a specific AWS region
///
public class AWSRegion
{
readonly SessionState _sessionState;
///
/// AWS system name for the region, for example 'us-west-2'.
///
public string Region { get; private set; }
///
/// The descriptive name for the region, for example 'US East (Virginia)'
///
public string Name { get; private set; }
///
/// Set to true if the region is the current default for the shell
///
public bool IsShellDefault
{
get
{
var shellRegion = this._sessionState.PSVariable.Get(SessionKeys.AWSRegionVariableName);
return (shellRegion != null
&& shellRegion.Value != null
&& string.Compare(this.Region,
shellRegion.Value.ToString(),
StringComparison.OrdinalIgnoreCase) == 0);
}
}
public override string ToString()
{
return this.Region;
}
internal AWSRegion(RegionEndpoint rep, SessionState sessionState)
{
this.Region = rep.SystemName;
this.Name = rep.DisplayName;
this._sessionState = sessionState;
}
}
}