/// <summary>
        /// Gets the value for the CommandOption either through the property value which means the
        /// user explicity set the value or through defaults for the project.
        ///
        /// If no value is found in either the property value or the defaults and the value
        /// is required the user will be prompted for the value if we are running in interactive
        /// mode.
        /// </summary>
        /// <param name="propertyValue"></param>
        /// <param name="option"></param>
        /// <param name="required"></param>
        /// <returns></returns>
        public string GetStringValueOrDefault(string propertyValue, CommandOption option, bool required)
        {
            if (!string.IsNullOrEmpty(propertyValue))
            {
                // If the user gave the short name of the role and not the ARN then look up the role and get its ARN.
                if (option == DefinedCommandOptions.ARGUMENT_FUNCTION_ROLE && !propertyValue.StartsWith(Constants.IAM_ARN_PREFIX))
                {
                    return(RoleHelper.ExpandRoleName(this.IAMClient, propertyValue));
                }
                return(propertyValue);
            }
            else if (!string.IsNullOrEmpty(DefaultConfig[option.Switch] as string))
            {
                var configDefault = DefaultConfig[option.Switch] as string;
                // If the user gave the short name of the role and not the ARN then look up the role and get its ARN.
                if (configDefault != null && option == DefinedCommandOptions.ARGUMENT_FUNCTION_ROLE && !configDefault.StartsWith(Constants.IAM_ARN_PREFIX))
                {
                    return(RoleHelper.ExpandRoleName(this.IAMClient, configDefault));
                }
                return(configDefault);
            }
            else if (required && this.EnableInteractive)
            {
                return(PromptForValue(option));
            }

            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the value for the CommandOption either through the property value which means the
        /// user explicity set the value or through defaults for the project.
        ///
        /// If no value is found in either the property value or the defaults and the value
        /// is required the user will be prompted for the value if we are running in interactive
        /// mode.
        /// </summary>
        /// <param name="propertyValue"></param>
        /// <param name="option"></param>
        /// <param name="required"></param>
        /// <returns></returns>
        public string GetStringValueOrDefault(string propertyValue, CommandOption option, bool required)
        {
            if (!string.IsNullOrEmpty(propertyValue))
            {
                // If the user gave the short name of the role and not the ARN then look up the role and get its ARN.
                if ((option == DefinedCommandOptions.ARGUMENT_FUNCTION_ROLE || option == DefinedCommandOptions.ARGUMENT_CLOUDFORMATION_ROLE) &&
                    !propertyValue.StartsWith(Constants.IAM_ARN_PREFIX))
                {
                    return(RoleHelper.ExpandRoleName(this.IAMClient, propertyValue));
                }
                return(propertyValue);
            }
            else if (!string.IsNullOrEmpty(DefaultConfig[option.Switch] as string))
            {
                var configDefault = DefaultConfig[option.Switch] as string;
                // If the user gave the short name of the role and not the ARN then look up the role and get its ARN.
                if (configDefault != null &&
                    (option == DefinedCommandOptions.ARGUMENT_FUNCTION_ROLE || option == DefinedCommandOptions.ARGUMENT_CLOUDFORMATION_ROLE) &&
                    !configDefault.StartsWith(Constants.IAM_ARN_PREFIX))
                {
                    return(RoleHelper.ExpandRoleName(this.IAMClient, configDefault));
                }
                return(configDefault);
            }
            else if (required && !this.DisableInteractive)
            {
                return(PromptForValue(option));
            }
            else if (_cachedRequestedValues.ContainsKey(option))
            {
                var cachedValue = _cachedRequestedValues[option];
                return(cachedValue);
            }

            if (required)
            {
                throw new LambdaToolsException($"Missing required parameter: {option.Switch}", LambdaToolsException.ErrorCode.MissingRequiredParameter);
            }

            return(null);
        }
Exemplo n.º 3
0
        public string GetRoleValueOrDefault(string propertyValue, CommandOption option, string assumeRolePrincipal, string awsManagedPolicyPrefix, Dictionary <string, string> knownManagedPolicyDescription, bool required)
        {
            if (!string.IsNullOrEmpty(propertyValue))
            {
                return(RoleHelper.ExpandRoleName(this.IAMClient, propertyValue));
            }
            else if (!string.IsNullOrEmpty(DefaultConfig[option.Switch] as string))
            {
                var configDefault = DefaultConfig[option.Switch] as string;
                return(RoleHelper.ExpandRoleName(this.IAMClient, configDefault));
            }
            else if (_cachedRequestedValues.ContainsKey(option))
            {
                var cachedValue = _cachedRequestedValues[option];
                return(cachedValue);
            }
            else if (required && !this.DisableInteractive)
            {
                var promptInfo = new RoleHelper.PromptRoleInfo
                {
                    AssumeRolePrincipal           = assumeRolePrincipal,
                    AWSManagedPolicyNamePrefix    = awsManagedPolicyPrefix,
                    KnownManagedPolicyDescription = knownManagedPolicyDescription
                };

                var role = RoleHelper.PromptForRole(this.IAMClient, promptInfo);
                if (!string.IsNullOrEmpty(role))
                {
                    _cachedRequestedValues[option] = role;
                }

                return(role);
            }

            return(null);
        }
Exemplo n.º 4
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.º 5
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.º 6
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);
        }