Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exceptions changes #7

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.dasein</groupId>
<artifactId>dasein-cloud-vcloud</artifactId>
<version>2016.02.1-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>
<name>Dasein Cloud vCloud</name>
<description>Dasein Cloud implementation for the vCloud API</description>

Expand Down Expand Up @@ -55,7 +55,7 @@
<dependency>
<groupId>org.dasein</groupId>
<artifactId>dasein-cloud-core</artifactId>
<version>2016.02.1-SNAPSHOT</version>
<version>0.9.0</version>
<scope>compile</scope>
<optional>false</optional>
</dependency>
Expand All @@ -69,7 +69,7 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.1</version>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
Expand All @@ -92,10 +92,16 @@
<dependency>
<groupId>org.dasein</groupId>
<artifactId>dasein-cloud-test</artifactId>
<version>2016.02.1-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>
<scope>test</scope>
<optional>false</optional>
</dependency>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>1.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
24 changes: 15 additions & 9 deletions src/main/java/org/dasein/cloud/vcloud/compute/DiskSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
package org.dasein.cloud.vcloud.compute;

import org.apache.log4j.Logger;
import org.dasein.cloud.CloudErrorType;
import org.dasein.cloud.CloudException;
import org.dasein.cloud.GeneralCloudException;
import org.dasein.cloud.InternalException;
import org.dasein.cloud.OperationNotSupportedException;
import org.dasein.cloud.ResourceNotFoundException;
import org.dasein.cloud.ResourceStatus;
import org.dasein.cloud.Tag;
import org.dasein.cloud.compute.AbstractVolumeSupport;
import org.dasein.cloud.compute.Platform;
import org.dasein.cloud.compute.Volume;
import org.dasein.cloud.compute.VolumeCreateOptions;
import org.dasein.cloud.compute.VolumeFormat;
Expand All @@ -37,7 +39,8 @@
import org.dasein.cloud.vcloud.vCloud;
import org.dasein.cloud.vcloud.vCloudMethod;
import org.dasein.util.CalendarWrapper;
import org.dasein.util.uom.storage.*;
import org.dasein.util.uom.storage.Gigabyte;
import org.dasein.util.uom.storage.Storage;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
Expand All @@ -46,7 +49,6 @@
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
Expand Down Expand Up @@ -109,17 +111,19 @@ public void attach(@Nonnull String volumeId, @Nonnull String toServer, @Nonnull
String response = method.post(vCloudMethod.CREATE_DISK, vdcId, xml.toString());

if( response.length() < 1 ) {
throw new CloudException("No error, but no volume");
throw new GeneralCloudException("No error, but no volume", CloudErrorType.GENERAL);
}

Document doc = method.parseXML(response);
String docElementTagName = doc.getDocumentElement().getTagName();
String nsString = "";
if(docElementTagName.contains(":"))nsString = docElementTagName.substring(0, docElementTagName.indexOf(":") + 1);
if(docElementTagName.contains(":")) {
nsString = docElementTagName.substring(0, docElementTagName.indexOf(":") + 1);
}
NodeList disks = doc.getElementsByTagName(nsString + "Disk");

if( disks.getLength() < 1 ) {
throw new CloudException("No error, but no volume");
throw new GeneralCloudException("No error, but no volume", CloudErrorType.GENERAL);
}
Node disk = disks.item(0);
Node href = disk.getAttributes().getNamedItem("href");
Expand Down Expand Up @@ -164,7 +168,7 @@ public void attach(@Nonnull String volumeId, @Nonnull String toServer, @Nonnull
}
return volumeId;
}
throw new CloudException("No ID provided in Disk XML");
throw new GeneralCloudException("No ID provided in Disk XML", CloudErrorType.GENERAL);
}
finally {
APITrace.end();
Expand All @@ -178,12 +182,14 @@ public void detach(@Nonnull String volumeId, boolean force) throws InternalExcep
Volume volume = getVolume(volumeId);

if( volume == null ) {
throw new CloudException("No such volume: " + volumeId);
throw new ResourceNotFoundException("Volume", volumeId);
}
String serverId = volume.getProviderVirtualMachineId();

if( serverId == null ) {
throw new CloudException("No virtual machine is attached to this volume");
//todo
//should we have a new exception for errors caused by user/client provided data?
throw new InternalException("No virtual machine is attached to this volume");
}
vCloudMethod method = new vCloudMethod(getProvider());
StringBuilder xml = new StringBuilder();
Expand Down
99 changes: 54 additions & 45 deletions src/main/java/org/dasein/cloud/vcloud/compute/TemplateSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@

import org.apache.log4j.Logger;
import org.dasein.cloud.AsynchronousTask;
import org.dasein.cloud.CloudErrorType;
import org.dasein.cloud.CloudException;
import org.dasein.cloud.GeneralCloudException;
import org.dasein.cloud.InternalException;
import org.dasein.cloud.ResourceNotFoundException;
import org.dasein.cloud.Tag;
import org.dasein.cloud.compute.AbstractImageSupport;
import org.dasein.cloud.compute.Architecture;
Expand All @@ -32,7 +35,6 @@
import org.dasein.cloud.compute.MachineImage;
import org.dasein.cloud.compute.MachineImageFormat;
import org.dasein.cloud.compute.MachineImageState;
import org.dasein.cloud.compute.MachineImageType;
import org.dasein.cloud.compute.Platform;
import org.dasein.cloud.compute.VirtualMachine;
import org.dasein.cloud.compute.VmState;
Expand Down Expand Up @@ -66,6 +68,7 @@
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
Expand Down Expand Up @@ -107,22 +110,24 @@ protected MachineImage capture(@Nonnull ImageCreateOptions options, @Nullable As
String vmId = options.getVirtualMachineId();

if( vmId == null ) {
throw new CloudException("A capture operation requires a valid VM ID");
//todo
//should we have a new exception for errors caused by user/client provided data?
throw new InternalException("A capture operation requires a valid VM ID");
}
VirtualMachine vm = ((vCloud)getProvider()).getComputeServices().getVirtualMachineSupport().getVirtualMachine(vmId);
String vAppId = (vm == null ? null : (String)vm.getTag(vAppSupport.PARENT_VAPP_ID));

if( vm == null ) {
throw new CloudException("No such virtual machine: " + vmId);
throw new ResourceNotFoundException("Virtual machine", vmId);
}
else if( vAppId == null ) {
throw new CloudException("Unable to determine virtual machine vApp for capture: " + vmId);
throw new ResourceNotFoundException("Virtual machine vApp for virtual machine", vmId);
}
long timeout = (System.currentTimeMillis() + CalendarWrapper.MINUTE * 10L);

while( timeout > System.currentTimeMillis() ) {
if( vm == null ) {
throw new CloudException("VM " + vmId + " went away");
throw new ResourceNotFoundException("VirtualMachine ", vmId);
}
if( !vm.getCurrentState().equals(VmState.PENDING) ) {
break;
Expand Down Expand Up @@ -151,7 +156,7 @@ else if( vAppId == null ) {
String response = method.post(vCloudMethod.CAPTURE_VAPP, vm.getProviderDataCenterId(), xml.toString());

if( response.equals("") ) {
throw new CloudException("No error or other information was in the response");
throw new GeneralCloudException("No error or other information was in the response", CloudErrorType.GENERAL);
}
Document doc = method.parseXML(response);

Expand All @@ -163,15 +168,15 @@ else if( vAppId == null ) {
logger.warn("The cloud thinks the vApp or VM is still running; going to check what's going on: " + e.getMessage());
vm = ((vCloud)getProvider()).getComputeServices().getVirtualMachineSupport().getVirtualMachine(vmId);
if( vm == null ) {
throw new CloudException("Virtual machine went away");
throw new ResourceNotFoundException("Virtual machine", vmId);
}
if( !vm.getCurrentState().equals(VmState.STOPPED) ) {
logger.warn("Current state of VM: " + vm.getCurrentState());
((vCloud)getProvider()).getComputeServices().getVirtualMachineSupport().undeploy(vappId, "shutdown");
}
response = method.post(vCloudMethod.CAPTURE_VAPP, vm.getProviderDataCenterId(), xml.toString());
if( response.equals("") ) {
throw new CloudException("No error or other information was in the response");
throw new GeneralCloudException("No error or other information was in the response", CloudErrorType.GENERAL);
}
doc = method.parseXML(response);
method.checkError(doc);
Expand All @@ -184,7 +189,7 @@ else if( vAppId == null ) {
NodeList vapps = doc.getElementsByTagName("VAppTemplate");

if( vapps.getLength() < 1 ) {
throw new CloudException("No vApp templates were found in response");
throw new ResourceNotFoundException("vApp templates were not found in response", "n/a");
}
Node vapp = vapps.item(0);
String imageId = null;
Expand All @@ -194,12 +199,12 @@ else if( vAppId == null ) {
imageId = ((vCloud)getProvider()).toID(href.getNodeValue().trim());
}
if( imageId == null || imageId.length() < 1 ) {
throw new CloudException("No imageId was found in response");
throw new ResourceNotFoundException("ImageId not found in response", "n/a");
}
MachineImage img = loadVapp(imageId, getContext().getAccountNumber(), false, options.getName(), options.getDescription(), System.currentTimeMillis());

if( img == null ) {
throw new CloudException("Image was lost");
throw new ResourceNotFoundException("Image", imageId);
}
method.waitFor(response);
publish(img);
Expand Down Expand Up @@ -258,11 +263,11 @@ private void publish(@Nonnull MachineImage img) throws CloudException, InternalE
}
}
if( href == null ) {
throw new CloudException("No catalog could be identified for publishing vApp template " + img.getProviderMachineImageId());
throw new ResourceNotFoundException("Catalog for publishing vApp template", img.getProviderMachineImageId());
}
c = getCatalog(false, href);
if( c == null ) {
throw new CloudException("No catalog could be identified for publishing vApp template " + img.getProviderMachineImageId());
throw new ResourceNotFoundException("Catalog for publishing vApp template ", img.getProviderMachineImageId());
}
}

Expand Down Expand Up @@ -504,6 +509,15 @@ private Iterable<Catalog> listPrivateCatalogs() throws CloudException, InternalE
Cache<MachineImage> cache = Cache.getInstance(getProvider(), "listImages", MachineImage.class, CacheLevel.REGION_ACCOUNT, new TimePeriod<Minute>(6, TimePeriod.MINUTE));
Iterable<MachineImage> imageList = cache.get(getContext());
if (imageList != null) {
if (options != null) {
List<MachineImage> tmp = new ArrayList<MachineImage>();
for (MachineImage img : imageList) {
if (options.matches(img)) {
tmp.add(img);
}
}
return tmp;
}
return imageList;
}

Expand Down Expand Up @@ -539,6 +553,15 @@ private Iterable<Catalog> listPrivateCatalogs() throws CloudException, InternalE
Iterable<MachineImage> imageList2 = cache.get(getContext());
if (imageList2 != null) {
// A thread we were waiting on has refreshed the cache
if (options != null) {
List<MachineImage> tmp = new ArrayList<MachineImage>();
for (MachineImage img : imageList2) {
if (options.matches(img)) {
tmp.add(img);
}
}
return tmp;
}
return imageList2;
}

Expand Down Expand Up @@ -584,25 +607,23 @@ private Iterable<Catalog> listPrivateCatalogs() throws CloudException, InternalE
String catalogItemId = ((vCloud)getProvider()).toID(href.getNodeValue().trim());
MachineImage image = loadTemplate(catalog.owner, catalogItemId, catalog.published);
if( image != null ) {
if( options == null || options.matches(image) ) {
image.setProviderOwnerId(catalog.owner);
try {
String metaData = method.get("vAppTemplate", image.getProviderMachineImageId() + "/metadata");
if( metaData != null && !metaData.equals("") ) {
method.parseMetaData(image, metaData);
}
image.setProviderOwnerId(catalog.owner);
try {
String metaData = method.get("vAppTemplate", image.getProviderMachineImageId() + "/metadata");
if( metaData != null && !metaData.equals("") ) {
method.parseMetaData(image, metaData);
}
catch( Throwable warning ) {
if (logger.isDebugEnabled()) {
logger.warn("Failed to get and parse image metadata.", warning);
}
else {
logger.warn("Failed to get and parse image metadata.");
}
}
catch( Throwable warning ) {
if (logger.isDebugEnabled()) {
logger.warn("Failed to get and parse image metadata.", warning);
}
else {
logger.warn("Failed to get and parse image metadata.");
}
image.setTag("catalogItemId", catalogItemId);
images.add(image);
}
image.setTag("catalogItemId", catalogItemId);
images.add(image);
}
}
}
Expand Down Expand Up @@ -1012,7 +1033,7 @@ public void remove(@Nonnull String providerImageId, boolean checkState) throws C
MachineImage image = getImage(providerImageId);

if( image == null ) {
throw new CloudException("No such image: " + providerImageId);
throw new ResourceNotFoundException("Image", providerImageId);
}
vCloudMethod method = new vCloudMethod((vCloud)getProvider());
String catalogItemId = (String)image.getTag("catalogItemId");
Expand Down Expand Up @@ -1077,6 +1098,9 @@ public void remove(@Nonnull String providerImageId, boolean checkState) throws C
if( options == null || options.matches(image) ) {
image.setProviderOwnerId(catalog.owner);
image.setTag("catalogItemId", catalogItemId);
if (image.getTag("public") != null && image.getTag("public").equals("true")) {
image = image.sharedWithPublic();
}
images.add(image);
}
}
Expand All @@ -1096,21 +1120,6 @@ public void remove(@Nonnull String providerImageId, boolean checkState) throws C
}
}

@Override
public boolean supportsCustomImages() {
return true;
}

@Override
public boolean supportsImageCapture(@Nonnull MachineImageType type) {
return type.equals(MachineImageType.VOLUME);
}

@Override
public boolean supportsPublicLibrary(@Nonnull ImageClass cls) {
return cls.equals(ImageClass.MACHINE);
}

@Override
public void setTags(@Nonnull String imageId, @Nonnull Tag... tags) throws CloudException, InternalException {
APITrace.begin(getProvider(), "Image.setTags");
Expand Down
Loading