diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/ProfitBricksApi.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/ProfitBricksApi.java index af7a31d252..34309bf2cc 100644 --- a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/ProfitBricksApi.java +++ b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/ProfitBricksApi.java @@ -20,6 +20,7 @@ import java.io.Closeable; import org.jclouds.profitbricks.features.DataCenterApi; import org.jclouds.profitbricks.features.FirewallApi; import org.jclouds.profitbricks.features.ImageApi; +import org.jclouds.profitbricks.features.IpBlockApi; import org.jclouds.profitbricks.features.NicApi; import org.jclouds.profitbricks.features.ServerApi; import org.jclouds.profitbricks.features.SnapshotApi; @@ -48,4 +49,8 @@ public interface ProfitBricksApi extends Closeable { @Delegate SnapshotApi snapshotApi(); + + @Delegate + IpBlockApi ipBlockApi(); + } diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/domain/IpBlock.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/domain/IpBlock.java new file mode 100644 index 0000000000..47741ffc1d --- /dev/null +++ b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/domain/IpBlock.java @@ -0,0 +1,125 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.profitbricks.domain; + +import com.google.auto.value.AutoValue; +import com.google.common.collect.ImmutableList; +import java.util.List; +import org.jclouds.javax.annotation.Nullable; + +@AutoValue +public abstract class IpBlock { + + public abstract String id(); + + public abstract Location location(); + + public abstract List publicIps(); + + public abstract List ips(); + + public static IpBlock create(String id, Location location, List publicIps, List ips) { + return new AutoValue_IpBlock(id, location, publicIps, ips != null ? ImmutableList.copyOf(ips) : ImmutableList.of()); + } + + public static Builder builder() { + return new Builder(); + } + + public static final class Builder { + + private String id; + private Location location; + private List publicIps; + private List ips; + + public Builder id(String id) { + this.id = id; + return this; + } + + public Builder location(Location location) { + this.location = location; + return this; + } + + public Builder publicIps(List publicIps) { + this.publicIps = publicIps; + return this; + } + + public Builder ips(List ips) { + this.ips = ips; + return this; + } + + public IpBlock build() { + return IpBlock.create(id, location, publicIps, ips); + } + + public Builder fromIpBlock(IpBlock in) { + return this.id(in.id()).location(in.location()).publicIps(in.publicIps()).ips(in.ips()); + } + + } + + @AutoValue + public abstract static class PublicIp { + + public abstract String ip(); + + @Nullable + public abstract String nicId(); + + public static PublicIp create(String ip, String nicId) { + return new AutoValue_IpBlock_PublicIp(ip, nicId); + } + + public static Builder builder() { + return new Builder(); + } + + public Builder toBuilder() { + return builder().fromPublicIp(this); + } + + public static final class Builder { + + private String ip; + private String nicId; + + public Builder ip(String ip) { + this.ip = ip; + return this; + } + + public Builder nicId(String nicId) { + this.nicId = nicId; + return this; + } + + public PublicIp build() { + return PublicIp.create(ip, nicId); + } + + public Builder fromPublicIp(PublicIp in) { + return this.ip(in.ip()).nicId(in.nicId()); + } + } + } + +} diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/features/IpBlockApi.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/features/IpBlockApi.java new file mode 100644 index 0000000000..a81293e421 --- /dev/null +++ b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/features/IpBlockApi.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.profitbricks.features; + +import java.util.List; +import javax.inject.Named; +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import org.jclouds.Fallbacks; +import org.jclouds.http.filters.BasicAuthentication; +import org.jclouds.profitbricks.domain.IpBlock; +import org.jclouds.profitbricks.http.filters.ProfitBricksSoapMessageEnvelope; +import org.jclouds.profitbricks.http.parser.RequestIdOnlyResponseHandler; +import org.jclouds.profitbricks.http.parser.ipblock.IpBlockListResponseHandler; +import org.jclouds.profitbricks.http.parser.ipblock.IpBlockResponseHandler; +import org.jclouds.rest.annotations.Fallback; +import org.jclouds.rest.annotations.Payload; +import org.jclouds.rest.annotations.PayloadParam; +import org.jclouds.rest.annotations.RequestFilters; +import org.jclouds.rest.annotations.XMLResponseParser; + +@RequestFilters({BasicAuthentication.class, ProfitBricksSoapMessageEnvelope.class}) +@Consumes(MediaType.TEXT_XML) +@Produces(MediaType.TEXT_XML) +public interface IpBlockApi { + + @POST + @Named("publicipblock:get") + @Payload("{id}") + @XMLResponseParser(IpBlockResponseHandler.class) + @Fallback(Fallbacks.NullOnNotFoundOr404.class) + IpBlock getIpBlock(@PayloadParam("id") String identifier); + + @POST + @Named("publicipblock:getall") + @Payload("") + @XMLResponseParser(IpBlockListResponseHandler.class) + @Fallback(Fallbacks.EmptyListOnNotFoundOr404.class) + List getAllIpBlock(); + + @POST + @Named("publicipblock:reserve") + @Payload("{blockSize}{location}") + @XMLResponseParser(IpBlockResponseHandler.class) + IpBlock reservePublicIpBlock(@PayloadParam("blockSize") String blockSize, @PayloadParam("location") String location); + + @POST + @Named("publicipblock:addip") + @Payload("{ip}{nicid}") + @XMLResponseParser(RequestIdOnlyResponseHandler.class) + String addPublicIpToNic(@PayloadParam("ip") String ip, @PayloadParam("nicid") String nicid); + + @POST + @Named("publicipblock:removeip") + @Payload("{ip}{nicid}") + @XMLResponseParser(RequestIdOnlyResponseHandler.class) + String removePublicIpFromNic(@PayloadParam("ip") String ip, @PayloadParam("nicid") String nicid); + + @POST + @Named("publicipblock:releaseblock") + @Payload("{blockid}") + @XMLResponseParser(RequestIdOnlyResponseHandler.class) + String releasePublicIpBlock(@PayloadParam("blockid") String blockid); +} diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/http/parser/ipblock/BaseIpBlockResponseHandler.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/http/parser/ipblock/BaseIpBlockResponseHandler.java new file mode 100644 index 0000000000..a4af29f60f --- /dev/null +++ b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/http/parser/ipblock/BaseIpBlockResponseHandler.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.profitbricks.http.parser.ipblock; + +import com.google.common.collect.Lists; +import java.util.List; +import org.jclouds.profitbricks.domain.IpBlock; +import org.jclouds.profitbricks.domain.Location; +import org.jclouds.profitbricks.http.parser.BaseProfitBricksResponseHandler; +import org.jclouds.profitbricks.http.parser.publicip.PublicIpListResponseHandler; +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; + +public abstract class BaseIpBlockResponseHandler extends BaseProfitBricksResponseHandler { + + protected final PublicIpListResponseHandler publicIpListResponseHandler; + protected final List publicIps = Lists.newArrayList(); + protected List ips; + + protected IpBlock.Builder builder; + protected boolean usePublicIpListParser = false; + + BaseIpBlockResponseHandler(PublicIpListResponseHandler publicIpListResponseHandler) { + this.builder = IpBlock.builder(); + this.publicIpListResponseHandler = publicIpListResponseHandler; + ips = Lists.newArrayList(); + } + + @Override + public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + if ("publicIps".equals(qName)) + usePublicIpListParser = true; + if (usePublicIpListParser) + publicIpListResponseHandler.startElement(uri, localName, qName, attributes); + } + + @Override + public void characters(char[] ch, int start, int length) { + if (usePublicIpListParser) + publicIpListResponseHandler.characters(ch, start, length); + else + super.characters(ch, start, length); + } + + @Override + protected void setPropertyOnEndTag(String qName) { + if ("blockId".equals(qName)) + builder.id(textToStringValue()); + else if ("location".equals(qName)) + builder.location(Location.fromId(textToStringValue())); + else if ("ips".equals(qName)) + ips.add(textToStringValue()); + } +} diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/http/parser/ipblock/IpBlockListResponseHandler.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/http/parser/ipblock/IpBlockListResponseHandler.java new file mode 100644 index 0000000000..c56f0eafd6 --- /dev/null +++ b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/http/parser/ipblock/IpBlockListResponseHandler.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.profitbricks.http.parser.ipblock; + +import autovalue.shaded.com.google.common.common.collect.Lists; +import com.google.inject.Inject; +import java.util.List; +import org.jclouds.profitbricks.domain.IpBlock; +import org.jclouds.profitbricks.http.parser.publicip.PublicIpListResponseHandler; +import org.xml.sax.SAXException; + +public class IpBlockListResponseHandler extends BaseIpBlockResponseHandler> { + + private final List ipBlocks; + + @Inject + IpBlockListResponseHandler(PublicIpListResponseHandler publicIpListResponseHandler) { + super(publicIpListResponseHandler); + ipBlocks = Lists.newArrayList(); + } + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { + if (usePublicIpListParser) + publicIpListResponseHandler.endElement(uri, localName, qName); + else { + setPropertyOnEndTag(qName); + if ("return".equals(qName)) { + ipBlocks.add(builder + .publicIps(publicIpListResponseHandler.getResult()) + .build()); + publicIpListResponseHandler.reset(); + builder = IpBlock.builder(); + } + clearTextBuffer(); + } + + if ("publicIps".equals(qName)) + usePublicIpListParser = false; + + } + + @Override + public List getResult() { + return ipBlocks; + } + +} diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/http/parser/ipblock/IpBlockResponseHandler.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/http/parser/ipblock/IpBlockResponseHandler.java new file mode 100644 index 0000000000..307db14ba3 --- /dev/null +++ b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/http/parser/ipblock/IpBlockResponseHandler.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.profitbricks.http.parser.ipblock; + +import com.google.inject.Inject; +import org.jclouds.profitbricks.domain.IpBlock; +import org.jclouds.profitbricks.http.parser.publicip.PublicIpListResponseHandler; +import org.xml.sax.SAXException; + +public class IpBlockResponseHandler extends BaseIpBlockResponseHandler { + + private boolean done = false; + + @Inject + IpBlockResponseHandler(PublicIpListResponseHandler publicIpListResponseHandler) { + super(publicIpListResponseHandler); + } + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { + if (done) + return; + + if (usePublicIpListParser) + publicIpListResponseHandler.endElement(uri, localName, qName); + else { + setPropertyOnEndTag(qName); + if ("return".equals(qName)) { + done = true; + builder.publicIps(publicIpListResponseHandler.getResult()); + builder.ips(ips); + } + clearTextBuffer(); + } + + if ("publicIps".equals(qName)) + usePublicIpListParser = false; + } + + @Override + public void reset() { + this.builder = IpBlock.builder(); + } + + @Override + public IpBlock getResult() { + return builder.build(); + } + +} diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/http/parser/publicip/BasePublicIpResponseHandler.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/http/parser/publicip/BasePublicIpResponseHandler.java new file mode 100644 index 0000000000..8561aad6e0 --- /dev/null +++ b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/http/parser/publicip/BasePublicIpResponseHandler.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.profitbricks.http.parser.publicip; + +import org.jclouds.profitbricks.domain.IpBlock; +import org.jclouds.profitbricks.http.parser.BaseProfitBricksResponseHandler; + +public abstract class BasePublicIpResponseHandler extends BaseProfitBricksResponseHandler { + + protected IpBlock.PublicIp.Builder builder; + + BasePublicIpResponseHandler() { + this.builder = IpBlock.PublicIp.builder(); + } + + @Override + protected void setPropertyOnEndTag(String qName) { + if ("ip".equals(qName)) + builder.ip(textToStringValue()); + else if ("nicId".equals(qName)) + builder.nicId(textToStringValue()); + + } +} diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/http/parser/publicip/PublicIpListResponseHandler.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/http/parser/publicip/PublicIpListResponseHandler.java new file mode 100644 index 0000000000..c68e7e67e7 --- /dev/null +++ b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/http/parser/publicip/PublicIpListResponseHandler.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.profitbricks.http.parser.publicip; + +import com.google.common.collect.Lists; +import java.util.List; +import org.jclouds.profitbricks.domain.IpBlock.PublicIp; +import org.xml.sax.SAXException; + +public class PublicIpListResponseHandler extends BasePublicIpResponseHandler> { + + private List publicIps; + + PublicIpListResponseHandler() { + this.publicIps = Lists.newArrayList(); + } + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { + setPropertyOnEndTag(qName); + + if ("publicIps".equals(qName)) + publicIps.add(builder.build()); + clearTextBuffer(); + } + + @Override + public void reset() { + this.publicIps = Lists.newArrayList(); + } + + @Override + public List getResult() { + return publicIps; + } + +} diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/IpBlockApiLiveTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/IpBlockApiLiveTest.java new file mode 100644 index 0000000000..227cfc7ee6 --- /dev/null +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/IpBlockApiLiveTest.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.profitbricks.features; + +import com.google.common.collect.Iterables; +import java.util.List; +import org.jclouds.profitbricks.BaseProfitBricksLiveTest; +import org.jclouds.profitbricks.domain.IpBlock; +import org.jclouds.profitbricks.domain.Location; +import org.jclouds.profitbricks.domain.Nic; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNotNull; +import org.testng.annotations.Test; + +@Test(groups = "live", testName = "IpBlockApiLiveTest", singleThreaded = true) +public class IpBlockApiLiveTest extends BaseProfitBricksLiveTest { + + private String nicid; + private IpBlock newIpBlock; + + @Override + public void initialize() { + super.initialize(); + + List nics = api.nicApi().getAllNics(); + + assertFalse(nics.isEmpty(), "At least one NIC is requred to test IpBlocks"); + + Nic nic = Iterables.getFirst(nics, null); + + nicid = nic.id(); + } + + @Test + public void testReservePublicIpBlock() { + newIpBlock = api.ipBlockApi().reservePublicIpBlock("2", Location.US_LAS.value()); + + assertNotNull(newIpBlock); + assertNotNull(newIpBlock.ips()); + assertFalse(newIpBlock.ips().isEmpty()); + } + + @Test + public void testGetAllIpBlocks() { + List ipBlocks = api.ipBlockApi().getAllIpBlock(); + + assertNotNull(ipBlocks); + assertFalse(ipBlocks.isEmpty()); + } + + @Test(dependsOnMethods = "testReservePublicIpBlock") + public void testGetOneIpBlock() { + IpBlock ipBlock = api.ipBlockApi().getIpBlock(newIpBlock.id()); + + assertNotNull(ipBlock); + } + + @Test(dependsOnMethods = "testReservePublicIpBlock") + public void testAddPublicIpToNic() { + String requestId = api.ipBlockApi().addPublicIpToNic(newIpBlock.ips().get(0), nicid); + + assertNotNull(requestId); + } + + @Test(dependsOnMethods = "testAddPublicIpToNic") + public void testRemovePublicIpFromNic() { + String requestId = api.ipBlockApi().removePublicIpFromNic(newIpBlock.ips().get(0), nicid); + + assertNotNull(requestId); + } + + @Test(dependsOnMethods = "testRemovePublicIpFromNic") + public void testReleasePublicIpBlock() { + String requestId = api.ipBlockApi().releasePublicIpBlock(newIpBlock.id()); + + assertNotNull(requestId); + } +} diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/IpBlockApiMockTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/IpBlockApiMockTest.java new file mode 100644 index 0000000000..b0d33561c5 --- /dev/null +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/IpBlockApiMockTest.java @@ -0,0 +1,200 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.profitbricks.features; + +import com.squareup.okhttp.mockwebserver.MockResponse; +import com.squareup.okhttp.mockwebserver.MockWebServer; +import java.util.List; +import org.jclouds.profitbricks.ProfitBricksApi; +import org.jclouds.profitbricks.domain.IpBlock; +import org.jclouds.profitbricks.domain.Location; +import org.jclouds.profitbricks.internal.BaseProfitBricksMockTest; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; +import org.testng.annotations.Test; + +@Test(groups = "unit", testName = "IpBlockApiMockTest") +public class IpBlockApiMockTest extends BaseProfitBricksMockTest { + + @Test + public void testGetOneIpBlock() throws Exception { + MockWebServer server = mockWebServer(); + server.enqueue(new MockResponse().setBody(payloadFromResource("/ipblock/ipblock.xml"))); + + ProfitBricksApi pbApi = api(server.getUrl(rootUrl)); + IpBlockApi api = pbApi.ipBlockApi(); + + String id = "qwertyui-qwer-qwer-qwer-qwertyyuiiop"; + + String content = "" + id + ""; + + try { + IpBlock ipBlock = api.getIpBlock(id); + assertRequestHasCommonProperties(server.takeRequest(), content); + assertNotNull(ipBlock); + assertEquals(ipBlock.id(), id); + } finally { + pbApi.close(); + server.shutdown(); + } + } + + @Test + public void testGetNonExisingIpBlock() throws Exception { + MockWebServer server = mockWebServer(); + server.enqueue(new MockResponse().setResponseCode(404)); + + ProfitBricksApi pbApi = api(server.getUrl(rootUrl)); + IpBlockApi api = pbApi.ipBlockApi(); + + String id = "qwertyui-qwer-qwer-qwer-qwertyyuiiop"; + + try { + IpBlock ipBlock = api.getIpBlock(id); + assertRequestHasCommonProperties(server.takeRequest()); + assertNull(ipBlock); + } finally { + pbApi.close(); + server.shutdown(); + } + } + + @Test + public void testGetAllIpBlock() throws Exception { + MockWebServer server = mockWebServer(); + server.enqueue(new MockResponse().setBody(payloadFromResource("/ipblock/ipblocks.xml"))); + + ProfitBricksApi pbApi = api(server.getUrl(rootUrl)); + IpBlockApi api = pbApi.ipBlockApi(); + + try { + List ipBlocks = api.getAllIpBlock(); + assertRequestHasCommonProperties(server.takeRequest()); + assertNotNull(ipBlocks); + } finally { + pbApi.close(); + server.shutdown(); + } + } + + @Test + public void testGetAllIpBlockReturning404() throws Exception { + MockWebServer server = mockWebServer(); + server.enqueue(new MockResponse().setResponseCode(404)); + + ProfitBricksApi pbApi = api(server.getUrl(rootUrl)); + IpBlockApi api = pbApi.ipBlockApi(); + + try { + List ipBlocks = api.getAllIpBlock(); + assertRequestHasCommonProperties(server.takeRequest()); + assertTrue(ipBlocks.isEmpty()); + } finally { + pbApi.close(); + server.shutdown(); + } + } + + @Test + public void testReservePublicIpBlock() throws Exception { + MockWebServer server = mockWebServer(); + server.enqueue(new MockResponse().setBody(payloadFromResource("/ipblock/ipblock-reserve.xml"))); + + ProfitBricksApi pbApi = api(server.getUrl(rootUrl)); + IpBlockApi api = pbApi.ipBlockApi(); + + String blockSize = "2"; + Location location = Location.US_LAS; + + String content = "" + blockSize + "" + location.value() + ""; + try { + IpBlock ipBlock = api.reservePublicIpBlock(blockSize, location.value()); + assertRequestHasCommonProperties(server.takeRequest(), content); + assertNotNull(ipBlock); + } finally { + pbApi.close(); + server.shutdown(); + } + } + + @Test + public void testAddPublicIpToNic() throws Exception { + MockWebServer server = mockWebServer(); + server.enqueue(new MockResponse().setBody(payloadFromResource("/ipblock/ipblock-addtonic.xml"))); + + ProfitBricksApi pbApi = api(server.getUrl(rootUrl)); + IpBlockApi api = pbApi.ipBlockApi(); + + String ip = "2"; + String nicid = "nicid"; + + String content = "" + ip + "" + nicid + ""; + try { + String requestId = api.addPublicIpToNic(ip, nicid); + assertRequestHasCommonProperties(server.takeRequest(), content); + assertNotNull(requestId); + } finally { + pbApi.close(); + server.shutdown(); + } + } + + @Test + public void testRemovePublicIpFromNic() throws Exception { + MockWebServer server = mockWebServer(); + server.enqueue(new MockResponse().setBody(payloadFromResource("/ipblock/ipblock-removefromnic.xml"))); + + ProfitBricksApi pbApi = api(server.getUrl(rootUrl)); + IpBlockApi api = pbApi.ipBlockApi(); + + String ip = "2"; + String nicid = "nicid"; + + String content = "" + ip + "" + nicid + ""; + try { + String requestId = api.removePublicIpFromNic(ip, nicid); + assertRequestHasCommonProperties(server.takeRequest(), content); + assertNotNull(requestId); + } finally { + pbApi.close(); + server.shutdown(); + } + } + + @Test + public void testReleasePublicIpBlock() throws Exception { + MockWebServer server = mockWebServer(); + server.enqueue(new MockResponse().setBody(payloadFromResource("/ipblock/ipblock-release.xml"))); + + ProfitBricksApi pbApi = api(server.getUrl(rootUrl)); + IpBlockApi api = pbApi.ipBlockApi(); + + String blockid = "2"; + + String content = "" + blockid + ""; + try { + String requestId = api.releasePublicIpBlock(blockid); + assertRequestHasCommonProperties(server.takeRequest(), content); + assertNotNull(requestId); + } finally { + pbApi.close(); + server.shutdown(); + } + } +} diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/http/parser/ipblock/IpBlockListResponseHandlerTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/http/parser/ipblock/IpBlockListResponseHandlerTest.java new file mode 100644 index 0000000000..49dbaba89f --- /dev/null +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/http/parser/ipblock/IpBlockListResponseHandlerTest.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.profitbricks.http.parser.ipblock; + +import com.google.common.collect.ImmutableList; +import java.util.List; +import org.jclouds.http.functions.ParseSax; +import org.jclouds.profitbricks.domain.IpBlock; +import org.jclouds.profitbricks.domain.Location; +import org.jclouds.profitbricks.domain.IpBlock.PublicIp; +import org.jclouds.profitbricks.http.parser.BaseResponseHandlerTest; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import org.testng.annotations.Test; + +@Test(groups = "unit", testName = "IpBlockListResponseHandlerTest") +public class IpBlockListResponseHandlerTest extends BaseResponseHandlerTest> { + + @Override + protected ParseSax> createParser() { + return factory.create(injector.getInstance(IpBlockListResponseHandler.class)); + } + + @Test + public void testParseResponseFromGetAllIpBlock() { + ParseSax> parser = createParser(); + + List actual = parser.parse(payloadFromResource("/ipblock/ipblocks.xml")); + assertNotNull(actual, "Parsed content returned null"); + + List expected = ImmutableList.of( + IpBlock.builder() + .id("block-id") + .location(Location.US_LAS) + .publicIps(ImmutableList.of( + PublicIp.builder() + .ip("1.1") + .nicId("nic-id") + .build(), + PublicIp.builder() + .ip("1.2") + .nicId("nic-id") + .build())) + .build(), + IpBlock.builder() + .id("block-id") + .location(Location.US_LAS) + .publicIps(ImmutableList.of( + PublicIp.builder() + .ip("2.1") + .nicId("nic-id") + .build(), + PublicIp.builder() + .ip("2.2") + .nicId("nic-id") + .build())) + .build() + ); + + assertEquals(actual, expected); + } + +} diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/http/parser/ipblock/IpBlockResponseHandlerTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/http/parser/ipblock/IpBlockResponseHandlerTest.java new file mode 100644 index 0000000000..54f48dcbb0 --- /dev/null +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/http/parser/ipblock/IpBlockResponseHandlerTest.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.profitbricks.http.parser.ipblock; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; +import java.util.List; +import org.jclouds.http.functions.ParseSax; +import org.jclouds.profitbricks.domain.IpBlock; +import org.jclouds.profitbricks.domain.Location; +import org.jclouds.profitbricks.domain.IpBlock.PublicIp; +import org.jclouds.profitbricks.http.parser.BaseResponseHandlerTest; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import org.testng.annotations.Test; + +@Test(groups = "unit", testName = "IpBlockResponseHandlerTest") +public class IpBlockResponseHandlerTest extends BaseResponseHandlerTest { + + @Override + protected ParseSax createParser() { + return factory.create(injector.getInstance(IpBlockResponseHandler.class)); + } + + @Test + public void testParseResponseFromGetIpBlock() { + ParseSax parser = createParser(); + + IpBlock actual = parser.parse(payloadFromResource("/ipblock/ipblock.xml")); + assertNotNull(actual, "Parsed content returned null"); + List emptyIpList = Lists.newArrayList(); + + IpBlock expected = IpBlock.builder() + .id("qwertyui-qwer-qwer-qwer-qwertyyuiiop") + .location(Location.US_LAS) + .publicIps(ImmutableList.of( + PublicIp.builder() + .ip("ip") + .nicId("nic-id") + .build(), + PublicIp.builder() + .ip("ip") + .nicId("nic-id") + .build())) + .ips(emptyIpList) + .build(); + assertEquals(actual, expected); + } +} diff --git a/providers/profitbricks/src/test/resources/ipblock/ipblock-addtonic.xml b/providers/profitbricks/src/test/resources/ipblock/ipblock-addtonic.xml new file mode 100644 index 0000000000..a9c1974cd1 --- /dev/null +++ b/providers/profitbricks/src/test/resources/ipblock/ipblock-addtonic.xml @@ -0,0 +1,12 @@ + + + + + + request-id + datacenter-id + datacenter-version + + + + \ No newline at end of file diff --git a/providers/profitbricks/src/test/resources/ipblock/ipblock-release.xml b/providers/profitbricks/src/test/resources/ipblock/ipblock-release.xml new file mode 100644 index 0000000000..a9f22bf746 --- /dev/null +++ b/providers/profitbricks/src/test/resources/ipblock/ipblock-release.xml @@ -0,0 +1,10 @@ + + + + + + request-id + + + + \ No newline at end of file diff --git a/providers/profitbricks/src/test/resources/ipblock/ipblock-removefromnic.xml b/providers/profitbricks/src/test/resources/ipblock/ipblock-removefromnic.xml new file mode 100644 index 0000000000..a9c1974cd1 --- /dev/null +++ b/providers/profitbricks/src/test/resources/ipblock/ipblock-removefromnic.xml @@ -0,0 +1,12 @@ + + + + + + request-id + datacenter-id + datacenter-version + + + + \ No newline at end of file diff --git a/providers/profitbricks/src/test/resources/ipblock/ipblock-reserve.xml b/providers/profitbricks/src/test/resources/ipblock/ipblock-reserve.xml new file mode 100644 index 0000000000..863e9ff786 --- /dev/null +++ b/providers/profitbricks/src/test/resources/ipblock/ipblock-reserve.xml @@ -0,0 +1,13 @@ + + + + + + request-id + block-id + us/las + ip + + + + \ No newline at end of file diff --git a/providers/profitbricks/src/test/resources/ipblock/ipblock.xml b/providers/profitbricks/src/test/resources/ipblock/ipblock.xml new file mode 100644 index 0000000000..2dd54f2907 --- /dev/null +++ b/providers/profitbricks/src/test/resources/ipblock/ipblock.xml @@ -0,0 +1,18 @@ + + + + + + qwertyui-qwer-qwer-qwer-qwertyyuiiop + us/las + + ip + nic-id + + + ip + + + + + \ No newline at end of file diff --git a/providers/profitbricks/src/test/resources/ipblock/ipblocks.xml b/providers/profitbricks/src/test/resources/ipblock/ipblocks.xml new file mode 100644 index 0000000000..4e436e1667 --- /dev/null +++ b/providers/profitbricks/src/test/resources/ipblock/ipblocks.xml @@ -0,0 +1,30 @@ + + + + + + block-id + us/las + + 1.1 + nic-id + + + 1.2 + nic-id + + + + block-id + us/las + + 2.1 + + + 2.2 + nic-id + + + + + \ No newline at end of file