mirror of
https://github.com/apache/jclouds.git
synced 2025-02-16 15:08:28 +00:00
cleaned security group syntax for rule creation
This commit is contained in:
parent
acd83ce9f4
commit
f2ce700ec1
@ -0,0 +1,73 @@
|
||||
/**
|
||||
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||
* contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. jclouds licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jclouds.openstack.nova.v1_1.binders;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import org.jclouds.http.HttpRequest;
|
||||
import org.jclouds.json.Json;
|
||||
import org.jclouds.openstack.nova.v1_1.domain.Ingress;
|
||||
import org.jclouds.rest.MapBinder;
|
||||
import org.jclouds.rest.binders.BindToJsonPayload;
|
||||
import org.jclouds.rest.internal.GeneratedHttpRequest;
|
||||
|
||||
import com.google.common.base.Predicates;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.ImmutableMap.Builder;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*
|
||||
*/
|
||||
@Singleton
|
||||
public class BindSecurityGroupRuleToJsonPayload extends BindToJsonPayload implements MapBinder {
|
||||
@Inject
|
||||
public BindSecurityGroupRuleToJsonPayload(Json jsonBinder) {
|
||||
super(jsonBinder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R extends HttpRequest> R bindToRequest(R request, Object toBind) {
|
||||
throw new IllegalStateException("BindCredentialsToJsonPayload needs parameters");
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) {
|
||||
Builder<String, String> payload = ImmutableMap.<String, String> builder();
|
||||
payload.putAll(postParams);
|
||||
checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest<?>,
|
||||
"this binder is only valid for GeneratedHttpRequests!");
|
||||
GeneratedHttpRequest<?> gRequest = (GeneratedHttpRequest<?>) request;
|
||||
|
||||
Ingress ingress = Ingress.class.cast(Iterables.find(gRequest.getArgs(), Predicates.instanceOf(Ingress.class)));
|
||||
payload.put("ip_protocol", ingress.getIpProtocol().toString());
|
||||
payload.put("from_port", ingress.getFromPort() + "");
|
||||
payload.put("to_port", ingress.getToPort() + "");
|
||||
|
||||
return super.bindToRequest(request, ImmutableMap.of("security_group_rule", payload.build()));
|
||||
}
|
||||
}
|
@ -0,0 +1,135 @@
|
||||
/**
|
||||
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||
* contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. jclouds licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jclouds.openstack.nova.v1_1.domain;
|
||||
|
||||
import static com.google.common.base.Objects.equal;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.common.base.Objects.ToStringHelper;
|
||||
|
||||
/**
|
||||
* Ingress access to a destination protocol on particular ports
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
@Beta
|
||||
public class Ingress {
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private IpProtocol ipProtocol;
|
||||
private int fromPort;
|
||||
private int toPort;
|
||||
|
||||
/**
|
||||
*
|
||||
* @see Ingress#getIpProtocol()
|
||||
*/
|
||||
public Builder ipProtocol(IpProtocol ipProtocol) {
|
||||
this.ipProtocol = ipProtocol;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @see Ingress#getFromPort()
|
||||
*/
|
||||
public Builder fromPort(int fromPort) {
|
||||
this.fromPort = fromPort;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @see Ingress#getToPort()
|
||||
*/
|
||||
public Builder toPort(int toPort) {
|
||||
this.toPort = toPort;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Ingress build() {
|
||||
return new Ingress(ipProtocol, fromPort, toPort);
|
||||
}
|
||||
}
|
||||
|
||||
private final IpProtocol ipProtocol;
|
||||
private final int fromPort;
|
||||
private final int toPort;
|
||||
|
||||
protected Ingress(IpProtocol ipProtocol, int fromPort, int toPort) {
|
||||
this.fromPort = fromPort;
|
||||
this.toPort = toPort;
|
||||
this.ipProtocol = checkNotNull(ipProtocol, "ipProtocol");
|
||||
}
|
||||
|
||||
/**
|
||||
* destination IP protocol
|
||||
*/
|
||||
public IpProtocol getIpProtocol() {
|
||||
return ipProtocol;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start of destination port range for the TCP and UDP protocols, or an ICMP type number. An ICMP
|
||||
* type number of -1 indicates a wildcard (i.e., any ICMP type number).
|
||||
*/
|
||||
public int getFromPort() {
|
||||
return fromPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* End of destination port range for the TCP and UDP protocols, or an ICMP code. An ICMP code of
|
||||
* -1 indicates a wildcard (i.e., any ICMP code).
|
||||
*/
|
||||
public int getToPort() {
|
||||
return toPort;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
// allow subtypes
|
||||
if (o == null || !(o instanceof Ingress))
|
||||
return false;
|
||||
Ingress that = Ingress.class.cast(o);
|
||||
return equal(this.ipProtocol, that.ipProtocol) && equal(this.fromPort, that.fromPort)
|
||||
&& equal(this.toPort, that.toPort);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hashCode(ipProtocol, fromPort, toPort);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return string().toString();
|
||||
}
|
||||
|
||||
protected ToStringHelper string() {
|
||||
return Objects.toStringHelper("").add("ipProtocol", ipProtocol).add("fromPort", fromPort).add("toPort", toPort);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||
* contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. jclouds licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jclouds.openstack.nova.v1_1.domain;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
public enum IpProtocol {
|
||||
TCP, UDP, ICMP, UNRECOGNIZED;
|
||||
public String value() {
|
||||
return name().toLowerCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return value();
|
||||
}
|
||||
|
||||
public static IpProtocol fromValue(String protocol) {
|
||||
try {
|
||||
return valueOf(checkNotNull(protocol, "protocol").toUpperCase());
|
||||
} catch (IllegalArgumentException e) {
|
||||
return UNRECOGNIZED;
|
||||
}
|
||||
}
|
||||
}
|
@ -19,7 +19,6 @@
|
||||
package org.jclouds.openstack.nova.v1_1.domain;
|
||||
|
||||
import static com.google.common.base.Objects.toStringHelper;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@ -31,26 +30,6 @@ import com.google.gson.annotations.SerializedName;
|
||||
*/
|
||||
public class SecurityGroupRule implements Comparable<SecurityGroupRule> {
|
||||
|
||||
public static enum IpProtocol {
|
||||
TCP, UDP, ICMP, UNRECOGNIZED;
|
||||
public String value() {
|
||||
return name().toLowerCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return value();
|
||||
}
|
||||
|
||||
public static IpProtocol fromValue(String protocol) {
|
||||
try {
|
||||
return valueOf(checkNotNull(protocol, "protocol").toUpperCase());
|
||||
} catch (IllegalArgumentException e) {
|
||||
return UNRECOGNIZED;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
@ -30,11 +30,14 @@ import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import org.jclouds.openstack.filters.AuthenticateRequest;
|
||||
import org.jclouds.openstack.nova.v1_1.binders.BindSecurityGroupRuleToJsonPayload;
|
||||
import org.jclouds.openstack.nova.v1_1.domain.Ingress;
|
||||
import org.jclouds.openstack.nova.v1_1.domain.SecurityGroup;
|
||||
import org.jclouds.openstack.nova.v1_1.domain.SecurityGroupRule;
|
||||
import org.jclouds.openstack.services.Extension;
|
||||
import org.jclouds.openstack.services.ServiceType;
|
||||
import org.jclouds.rest.annotations.ExceptionParser;
|
||||
import org.jclouds.rest.annotations.MapBinder;
|
||||
import org.jclouds.rest.annotations.Payload;
|
||||
import org.jclouds.rest.annotations.PayloadParam;
|
||||
import org.jclouds.rest.annotations.RequestFilters;
|
||||
@ -52,14 +55,13 @@ import com.google.common.util.concurrent.ListenableFuture;
|
||||
*
|
||||
* @see SecurityGroupClient
|
||||
* @author Jeremy Daggett
|
||||
* @see <a href=
|
||||
* "http://docs.openstack.org/api/openstack-compute/2/content/Extensions-d1e1444.html"
|
||||
* @see <a href= "http://docs.openstack.org/api/openstack-compute/2/content/Extensions-d1e1444.html"
|
||||
* />
|
||||
* @see <a href="http://nova.openstack.org/api_ext" />
|
||||
* @see <a href="http://wiki.openstack.org/os-security-groups" />
|
||||
*/
|
||||
@Extension(of = ServiceType.COMPUTE, namespace = ExtensionNamespaces.SECURITY_GROUPS)
|
||||
@SkipEncoding({ '/', '=' })
|
||||
@SkipEncoding( { '/', '=' })
|
||||
@RequestFilters(AuthenticateRequest.class)
|
||||
public interface SecurityGroupAsyncClient {
|
||||
|
||||
@ -94,7 +96,7 @@ public interface SecurityGroupAsyncClient {
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Payload("%7B\"security_group\":%7B\"name\":\"{name}\",\"description\":\"{description}\"%7D%7D")
|
||||
ListenableFuture<SecurityGroup> createSecurityGroupWithNameAndDescription(@PayloadParam("name") String name,
|
||||
@PayloadParam("description") String description);
|
||||
@PayloadParam("description") String description);
|
||||
|
||||
/**
|
||||
* @see SecurityGroupClient#deleteSecurityGroup
|
||||
@ -106,7 +108,7 @@ public interface SecurityGroupAsyncClient {
|
||||
ListenableFuture<Boolean> deleteSecurityGroup(@PathParam("id") String id);
|
||||
|
||||
/**
|
||||
* @see SecurityGroupClient#createSecurityGroupRule
|
||||
* @see SecurityGroupClient#createSecurityGroupRuleAllowingCidrBlock
|
||||
*/
|
||||
@POST
|
||||
@Path("/os-security-group-rules")
|
||||
@ -114,13 +116,24 @@ public interface SecurityGroupAsyncClient {
|
||||
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Payload("%7B\"security_group_rule\":%7B\"ip_protocol\":\"{ip_protocol}\","
|
||||
+ "\"from_port\":\"{from_port}\",\"to_port\":\"{to_port}\","
|
||||
+ "\"cidr\":\"{cidr}\",\"group_id\":\"{group_id}\",\"parent_group_id\":\"{parent_group_id}\"%7D%7D")
|
||||
ListenableFuture<SecurityGroupRule> createSecurityGroupRule(@PayloadParam("ip_protocol") String ip_protocol,
|
||||
@PayloadParam("from_port") String from_port, @PayloadParam("to_port") String to_port,
|
||||
@PayloadParam("cidr") String cidr, @PayloadParam("group_id") String group_id,
|
||||
@PayloadParam("parent_group_id") String parent_group_id);
|
||||
@MapBinder(BindSecurityGroupRuleToJsonPayload.class)
|
||||
ListenableFuture<SecurityGroupRule> createSecurityGroupRuleAllowingCidrBlock(
|
||||
@PayloadParam("parent_group_id") String parent_group_id, Ingress ip_protocol,
|
||||
@PayloadParam("cidr") String cidr);
|
||||
|
||||
/**
|
||||
* @see SecurityGroupClient#createRuleOnSecurityGroupToCidrBlock
|
||||
*/
|
||||
@POST
|
||||
@Path("/os-security-group-rules")
|
||||
@SelectJson("security_group_rule")
|
||||
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@MapBinder(BindSecurityGroupRuleToJsonPayload.class)
|
||||
ListenableFuture<SecurityGroupRule> createSecurityGroupRuleAllowingSecurityGroupId(
|
||||
@PayloadParam("parent_group_id") String parent_group_id, Ingress ip_protocol,
|
||||
@PayloadParam("group_id") String group_id);
|
||||
|
||||
/**
|
||||
* @see SecurityGroupClient#deleteSecurityGroupRule
|
||||
|
@ -22,6 +22,7 @@ import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.jclouds.concurrent.Timeout;
|
||||
import org.jclouds.openstack.nova.v1_1.domain.Ingress;
|
||||
import org.jclouds.openstack.nova.v1_1.domain.SecurityGroup;
|
||||
import org.jclouds.openstack.nova.v1_1.domain.SecurityGroupRule;
|
||||
import org.jclouds.openstack.services.Extension;
|
||||
@ -71,8 +72,15 @@ public interface SecurityGroupClient {
|
||||
*
|
||||
* @return a new Security Group Rule
|
||||
*/
|
||||
SecurityGroupRule createSecurityGroupRule(String ip_protocol, String from_port, String to_port, String cidr,
|
||||
String group_id, String parent_group_id);
|
||||
SecurityGroupRule createSecurityGroupRuleAllowingCidrBlock(String parent_group_id, Ingress ip_protocol, String cidr);
|
||||
|
||||
/**
|
||||
* Create a Security Group Rule.
|
||||
*
|
||||
* @return a new Security Group Rule
|
||||
*/
|
||||
SecurityGroupRule createSecurityGroupRuleAllowingSecurityGroupId(String group_id, Ingress ip_protocol,
|
||||
String parent_group_id);
|
||||
|
||||
/**
|
||||
* Delete a Security Group Rule.
|
||||
|
@ -27,6 +27,8 @@ import java.net.URI;
|
||||
import org.jclouds.http.HttpRequest;
|
||||
import org.jclouds.http.HttpResponse;
|
||||
import org.jclouds.openstack.nova.v1_1.NovaClient;
|
||||
import org.jclouds.openstack.nova.v1_1.domain.Ingress;
|
||||
import org.jclouds.openstack.nova.v1_1.domain.IpProtocol;
|
||||
import org.jclouds.openstack.nova.v1_1.domain.SecurityGroup;
|
||||
import org.jclouds.openstack.nova.v1_1.domain.SecurityGroupRule;
|
||||
import org.jclouds.openstack.nova.v1_1.internal.BaseNovaClientExpectTest;
|
||||
@ -46,185 +48,193 @@ import com.google.common.collect.ImmutableSet;
|
||||
@Test(groups = "unit", testName = "SecurityGroupClientExpectTest")
|
||||
public class SecurityGroupClientExpectTest extends BaseNovaClientExpectTest {
|
||||
public void testListSecurityGroupsWhenResponseIs2xx() throws Exception {
|
||||
HttpRequest listSecurityGroups = HttpRequest
|
||||
.builder()
|
||||
.method("GET")
|
||||
.endpoint(URI.create("https://compute.north.host/v1.1/3456/os-security-groups"))
|
||||
.headers(
|
||||
ImmutableMultimap.<String, String> builder().put("Accept", "application/json")
|
||||
.put("X-Auth-Token", authToken).build()).build();
|
||||
HttpRequest listSecurityGroups = HttpRequest.builder().method("GET").endpoint(
|
||||
URI.create("https://compute.north.host/v1.1/3456/os-security-groups")).headers(
|
||||
ImmutableMultimap.<String, String> builder().put("Accept", "application/json").put("X-Auth-Token",
|
||||
authToken).build()).build();
|
||||
|
||||
HttpResponse listSecurityGroupsResponse = HttpResponse.builder().statusCode(200)
|
||||
.payload(payloadFromResource("/securitygroup_list.json")).build();
|
||||
HttpResponse listSecurityGroupsResponse = HttpResponse.builder().statusCode(200).payload(
|
||||
payloadFromResource("/securitygroup_list.json")).build();
|
||||
|
||||
NovaClient clientWhenSecurityGroupsExist = requestsSendResponses(keystoneAuthWithAccessKeyAndSecretKey,
|
||||
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, listSecurityGroups,
|
||||
listSecurityGroupsResponse);
|
||||
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, listSecurityGroups,
|
||||
listSecurityGroupsResponse);
|
||||
|
||||
assertEquals(clientWhenSecurityGroupsExist.getConfiguredZones(), ImmutableSet.of("az-1.region-a.geo-1"));
|
||||
|
||||
assertEquals(clientWhenSecurityGroupsExist.getSecurityGroupExtensionForZone("az-1.region-a.geo-1").get().listSecurityGroups()
|
||||
.toString(), new ParseSecurityGroupListTest().expected().toString());
|
||||
assertEquals(clientWhenSecurityGroupsExist.getSecurityGroupExtensionForZone("az-1.region-a.geo-1").get()
|
||||
.listSecurityGroups().toString(), new ParseSecurityGroupListTest().expected().toString());
|
||||
}
|
||||
|
||||
public void testListSecurityGroupsWhenReponseIs404IsEmpty() throws Exception {
|
||||
HttpRequest listListSecurityGroups = HttpRequest
|
||||
.builder()
|
||||
.method("GET")
|
||||
.endpoint(URI.create("https://compute.north.host/v1.1/3456/os-security-groups"))
|
||||
.headers(
|
||||
ImmutableMultimap.<String, String> builder().put("Accept", "application/json")
|
||||
.put("X-Auth-Token", authToken).build()).build();
|
||||
HttpRequest listListSecurityGroups = HttpRequest.builder().method("GET").endpoint(
|
||||
URI.create("https://compute.north.host/v1.1/3456/os-security-groups")).headers(
|
||||
ImmutableMultimap.<String, String> builder().put("Accept", "application/json").put("X-Auth-Token",
|
||||
authToken).build()).build();
|
||||
|
||||
HttpResponse listListSecurityGroupsResponse = HttpResponse.builder().statusCode(404).build();
|
||||
|
||||
NovaClient clientWhenNoSecurityGroupsExist = requestsSendResponses(keystoneAuthWithAccessKeyAndSecretKey,
|
||||
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, listListSecurityGroups,
|
||||
listListSecurityGroupsResponse);
|
||||
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, listListSecurityGroups,
|
||||
listListSecurityGroupsResponse);
|
||||
|
||||
assertTrue(clientWhenNoSecurityGroupsExist.getSecurityGroupExtensionForZone("az-1.region-a.geo-1").get().listSecurityGroups()
|
||||
.isEmpty());
|
||||
assertTrue(clientWhenNoSecurityGroupsExist.getSecurityGroupExtensionForZone("az-1.region-a.geo-1").get()
|
||||
.listSecurityGroups().isEmpty());
|
||||
}
|
||||
|
||||
public void testGetSecurityGroupWhenResponseIs2xx() throws Exception {
|
||||
|
||||
HttpRequest getSecurityGroup = HttpRequest
|
||||
.builder()
|
||||
.method("GET")
|
||||
.endpoint(URI.create("https://compute.north.host/v1.1/3456/os-security-groups/0"))
|
||||
.headers(
|
||||
ImmutableMultimap.<String, String> builder().put("Accept", "application/json")
|
||||
.put("X-Auth-Token", authToken).build()).build();
|
||||
HttpRequest getSecurityGroup = HttpRequest.builder().method("GET").endpoint(
|
||||
URI.create("https://compute.north.host/v1.1/3456/os-security-groups/0")).headers(
|
||||
ImmutableMultimap.<String, String> builder().put("Accept", "application/json").put("X-Auth-Token",
|
||||
authToken).build()).build();
|
||||
|
||||
HttpResponse getSecurityGroupResponse = HttpResponse.builder().statusCode(200)
|
||||
.payload(payloadFromResource("/securitygroup_details.json")).build();
|
||||
HttpResponse getSecurityGroupResponse = HttpResponse.builder().statusCode(200).payload(
|
||||
payloadFromResource("/securitygroup_details.json")).build();
|
||||
|
||||
NovaClient clientWhenSecurityGroupsExist = requestsSendResponses(keystoneAuthWithAccessKeyAndSecretKey,
|
||||
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, getSecurityGroup,
|
||||
getSecurityGroupResponse);
|
||||
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, getSecurityGroup,
|
||||
getSecurityGroupResponse);
|
||||
|
||||
assertEquals(clientWhenSecurityGroupsExist.getSecurityGroupExtensionForZone("az-1.region-a.geo-1").get()
|
||||
.getSecurityGroup("0").toString(), new ParseSecurityGroupTest().expected().toString());
|
||||
.getSecurityGroup("0").toString(), new ParseSecurityGroupTest().expected().toString());
|
||||
}
|
||||
|
||||
public void testGetSecurityGroupWhenResponseIs404() throws Exception {
|
||||
HttpRequest getSecurityGroup = HttpRequest
|
||||
.builder()
|
||||
.method("GET")
|
||||
.endpoint(URI.create("https://compute.north.host/v1.1/3456/os-security-groups/0"))
|
||||
.headers(
|
||||
ImmutableMultimap.<String, String> builder().put("Accept", "application/json")
|
||||
.put("X-Auth-Token", authToken).build()).build();
|
||||
HttpRequest getSecurityGroup = HttpRequest.builder().method("GET").endpoint(
|
||||
URI.create("https://compute.north.host/v1.1/3456/os-security-groups/0")).headers(
|
||||
ImmutableMultimap.<String, String> builder().put("Accept", "application/json").put("X-Auth-Token",
|
||||
authToken).build()).build();
|
||||
|
||||
HttpResponse getSecurityGroupResponse = HttpResponse.builder().statusCode(404).build();
|
||||
|
||||
NovaClient clientWhenNoSecurityGroupsExist = requestsSendResponses(keystoneAuthWithAccessKeyAndSecretKey,
|
||||
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, getSecurityGroup,
|
||||
getSecurityGroupResponse);
|
||||
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, getSecurityGroup,
|
||||
getSecurityGroupResponse);
|
||||
|
||||
assertNull(clientWhenNoSecurityGroupsExist.getSecurityGroupExtensionForZone("az-1.region-a.geo-1").get()
|
||||
.getSecurityGroup("0"));
|
||||
.getSecurityGroup("0"));
|
||||
|
||||
}
|
||||
|
||||
public void testCreateSecurityGroupWhenResponseIs2xx() throws Exception {
|
||||
HttpRequest createSecurityGroup = HttpRequest
|
||||
.builder()
|
||||
.method("POST")
|
||||
.endpoint(URI.create("https://compute.north.host/v1.1/3456/os-security-groups"))
|
||||
.headers(
|
||||
ImmutableMultimap.<String, String> builder().put("Accept", "application/json")
|
||||
.put("X-Auth-Token", authToken).build())
|
||||
.payload(
|
||||
payloadFromStringWithContentType(
|
||||
"{\"security_group\":{\"name\":\"name\",\"description\":\"description\"}}", "application/json"))
|
||||
.build();
|
||||
HttpRequest createSecurityGroup = HttpRequest.builder().method("POST").endpoint(
|
||||
URI.create("https://compute.north.host/v1.1/3456/os-security-groups")).headers(
|
||||
ImmutableMultimap.<String, String> builder().put("Accept", "application/json").put("X-Auth-Token",
|
||||
authToken).build())
|
||||
.payload(
|
||||
payloadFromStringWithContentType(
|
||||
"{\"security_group\":{\"name\":\"name\",\"description\":\"description\"}}",
|
||||
"application/json")).build();
|
||||
|
||||
HttpResponse createSecurityGroupResponse = HttpResponse.builder().statusCode(200)
|
||||
.payload(payloadFromResource("/securitygroup_created.json")).build();
|
||||
HttpResponse createSecurityGroupResponse = HttpResponse.builder().statusCode(200).payload(
|
||||
payloadFromResource("/securitygroup_created.json")).build();
|
||||
|
||||
NovaClient clientWhenSecurityGroupsExist = requestsSendResponses(keystoneAuthWithAccessKeyAndSecretKey,
|
||||
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, createSecurityGroup,
|
||||
createSecurityGroupResponse);
|
||||
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, createSecurityGroup,
|
||||
createSecurityGroupResponse);
|
||||
|
||||
assertEquals(
|
||||
clientWhenSecurityGroupsExist.getSecurityGroupExtensionForZone("az-1.region-a.geo-1").get()
|
||||
.createSecurityGroupWithNameAndDescription("name", "description").toString(), createSecurityGroupExpected().toString());
|
||||
assertEquals(clientWhenSecurityGroupsExist.getSecurityGroupExtensionForZone("az-1.region-a.geo-1").get()
|
||||
.createSecurityGroupWithNameAndDescription("name", "description").toString(),
|
||||
createSecurityGroupExpected().toString());
|
||||
}
|
||||
|
||||
public void testDeleteSecurityGroupWhenResponseIs2xx() throws Exception {
|
||||
HttpRequest deleteSecurityGroup = HttpRequest
|
||||
.builder()
|
||||
.method("DELETE")
|
||||
.endpoint(URI.create("https://compute.north.host/v1.1/3456/os-security-groups/160"))
|
||||
.headers(
|
||||
ImmutableMultimap.<String, String> builder().put("Accept", "*/*").put("X-Auth-Token", authToken)
|
||||
.build()).build();
|
||||
HttpRequest deleteSecurityGroup = HttpRequest.builder().method("DELETE").endpoint(
|
||||
URI.create("https://compute.north.host/v1.1/3456/os-security-groups/160"))
|
||||
.headers(
|
||||
ImmutableMultimap.<String, String> builder().put("Accept", "*/*")
|
||||
.put("X-Auth-Token", authToken).build()).build();
|
||||
|
||||
HttpResponse deleteSecurityGroupResponse = HttpResponse.builder().statusCode(202).build();
|
||||
|
||||
NovaClient clientWhenServersExist = requestsSendResponses(keystoneAuthWithAccessKeyAndSecretKey,
|
||||
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, deleteSecurityGroup,
|
||||
deleteSecurityGroupResponse);
|
||||
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, deleteSecurityGroup,
|
||||
deleteSecurityGroupResponse);
|
||||
|
||||
assertTrue(clientWhenServersExist.getSecurityGroupExtensionForZone("az-1.region-a.geo-1").get().deleteSecurityGroup("160"));
|
||||
assertTrue(clientWhenServersExist.getSecurityGroupExtensionForZone("az-1.region-a.geo-1").get()
|
||||
.deleteSecurityGroup("160"));
|
||||
|
||||
}
|
||||
|
||||
public void testCreateSecurityGroupRuleWhenResponseIs2xx() throws Exception {
|
||||
public void testCreateSecurityGroupRuleForCidrBlockWhenResponseIs2xx() throws Exception {
|
||||
HttpRequest createSecurityGroupRule = HttpRequest
|
||||
.builder()
|
||||
.method("POST")
|
||||
.endpoint(URI.create("https://compute.north.host/v1.1/3456/os-security-group-rules"))
|
||||
.headers(
|
||||
ImmutableMultimap.<String, String> builder().put("Accept", "application/json")
|
||||
.put("X-Auth-Token", authToken).build())
|
||||
.payload(
|
||||
payloadFromStringWithContentType(
|
||||
"{\"security_group_rule\":{\"ip_protocol\":\"tcp\",\"from_port\":\"80\",\"to_port\":\"8080\",\"cidr\":\"0.0.0.0/0\",\"group_id\":\"\",\"parent_group_id\":\"161\"}}",
|
||||
"application/json")).build();
|
||||
.builder()
|
||||
.method("POST")
|
||||
.endpoint(URI.create("https://compute.north.host/v1.1/3456/os-security-group-rules"))
|
||||
.headers(
|
||||
ImmutableMultimap.<String, String> builder().put("Accept", "application/json").put(
|
||||
"X-Auth-Token", authToken).build())
|
||||
.payload(
|
||||
payloadFromStringWithContentType(
|
||||
"{\"security_group_rule\":{\"parent_group_id\":\"161\",\"cidr\":\"0.0.0.0/0\",\"ip_protocol\":\"tcp\",\"from_port\":\"80\",\"to_port\":\"8080\"}}",
|
||||
"application/json")).build();
|
||||
|
||||
HttpResponse createSecurityGroupRuleResponse = HttpResponse.builder().statusCode(200)
|
||||
.payload(payloadFromResource("/securitygrouprule_created.json")).build();
|
||||
HttpResponse createSecurityGroupRuleResponse = HttpResponse.builder().statusCode(200).payload(
|
||||
payloadFromResource("/securitygrouprule_created.json")).build();
|
||||
|
||||
NovaClient clientWhenSecurityGroupsExist = requestsSendResponses(keystoneAuthWithAccessKeyAndSecretKey,
|
||||
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, createSecurityGroupRule,
|
||||
createSecurityGroupRuleResponse);
|
||||
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, createSecurityGroupRule,
|
||||
createSecurityGroupRuleResponse);
|
||||
|
||||
assertEquals(clientWhenSecurityGroupsExist.getSecurityGroupExtensionForZone("az-1.region-a.geo-1").get()
|
||||
.createSecurityGroupRule("tcp", "80", "8080", "0.0.0.0/0", "", "161").toString(),
|
||||
createSecurityGroupRuleExpected().toString());
|
||||
.createSecurityGroupRuleAllowingCidrBlock("161",
|
||||
Ingress.builder().ipProtocol(IpProtocol.TCP).fromPort(80).toPort(8080).build(), "0.0.0.0/0")
|
||||
.toString(), createSecurityGroupRuleExpected().toString());
|
||||
}
|
||||
|
||||
public void testCreateSecurityGroupRuleForSecurityGroupIdWhenResponseIs2xx() throws Exception {
|
||||
HttpRequest createSecurityGroupRule = HttpRequest
|
||||
.builder()
|
||||
.method("POST")
|
||||
.endpoint(URI.create("https://compute.north.host/v1.1/3456/os-security-group-rules"))
|
||||
.headers(
|
||||
ImmutableMultimap.<String, String> builder().put("Accept", "application/json").put(
|
||||
"X-Auth-Token", authToken).build())
|
||||
.payload(
|
||||
payloadFromStringWithContentType(
|
||||
"{\"security_group_rule\":{\"group_id\":\"999\",\"parent_group_id\":\"161\",\"ip_protocol\":\"tcp\",\"from_port\":\"80\",\"to_port\":\"8080\"}}",
|
||||
"application/json")).build();
|
||||
|
||||
HttpResponse createSecurityGroupRuleResponse = HttpResponse.builder().statusCode(200).payload(
|
||||
payloadFromResource("/securitygrouprule_created.json")).build();
|
||||
|
||||
NovaClient clientWhenSecurityGroupsExist = requestsSendResponses(keystoneAuthWithAccessKeyAndSecretKey,
|
||||
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, createSecurityGroupRule,
|
||||
createSecurityGroupRuleResponse);
|
||||
|
||||
assertEquals(clientWhenSecurityGroupsExist.getSecurityGroupExtensionForZone("az-1.region-a.geo-1").get()
|
||||
.createSecurityGroupRuleAllowingSecurityGroupId("161",
|
||||
Ingress.builder().ipProtocol(IpProtocol.TCP).fromPort(80).toPort(8080).build(), "999")
|
||||
.toString(), createSecurityGroupRuleExpected().toString());
|
||||
}
|
||||
|
||||
public void testDeleteSecurityGroupRuleWhenResponseIs2xx() throws Exception {
|
||||
HttpRequest deleteSecurityGroupRule = HttpRequest
|
||||
.builder()
|
||||
.method("DELETE")
|
||||
.endpoint(URI.create("https://compute.north.host/v1.1/3456/os-security-group-rules/161"))
|
||||
.headers(
|
||||
ImmutableMultimap.<String, String> builder().put("Accept", "*/*").put("X-Auth-Token", authToken)
|
||||
.build()).build();
|
||||
HttpRequest deleteSecurityGroupRule = HttpRequest.builder().method("DELETE").endpoint(
|
||||
URI.create("https://compute.north.host/v1.1/3456/os-security-group-rules/161"))
|
||||
.headers(
|
||||
ImmutableMultimap.<String, String> builder().put("Accept", "*/*")
|
||||
.put("X-Auth-Token", authToken).build()).build();
|
||||
|
||||
HttpResponse deleteSecurityGroupRuleResponse = HttpResponse.builder().statusCode(202).build();
|
||||
|
||||
NovaClient clientWhenSecurityGroupsExist = requestsSendResponses(keystoneAuthWithAccessKeyAndSecretKey,
|
||||
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, deleteSecurityGroupRule,
|
||||
deleteSecurityGroupRuleResponse);
|
||||
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, deleteSecurityGroupRule,
|
||||
deleteSecurityGroupRuleResponse);
|
||||
|
||||
assertTrue(clientWhenSecurityGroupsExist.getSecurityGroupExtensionForZone("az-1.region-a.geo-1").get()
|
||||
.deleteSecurityGroupRule("161"));
|
||||
.deleteSecurityGroupRule("161"));
|
||||
|
||||
}
|
||||
|
||||
private SecurityGroup createSecurityGroupExpected() {
|
||||
return SecurityGroup.builder().description("description").id("160").name("name")
|
||||
.rules(ImmutableSet.<SecurityGroupRule> of()).tenantId("dev_16767499955063").build();
|
||||
return SecurityGroup.builder().description("description").id("160").name("name").rules(
|
||||
ImmutableSet.<SecurityGroupRule> of()).tenantId("dev_16767499955063").build();
|
||||
}
|
||||
|
||||
private SecurityGroupRule createSecurityGroupRuleExpected() {
|
||||
return SecurityGroupRule.builder().fromPort(80).group(ImmutableMap.<String, String> of()).id("218")
|
||||
.ipProtocol(SecurityGroupRule.IpProtocol.TCP).ipRange(ImmutableMap.of("cidr", "0.0.0.0/0"))
|
||||
.parentGroupId("161").toPort(8080).build();
|
||||
return SecurityGroupRule.builder().fromPort(80).group(ImmutableMap.<String, String> of()).id("218").ipProtocol(
|
||||
IpProtocol.TCP).ipRange(ImmutableMap.of("cidr", "0.0.0.0/0")).parentGroupId("161").toPort(8080).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -22,6 +22,8 @@ import static org.testng.Assert.assertNotNull;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.jclouds.openstack.nova.v1_1.domain.Ingress;
|
||||
import org.jclouds.openstack.nova.v1_1.domain.IpProtocol;
|
||||
import org.jclouds.openstack.nova.v1_1.domain.SecurityGroup;
|
||||
import org.jclouds.openstack.nova.v1_1.domain.SecurityGroupRule;
|
||||
import org.jclouds.openstack.nova.v1_1.internal.BaseNovaClientLiveTest;
|
||||
@ -75,14 +77,16 @@ public class SecurityGroupClientLiveTest extends BaseNovaClientLiveTest {
|
||||
.createSecurityGroupWithNameAndDescription(SECURITY_GROUP_NAME, "test security group");
|
||||
assertNotNull(securityGroup);
|
||||
|
||||
SecurityGroupRule rule = client.createSecurityGroupRule("tcp", "443", "443", "0.0.0.0/0", "", securityGroup
|
||||
.getId());
|
||||
SecurityGroupRule rule = client.createSecurityGroupRuleAllowingCidrBlock(securityGroup.getId(), Ingress
|
||||
.builder().ipProtocol(IpProtocol.TCP).fromPort(443).toPort(443).build(), "0.0.0.0/0");
|
||||
assertNotNull(rule);
|
||||
|
||||
SecurityGroupRule rule2 = client.createSecurityGroupRule("tcp", "443", "443", "", securityGroup.getId(),
|
||||
securityGroup.getId());
|
||||
assertNotNull(rule2);
|
||||
SecurityGroupRule rule2 = client.createSecurityGroupRuleAllowingSecurityGroupId(securityGroup.getId(),
|
||||
Ingress.builder().ipProtocol(IpProtocol.TCP).fromPort(443).toPort(443).build(), securityGroup
|
||||
.getId());
|
||||
|
||||
assertNotNull(rule2);
|
||||
|
||||
securityGroup = client.getSecurityGroup(securityGroup.getId());
|
||||
|
||||
} finally {
|
||||
|
@ -28,6 +28,7 @@ import javax.ws.rs.core.MediaType;
|
||||
import org.jclouds.json.BaseSetParserTest;
|
||||
import org.jclouds.json.config.GsonModule;
|
||||
import org.jclouds.openstack.nova.v1_1.config.NovaParserModule;
|
||||
import org.jclouds.openstack.nova.v1_1.domain.IpProtocol;
|
||||
import org.jclouds.openstack.nova.v1_1.domain.SecurityGroup;
|
||||
import org.jclouds.openstack.nova.v1_1.domain.SecurityGroupRule;
|
||||
import org.jclouds.rest.annotations.SelectJson;
|
||||
@ -59,13 +60,13 @@ public class ParseSecurityGroupListTest extends BaseSetParserTest<SecurityGroup>
|
||||
|
||||
Set<SecurityGroupRule> securityGroupRules = ImmutableSet.<SecurityGroupRule> of(
|
||||
SecurityGroupRule.builder().fromPort(22).group(new HashMap<String, String>())
|
||||
.ipProtocol(SecurityGroupRule.IpProtocol.TCP).toPort(22).parentGroupId("3").ipRange(anyCidr)
|
||||
.ipProtocol(IpProtocol.TCP).toPort(22).parentGroupId("3").ipRange(anyCidr)
|
||||
.id("107").build(),
|
||||
SecurityGroupRule.builder().fromPort(7600).group(new HashMap<String, String>())
|
||||
.ipProtocol(SecurityGroupRule.IpProtocol.TCP).toPort(7600).parentGroupId("3").ipRange(anyCidr)
|
||||
.ipProtocol(IpProtocol.TCP).toPort(7600).parentGroupId("3").ipRange(anyCidr)
|
||||
.id("118").build(),
|
||||
SecurityGroupRule.builder().fromPort(8084).group(new HashMap<String, String>())
|
||||
.ipProtocol(SecurityGroupRule.IpProtocol.TCP).toPort(8084).parentGroupId("3").ipRange(anyCidr)
|
||||
.ipProtocol(IpProtocol.TCP).toPort(8084).parentGroupId("3").ipRange(anyCidr)
|
||||
.id("119").build());
|
||||
|
||||
return ImmutableSet.of(SecurityGroup.builder().description("description1").id("1").tenantId("tenant1")
|
||||
|
@ -27,6 +27,7 @@ import javax.ws.rs.core.MediaType;
|
||||
import org.jclouds.json.BaseItemParserTest;
|
||||
import org.jclouds.json.config.GsonModule;
|
||||
import org.jclouds.openstack.nova.v1_1.config.NovaParserModule;
|
||||
import org.jclouds.openstack.nova.v1_1.domain.IpProtocol;
|
||||
import org.jclouds.openstack.nova.v1_1.domain.SecurityGroup;
|
||||
import org.jclouds.openstack.nova.v1_1.domain.SecurityGroupRule;
|
||||
import org.jclouds.rest.annotations.SelectJson;
|
||||
@ -56,10 +57,10 @@ public class ParseSecurityGroupTest extends BaseItemParserTest<SecurityGroup> {
|
||||
|
||||
Set<SecurityGroupRule> securityGroupRules = ImmutableSet.<SecurityGroupRule> of(
|
||||
SecurityGroupRule.builder().fromPort(22).group(new HashMap<String, String>())
|
||||
.ipProtocol(SecurityGroupRule.IpProtocol.TCP).toPort(22).parentGroupId("28")
|
||||
.ipProtocol(IpProtocol.TCP).toPort(22).parentGroupId("28")
|
||||
.ipRange(ImmutableMap.of("cidr", "10.2.6.0/24")).id("108").build(),
|
||||
SecurityGroupRule.builder().fromPort(22).group(ImmutableMap.of("tenant_id", "admin", "name", "11111"))
|
||||
.ipProtocol(SecurityGroupRule.IpProtocol.TCP).toPort(22).parentGroupId("28")
|
||||
.ipProtocol(IpProtocol.TCP).toPort(22).parentGroupId("28")
|
||||
.ipRange(new HashMap<String, String>()).id("109").build());
|
||||
|
||||
return SecurityGroup.builder().description("description0").id("0").tenantId("tenant0").rules(securityGroupRules)
|
||||
|
Loading…
x
Reference in New Issue
Block a user