protected async Task UpdateHandlerAsync(IAmazonLambda lambdaClient, string handler, Dictionary <string, string> environmentVariables = null)
        {
            var updateFunctionConfigurationRequest = new UpdateFunctionConfigurationRequest
            {
                FunctionName = FunctionName,
                Handler      = handler,
                Environment  = new Model.Environment
                {
                    IsVariablesSet = true,
                    Variables      = environmentVariables ?? new Dictionary <string, string>()
                }
            };
            await lambdaClient.UpdateFunctionConfigurationAsync(updateFunctionConfigurationRequest);

            // Wait for eventual consistency of function change.
            var getConfigurationRequest = new GetFunctionConfigurationRequest {
                FunctionName = FunctionName
            };
            GetFunctionConfigurationResponse getConfigurationResponse = null;

            do
            {
                await Task.Delay(1000);

                getConfigurationResponse = await lambdaClient.GetFunctionConfigurationAsync(getConfigurationRequest);
            } while (getConfigurationResponse.State == State.Pending);
            await Task.Delay(1000);
        }
        public static async Task WaitTillFunctionAvailableAsync(IToolLogger logger, IAmazonLambda lambdaClient, string functionName)
        {
            const int POLL_INTERVAL       = 3000;
            const int MAX_TIMEOUT_MINUTES = 20;

            try
            {
                var request = new GetFunctionConfigurationRequest
                {
                    FunctionName = functionName
                };

                GetFunctionConfigurationResponse response = null;

                bool logInitialMessage = false;
                var  timeout           = DateTime.UtcNow.AddMinutes(MAX_TIMEOUT_MINUTES);
                var  startTime         = DateTime.UtcNow;
                do
                {
                    response = await lambdaClient.GetFunctionConfigurationAsync(request);

                    if (response.LastUpdateStatus != LastUpdateStatus.InProgress && response.State != State.Pending)
                    {
                        if (response.LastUpdateStatus == LastUpdateStatus.Failed)
                        {
                            // Not throwing exception because it is possible the calling code could be fixing the failed state.
                            logger.WriteLine($"Warning: function {functionName} is currently in failed state: {response.LastUpdateStatusReason}");
                        }

                        return;
                    }

                    if (!logInitialMessage)
                    {
                        logger.WriteLine($"An update is currently in progress for Lambda function {functionName}. Waiting till update completes.");
                        logInitialMessage = true;
                    }
                    else
                    {
                        var ts = DateTime.UtcNow - startTime;
                        logger.WriteLine($"... Waiting ({ts.TotalSeconds.ToString("N2")} seconds)");
                    }
                    await Task.Delay(POLL_INTERVAL);
                } while (DateTime.UtcNow < timeout);
            }
            catch (Exception e)
            {
                throw new LambdaToolsException($"Error waiting for Lambda function to be in available status: {e.Message}", LambdaToolsException.LambdaErrorCode.LambdaWaitTillFunctionAvailable);
            }

            throw new LambdaToolsException($"Timeout waiting for function {functionName} to become available", LambdaToolsException.LambdaErrorCode.LambdaWaitTillFunctionAvailable);
        }
Exemplo n.º 3
0
        protected async Task UpdateConfigAsync(GetFunctionConfigurationResponse existingConfiguration, string dotnetSharedStoreValue)
        {
            var request = CreateConfigurationRequestIfDifferent(existingConfiguration, dotnetSharedStoreValue);

            if (request != null)
            {
                this.Logger.WriteLine($"Updating runtime configuration for function {this.GetStringValueOrDefault(this.FunctionName, LambdaDefinedCommandOptions.ARGUMENT_FUNCTION_NAME, true)}");
                try
                {
                    await this.LambdaClient.UpdateFunctionConfigurationAsync(request);
                }
                catch (Exception e)
                {
                    throw new LambdaToolsException($"Error updating configuration for Lambda function: {e.Message}", LambdaToolsException.LambdaErrorCode.LambdaUpdateFunctionConfiguration, e);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            GetFunctionConfigurationResponse response = new GetFunctionConfigurationResponse();

            context.Read();
            int targetDepth = context.CurrentDepth;

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.TestExpression("CodeSha256", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.CodeSha256 = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("CodeSize", targetDepth))
                {
                    var unmarshaller = LongUnmarshaller.Instance;
                    response.CodeSize = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("DeadLetterConfig", targetDepth))
                {
                    var unmarshaller = DeadLetterConfigUnmarshaller.Instance;
                    response.DeadLetterConfig = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("Description", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.Description = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("Environment", targetDepth))
                {
                    var unmarshaller = EnvironmentResponseUnmarshaller.Instance;
                    response.Environment = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("FileSystemConfigs", targetDepth))
                {
                    var unmarshaller = new ListUnmarshaller <FileSystemConfig, FileSystemConfigUnmarshaller>(FileSystemConfigUnmarshaller.Instance);
                    response.FileSystemConfigs = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("FunctionArn", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.FunctionArn = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("FunctionName", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.FunctionName = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("Handler", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.Handler = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("KMSKeyArn", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.KMSKeyArn = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("LastModified", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.LastModified = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("LastUpdateStatus", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.LastUpdateStatus = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("LastUpdateStatusReason", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.LastUpdateStatusReason = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("LastUpdateStatusReasonCode", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.LastUpdateStatusReasonCode = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("Layers", targetDepth))
                {
                    var unmarshaller = new ListUnmarshaller <Layer, LayerUnmarshaller>(LayerUnmarshaller.Instance);
                    response.Layers = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("MasterArn", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.MasterArn = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("MemorySize", targetDepth))
                {
                    var unmarshaller = IntUnmarshaller.Instance;
                    response.MemorySize = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("RevisionId", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.RevisionId = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("Role", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.Role = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("Runtime", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.Runtime = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("State", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.State = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("StateReason", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.StateReason = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("StateReasonCode", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.StateReasonCode = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("Timeout", targetDepth))
                {
                    var unmarshaller = IntUnmarshaller.Instance;
                    response.Timeout = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("TracingConfig", targetDepth))
                {
                    var unmarshaller = TracingConfigResponseUnmarshaller.Instance;
                    response.TracingConfig = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("Version", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.Version = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("VpcConfig", targetDepth))
                {
                    var unmarshaller = VpcConfigDetailUnmarshaller.Instance;
                    response.VpcConfig = unmarshaller.Unmarshall(context);
                    continue;
                }
            }

            return(response);
        }
        /// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            GetFunctionConfigurationResponse response = new GetFunctionConfigurationResponse();

            context.Read();
            int targetDepth = context.CurrentDepth;

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.TestExpression("CodeSize", targetDepth))
                {
                    var unmarshaller = LongUnmarshaller.Instance;
                    response.CodeSize = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("Description", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.Description = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("FunctionArn", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.FunctionArn = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("FunctionName", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.FunctionName = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("Handler", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.Handler = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("LastModified", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.LastModified = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("MemorySize", targetDepth))
                {
                    var unmarshaller = IntUnmarshaller.Instance;
                    response.MemorySize = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("Role", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.Role = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("Runtime", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.Runtime = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("Timeout", targetDepth))
                {
                    var unmarshaller = IntUnmarshaller.Instance;
                    response.Timeout = unmarshaller.Unmarshall(context);
                    continue;
                }
            }

            return(response);
        }
Exemplo n.º 6
0
        public override async Task <bool> ExecuteAsync()
        {
            try
            {
                GetFunctionConfigurationResponse response = null;

                try
                {
                    response = await this.LambdaClient.GetFunctionConfigurationAsync(this.GetStringValueOrDefault(this.FunctionName, DefinedCommandOptions.ARGUMENT_FUNCTION_NAME, true));
                }
                catch (Exception e)
                {
                    throw new LambdaToolsException("Error getting configuration for Lambda function: " + e.Message, LambdaToolsException.ErrorCode.LambdaGetConfiguration, e);
                }

                const int PAD_SIZE = 20;
                this.Logger.WriteLine("Name:".PadRight(PAD_SIZE) + response.FunctionName);
                this.Logger.WriteLine("Arn:".PadRight(PAD_SIZE) + response.FunctionArn);
                if (!string.IsNullOrEmpty(response.Description))
                {
                    this.Logger.WriteLine("Description:".PadRight(PAD_SIZE) + response.Description);
                }
                this.Logger.WriteLine("Handler:".PadRight(PAD_SIZE) + response.Handler);
                this.Logger.WriteLine("Last Modified:".PadRight(PAD_SIZE) + response.LastModified);
                this.Logger.WriteLine("Memory Size:".PadRight(PAD_SIZE) + response.MemorySize);
                this.Logger.WriteLine("Role:".PadRight(PAD_SIZE) + response.Role);
                this.Logger.WriteLine("Timeout:".PadRight(PAD_SIZE) + response.Timeout);
                this.Logger.WriteLine("Version:".PadRight(PAD_SIZE) + response.Version);

                if (!string.IsNullOrEmpty(response.KMSKeyArn))
                {
                    this.Logger.WriteLine("KMS Key ARN:".PadRight(PAD_SIZE) + response.KMSKeyArn);
                }
                else
                {
                    this.Logger.WriteLine("KMS Key ARN:".PadRight(PAD_SIZE) + "(default) aws/lambda");
                }

                if (!string.IsNullOrEmpty(response.DeadLetterConfig?.TargetArn))
                {
                    this.Logger.WriteLine("Dead Letter Target:".PadRight(PAD_SIZE) + response.DeadLetterConfig.TargetArn);
                }


                if (response.Environment?.Variables?.Count > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (var kvp in response.Environment.Variables)
                    {
                        if (sb.Length > 0)
                        {
                            sb.Append(";");
                        }
                        sb.Append($"{kvp.Key}={kvp.Value}");
                    }
                    this.Logger.WriteLine("Environment Vars:".PadRight(PAD_SIZE) + sb.ToString());
                }


                if (response.VpcConfig != null && !string.IsNullOrEmpty(response.VpcConfig.VpcId))
                {
                    this.Logger.WriteLine("VPC Config");
                    this.Logger.WriteLine("   VPC: ".PadRight(22) + response.VpcConfig.VpcId);
                    this.Logger.WriteLine("   Security Groups: ".PadRight(22) + string.Join(",", response.VpcConfig?.SecurityGroupIds));
                    this.Logger.WriteLine("   Subnets: ".PadRight(22) + string.Join(",", response.VpcConfig?.SubnetIds));
                }
            }
            catch (LambdaToolsException e)
            {
                this.Logger.WriteLine(e.Message);
                this.LastToolsException = e;
                return(false);
            }
            catch (Exception e)
            {
                this.Logger.WriteLine($"Unknown error getting configuration for Lambda function: {e.Message}");
                this.Logger.WriteLine(e.StackTrace);
                return(false);
            }

            return(true);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Create an UpdateFunctionConfigurationRequest if any fields have changed. Otherwise it returns back null causing the Update
        /// to skip.
        /// </summary>
        /// <param name="existingConfiguration"></param>
        /// <returns></returns>
        private UpdateFunctionConfigurationRequest CreateConfigurationRequestIfDifferent(GetFunctionConfigurationResponse existingConfiguration, string dotnetSharedStoreValue)
        {
            bool different = false;
            var  request   = new UpdateFunctionConfigurationRequest
            {
                FunctionName = this.GetStringValueOrDefault(this.FunctionName, LambdaDefinedCommandOptions.ARGUMENT_FUNCTION_NAME, true)
            };

            var description = this.GetStringValueOrDefault(this.Description, LambdaDefinedCommandOptions.ARGUMENT_FUNCTION_DESCRIPTION, false);

            if (!string.IsNullOrEmpty(description) && !string.Equals(description, existingConfiguration.Description, StringComparison.Ordinal))
            {
                request.Description = description;
                different           = true;
            }

            var role = this.GetStringValueOrDefault(this.Role, LambdaDefinedCommandOptions.ARGUMENT_FUNCTION_ROLE, false);

            if (!string.IsNullOrEmpty(role))
            {
                string fullRole;
                if (role.StartsWith(LambdaConstants.IAM_ARN_PREFIX))
                {
                    fullRole = role;
                }
                else
                {
                    fullRole = RoleHelper.ExpandRoleName(this.IAMClient, role);
                }

                if (!string.Equals(fullRole, existingConfiguration.Role, StringComparison.Ordinal))
                {
                    request.Role = fullRole;
                    different    = true;
                }
            }

            var handler = this.GetStringValueOrDefault(this.Handler, LambdaDefinedCommandOptions.ARGUMENT_FUNCTION_HANDLER, false);

            if (!string.IsNullOrEmpty(handler) && !string.Equals(handler, existingConfiguration.Handler, StringComparison.Ordinal))
            {
                request.Handler = handler;
                different       = true;
            }

            var memorySize = this.GetIntValueOrDefault(this.MemorySize, LambdaDefinedCommandOptions.ARGUMENT_FUNCTION_MEMORY_SIZE, false);

            if (memorySize.HasValue && memorySize.Value != existingConfiguration.MemorySize)
            {
                request.MemorySize = memorySize.Value;
                different          = true;
            }

            var runtime = this.GetStringValueOrDefault(this.Runtime, LambdaDefinedCommandOptions.ARGUMENT_FUNCTION_RUNTIME, false);

            if (runtime != null && runtime != existingConfiguration.Runtime)
            {
                request.Runtime = runtime;
                different       = true;
            }

            var timeout = this.GetIntValueOrDefault(this.Timeout, LambdaDefinedCommandOptions.ARGUMENT_FUNCTION_TIMEOUT, false);

            if (timeout.HasValue && timeout.Value != existingConfiguration.Timeout)
            {
                request.Timeout = timeout.Value;
                different       = true;
            }

            var layerVersionArns = this.GetStringValuesOrDefault(this.LayerVersionArns, LambdaDefinedCommandOptions.ARGUMENT_FUNCTION_LAYERS, false);

            if (layerVersionArns != null && AreDifferent(layerVersionArns, existingConfiguration.Layers?.Select(x => x.Arn)))
            {
                request.Layers = layerVersionArns.ToList();
                different      = true;
            }

            var subnetIds = this.GetStringValuesOrDefault(this.SubnetIds, LambdaDefinedCommandOptions.ARGUMENT_FUNCTION_SUBNETS, false);

            if (subnetIds != null)
            {
                if (request.VpcConfig == null)
                {
                    request.VpcConfig = new VpcConfig
                    {
                        SubnetIds = subnetIds.ToList()
                    };
                    different = true;
                }
                if (AreDifferent(subnetIds, request.VpcConfig.SubnetIds))
                {
                    request.VpcConfig.SubnetIds = subnetIds.ToList();
                    different = true;
                }
            }

            var securityGroupIds = this.GetStringValuesOrDefault(this.SecurityGroupIds, LambdaDefinedCommandOptions.ARGUMENT_FUNCTION_SECURITY_GROUPS, false);

            if (securityGroupIds != null)
            {
                if (request.VpcConfig == null)
                {
                    request.VpcConfig = new VpcConfig
                    {
                        SecurityGroupIds = securityGroupIds.ToList()
                    };
                    different = true;
                }
                if (AreDifferent(securityGroupIds, request.VpcConfig.SecurityGroupIds))
                {
                    request.VpcConfig.SecurityGroupIds = securityGroupIds.ToList();
                    different = true;
                }
            }

            var deadLetterTargetArn = this.GetStringValueOrDefault(this.DeadLetterTargetArn, LambdaDefinedCommandOptions.ARGUMENT_DEADLETTER_TARGET_ARN, false);

            if (deadLetterTargetArn != null)
            {
                if (!string.IsNullOrEmpty(deadLetterTargetArn) && !string.Equals(deadLetterTargetArn, existingConfiguration.DeadLetterConfig?.TargetArn, StringComparison.Ordinal))
                {
                    request.DeadLetterConfig           = existingConfiguration.DeadLetterConfig ?? new DeadLetterConfig();
                    request.DeadLetterConfig.TargetArn = deadLetterTargetArn;
                    different = true;
                }
                else if (string.IsNullOrEmpty(deadLetterTargetArn) && !string.IsNullOrEmpty(existingConfiguration.DeadLetterConfig?.TargetArn))
                {
                    request.DeadLetterConfig           = null;
                    request.DeadLetterConfig           = existingConfiguration.DeadLetterConfig ?? new DeadLetterConfig();
                    request.DeadLetterConfig.TargetArn = string.Empty;
                    different = true;
                }
            }

            var tracingMode = this.GetStringValueOrDefault(this.TracingMode, LambdaDefinedCommandOptions.ARGUMENT_TRACING_MODE, false);

            if (tracingMode != null)
            {
                var eTraceMode = !string.Equals(tracingMode, string.Empty) ? Amazon.Lambda.TracingMode.FindValue(tracingMode) : null;
                if (eTraceMode != existingConfiguration.TracingConfig?.Mode)
                {
                    request.TracingConfig      = new TracingConfig();
                    request.TracingConfig.Mode = eTraceMode;
                    different = true;
                }
            }

            var kmsKeyArn = this.GetStringValueOrDefault(this.KMSKeyArn, LambdaDefinedCommandOptions.ARGUMENT_KMS_KEY_ARN, false);

            if (!string.IsNullOrEmpty(kmsKeyArn) && !string.Equals(kmsKeyArn, existingConfiguration.KMSKeyArn, StringComparison.Ordinal))
            {
                request.KMSKeyArn = kmsKeyArn;
                different         = true;
            }

            var environmentVariables = GetEnvironmentVariables(existingConfiguration?.Environment?.Variables);

            // If runtime package store layers were set, then set the environment variable to tell the .NET Core runtime
            // to look for assemblies in the folder where the layer will be expanded.
            if (!string.IsNullOrEmpty(dotnetSharedStoreValue))
            {
                if (environmentVariables == null)
                {
                    environmentVariables = new Dictionary <string, string>();
                }
                environmentVariables[LambdaConstants.ENV_DOTNET_SHARED_STORE] = dotnetSharedStoreValue;
            }

            if (environmentVariables != null && AreDifferent(environmentVariables, existingConfiguration?.Environment?.Variables))
            {
                request.Environment = new Model.Environment {
                    Variables = environmentVariables
                };
                different = true;
            }



            if (!different)
            {
                return(null);
            }

            return(request);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Create an UpdateFunctionConfigurationRequest if any fields have changed. Otherwise it returns back null causing the Update
        /// to skip.
        /// </summary>
        /// <param name="existingConfiguration"></param>
        /// <returns></returns>
        private UpdateFunctionConfigurationRequest CreateConfigurationRequestIfDifferent(GetFunctionConfigurationResponse existingConfiguration)
        {
            bool different = false;
            var  request   = new UpdateFunctionConfigurationRequest
            {
                FunctionName = this.GetStringValueOrDefault(this.FunctionName, DefinedCommandOptions.ARGUMENT_FUNCTION_NAME, true)
            };

            if (!string.IsNullOrEmpty(this.Description) && !string.Equals(this.Description, existingConfiguration.Description, StringComparison.Ordinal))
            {
                request.Description = Description;
                different           = true;
            }

            if (!string.IsNullOrEmpty(this.Role))
            {
                string fullRole;
                if (this.Role.StartsWith(Constants.IAM_ARN_PREFIX))
                {
                    fullRole = this.Role;
                }
                else
                {
                    fullRole = RoleHelper.ExpandRoleName(this.IAMClient, this.Role);
                }

                if (!string.Equals(fullRole, existingConfiguration.Role, StringComparison.Ordinal))
                {
                    request.Role = fullRole;
                    different    = true;
                }
            }

            if (!string.IsNullOrEmpty(this.Handler) && !string.Equals(this.Handler, existingConfiguration.Handler, StringComparison.Ordinal))
            {
                request.Handler = Handler;
                different       = true;
            }

            if (MemorySize.HasValue && MemorySize.Value != existingConfiguration.MemorySize)
            {
                request.MemorySize = MemorySize.Value;
                different          = true;
            }

            if (Runtime != null && Runtime != existingConfiguration.Runtime)
            {
                request.Runtime = Runtime;
                different       = true;
            }

            if (Timeout.HasValue && Timeout.Value != existingConfiguration.Timeout)
            {
                request.Timeout = Timeout.Value;
                different       = true;
            }

            if (this.SubnetIds != null)
            {
                if (request.VpcConfig == null)
                {
                    request.VpcConfig = new VpcConfig
                    {
                        SubnetIds = this.SubnetIds.ToList()
                    };
                    different = true;
                }
                if (AreDifferent(this.SubnetIds, request.VpcConfig.SubnetIds))
                {
                    request.VpcConfig.SubnetIds = this.SubnetIds.ToList();
                    different = true;
                }
            }
            if (this.SecurityGroupIds != null)
            {
                if (request.VpcConfig == null)
                {
                    request.VpcConfig = new VpcConfig
                    {
                        SecurityGroupIds = this.SecurityGroupIds.ToList()
                    };
                    different = true;
                }
                if (AreDifferent(this.SecurityGroupIds, request.VpcConfig.SecurityGroupIds))
                {
                    request.VpcConfig.SecurityGroupIds = this.SecurityGroupIds.ToList();
                    different = true;
                }
            }

            if (!string.IsNullOrEmpty(this.DeadLetterTargetArn) && !string.Equals(this.DeadLetterTargetArn, existingConfiguration.DeadLetterConfig?.TargetArn, StringComparison.Ordinal))
            {
                request.DeadLetterConfig           = existingConfiguration.DeadLetterConfig ?? new DeadLetterConfig();
                request.DeadLetterConfig.TargetArn = this.DeadLetterTargetArn;
                different = true;
            }
            else if (string.IsNullOrEmpty(this.DeadLetterTargetArn) && !string.IsNullOrEmpty(existingConfiguration.DeadLetterConfig?.TargetArn))
            {
                request.DeadLetterConfig           = null;
                request.DeadLetterConfig           = existingConfiguration.DeadLetterConfig ?? new DeadLetterConfig();
                request.DeadLetterConfig.TargetArn = string.Empty;
                different = true;
            }


            if (!string.IsNullOrEmpty(this.KMSKeyArn) && !string.Equals(this.KMSKeyArn, existingConfiguration.KMSKeyArn, StringComparison.Ordinal))
            {
                request.KMSKeyArn = this.KMSKeyArn;
                different         = true;
            }
            if (this.EnvironmentVariables != null && AreDifferent(this.EnvironmentVariables, existingConfiguration?.Environment?.Variables))
            {
                request.Environment = new Model.Environment {
                    Variables = this.EnvironmentVariables
                };
                different = true;
            }



            if (!different)
            {
                return(null);
            }

            return(request);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Create an UpdateFunctionConfigurationRequest if any fields have changed. Otherwise it returns back null causing the Update
        /// to skip.
        /// </summary>
        /// <param name="existingConfiguration"></param>
        /// <returns></returns>
        private UpdateFunctionConfigurationRequest CreateConfigurationRequestIfDifferent(GetFunctionConfigurationResponse existingConfiguration)
        {
            bool applyDefaultsFile = this.GetBoolValueOrDefault(this.ApplyDefaultsForUpdate, DefinedCommandOptions.ARGUMENT_APPLY_DEFAULTS_FOR_UPDATE, false).GetValueOrDefault();

            if (applyDefaultsFile)
            {
                this.Logger.WriteLine("Apply defaults values from defaults file while updating function configuration");
            }

            bool different = false;
            var  request   = new UpdateFunctionConfigurationRequest
            {
                FunctionName = this.GetStringValueOrDefault(this.FunctionName, DefinedCommandOptions.ARGUMENT_FUNCTION_NAME, true)
            };

            var description = applyDefaultsFile ? this.GetStringValueOrDefault(this.Description, DefinedCommandOptions.ARGUMENT_FUNCTION_DESCRIPTION, false) : Description;

            if (!string.IsNullOrEmpty(description) && !string.Equals(description, existingConfiguration.Description, StringComparison.Ordinal))
            {
                request.Description = description;
                different           = true;
            }

            var role = applyDefaultsFile ? this.GetStringValueOrDefault(this.Role, DefinedCommandOptions.ARGUMENT_FUNCTION_ROLE, false) : this.Role;

            if (!string.IsNullOrEmpty(role))
            {
                string fullRole;
                if (role.StartsWith(Constants.IAM_ARN_PREFIX))
                {
                    fullRole = role;
                }
                else
                {
                    fullRole = RoleHelper.ExpandRoleName(this.IAMClient, role);
                }

                if (!string.Equals(fullRole, existingConfiguration.Role, StringComparison.Ordinal))
                {
                    request.Role = fullRole;
                    different    = true;
                }
            }

            var handler = applyDefaultsFile ? this.GetStringValueOrDefault(this.Handler, DefinedCommandOptions.ARGUMENT_FUNCTION_HANDLER, false) : this.Handler;

            if (!string.IsNullOrEmpty(handler) && !string.Equals(handler, existingConfiguration.Handler, StringComparison.Ordinal))
            {
                request.Handler = handler;
                different       = true;
            }

            var memorySize = applyDefaultsFile ? this.GetIntValueOrDefault(this.MemorySize, DefinedCommandOptions.ARGUMENT_FUNCTION_MEMORY_SIZE, false) : this.MemorySize;

            if (memorySize.HasValue && memorySize.Value != existingConfiguration.MemorySize)
            {
                request.MemorySize = memorySize.Value;
                different          = true;
            }

            var runtime = applyDefaultsFile ? this.GetStringValueOrDefault(this.Runtime, DefinedCommandOptions.ARGUMENT_FUNCTION_RUNTIME, false) : this.Runtime?.Value;

            if (runtime != null && runtime != existingConfiguration.Runtime)
            {
                request.Runtime = runtime;
                different       = true;
            }

            var timeout = applyDefaultsFile ? this.GetIntValueOrDefault(this.Timeout, DefinedCommandOptions.ARGUMENT_FUNCTION_TIMEOUT, false) : this.Timeout;

            if (timeout.HasValue && timeout.Value != existingConfiguration.Timeout)
            {
                request.Timeout = timeout.Value;
                different       = true;
            }

            var subnetIds = applyDefaultsFile ? this.GetStringValuesOrDefault(this.SubnetIds, DefinedCommandOptions.ARGUMENT_FUNCTION_SUBNETS, false) : this.SubnetIds;

            if (subnetIds != null)
            {
                if (request.VpcConfig == null)
                {
                    request.VpcConfig = new VpcConfig
                    {
                        SubnetIds = subnetIds.ToList()
                    };
                    different = true;
                }
                if (AreDifferent(subnetIds, request.VpcConfig.SubnetIds))
                {
                    request.VpcConfig.SubnetIds = subnetIds.ToList();
                    different = true;
                }
            }

            var securityGroupIds = applyDefaultsFile ? this.GetStringValuesOrDefault(this.SecurityGroupIds, DefinedCommandOptions.ARGUMENT_FUNCTION_SECURITY_GROUPS, false) : this.SecurityGroupIds;

            if (securityGroupIds != null)
            {
                if (request.VpcConfig == null)
                {
                    request.VpcConfig = new VpcConfig
                    {
                        SecurityGroupIds = securityGroupIds.ToList()
                    };
                    different = true;
                }
                if (AreDifferent(securityGroupIds, request.VpcConfig.SecurityGroupIds))
                {
                    request.VpcConfig.SecurityGroupIds = securityGroupIds.ToList();
                    different = true;
                }
            }

            var deadLetterTargetArn = applyDefaultsFile ? this.GetStringValueOrDefault(this.DeadLetterTargetArn, DefinedCommandOptions.ARGUMENT_DEADLETTER_TARGET_ARN, false) : this.DeadLetterTargetArn;

            if (deadLetterTargetArn != null)
            {
                if (!string.IsNullOrEmpty(deadLetterTargetArn) && !string.Equals(deadLetterTargetArn, existingConfiguration.DeadLetterConfig?.TargetArn, StringComparison.Ordinal))
                {
                    request.DeadLetterConfig           = existingConfiguration.DeadLetterConfig ?? new DeadLetterConfig();
                    request.DeadLetterConfig.TargetArn = deadLetterTargetArn;
                    different = true;
                }
                else if (string.IsNullOrEmpty(deadLetterTargetArn) && !string.IsNullOrEmpty(existingConfiguration.DeadLetterConfig?.TargetArn))
                {
                    request.DeadLetterConfig           = null;
                    request.DeadLetterConfig           = existingConfiguration.DeadLetterConfig ?? new DeadLetterConfig();
                    request.DeadLetterConfig.TargetArn = string.Empty;
                    different = true;
                }
            }

            var tracingMode = applyDefaultsFile ? this.GetStringValueOrDefault(this.TracingMode, DefinedCommandOptions.ARGUMENT_TRACING_MODE, false) : this.TracingMode;

            if (tracingMode != null)
            {
                var eTraceMode = Amazon.Lambda.TracingMode.FindValue(tracingMode);
                if (eTraceMode != existingConfiguration.TracingConfig?.Mode)
                {
                    request.TracingConfig      = new TracingConfig();
                    request.TracingConfig.Mode = eTraceMode;
                    different = true;
                }
            }

            var kmsKeyArn = applyDefaultsFile ? this.GetStringValueOrDefault(this.KMSKeyArn, DefinedCommandOptions.ARGUMENT_KMS_KEY_ARN, false) : this.KMSKeyArn;

            if (!string.IsNullOrEmpty(kmsKeyArn) && !string.Equals(kmsKeyArn, existingConfiguration.KMSKeyArn, StringComparison.Ordinal))
            {
                request.KMSKeyArn = kmsKeyArn;
                different         = true;
            }

            var environmentVariables = applyDefaultsFile ? this.GetKeyValuePairOrDefault(this.EnvironmentVariables, DefinedCommandOptions.ARGUMENT_ENVIRONMENT_VARIABLES, false) : this.EnvironmentVariables;

            if (environmentVariables != null && AreDifferent(environmentVariables, existingConfiguration?.Environment?.Variables))
            {
                request.Environment = new Model.Environment {
                    Variables = environmentVariables
                };
                different = true;
            }



            if (!different)
            {
                return(null);
            }

            return(request);
        }