Example #1
0
  public static void createSecurityGroup(AmazonEC2 ec2, String securityGroup) {

    List<SecurityGroup> secGroupList = ec2.describeSecurityGroups().getSecurityGroups();
    for (SecurityGroup secGroup : secGroupList) {
      // System.out.println(secGroup.getGroupName());
      if (securityGroup.equalsIgnoreCase(secGroup.getGroupName())) {
        System.out.println("Using Security Group " + securityGroup);
        return;
      }
    }

    CreateSecurityGroupRequest createSecurityGroupRequest = new CreateSecurityGroupRequest();

    createSecurityGroupRequest
        .withGroupName(securityGroup)
        .withDescription("My Java Security Group");

    CreateSecurityGroupResult createSecurityGroupResult =
        ec2.createSecurityGroup(createSecurityGroupRequest);

    // SSH
    IpPermission ipPermission1 = new IpPermission();
    ipPermission1.withIpRanges("0.0.0.0/0").withIpProtocol("tcp").withFromPort(22).withToPort(22);
    // http
    IpPermission ipPermission2 = new IpPermission();
    ipPermission2.withIpRanges("0.0.0.0/0").withIpProtocol("tcp").withFromPort(80).withToPort(80);
    // https
    IpPermission ipPermission3 = new IpPermission();
    ipPermission3.withIpRanges("0.0.0.0/0").withIpProtocol("tcp").withFromPort(443).withToPort(443);
    // tcp
    IpPermission ipPermission4 = new IpPermission();
    ipPermission4
        .withIpRanges("0.0.0.0/0")
        .withIpProtocol("tcp")
        .withFromPort(65535)
        .withToPort(65535);
    // telnet
    IpPermission ipPermission5 = new IpPermission();
    ipPermission5.withIpRanges("0.0.0.0/0").withIpProtocol("tcp").withFromPort(23).withToPort(23);

    List<IpPermission> permissions = new ArrayList<IpPermission>();
    permissions.add(ipPermission1);
    permissions.add(ipPermission2);
    permissions.add(ipPermission3);
    permissions.add(ipPermission4);
    permissions.add(ipPermission5);

    AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest =
        new AuthorizeSecurityGroupIngressRequest();

    authorizeSecurityGroupIngressRequest
        .withGroupName(securityGroup)
        .withIpPermissions(permissions);

    ec2.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest);

    System.out.println("Created Security Group " + securityGroup);
  }
  public static void addSecurityGroup(String groupName) {

    CreateSecurityGroupRequest csgr = new CreateSecurityGroupRequest();
    csgr.withGroupName(groupName).withDescription("Allow on all ports");

    ec2.createSecurityGroup(csgr);

    IpPermission ipPermission = new IpPermission();

    ipPermission.withIpRanges("0.0.0.0/0").withIpProtocol("tcp").withFromPort(0).withToPort(65535);

    AuthorizeSecurityGroupIngressRequest incoming = new AuthorizeSecurityGroupIngressRequest();
    incoming.withGroupName(groupName).withIpPermissions(ipPermission);

    ec2.authorizeSecurityGroupIngress(incoming);

    System.out.println("Security Group " + groupName + " added");
  }
  public Request<AuthorizeSecurityGroupEgressRequest> marshall(
      AuthorizeSecurityGroupEgressRequest authorizeSecurityGroupEgressRequest) {

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

    Request<AuthorizeSecurityGroupEgressRequest> request =
        new DefaultRequest<AuthorizeSecurityGroupEgressRequest>(
            authorizeSecurityGroupEgressRequest, "AmazonEC2");
    request.addParameter("Action", "AuthorizeSecurityGroupEgress");
    request.addParameter("Version", "2014-06-15");

    if (authorizeSecurityGroupEgressRequest.getGroupId() != null) {
      request.addParameter(
          "GroupId", StringUtils.fromString(authorizeSecurityGroupEgressRequest.getGroupId()));
    }
    if (authorizeSecurityGroupEgressRequest.getSourceSecurityGroupName() != null) {
      request.addParameter(
          "SourceSecurityGroupName",
          StringUtils.fromString(authorizeSecurityGroupEgressRequest.getSourceSecurityGroupName()));
    }
    if (authorizeSecurityGroupEgressRequest.getSourceSecurityGroupOwnerId() != null) {
      request.addParameter(
          "SourceSecurityGroupOwnerId",
          StringUtils.fromString(
              authorizeSecurityGroupEgressRequest.getSourceSecurityGroupOwnerId()));
    }
    if (authorizeSecurityGroupEgressRequest.getIpProtocol() != null) {
      request.addParameter(
          "IpProtocol",
          StringUtils.fromString(authorizeSecurityGroupEgressRequest.getIpProtocol()));
    }
    if (authorizeSecurityGroupEgressRequest.getFromPort() != null) {
      request.addParameter(
          "FromPort", StringUtils.fromInteger(authorizeSecurityGroupEgressRequest.getFromPort()));
    }
    if (authorizeSecurityGroupEgressRequest.getToPort() != null) {
      request.addParameter(
          "ToPort", StringUtils.fromInteger(authorizeSecurityGroupEgressRequest.getToPort()));
    }
    if (authorizeSecurityGroupEgressRequest.getCidrIp() != null) {
      request.addParameter(
          "CidrIp", StringUtils.fromString(authorizeSecurityGroupEgressRequest.getCidrIp()));
    }

    java.util.List<IpPermission> ipPermissionsList =
        authorizeSecurityGroupEgressRequest.getIpPermissions();
    int ipPermissionsListIndex = 1;

    for (IpPermission ipPermissionsListValue : ipPermissionsList) {
      IpPermission ipPermissionMember = ipPermissionsListValue;
      if (ipPermissionMember != null) {
        if (ipPermissionMember.getIpProtocol() != null) {
          request.addParameter(
              "IpPermissions." + ipPermissionsListIndex + ".IpProtocol",
              StringUtils.fromString(ipPermissionMember.getIpProtocol()));
        }
        if (ipPermissionMember.getFromPort() != null) {
          request.addParameter(
              "IpPermissions." + ipPermissionsListIndex + ".FromPort",
              StringUtils.fromInteger(ipPermissionMember.getFromPort()));
        }
        if (ipPermissionMember.getToPort() != null) {
          request.addParameter(
              "IpPermissions." + ipPermissionsListIndex + ".ToPort",
              StringUtils.fromInteger(ipPermissionMember.getToPort()));
        }

        java.util.List<UserIdGroupPair> userIdGroupPairsList =
            ipPermissionMember.getUserIdGroupPairs();
        int userIdGroupPairsListIndex = 1;

        for (UserIdGroupPair userIdGroupPairsListValue : userIdGroupPairsList) {
          UserIdGroupPair userIdGroupPairMember = userIdGroupPairsListValue;
          if (userIdGroupPairMember != null) {
            if (userIdGroupPairMember.getUserId() != null) {
              request.addParameter(
                  "IpPermissions."
                      + ipPermissionsListIndex
                      + ".Groups."
                      + userIdGroupPairsListIndex
                      + ".UserId",
                  StringUtils.fromString(userIdGroupPairMember.getUserId()));
            }
            if (userIdGroupPairMember.getGroupName() != null) {
              request.addParameter(
                  "IpPermissions."
                      + ipPermissionsListIndex
                      + ".Groups."
                      + userIdGroupPairsListIndex
                      + ".GroupName",
                  StringUtils.fromString(userIdGroupPairMember.getGroupName()));
            }
            if (userIdGroupPairMember.getGroupId() != null) {
              request.addParameter(
                  "IpPermissions."
                      + ipPermissionsListIndex
                      + ".Groups."
                      + userIdGroupPairsListIndex
                      + ".GroupId",
                  StringUtils.fromString(userIdGroupPairMember.getGroupId()));
            }
          }

          userIdGroupPairsListIndex++;
        }

        java.util.List<String> ipRangesList = ipPermissionMember.getIpRanges();
        int ipRangesListIndex = 1;

        for (String ipRangesListValue : ipRangesList) {
          if (ipRangesListValue != null) {
            request.addParameter(
                "IpPermissions."
                    + ipPermissionsListIndex
                    + ".IpRanges."
                    + ipRangesListIndex
                    + ".CidrIp",
                StringUtils.fromString(ipRangesListValue));
          }

          ipRangesListIndex++;
        }
      }

      ipPermissionsListIndex++;
    }

    return request;
  }
  public Request<AuthorizeSecurityGroupIngressRequest> marshall(
      AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest) {
    Request<AuthorizeSecurityGroupIngressRequest> request =
        new DefaultRequest<AuthorizeSecurityGroupIngressRequest>(
            authorizeSecurityGroupIngressRequest, "AmazonEC2");
    request.addParameter("Action", "AuthorizeSecurityGroupIngress");
    request.addParameter("Version", "2011-02-28");
    if (authorizeSecurityGroupIngressRequest != null) {
      if (authorizeSecurityGroupIngressRequest.getGroupName() != null) {
        request.addParameter(
            "GroupName",
            StringUtils.fromString(authorizeSecurityGroupIngressRequest.getGroupName()));
      }
    }
    if (authorizeSecurityGroupIngressRequest != null) {
      if (authorizeSecurityGroupIngressRequest.getGroupId() != null) {
        request.addParameter(
            "GroupId", StringUtils.fromString(authorizeSecurityGroupIngressRequest.getGroupId()));
      }
    }
    if (authorizeSecurityGroupIngressRequest != null) {
      if (authorizeSecurityGroupIngressRequest.getSourceSecurityGroupName() != null) {
        request.addParameter(
            "SourceSecurityGroupName",
            StringUtils.fromString(
                authorizeSecurityGroupIngressRequest.getSourceSecurityGroupName()));
      }
    }
    if (authorizeSecurityGroupIngressRequest != null) {
      if (authorizeSecurityGroupIngressRequest.getSourceSecurityGroupOwnerId() != null) {
        request.addParameter(
            "SourceSecurityGroupOwnerId",
            StringUtils.fromString(
                authorizeSecurityGroupIngressRequest.getSourceSecurityGroupOwnerId()));
      }
    }
    if (authorizeSecurityGroupIngressRequest != null) {
      if (authorizeSecurityGroupIngressRequest.getIpProtocol() != null) {
        request.addParameter(
            "IpProtocol",
            StringUtils.fromString(authorizeSecurityGroupIngressRequest.getIpProtocol()));
      }
    }
    if (authorizeSecurityGroupIngressRequest != null) {
      if (authorizeSecurityGroupIngressRequest.getFromPort() != null) {
        request.addParameter(
            "FromPort",
            StringUtils.fromInteger(authorizeSecurityGroupIngressRequest.getFromPort()));
      }
    }
    if (authorizeSecurityGroupIngressRequest != null) {
      if (authorizeSecurityGroupIngressRequest.getToPort() != null) {
        request.addParameter(
            "ToPort", StringUtils.fromInteger(authorizeSecurityGroupIngressRequest.getToPort()));
      }
    }
    if (authorizeSecurityGroupIngressRequest != null) {
      if (authorizeSecurityGroupIngressRequest.getCidrIp() != null) {
        request.addParameter(
            "CidrIp", StringUtils.fromString(authorizeSecurityGroupIngressRequest.getCidrIp()));
      }
    }

    if (authorizeSecurityGroupIngressRequest != null) {
      java.util.List<IpPermission> ipPermissionsList =
          authorizeSecurityGroupIngressRequest.getIpPermissions();
      int ipPermissionsListIndex = 1;
      for (IpPermission ipPermissionsListValue : ipPermissionsList) {
        if (ipPermissionsListValue != null) {
          if (ipPermissionsListValue.getIpProtocol() != null) {
            request.addParameter(
                "IpPermissions." + ipPermissionsListIndex + ".IpProtocol",
                StringUtils.fromString(ipPermissionsListValue.getIpProtocol()));
          }
        }
        if (ipPermissionsListValue != null) {
          if (ipPermissionsListValue.getFromPort() != null) {
            request.addParameter(
                "IpPermissions." + ipPermissionsListIndex + ".FromPort",
                StringUtils.fromInteger(ipPermissionsListValue.getFromPort()));
          }
        }
        if (ipPermissionsListValue != null) {
          if (ipPermissionsListValue.getToPort() != null) {
            request.addParameter(
                "IpPermissions." + ipPermissionsListIndex + ".ToPort",
                StringUtils.fromInteger(ipPermissionsListValue.getToPort()));
          }
        }

        if (ipPermissionsListValue != null) {
          java.util.List<UserIdGroupPair> userIdGroupPairsList =
              ipPermissionsListValue.getUserIdGroupPairs();
          int userIdGroupPairsListIndex = 1;
          for (UserIdGroupPair userIdGroupPairsListValue : userIdGroupPairsList) {
            if (userIdGroupPairsListValue != null) {
              if (userIdGroupPairsListValue.getUserId() != null) {
                request.addParameter(
                    "IpPermissions."
                        + ipPermissionsListIndex
                        + ".Groups."
                        + userIdGroupPairsListIndex
                        + ".UserId",
                    StringUtils.fromString(userIdGroupPairsListValue.getUserId()));
              }
            }
            if (userIdGroupPairsListValue != null) {
              if (userIdGroupPairsListValue.getGroupName() != null) {
                request.addParameter(
                    "IpPermissions."
                        + ipPermissionsListIndex
                        + ".Groups."
                        + userIdGroupPairsListIndex
                        + ".GroupName",
                    StringUtils.fromString(userIdGroupPairsListValue.getGroupName()));
              }
            }
            if (userIdGroupPairsListValue != null) {
              if (userIdGroupPairsListValue.getGroupId() != null) {
                request.addParameter(
                    "IpPermissions."
                        + ipPermissionsListIndex
                        + ".Groups."
                        + userIdGroupPairsListIndex
                        + ".GroupId",
                    StringUtils.fromString(userIdGroupPairsListValue.getGroupId()));
              }
            }

            userIdGroupPairsListIndex++;
          }
        }
        if (ipPermissionsListValue != null) {
          java.util.List<String> ipRangesList = ipPermissionsListValue.getIpRanges();
          int ipRangesListIndex = 1;

          for (String ipRangesListValue : ipRangesList) {
            if (ipRangesListValue != null) {
              request.addParameter(
                  "IpPermissions."
                      + ipPermissionsListIndex
                      + ".IpRanges."
                      + ipRangesListIndex
                      + ".CidrIp",
                  StringUtils.fromString(ipRangesListValue));
            }
            ipRangesListIndex++;
          }
        }

        ipPermissionsListIndex++;
      }
    }

    return request;
  }
Example #5
0
  @Override
  public boolean equals(Object obj) {
    if (this == obj) return true;
    if (obj == null) return false;

    if (obj instanceof IpPermission == false) return false;
    IpPermission other = (IpPermission) obj;

    if (other.getIpProtocol() == null ^ this.getIpProtocol() == null) return false;
    if (other.getIpProtocol() != null
        && other.getIpProtocol().equals(this.getIpProtocol()) == false) return false;
    if (other.getFromPort() == null ^ this.getFromPort() == null) return false;
    if (other.getFromPort() != null && other.getFromPort().equals(this.getFromPort()) == false)
      return false;
    if (other.getToPort() == null ^ this.getToPort() == null) return false;
    if (other.getToPort() != null && other.getToPort().equals(this.getToPort()) == false)
      return false;
    if (other.getUserIdGroupPairs() == null ^ this.getUserIdGroupPairs() == null) return false;
    if (other.getUserIdGroupPairs() != null
        && other.getUserIdGroupPairs().equals(this.getUserIdGroupPairs()) == false) return false;
    if (other.getIpRanges() == null ^ this.getIpRanges() == null) return false;
    if (other.getIpRanges() != null && other.getIpRanges().equals(this.getIpRanges()) == false)
      return false;
    return true;
  }