Skip to content
Permalink
Browse files
JCLOUDS-1138 oneandone-image-api
  • Loading branch information
alibazlamit authored and nacx committed Jul 22, 2016
1 parent 787af1d commit f997b06159e4d80690a391b1db10fbd75429b8b3
Show file tree
Hide file tree
Showing 14 changed files with 600 additions and 15 deletions.
@@ -17,11 +17,16 @@
package org.apache.jclouds.oneandone.rest;

import java.io.Closeable;
import org.apache.jclouds.oneandone.rest.features.ImageApi;
import org.apache.jclouds.oneandone.rest.features.ServerApi;
import org.jclouds.rest.annotations.Delegate;

public interface OneAndOneApi extends Closeable {

@Delegate
ServerApi serverApi();

@Delegate
ImageApi imageApi();

}
@@ -21,15 +21,14 @@

@AutoValue
public abstract class DataCenter {
public abstract String id();

public abstract String id();
public abstract String countryCode();

public abstract String countryCode();
public abstract String location();

public abstract String location();

@SerializedNames({"id", "country_code", "location"})
public static DataCenter create(String id, String countryCode, String location) {
return new AutoValue_DataCenter(id, countryCode, location);
}
@SerializedNames({"id", "country_code", "location"})
public static DataCenter create(String id, String countryCode, String location) {
return new AutoValue_DataCenter(id, countryCode, location);
}
}
@@ -21,13 +21,12 @@

@AutoValue
public abstract class Dvd {
public abstract String id();

public abstract String id();
public abstract String name();

public abstract String name();

@SerializedNames({"id", "name"})
public static Dvd create(String id, String name) {
return new AutoValue_Dvd(id, name);
}
@SerializedNames({"id", "name"})
public static Dvd create(String id, String name) {
return new AutoValue_Dvd(id, name);
}
}
@@ -28,4 +28,5 @@ public abstract class FixedInstanceHardware {
public static FixedInstanceHardware create(String fixedInstanceSizeId) {
return new AutoValue_FixedInstanceHardware(fixedInstanceSizeId);
}

}
@@ -68,4 +68,5 @@ public static UpdateHardware create(double vcore, double coresPerProcessor, doub
return new AutoValue_Hardware_UpdateHardware(vcore, coresPerProcessor, ram);
}
}

}
@@ -69,4 +69,5 @@ public static Hdd create(String unit, int size, boolean isMain) {
}
}
}

}
@@ -83,4 +83,75 @@ public static Image create(String id, String name, OSFamliyType osFamily, OSType
architecture, osImageType, type, minHddSize, licenses == null ? ImmutableList.<Licenses>of() : licenses, state,
description, hdds == null ? ImmutableList.<Hdd>of() : hdds, serverId, frequency, numImages, creationDate);
}

@AutoValue
public abstract static class CreateImage {

public abstract String serverId();

public abstract String name();

public abstract ImageFrequency frequency();

@Nullable
public abstract String description();

public abstract int numImages();

@Nullable
public abstract String datacenterId();

@SerializedNames({"server_id", "name", "frequency", "description", "num_images", "datacenter_id"})
public static CreateImage create(final String serverId, final String name, final ImageFrequency frequency, final String description, final int numImages,
final String dataCenterId) {
return builder()
.name(name)
.serverId(serverId)
.description(description)
.datacenterId(dataCenterId)
.frequency(frequency)
.numImages(numImages)
.build();
}

public static Builder builder() {
return new AutoValue_Image_CreateImage.Builder();
}

@AutoValue.Builder
public abstract static class Builder {

public abstract Builder name(String name);

public abstract Builder serverId(String serverId);

public abstract Builder frequency(ImageFrequency description);

public abstract Builder description(String description);

public abstract Builder numImages(int numImages);

public abstract Builder datacenterId(String datacenterId);

public abstract CreateImage build();
}
}

@AutoValue
public abstract static class UpdateImage {

@Nullable
public abstract String name();

@Nullable
public abstract String description();

@Nullable
public abstract ImageFrequency frequency();

@SerializedNames({"name", "description", "frequency"})
public static UpdateImage create(final String name, final String description, final ImageFrequency frequency) {
return new AutoValue_Image_UpdateImage(name, description, frequency);
}
}
}
@@ -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.apache.jclouds.oneandone.rest.features;

import java.io.Closeable;
import java.util.List;
import javax.inject.Named;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import org.apache.jclouds.oneandone.rest.domain.Image;
import org.apache.jclouds.oneandone.rest.domain.options.GenericQueryOptions;
import org.apache.jclouds.oneandone.rest.filters.AuthenticateRequest;
import org.jclouds.Fallbacks;
import org.jclouds.rest.annotations.BinderParam;
import org.jclouds.rest.annotations.Fallback;
import org.jclouds.rest.annotations.MapBinder;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.binders.BindToJsonPayload;

@Path("images")
@Produces("application/json")
@Consumes("application/json")
@RequestFilters(AuthenticateRequest.class)
public interface ImageApi extends Closeable {

@Named("images:list")
@GET
@Fallback(Fallbacks.EmptyListOnNotFoundOr404.class)
List<Image> list();

@Named("images:list")
@GET
@Fallback(Fallbacks.EmptyListOnNotFoundOr404.class)
List<Image> list(GenericQueryOptions options);

@Named("image:get")
@GET
@Path("/{imageId}")
@Fallback(Fallbacks.NullOnNotFoundOr404.class)
Image get(@PathParam("imageId") String imageId);

@Named("image:create")
@POST
Image createImage(@BinderParam(BindToJsonPayload.class) Image.CreateImage image);

@Named("image:update")
@PUT
@Path("/{imageId}")
Image update(@PathParam("imageId") String imageId, @BinderParam(BindToJsonPayload.class) Image.UpdateImage image);

@Named("image:delete")
@DELETE
@Path("/{imageId}")
@MapBinder(BindToJsonPayload.class)
@Fallback(Fallbacks.NullOnNotFoundOr404.class)
Image delete(@PathParam("imageId") String imageId);
}
@@ -0,0 +1,114 @@
/*
* 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.apache.jclouds.oneandone.rest.features;

import java.util.List;
import org.apache.jclouds.oneandone.rest.domain.Image;
import org.apache.jclouds.oneandone.rest.domain.Image.CreateImage;
import org.apache.jclouds.oneandone.rest.domain.Image.UpdateImage;
import org.apache.jclouds.oneandone.rest.domain.Server;
import org.apache.jclouds.oneandone.rest.domain.Types;
import org.apache.jclouds.oneandone.rest.domain.options.GenericQueryOptions;
import org.apache.jclouds.oneandone.rest.internal.BaseOneAndOneLiveTest;
import org.testng.Assert;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

@Test(groups = "live", testName = "ImageApiLiveTest")
public class ImageApiLiveTest extends BaseOneAndOneLiveTest {

private Image currentImage;
private Server currentServer;
private List<Image> images;

private ImageApi imageApi() {
return api.imageApi();
}

@BeforeClass
public void setupTest() {
currentServer = createServer("image jclouds server");
assertNodeAvailable(currentServer);
currentImage = imageApi().createImage(CreateImage.builder()
.name("jcloudsimage")
.numImages(1)
.frequency(Types.ImageFrequency.WEEKLY)
.serverId(currentServer.id())
.build());
Image checkImage = imageApi().get(currentImage.id());

assertNotNull(currentImage);
assertNotNull(checkImage);

}

@Test
public void testList() {
images = imageApi().list();

assertNotNull(images);
assertFalse(images.isEmpty());
Assert.assertTrue(images.size() > 0);
}

@Test
public void testListWithOption() {
GenericQueryOptions options = new GenericQueryOptions();
options.options(0, 0, null, "jcloudsimage", null);
List<Image> imageWithQuery = imageApi().list(options);

assertNotNull(imageWithQuery);
assertFalse(imageWithQuery.isEmpty());
Assert.assertTrue(imageWithQuery.size() > 0);
}

@Test
public void testGetImage() {
Image result = imageApi().get(currentImage.id());

assertNotNull(result);
assertEquals(result.id(), currentImage.id());
}

@Test(dependsOnMethods = "testGetImage")
public void testUpdateImage() throws InterruptedException {
String updatedName = "Updatedjava";

Image updateResult = imageApi().update(currentImage.id(), UpdateImage.create(updatedName, "description", Types.ImageFrequency.ONCE));

assertNotNull(updateResult);

assertEquals(updateResult.name(), updatedName);

}

@AfterClass(alwaysRun = true)
public void teardownTest() throws InterruptedException {
if (currentImage != null) {
imageApi().delete(currentImage.id());
}
if (currentServer != null) {
assertNodeAvailable(currentServer);
deleteServer(currentServer.id());
}
}

}

0 comments on commit f997b06

Please sign in to comment.