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.");
     }
 }
    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 IsNullRequest(Ec2Request parms)
    {
        bool isNull = true;

        if (parms != null)
        {
            isNull = parms.GetType().GetProperties().All(p => p.GetValue(parms) == null);
        }
        return(isNull);
    }
    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 override ExecuteResult Execute(HandlerStartInfo startInfo)
    {
        string message;
        string xslt = "";

        try
        {
            message = "Deserializing incoming request...";
            UpdateProgress(message, StatusType.Initializing);
            Ec2Request parms = DeserializeOrNew <Ec2Request>(startInfo.Parameters);

            message = "Processing request...";
            UpdateProgress(message, StatusType.Running);
            ValidateRequest(parms);
            xslt = parms.Xslt;
            GetFilteredInstances(parms);

            message = "Querying against AWS has been processed" + (_encounteredFailure ? " with error" : "") + ".";
            UpdateProgress(message, _encounteredFailure ? StatusType.CompletedWithErrors : StatusType.Success);
            _response.Summary  = message;
            _response.ExitCode = _encounteredFailure ? -1 : 0;
        }
        catch (Exception ex)
        {
            UpdateProgress(ex.Message, StatusType.Failed);
            _encounteredFailure = true;
            _response.Summary   = ex.Message;
            _response.ExitCode  = -1;
        }
        finally
        {
            message = "Serializing response...";
            UpdateProgress(message);
            try
            {
                string xmlResponse    = Utilities.SerializeXmlResponse(_response);
                string transformedXml = Utilities.TransformXml(xmlResponse, xslt);
                string serializedData = Utilities.SerializeTargetFormat(transformedXml, _returnFormat);
                _result.ExitData = serializedData;
            }
            catch (Exception ex)
            {
                _result.ExitData = ex.Message;
            }
        }

        return(_result);
    }
    private bool IsValidFilters(Ec2Request request)
    {
        bool isValid = false;


        if (request.RequestType == "instance-uptime")
        {
            isValid = IsValidInstanceUptimeFilter(request.Uptime);
        }

        if (request.RequestType == "missing-tags")
        {
            if (request.MissingTags != null)
            {
                isValid = request.MissingTags.Count > 0;
            }
        }
        return(isValid);
    }