Example #1
0
 private void ValidateRequest(Ec2Request parms)
 {
     if (!IsNullRequest(parms))
     {
         if (!AwsServices.IsValidRegion(parms.Region))
         {
             throw new Exception("AWS region is not valid.");
         }
         if (!IsValidCloudEnvironment(parms.CloudEnvironment))
         {
             throw new Exception("Cloud environment can not be found.");
         }
         if (!IsValidAction(parms.Action))
         {
             throw new Exception("Request action is not valid.");
         }
         if (!SetReturnFormat(parms.ReturnFormat))
         {
             throw new Exception("Valid return formats are json, xml or yaml.");
         }
         if (!Utilities.IsValidXml(parms.Xslt))
         {
             throw new Exception("XSLT is not well-formed.");
         }
     }
     else
     {
         throw new Exception("No parameter is found in the request.");
     }
 }
Example #2
0
    private void ProcessInstanceMissingTagsRequest(Ec2Request parms, bool isDryRun = false)
    {
        List <Ec2Instance> resultInstances = new List <Ec2Instance>();

        try
        {
            string profile;
            _config.AwsEnvironmentProfile.TryGetValue(parms.CloudEnvironment, out profile);
            List <Instance> instances = AwsServices.DescribeEc2Instances(null, parms.Region, profile);
            foreach (Instance instance in instances)
            {
                if (HasMissingTags(instance.Tags, parms.MissingTags))
                {
                    Ec2Instance mappedInstance = Mapper.Map <Instance, Ec2Instance>(instance);
                    resultInstances.Add(mappedInstance);
                }
            }
        }
        catch (Exception ex)
        {
            _response.Summary   = (isDryRun ? "Dry run has been completed. " : "") + ex.Message;
            _encounteredFailure = true;
        }

        _response.Ec2Instances = resultInstances;
        _response.Count        = resultInstances.Count;
    }
    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);
    }
Example #4
0
    private void GetFilteredInstances(Ec2Request parms)
    {
        string profile;

        _config.AwsEnvironmentProfile.TryGetValue(parms.CloudEnvironment, out profile);
        List <Instance>    instances       = AwsServices.DescribeEc2Instances(parms.Filters, parms.Region, profile);
        List <Ec2Instance> resultInstances = Mapper.Map <List <Instance>, List <Ec2Instance> >(instances);

        _response.Ec2Instances = resultInstances;
        _response.Count        = resultInstances.Count;
    }
    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);
    }