/*******************************************************************************
* Copyright 2012-2019 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.Management.Automation;
using System.Text;
using Amazon.PowerShell.Common;
using Amazon.Runtime;
using Amazon.SimpleSystemsManagement;
using Amazon.SimpleSystemsManagement.Model;
namespace Amazon.PowerShell.Cmdlets.SSM
{
///
/// Initiates execution of an Automation runbook.
///
[Cmdlet("Start", "SSMAutomationExecution", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)]
[OutputType("System.String")]
[AWSCmdlet("Calls the AWS Systems Manager StartAutomationExecution API operation.", Operation = new[] {"StartAutomationExecution"}, SelectReturnType = typeof(Amazon.SimpleSystemsManagement.Model.StartAutomationExecutionResponse))]
[AWSCmdletOutput("System.String or Amazon.SimpleSystemsManagement.Model.StartAutomationExecutionResponse",
"This cmdlet returns a System.String object.",
"The service call response (type Amazon.SimpleSystemsManagement.Model.StartAutomationExecutionResponse) can also be referenced from properties attached to the cmdlet entry in the $AWSHistory stack."
)]
public partial class StartSSMAutomationExecutionCmdlet : AmazonSimpleSystemsManagementClientCmdlet, IExecutor
{
#region Parameter AlarmConfiguration_Alarm
///
///
/// The name of the CloudWatch alarm specified in the configuration.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("AlarmConfiguration_Alarms")]
public Amazon.SimpleSystemsManagement.Model.Alarm[] AlarmConfiguration_Alarm { get; set; }
#endregion
#region Parameter DocumentName
///
///
/// The name of the SSM document to run. This can be a public document or a custom document.
/// To run a shared document belonging to another account, specify the document ARN. For
/// more information about how to use shared documents, see Using
/// shared SSM documents in the Amazon Web Services Systems Manager User Guide.
///
///
#if !MODULAR
[System.Management.Automation.Parameter(Position = 0, ValueFromPipelineByPropertyName = true, ValueFromPipeline = true)]
#else
[System.Management.Automation.Parameter(Position = 0, ValueFromPipelineByPropertyName = true, ValueFromPipeline = true, Mandatory = true)]
[System.Management.Automation.AllowEmptyString]
[System.Management.Automation.AllowNull]
#endif
[Amazon.PowerShell.Common.AWSRequiredParameter]
public System.String DocumentName { get; set; }
#endregion
#region Parameter DocumentVersion
///
///
/// The version of the Automation runbook to use for this execution.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String DocumentVersion { get; set; }
#endregion
#region Parameter AlarmConfiguration_IgnorePollAlarmFailure
///
///
/// When this value is true, your automation or command continues to run in cases
/// where we can’t retrieve alarm status information from CloudWatch. In cases where we
/// successfully retrieve an alarm status of OK or INSUFFICIENT_DATA, the automation or
/// command continues to run, regardless of this value. Default is false.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.Boolean? AlarmConfiguration_IgnorePollAlarmFailure { get; set; }
#endregion
#region Parameter MaxConcurrency
///
///
/// The maximum number of targets allowed to run this task in parallel. You can specify
/// a number, such as 10, or a percentage, such as 10%. The default value is 10
.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String MaxConcurrency { get; set; }
#endregion
#region Parameter MaxError
///
///
/// The number of errors that are allowed before the system stops running the automation
/// on additional targets. You can specify either an absolute number of errors, for example
/// 10, or a percentage of the target set, for example 10%. If you specify 3, for example,
/// the system stops running the automation when the fourth error is received. If you
/// specify 0, then the system stops running the automation on additional targets after
/// the first error result is returned. If you run an automation on 50 resources and set
/// max-errors to 10%, then the system stops running the automation on additional targets
/// when the sixth error is received.Executions that are already running an automation when max-errors is reached are allowed
/// to complete, but some of these executions may fail as well. If you need to ensure
/// that there won't be more than max-errors failed executions, set max-concurrency to
/// 1 so the executions proceed one at a time.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("MaxErrors")]
public System.String MaxError { get; set; }
#endregion
#region Parameter Mode
///
///
/// The execution mode of the automation. Valid modes include the following: Auto and
/// Interactive. The default mode is Auto.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[AWSConstantClassSource("Amazon.SimpleSystemsManagement.ExecutionMode")]
public Amazon.SimpleSystemsManagement.ExecutionMode Mode { get; set; }
#endregion
#region Parameter Parameter
///
///
/// A key-value map of execution parameters, which match the declared parameters in the
/// Automation runbook.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("Parameters")]
public System.Collections.Hashtable Parameter { get; set; }
#endregion
#region Parameter Tag
///
///
/// Optional metadata that you assign to a resource. You can specify a maximum of five
/// tags for an automation. Tags enable you to categorize a resource in different ways,
/// such as by purpose, owner, or environment. For example, you might want to tag an automation
/// to identify an environment or operating system. In this case, you could specify the
/// following key-value pairs:Key=environment,Value=test
Key=OS,Value=Windows
To add tags to an existing automation, use the AddTagsToResource operation.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("Tags")]
public Amazon.SimpleSystemsManagement.Model.Tag[] Tag { get; set; }
#endregion
#region Parameter TargetLocation
///
///
/// A location is a combination of Amazon Web Services Regions and/or Amazon Web Services
/// accounts where you want to run the automation. Use this operation to start an automation
/// in multiple Amazon Web Services Regions and multiple Amazon Web Services accounts.
/// For more information, see Running
/// Automation workflows in multiple Amazon Web Services Regions and Amazon Web Services
/// accounts in the Amazon Web Services Systems Manager User Guide.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("TargetLocations")]
public Amazon.SimpleSystemsManagement.Model.TargetLocation[] TargetLocation { get; set; }
#endregion
#region Parameter TargetMap
///
///
/// A key-value mapping of document parameters to target resources. Both Targets and TargetMaps
/// can't be specified together.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("TargetMaps")]
public System.Collections.Hashtable[] TargetMap { get; set; }
#endregion
#region Parameter TargetParameterName
///
///
/// The name of the parameter used as the target resource for the rate-controlled execution.
/// Required if you specify targets.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String TargetParameterName { get; set; }
#endregion
#region Parameter Target
///
///
/// A key-value mapping to target resources. Required if you specify TargetParameterName.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("Targets")]
public Amazon.SimpleSystemsManagement.Model.Target[] Target { get; set; }
#endregion
#region Parameter ClientToken
///
///
/// User-provided idempotency token. The token must be unique, is case insensitive, enforces
/// the UUID format, and can't be reused.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String ClientToken { get; set; }
#endregion
#region Parameter Select
///
/// Use the -Select parameter to control the cmdlet output. The default value is 'AutomationExecutionId'.
/// Specifying -Select '*' will result in the cmdlet returning the whole service response (Amazon.SimpleSystemsManagement.Model.StartAutomationExecutionResponse).
/// Specifying the name of a property of type Amazon.SimpleSystemsManagement.Model.StartAutomationExecutionResponse will result in that property being returned.
/// Specifying -Select '^ParameterName' will result in the cmdlet returning the selected cmdlet parameter value.
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public string Select { get; set; } = "AutomationExecutionId";
#endregion
#region Parameter PassThru
///
/// Changes the cmdlet behavior to return the value passed to the DocumentName parameter.
/// The -PassThru parameter is deprecated, use -Select '^DocumentName' instead. This parameter will be removed in a future version.
///
[System.Obsolete("The -PassThru parameter is deprecated, use -Select '^DocumentName' instead. This parameter will be removed in a future version.")]
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public SwitchParameter PassThru { get; set; }
#endregion
#region Parameter Force
///
/// This parameter overrides confirmation prompts to force
/// the cmdlet to continue its operation. This parameter should always
/// be used with caution.
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public SwitchParameter Force { get; set; }
#endregion
protected override void ProcessRecord()
{
this._AWSSignerType = "v4";
base.ProcessRecord();
var resourceIdentifiersText = FormatParameterValuesForConfirmationMsg(nameof(this.DocumentName), MyInvocation.BoundParameters);
if (!ConfirmShouldProceed(this.Force.IsPresent, resourceIdentifiersText, "Start-SSMAutomationExecution (StartAutomationExecution)"))
{
return;
}
var context = new CmdletContext();
// allow for manipulation of parameters prior to loading into context
PreExecutionContextLoad(context);
#pragma warning disable CS0618, CS0612 //A class member was marked with the Obsolete attribute
if (ParameterWasBound(nameof(this.Select)))
{
context.Select = CreateSelectDelegate(Select) ??
throw new System.ArgumentException("Invalid value for -Select parameter.", nameof(this.Select));
if (this.PassThru.IsPresent)
{
throw new System.ArgumentException("-PassThru cannot be used when -Select is specified.", nameof(this.Select));
}
}
else if (this.PassThru.IsPresent)
{
context.Select = (response, cmdlet) => this.DocumentName;
}
#pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute
if (this.AlarmConfiguration_Alarm != null)
{
context.AlarmConfiguration_Alarm = new List(this.AlarmConfiguration_Alarm);
}
context.AlarmConfiguration_IgnorePollAlarmFailure = this.AlarmConfiguration_IgnorePollAlarmFailure;
context.ClientToken = this.ClientToken;
context.DocumentName = this.DocumentName;
#if MODULAR
if (this.DocumentName == null && ParameterWasBound(nameof(this.DocumentName)))
{
WriteWarning("You are passing $null as a value for parameter DocumentName which is marked as required. In case you believe this parameter was incorrectly marked as required, report this by opening an issue at https://github.com/aws/aws-tools-for-powershell/issues.");
}
#endif
context.DocumentVersion = this.DocumentVersion;
context.MaxConcurrency = this.MaxConcurrency;
context.MaxError = this.MaxError;
context.Mode = this.Mode;
if (this.Parameter != null)
{
context.Parameter = new Dictionary>(StringComparer.Ordinal);
foreach (var hashKey in this.Parameter.Keys)
{
object hashValue = this.Parameter[hashKey];
if (hashValue == null)
{
context.Parameter.Add((String)hashKey, null);
continue;
}
var enumerable = SafeEnumerable(hashValue);
var valueSet = new List();
foreach (var s in enumerable)
{
valueSet.Add((System.String)s);
}
context.Parameter.Add((String)hashKey, valueSet);
}
}
if (this.Tag != null)
{
context.Tag = new List(this.Tag);
}
if (this.TargetLocation != null)
{
context.TargetLocation = new List(this.TargetLocation);
}
if (this.TargetMap != null)
{
context.TargetMap = new List>>();
foreach (var hashTable in this.TargetMap)
{
var d = new Dictionary>();
foreach (var hashKey in hashTable.Keys)
{
object hashValue = hashTable[hashKey];
if (hashValue == null)
{
d.Add((String)hashKey, null);
continue;
}
var enumerable = SafeEnumerable(hashValue);
var valueSet = new List();
foreach (var s in enumerable)
{
valueSet.Add((System.String)s);
}
d.Add((String)hashKey, valueSet);
}
context.TargetMap.Add(d);
}
}
context.TargetParameterName = this.TargetParameterName;
if (this.Target != null)
{
context.Target = new List(this.Target);
}
// allow further manipulation of loaded context prior to processing
PostExecutionContextLoad(context);
var output = Execute(context) as CmdletOutput;
ProcessOutput(output);
}
#region IExecutor Members
public object Execute(ExecutorContext context)
{
var cmdletContext = context as CmdletContext;
// create request
var request = new Amazon.SimpleSystemsManagement.Model.StartAutomationExecutionRequest();
// populate AlarmConfiguration
var requestAlarmConfigurationIsNull = true;
request.AlarmConfiguration = new Amazon.SimpleSystemsManagement.Model.AlarmConfiguration();
List requestAlarmConfiguration_alarmConfiguration_Alarm = null;
if (cmdletContext.AlarmConfiguration_Alarm != null)
{
requestAlarmConfiguration_alarmConfiguration_Alarm = cmdletContext.AlarmConfiguration_Alarm;
}
if (requestAlarmConfiguration_alarmConfiguration_Alarm != null)
{
request.AlarmConfiguration.Alarms = requestAlarmConfiguration_alarmConfiguration_Alarm;
requestAlarmConfigurationIsNull = false;
}
System.Boolean? requestAlarmConfiguration_alarmConfiguration_IgnorePollAlarmFailure = null;
if (cmdletContext.AlarmConfiguration_IgnorePollAlarmFailure != null)
{
requestAlarmConfiguration_alarmConfiguration_IgnorePollAlarmFailure = cmdletContext.AlarmConfiguration_IgnorePollAlarmFailure.Value;
}
if (requestAlarmConfiguration_alarmConfiguration_IgnorePollAlarmFailure != null)
{
request.AlarmConfiguration.IgnorePollAlarmFailure = requestAlarmConfiguration_alarmConfiguration_IgnorePollAlarmFailure.Value;
requestAlarmConfigurationIsNull = false;
}
// determine if request.AlarmConfiguration should be set to null
if (requestAlarmConfigurationIsNull)
{
request.AlarmConfiguration = null;
}
if (cmdletContext.ClientToken != null)
{
request.ClientToken = cmdletContext.ClientToken;
}
if (cmdletContext.DocumentName != null)
{
request.DocumentName = cmdletContext.DocumentName;
}
if (cmdletContext.DocumentVersion != null)
{
request.DocumentVersion = cmdletContext.DocumentVersion;
}
if (cmdletContext.MaxConcurrency != null)
{
request.MaxConcurrency = cmdletContext.MaxConcurrency;
}
if (cmdletContext.MaxError != null)
{
request.MaxErrors = cmdletContext.MaxError;
}
if (cmdletContext.Mode != null)
{
request.Mode = cmdletContext.Mode;
}
if (cmdletContext.Parameter != null)
{
request.Parameters = cmdletContext.Parameter;
}
if (cmdletContext.Tag != null)
{
request.Tags = cmdletContext.Tag;
}
if (cmdletContext.TargetLocation != null)
{
request.TargetLocations = cmdletContext.TargetLocation;
}
if (cmdletContext.TargetMap != null)
{
request.TargetMaps = cmdletContext.TargetMap;
}
if (cmdletContext.TargetParameterName != null)
{
request.TargetParameterName = cmdletContext.TargetParameterName;
}
if (cmdletContext.Target != null)
{
request.Targets = cmdletContext.Target;
}
CmdletOutput output;
// issue call
var client = Client ?? CreateClient(_CurrentCredentials, _RegionEndpoint);
try
{
var response = CallAWSServiceOperation(client, request);
object pipelineOutput = null;
pipelineOutput = cmdletContext.Select(response, this);
output = new CmdletOutput
{
PipelineOutput = pipelineOutput,
ServiceResponse = response
};
}
catch (Exception e)
{
output = new CmdletOutput { ErrorResponse = e };
}
return output;
}
public ExecutorContext CreateContext()
{
return new CmdletContext();
}
#endregion
#region AWS Service Operation Call
private Amazon.SimpleSystemsManagement.Model.StartAutomationExecutionResponse CallAWSServiceOperation(IAmazonSimpleSystemsManagement client, Amazon.SimpleSystemsManagement.Model.StartAutomationExecutionRequest request)
{
Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "AWS Systems Manager", "StartAutomationExecution");
try
{
#if DESKTOP
return client.StartAutomationExecution(request);
#elif CORECLR
return client.StartAutomationExecutionAsync(request).GetAwaiter().GetResult();
#else
#error "Unknown build edition"
#endif
}
catch (AmazonServiceException exc)
{
var webException = exc.InnerException as System.Net.WebException;
if (webException != null)
{
throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException);
}
throw;
}
}
#endregion
internal partial class CmdletContext : ExecutorContext
{
public List AlarmConfiguration_Alarm { get; set; }
public System.Boolean? AlarmConfiguration_IgnorePollAlarmFailure { get; set; }
public System.String ClientToken { get; set; }
public System.String DocumentName { get; set; }
public System.String DocumentVersion { get; set; }
public System.String MaxConcurrency { get; set; }
public System.String MaxError { get; set; }
public Amazon.SimpleSystemsManagement.ExecutionMode Mode { get; set; }
public Dictionary> Parameter { get; set; }
public List Tag { get; set; }
public List TargetLocation { get; set; }
public List>> TargetMap { get; set; }
public System.String TargetParameterName { get; set; }
public List Target { get; set; }
public System.Func Select { get; set; } =
(response, cmdlet) => response.AutomationExecutionId;
}
}
}