private bool ValidateRequest(ResizeDetail parms) { bool areValid = true; if (!IsNullRequest(parms)) { if (!IsValidEnvironment(parms.Environment)) { UpdateProgress("Environment can not be found."); areValid = false; } if (!AwsServices.IsValidRegion(parms.Region)) { UpdateProgress("AWS region is not valid."); areValid = false; } if (!AwsServices.IsValidInstanceType(parms.NewInstanceType)) { UpdateProgress("EC2 instance type is not valid."); areValid = false; } if (!SetReturnFormat(parms.ReturnFormat)) { UpdateProgress("Valid return formats are json, xml or yaml."); areValid = false; } } else { UpdateProgress("No parameter is found in the request."); areValid = false; } return(areValid); }
private bool IsNullRequest(ResizeDetail parms) { bool isNull = true; if (parms != null) { isNull = parms.GetType().GetProperties().All(p => p.GetValue(parms) == null); } return(isNull); }
public bool ExecuteEc2Resize(ResizeDetail request, bool isDryRun = false) { // string profile; // _config.AwsEnvironmentProfile.TryGetValue(parms.Environment, out profile); // // // Is instance stopped // Instance instance = AwsServices.GetInstance(parms.InstanceId, parms.Region, profile); // // if (instance != null) // { // if (instance.InstanceType == InstanceType.FindValue(parms.InstanceType.ToLower())) // { // _response.ExitCode = 0; // _response.Summary = "EC2 instance is already of the given type."; // } // else if (instance.State.Name != InstanceStateName.Stopped) // { // if (parms.StopRunningInstance) // { // // } // } // } // else // { // throw new Exception("Specified instance is not found."); // } // // Stop instance // // // // Change instance type bool isSuccess = false; string profile; _config.AwsEnvironmentProfile.TryGetValue(request.Environment, out profile); if (!string.IsNullOrWhiteSpace(profile)) { // Describe instance Instance instance = AwsServices.GetInstance(request.InstanceId, request.Region, profile); // Check if instance type is different if (instance != null) { if (instance.InstanceType != request.NewInstanceType.ToLower()) { AwsServices.StopInstance(request.InstanceId, request.Region, profile); string state; do { Thread.Sleep(5000); instance = AwsServices.GetInstance(request.InstanceId, request.Region, profile); state = instance.State.Name.Value; } while (state != "stopped"); AwsServices.ModifyInstance(request.InstanceId, request.NewInstanceType, request.Region, profile); if (request.StartStoppedInstance) { AwsServices.StartInstance(request.InstanceId, request.Region, profile); } } else { throw new Exception($"The instance is already of type '{request.NewInstanceType}'."); } } else { throw new Exception("Failed to obtain the EC2 instance detail."); } // If it is the same, no action is taken // If different and allow stopping running instance, stop the instance // Wait until instance is stopped // Resize the instance // If instructed to start the instance, start the instance } else { throw new Exception("Specified environment is not found."); } return(isSuccess); }