Added generic refresh method in Abiquo

In Abiquo every object contains a link to itself with rel="edit". This
commit adds a generic method in the DomainWrapper base class that reads
the link and performs a GET operation to refresh the object.

This method was already used in conversions and virtual machines, and
now every object will have it implemented.
This commit is contained in:
Ignasi Barrera 2013-02-13 16:27:28 +01:00
parent 49990d97e4
commit a82b9db678
5 changed files with 24 additions and 32 deletions

View File

@ -35,6 +35,9 @@ import org.jclouds.abiquo.domain.exception.WrapperException;
import org.jclouds.abiquo.domain.task.AsyncTask; import org.jclouds.abiquo.domain.task.AsyncTask;
import org.jclouds.abiquo.domain.util.LinkUtils; import org.jclouds.abiquo.domain.util.LinkUtils;
import org.jclouds.abiquo.reference.ValidationErrors; import org.jclouds.abiquo.reference.ValidationErrors;
import org.jclouds.abiquo.rest.internal.ExtendedUtils;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.ParseXMLWithJAXB;
import org.jclouds.rest.RestContext; import org.jclouds.rest.RestContext;
import com.abiquo.model.rest.RESTLink; import com.abiquo.model.rest.RESTLink;
@ -46,6 +49,7 @@ import com.google.common.base.Function;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.reflect.Invokable; import com.google.common.reflect.Invokable;
import com.google.inject.TypeLiteral;
/** /**
* This class is used to decorate transport objects with high level * This class is used to decorate transport objects with high level
@ -84,6 +88,22 @@ public abstract class DomainWrapper<T extends SingleResourceTransportDto> {
return target; return target;
} }
/**
* Refresh the state of the current object.
*/
@SuppressWarnings("unchecked")
public void refresh() {
RESTLink link = checkNotNull(LinkUtils.getSelfLink(target), ValidationErrors.MISSING_REQUIRED_LINK + " edit/self");
ExtendedUtils utils = (ExtendedUtils) context.getUtils();
HttpResponse response = utils.getAbiquoHttpClient().get(link);
ParseXMLWithJAXB<T> parser = new ParseXMLWithJAXB<T>(utils.getXml(),
TypeLiteral.get((Class<T>) target.getClass()));
target = parser.apply(response);
}
/** /**
* Read the ID of the parent resource from the given link. * Read the ID of the parent resource from the given link.
* *

View File

@ -58,20 +58,6 @@ public class Conversion extends DomainWithTasksWrapper<ConversionDto> {
super(context, target); super(context, target);
} }
// Domain methods
public void refresh() {
RESTLink link = checkNotNull(target.searchLink("edit"), ValidationErrors.MISSING_REQUIRED_LINK + "edit");
ExtendedUtils utils = (ExtendedUtils) context.getUtils();
HttpResponse response = checkNotNull(utils.getAbiquoHttpClient().get(link), "conversion");
ParseXMLWithJAXB<ConversionDto> parser = new ParseXMLWithJAXB<ConversionDto>(utils.getXml(),
TypeLiteral.get(ConversionDto.class));
target = parser.apply(response);
}
// Parent access // Parent access
/** /**

View File

@ -220,18 +220,6 @@ public class VirtualMachine extends DomainWithTasksWrapper<VirtualMachineWithNod
return state; return state;
} }
public void refresh() {
RESTLink link = checkNotNull(target.getEditLink(), ValidationErrors.MISSING_REQUIRED_LINK + " edit");
ExtendedUtils utils = (ExtendedUtils) context.getUtils();
HttpResponse response = utils.getAbiquoHttpClient().get(link);
ParseXMLWithJAXB<VirtualMachineWithNodeExtendedDto> parser = new ParseXMLWithJAXB<VirtualMachineWithNodeExtendedDto>(
utils.getXml(), TypeLiteral.get(VirtualMachineWithNodeExtendedDto.class));
target = parser.apply(response);
}
// Parent access // Parent access
/** /**

View File

@ -112,6 +112,7 @@ public class StoragePool extends DomainWrapper<StoragePoolDto> {
target = context.getApi().getInfrastructureApi().updateStoragePool(target); target = context.getApi().getInfrastructureApi().updateStoragePool(target);
} }
@Override
public void refresh() { public void refresh() {
target = context.getApi().getInfrastructureApi() target = context.getApi().getInfrastructureApi()
.refreshStoragePool(target, StoragePoolOptions.builder().sync(true).build()); .refreshStoragePool(target, StoragePoolOptions.builder().sync(true).build());
@ -187,7 +188,6 @@ public class StoragePool extends DomainWrapper<StoragePoolDto> {
private String name; private String name;
private Long totalSizeInMb; private Long totalSizeInMb;
public Builder(final RestContext<AbiquoApi, AbiquoAsyncApi> context, final StorageDevice storageDevice) { public Builder(final RestContext<AbiquoApi, AbiquoAsyncApi> context, final StorageDevice storageDevice) {
super(); super();
checkNotNull(storageDevice, ValidationErrors.NULL_RESOURCE + StorageDevice.class); checkNotNull(storageDevice, ValidationErrors.NULL_RESOURCE + StorageDevice.class);
@ -206,13 +206,11 @@ public class StoragePool extends DomainWrapper<StoragePoolDto> {
return this; return this;
} }
public Builder totalSizeInMb(final long totalSizeInMb) { public Builder totalSizeInMb(final long totalSizeInMb) {
this.totalSizeInMb = totalSizeInMb; this.totalSizeInMb = totalSizeInMb;
return this; return this;
} }
public StoragePool build() { public StoragePool build() {
StoragePoolDto dto = new StoragePoolDto(); StoragePoolDto dto = new StoragePoolDto();
dto.setName(name); dto.setName(name);
@ -223,8 +221,7 @@ public class StoragePool extends DomainWrapper<StoragePoolDto> {
} }
public static Builder fromStoragePool(final StoragePool in) { public static Builder fromStoragePool(final StoragePool in) {
Builder builder = StoragePool.builder(in.context, in.getStorageDevice()) Builder builder = StoragePool.builder(in.context, in.getStorageDevice()).totalSizeInMb(in.getTotalSizeInMb());
.totalSizeInMb(in.getTotalSizeInMb());
return builder; return builder;
} }

View File

@ -23,8 +23,8 @@ import static com.google.common.base.Preconditions.checkNotNull;
import java.util.List; import java.util.List;
import org.jclouds.abiquo.AbiquoAsyncApi;
import org.jclouds.abiquo.AbiquoApi; import org.jclouds.abiquo.AbiquoApi;
import org.jclouds.abiquo.AbiquoAsyncApi;
import org.jclouds.abiquo.domain.DomainWrapper; import org.jclouds.abiquo.domain.DomainWrapper;
import org.jclouds.abiquo.reference.ValidationErrors; import org.jclouds.abiquo.reference.ValidationErrors;
import org.jclouds.rest.RestContext; import org.jclouds.rest.RestContext;
@ -52,6 +52,7 @@ public class AsyncTask extends DomainWrapper<TaskDto> {
/** /**
* Refresh the state of the task. * Refresh the state of the task.
*/ */
@Override
public void refresh() { public void refresh() {
RESTLink self = checkNotNull(target.searchLink("self"), ValidationErrors.MISSING_REQUIRED_LINK + "self"); RESTLink self = checkNotNull(target.searchLink("self"), ValidationErrors.MISSING_REQUIRED_LINK + "self");