private Builder() { rule = new Rule(); }
@Override public IpPermission apply(Rule from) { if (from.getDirection() == RuleDirection.EGRESS) return null; IpPermission.Builder builder = IpPermission.builder(); if (from.getProtocol() != null) { builder.ipProtocol(IpProtocol.fromValue(from.getProtocol().name())); } else { builder.ipProtocol(IpProtocol.TCP); } if (from.getPortRangeMin() != null) builder.fromPort(from.getPortRangeMin()); if (from.getPortRangeMax() != null) builder.toPort(from.getPortRangeMax()); if (from.getRemoteGroupId() != null) { builder.groupId(regionId + "/" + from.getRemoteGroupId()); } else if (from.getRemoteIpPrefix() != null){ builder.cidrBlock(from.getRemoteIpPrefix()); } return builder.build(); } }), Predicates.notNull()));
@Override public boolean apply(@Nullable Rule input) { return input.getRemoteIpPrefix() != null && input.getRemoteIpPrefix().equals(cidr) && input.getProtocol() != null && input.getProtocol().name().equals(ipPermission.getIpProtocol().name()) && input.getPortRangeMin() != null && input.getPortRangeMin() == ipPermission.getFromPort() && input.getPortRangeMax() != null && input.getPortRangeMax() == ipPermission.getToPort(); } })) {
@Override public boolean apply(@Nullable Rule input) { return input.getRemoteGroupId() != null && input.getRemoteGroupId().equals(groupId) && input.getProtocol() != null && input.getProtocol().name().equals(ipPermission.getIpProtocol().name()) && input.getPortRangeMin() != null && input.getPortRangeMin() == ipPermission.getFromPort() && input.getPortRangeMax() != null && input.getPortRangeMax() == ipPermission.getToPort(); } })) {
SecurityGroupApi api = neutronApi.getSecurityGroupApi("RegionOne"); Rule.CreateRule createSecurityGroupRule = Rule.createBuilder( RuleDirection.INGRESS, "a7734e61-b545-452d-a3cd-0189cbd9747a") .portRangeMin(80) assertEquals(rule.getId(), "2bc0accf-312e-429a-956e-e4407625eb62"); assertEquals(rule.getTenantId(), "e4f50856753b4dc6afee5fa6b9b6c550"); assertEquals(rule.getDirection(), RuleDirection.INGRESS); assertEquals(rule.getPortRangeMax().intValue(), 80); assertEquals(rule.getPortRangeMin().intValue(), 80); assertEquals(rule.getEthertype(), RuleEthertype.IPV4); assertEquals(rule.getProtocol(), RuleProtocol.TCP); assertEquals(rule.getRemoteGroupId(), "85cc3048-abc3-43cc-89b3-377341426ac5"); assertEquals(rule.getSecurityGroupId(), "a7734e61-b545-452d-a3cd-0189cbd9747a");
Rule.createBuilder(RuleDirection.INGRESS, securityGroup.getId()) .ethertype(RuleEthertype.IPV6) .portRangeMax(90) if (sgr.getId().equals(rule.getId())) { newSecGroupRule = sgr; break; assertEquals(rule.getEthertype(), RuleEthertype.IPV6); assertEquals(rule.getProtocol(), RuleProtocol.TCP); assertEquals(rule.getPortRangeMax().intValue(), 90); assertEquals(rule.getPortRangeMin().intValue(), 80); assertEquals(rule.getDirection(), RuleDirection.INGRESS); } finally { if (sgApi != null) { try { if (rule != null) { assertTrue(sgApi.deleteRule(rule.getId()));
public void testGetSecurityGroupRule() throws IOException, InterruptedException, URISyntaxException { MockWebServer server = mockOpenStackServer(); server.enqueue(addCommonHeaders(new MockResponse().setBody(stringFromResource("/access.json")))); server.enqueue(addCommonHeaders( new MockResponse().setResponseCode(201).setBody(stringFromResource("/security_group_rule_get_response.json")))); try { NeutronApi neutronApi = api(server.getUrl("/").toString(), "openstack-neutron", overrides); SecurityGroupApi api = neutronApi.getSecurityGroupApi("RegionOne"); Rule rule = api.get("12345"); /* * Check request */ assertEquals(server.getRequestCount(), 2); assertAuthentication(server); assertRequest(server.takeRequest(), "GET", uriApiVersion + "/security-group-rules/12345"); /* * Check response */ assertNotNull(rule); assertEquals(rule.getDirection(), RuleDirection.EGRESS); assertEquals(rule.getEthertype(), RuleEthertype.IPV6); assertEquals(rule.getId(), "3c0e45ff-adaf-4124-b083-bf390e5482ff"); assertEquals(rule.getTenantId(), "e4f50856753b4dc6afee5fa6b9b6c550"); assertEquals(rule.getSecurityGroupId(), "85cc3048-abc3-43cc-89b3-377341426ac5"); } finally { server.shutdown(); } }
@Override public boolean apply(Rule input) { return RuleDirection.INGRESS.equals(input.getDirection()); } })), 1);
@Override public boolean apply(@Nullable Rule input) { return input.getRemoteGroupId() != null && input.getRemoteGroupId().equals(groupId) && input.getProtocol() != null && input.getProtocol().name().equals(ipPermission.getIpProtocol().name()) && input.getPortRangeMin() != null && input.getPortRangeMin() == ipPermission.getFromPort() && input.getPortRangeMax() != null && input.getPortRangeMax() == ipPermission.getToPort(); } })) {
@Override public boolean apply(@Nullable Rule input) { return input.getRemoteIpPrefix() != null && input.getRemoteIpPrefix().equals(cidr) && input.getProtocol() != null && input.getProtocol().name().equals(ipPermission.getIpProtocol().name()) && input.getPortRangeMin() != null && input.getPortRangeMin() == ipPermission.getFromPort() && input.getPortRangeMax() != null && input.getPortRangeMax() == ipPermission.getToPort(); } })) {
@Override public IpPermission apply(Rule from) { if (from.getDirection() == RuleDirection.EGRESS) return null; IpPermission.Builder builder = IpPermission.builder(); if (from.getProtocol() != null) { builder.ipProtocol(IpProtocol.fromValue(from.getProtocol().name())); } else { builder.ipProtocol(IpProtocol.TCP); } if (from.getPortRangeMin() != null) builder.fromPort(from.getPortRangeMin()); if (from.getPortRangeMax() != null) builder.toPort(from.getPortRangeMax()); if (from.getRemoteGroupId() != null) { builder.groupId(regionId + "/" + from.getRemoteGroupId()); } else if (from.getRemoteIpPrefix() != null){ builder.cidrBlock(from.getRemoteIpPrefix()); } return builder.build(); } }), Predicates.notNull()));
public void testListSpecificPageSecurityGroupRule() throws IOException, InterruptedException, URISyntaxException { MockWebServer server = mockOpenStackServer(); server.enqueue(addCommonHeaders(new MockResponse().setBody(stringFromResource("/access.json")))); server.enqueue(addCommonHeaders(new MockResponse().setResponseCode(200).setBody(stringFromResource("/security_group_rule_list_response_paged1.json")))); try { NeutronApi neutronApi = api(server.getUrl("/").toString(), "openstack-neutron", overrides); SecurityGroupApi api = neutronApi.getSecurityGroupApi("RegionOne"); Rules rules = api.listRules(PaginationOptions.Builder.limit(2).marker("abcdefg")); /* * Check request */ assertEquals(server.getRequestCount(), 2); assertAuthentication(server); assertRequest(server.takeRequest(), "GET", uriApiVersion + "/security-group-rules?limit=2&marker=abcdefg"); /* * Check response */ assertNotNull(rules); assertEquals(rules.size(), 4); // Ensures the full collection is parsed and ordering is preserved. assertEquals(rules.first().get().getId(), "3c0e45ff-adaf-4124-b083-bf390e5482ff"); assertEquals(rules.get(3).getId(), "f7d45c89-008e-4bab-88ad-d6811724c51c"); } finally { server.shutdown(); } }
private Builder() { rule = new Rule(); }
public void testListPagedSecurityGroupRules() throws IOException, InterruptedException, URISyntaxException { MockWebServer server = mockOpenStackServer(); server.enqueue(addCommonHeaders(new MockResponse().setBody(stringFromResource("/access.json")))); server.enqueue(addCommonHeaders(new MockResponse().setResponseCode(200).setBody(stringFromResource("/security_group_rule_list_response_paged1.json")))); server.enqueue(addCommonHeaders(new MockResponse().setResponseCode(200).setBody(stringFromResource("/security_group_rule_list_response_paged2.json")))); try { NeutronApi neutronApi = api(server.getUrl("/").toString(), "openstack-neutron", overrides); SecurityGroupApi api = neutronApi.getSecurityGroupApi("RegionOne"); // Note: Lazy! Have to actually look at the collection. List<Rule> rules = api.listRules().concat().toList(); /* * Check request */ assertEquals(server.getRequestCount(), 3); assertAuthentication(server); assertRequest(server.takeRequest(), "GET", uriApiVersion + "/security-group-rules"); assertRequest(server.takeRequest(), "GET", uriApiVersion + "/security-group-rules?marker=71c1e68c-171a-4aa2-aca5-50ea153a3718"); /* * Check response */ assertNotNull(rules); assertEquals(rules.size(), 8); // Ensures both pages are tested and ordering is preserved. assertEquals(rules.get(0).getId(), "3c0e45ff-adaf-4124-b083-bf390e5482ff"); assertEquals(rules.get(7).getId(), "f7d45c89-008e-4bab-88ad-d6811724c51c2"); } finally { server.shutdown(); } }
public void testGetSecurityGroup() throws IOException, InterruptedException, URISyntaxException { MockWebServer server = mockOpenStackServer(); server.enqueue(addCommonHeaders(new MockResponse().setBody(stringFromResource("/access.json")))); server.enqueue(addCommonHeaders( new MockResponse().setResponseCode(201).setBody(stringFromResource("/security_group_get_response.json")))); try { NeutronApi neutronApi = api(server.getUrl("/").toString(), "openstack-neutron", overrides); SecurityGroupApi api = neutronApi.getSecurityGroupApi("RegionOne"); SecurityGroup securityGroup = api.getSecurityGroup("12345"); /* * Check request */ assertEquals(server.getRequestCount(), 2); assertAuthentication(server); assertRequest(server.takeRequest(), "GET", uriApiVersion + "/security-groups/12345"); /* * Check response */ assertNotNull(securityGroup); assertEquals(securityGroup.getName(), "default"); assertEquals(securityGroup.getDescription(), "default"); assertEquals(securityGroup.getId(), "85cc3048-abc3-43cc-89b3-377341426ac5"); assertEquals(securityGroup.getTenantId(), "e4f50856753b4dc6afee5fa6b9b6c550"); Rule sgr = securityGroup.getRules().get(0); assertEquals(sgr.getId(), "3c0e45ff-adaf-4124-b083-bf390e5482ff"); } finally { server.shutdown(); } }