From ebac4539d02cbc846fdf33412f0864775d3ebad8 Mon Sep 17 00:00:00 2001 From: Kris McQueen Date: Thu, 28 Oct 2010 17:51:48 -0700 Subject: [PATCH] it's possible createSnapshotDB will return null, handle that case. However, if createSnapshotDB returns null because there was no work to do (empty snapshot) we should probably throw an exception that reports that fact instead of returning null and throwing an internal error --- .../cloud/api/commands/CreateSnapshotCmd.java | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/server/src/com/cloud/api/commands/CreateSnapshotCmd.java b/server/src/com/cloud/api/commands/CreateSnapshotCmd.java index d34a8a90164..2996c266617 100644 --- a/server/src/com/cloud/api/commands/CreateSnapshotCmd.java +++ b/server/src/com/cloud/api/commands/CreateSnapshotCmd.java @@ -22,8 +22,10 @@ import org.apache.log4j.Logger; import com.cloud.api.ApiDBUtils; import com.cloud.api.BaseAsyncCreateCmd; +import com.cloud.api.BaseCmd; import com.cloud.api.Implementation; import com.cloud.api.Parameter; +import com.cloud.api.ServerApiException; import com.cloud.api.response.SnapshotResponse; import com.cloud.event.EventTypes; import com.cloud.storage.Snapshot.SnapshotType; @@ -104,26 +106,30 @@ public class CreateSnapshotCmd extends BaseAsyncCreateCmd { public SnapshotResponse getResponse() { SnapshotVO snapshot = (SnapshotVO)getResponseObject(); - SnapshotResponse response = new SnapshotResponse(); - response.setId(snapshot.getId()); + if (snapshot != null) { + SnapshotResponse response = new SnapshotResponse(); + response.setId(snapshot.getId()); - Account account = ApiDBUtils.findAccountById(snapshot.getAccountId()); - if (account != null) { - response.setAccountName(account.getAccountName()); - response.setDomainId(account.getDomainId()); - response.setDomainName(ApiDBUtils.findDomainById(account.getDomainId()).getName()); + Account account = ApiDBUtils.findAccountById(snapshot.getAccountId()); + if (account != null) { + response.setAccountName(account.getAccountName()); + response.setDomainId(account.getDomainId()); + response.setDomainName(ApiDBUtils.findDomainById(account.getDomainId()).getName()); + } + + VolumeVO volume = ApiDBUtils.findVolumeById(snapshot.getVolumeId()); + String snapshotTypeStr = SnapshotType.values()[snapshot.getSnapshotType()].name(); + response.setSnapshotType(snapshotTypeStr); + response.setVolumeId(snapshot.getVolumeId()); + response.setVolumeName(volume.getName()); + response.setVolumeType(volume.getVolumeType().toString()); + response.setCreated(snapshot.getCreated()); + response.setName(snapshot.getName()); + + response.setResponseName(getName()); + return response; } - VolumeVO volume = ApiDBUtils.findVolumeById(snapshot.getVolumeId()); - String snapshotTypeStr = SnapshotType.values()[snapshot.getSnapshotType()].name(); - response.setSnapshotType(snapshotTypeStr); - response.setVolumeId(snapshot.getVolumeId()); - response.setVolumeName(volume.getName()); - response.setVolumeType(volume.getVolumeType().toString()); - response.setCreated(snapshot.getCreated()); - response.setName(snapshot.getName()); - - response.setResponseName(getName()); - return response; + throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to create snapshot due to an internal error creating snapshot for volume " + volumeId); } }