Ejemplo n.º 1
0
 @Override
 public IpPermission apply(IngressRule rule) {
   IpPermission.Builder builder = IpPermission.builder();
   builder.ipProtocol(IpProtocol.fromValue(rule.getProtocol()));
   builder.fromPort(rule.getStartPort());
   builder.toPort(rule.getEndPort());
   if (rule.getCIDR() != null) {
     builder.cidrBlock(rule.getCIDR());
   }
   if (rule.getSecurityGroupName() != null && rule.getAccount() != null) {
     builder.tenantIdGroupNamePair(rule.getAccount(), rule.getSecurityGroupName());
   }
   return builder.build();
 }
  @Test(dependsOnMethods = "testCreateDestroySecurityGroup")
  public void testCreateIngress() throws Exception {
    if (!securityGroupsSupported) return;
    String cidr = getCurrentCIDR();
    ImmutableSet<String> cidrs = ImmutableSet.of(cidr);
    assert jobComplete.apply(
            client.getSecurityGroupClient().authorizeIngressICMPToCIDRs(group.getId(), 0, 8, cidrs))
        : group;
    assert jobComplete.apply(
            client
                .getSecurityGroupClient()
                .authorizeIngressPortsToCIDRs(group.getId(), "TCP", 22, 22, cidrs))
        : group;

    AccountInDomainOptions.Builder.accountInDomain(group.getAccount(), group.getDomainId());

    // replace with get once bug is fixed where getGroup returns only one
    // ingress rule
    group =
        Iterables.find(
            client.getSecurityGroupClient().listSecurityGroups(),
            new Predicate<SecurityGroup>() {

              @Override
              public boolean apply(SecurityGroup input) {
                return input.getId() == group.getId();
              }
            });

    IngressRule ICMPPingRule =
        Iterables.find(
            group.getIngressRules(),
            new Predicate<IngressRule>() {

              @Override
              public boolean apply(IngressRule input) {
                return "icmp".equals(input.getProtocol());
              }
            });

    assert ICMPPingRule.getId() > 0 : ICMPPingRule;
    assert "icmp".equals(ICMPPingRule.getProtocol()) : ICMPPingRule;
    assert ICMPPingRule.getStartPort() == -1 : ICMPPingRule;
    assert ICMPPingRule.getEndPort() == -1 : ICMPPingRule;
    assert ICMPPingRule.getICMPCode() == 0 : ICMPPingRule;
    assert ICMPPingRule.getICMPType() == 8 : ICMPPingRule;
    assert ICMPPingRule.getAccount() == null : ICMPPingRule;
    assert ICMPPingRule.getSecurityGroupName() == null : ICMPPingRule;
    assert cidr.equals(ICMPPingRule.getCIDR()) : ICMPPingRule;

    IngressRule SSHRule =
        Iterables.find(
            group.getIngressRules(),
            new Predicate<IngressRule>() {

              @Override
              public boolean apply(IngressRule input) {
                return "tcp".equals(input.getProtocol());
              }
            });

    assert SSHRule.getId() > 0 : SSHRule;
    assert "tcp".equals(SSHRule.getProtocol()) : SSHRule;
    assert SSHRule.getStartPort() == 22 : SSHRule;
    assert SSHRule.getEndPort() == 22 : SSHRule;
    assert SSHRule.getICMPCode() == -1 : SSHRule;
    assert SSHRule.getICMPType() == -1 : SSHRule;
    assert SSHRule.getAccount() == null : SSHRule;
    assert SSHRule.getSecurityGroupName() == null : SSHRule;
    assert cidr.equals(SSHRule.getCIDR()) : SSHRule;
  }