/** Get a list of security group ids for the slave */
  private List<String> getEc2SecurityGroups(AmazonEC2 ec2) throws AmazonClientException {
    List<String> groupIds = new ArrayList<String>();

    DescribeSecurityGroupsResult groupResult =
        getSecurityGroupsBy("group-name", securityGroupSet, ec2);
    if (groupResult.getSecurityGroups().size() == 0) {
      groupResult = getSecurityGroupsBy("group-id", securityGroupSet, ec2);
    }

    for (SecurityGroup group : groupResult.getSecurityGroups()) {
      if (group.getVpcId() != null && !group.getVpcId().isEmpty()) {
        List<Filter> filters = new ArrayList<Filter>();
        filters.add(new Filter("vpc-id").withValues(group.getVpcId()));
        filters.add(new Filter("state").withValues("available"));
        filters.add(new Filter("subnet-id").withValues(getSubnetId()));

        DescribeSubnetsRequest subnetReq = new DescribeSubnetsRequest();
        subnetReq.withFilters(filters);
        DescribeSubnetsResult subnetResult = ec2.describeSubnets(subnetReq);

        List<Subnet> subnets = subnetResult.getSubnets();
        if (subnets != null && !subnets.isEmpty()) {
          groupIds.add(group.getGroupId());
        }
      }
    }

    if (securityGroupSet.size() != groupIds.size()) {
      throw new AmazonClientException(
          "Security groups must all be VPC security groups to work in a VPC context");
    }

    return groupIds;
  }
  public Request<DescribeSubnetsRequest> marshall(DescribeSubnetsRequest describeSubnetsRequest) {

    if (describeSubnetsRequest == null) {
      throw new AmazonClientException("Invalid argument passed to marshall(...)");
    }

    Request<DescribeSubnetsRequest> request =
        new DefaultRequest<DescribeSubnetsRequest>(describeSubnetsRequest, "AmazonEC2");
    request.addParameter("Action", "DescribeSubnets");
    request.addParameter("Version", "2014-05-01");

    java.util.List<String> subnetIdsList = describeSubnetsRequest.getSubnetIds();
    int subnetIdsListIndex = 1;

    for (String subnetIdsListValue : subnetIdsList) {
      if (subnetIdsListValue != null) {
        request.addParameter(
            "SubnetId." + subnetIdsListIndex, StringUtils.fromString(subnetIdsListValue));
      }

      subnetIdsListIndex++;
    }

    java.util.List<Filter> filtersList = describeSubnetsRequest.getFilters();
    int filtersListIndex = 1;

    for (Filter filtersListValue : filtersList) {
      Filter filterMember = filtersListValue;
      if (filterMember != null) {
        if (filterMember.getName() != null) {
          request.addParameter(
              "Filter." + filtersListIndex + ".Name",
              StringUtils.fromString(filterMember.getName()));
        }

        java.util.List<String> valuesList = filterMember.getValues();
        int valuesListIndex = 1;

        for (String valuesListValue : valuesList) {
          if (valuesListValue != null) {
            request.addParameter(
                "Filter." + filtersListIndex + ".Value." + valuesListIndex,
                StringUtils.fromString(valuesListValue));
          }

          valuesListIndex++;
        }
      }

      filtersListIndex++;
    }

    return request;
  }