/* * Copyright 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. */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using Amazon.Runtime; using Amazon.EC2.Model; using System.IO; using Amazon.Runtime.Internal; using System.Text.RegularExpressions; namespace Amazon.EC2.Internal { /// /// Custom pipeline handler /// public class AmazonEC2PostMarshallHandler : PipelineHandler { /// /// Calls pre invoke logic before calling the next handler /// in the pipeline. /// /// The execution context which contains both the /// requests and response context. public override void InvokeSync(IExecutionContext executionContext) { PreInvoke(executionContext); base.InvokeSync(executionContext); } #if AWS_ASYNC_API /// /// Calls pre invoke logic before calling the next handler /// in the pipeline. /// /// The response type for the current request. /// The execution context, it contains the /// request and response context. /// A task that represents the asynchronous operation. public override System.Threading.Tasks.Task InvokeAsync(IExecutionContext executionContext) { PreInvoke(executionContext); return base.InvokeAsync(executionContext); } #elif AWS_APM_API /// /// Calls pre invoke logic before calling the next handler /// in the pipeline. /// /// The execution context which contains both the /// requests and response context. /// IAsyncResult which represent an async operation. public override IAsyncResult InvokeAsync(IAsyncExecutionContext executionContext) { PreInvoke(ExecutionContext.CreateFromAsyncContext(executionContext)); return base.InvokeAsync(executionContext); } #endif /// /// Custom pipeline handler /// /// protected virtual void PreInvoke(IExecutionContext executionContext) { var originalRequest = executionContext.RequestContext.OriginalRequest; var request = executionContext.RequestContext.Request; var copySnapshotRequest = originalRequest as CopySnapshotRequest; if (copySnapshotRequest != null) { request.AlternateEndpoint = RegionEndpoint.GetBySystemName(copySnapshotRequest.DestinationRegion); request.Endpoint = EndpointResolver.DetermineEndpoint(executionContext.RequestContext.ClientConfig, request); return; } var requestSpotInstancesRequest = originalRequest as RequestSpotInstancesRequest; if (requestSpotInstancesRequest != null) { if (requestSpotInstancesRequest.LaunchSpecification != null) { if (requestSpotInstancesRequest.LaunchSpecification.AllSecurityGroups != null) { int count = 1; foreach (var group in requestSpotInstancesRequest.LaunchSpecification.AllSecurityGroups) { if (group != null && group.GroupId != null) { var key = "LaunchSpecification.SecurityGroupId." + count++; request.Parameters[key] = group.GroupId; } } } if (requestSpotInstancesRequest.LaunchSpecification.SecurityGroups != null) { int count = 1; foreach (var group in requestSpotInstancesRequest.LaunchSpecification.SecurityGroups) { var key = "LaunchSpecification.SecurityGroup." + count++; request.Parameters[key] = group; } } } if (request.Parameters.ContainsKey("LaunchSpecification.Monitoring")) { var val = request.Parameters["LaunchSpecification.Monitoring"]; request.Parameters.Remove("LaunchSpecification.Monitoring"); request.Parameters.Add("LaunchSpecification.Monitoring.Enabled", val); } var keysToRemove = new List(); foreach (var parameter in request.Parameters.Keys) { if (parameter.StartsWith("LaunchSpecification.GroupSet.", StringComparison.Ordinal)) keysToRemove.Add(parameter); } foreach (var key in keysToRemove) { request.Parameters.Remove(key); } } } } }