From 3f7ce01f2861843e8c104d07cdc7623027a2a362 Mon Sep 17 00:00:00 2001 From: Edison Su Date: Wed, 26 Oct 2011 16:27:28 -0700 Subject: [PATCH] bug 10803: using updatehost api to change secondary storage url, the url need to be nfs://host/path format status 10803: resolved fixed --- .../com/cloud/api/commands/UpdateHostCmd.java | 7 +++ api/src/com/cloud/storage/StorageService.java | 2 + .../cloud/resource/ResourceManagerImpl.java | 5 +++ .../src/com/cloud/storage/StorageManager.java | 2 + .../com/cloud/storage/StorageManagerImpl.java | 44 +++++++++++++++++++ 5 files changed, 60 insertions(+) diff --git a/api/src/com/cloud/api/commands/UpdateHostCmd.java b/api/src/com/cloud/api/commands/UpdateHostCmd.java index eec36ccafe3..ee808f13f51 100644 --- a/api/src/com/cloud/api/commands/UpdateHostCmd.java +++ b/api/src/com/cloud/api/commands/UpdateHostCmd.java @@ -52,6 +52,9 @@ public class UpdateHostCmd extends BaseCmd { @Parameter(name=ApiConstants.HOST_TAGS, type=CommandType.LIST, collectionType=CommandType.STRING, description="list of tags to be added to the host") private List hostTags; + + @Parameter(name=ApiConstants.URL, type=CommandType.STRING, description="the new uri for the secondary storage: nfs://host/path") + private String url; ///////////////////////////////////////////////////// /////////////////// Accessors /////////////////////// @@ -72,6 +75,10 @@ public class UpdateHostCmd extends BaseCmd { public List getHostTags() { return hostTags; } + + public String getUrl() { + return url; + } ///////////////////////////////////////////////////// /////////////// API Implementation/////////////////// diff --git a/api/src/com/cloud/storage/StorageService.java b/api/src/com/cloud/storage/StorageService.java index 0c2754e25d4..2a4cc9abae2 100644 --- a/api/src/com/cloud/storage/StorageService.java +++ b/api/src/com/cloud/storage/StorageService.java @@ -32,6 +32,7 @@ import com.cloud.exception.PermissionDeniedException; import com.cloud.exception.ResourceAllocationException; import com.cloud.exception.ResourceInUseException; import com.cloud.exception.ResourceUnavailableException; +import com.cloud.host.Host; public interface StorageService { /** @@ -107,4 +108,5 @@ public interface StorageService { public StoragePool getStoragePool(long id); Volume migrateVolume(Long volumeId, Long storagePoolId) throws ConcurrentOperationException; + } diff --git a/server/src/com/cloud/resource/ResourceManagerImpl.java b/server/src/com/cloud/resource/ResourceManagerImpl.java index fe69b376b4a..189ad624515 100755 --- a/server/src/com/cloud/resource/ResourceManagerImpl.java +++ b/server/src/com/cloud/resource/ResourceManagerImpl.java @@ -957,6 +957,11 @@ public class ResourceManagerImpl implements ResourceManager, ResourceService, Ma } _hostTagsDao.persist(hostId, hostTags); } + + String url = cmd.getUrl(); + if (url != null) { + _storageMgr.updateSecondaryStorage(cmd.getId(), cmd.getUrl()); + } HostVO updatedHost = _hostDao.findById(hostId); return updatedHost; diff --git a/server/src/com/cloud/storage/StorageManager.java b/server/src/com/cloud/storage/StorageManager.java index f91033e318f..cde5c2bd04b 100755 --- a/server/src/com/cloud/storage/StorageManager.java +++ b/server/src/com/cloud/storage/StorageManager.java @@ -220,4 +220,6 @@ public interface StorageManager extends Manager { VolumeVO allocateDuplicateVolume(VolumeVO oldVol, Long templateId); + Host updateSecondaryStorage(long secStorageId, String newUrl); + } diff --git a/server/src/com/cloud/storage/StorageManagerImpl.java b/server/src/com/cloud/storage/StorageManagerImpl.java index f0d6c6cbf5f..f41709acb77 100755 --- a/server/src/com/cloud/storage/StorageManagerImpl.java +++ b/server/src/com/cloud/storage/StorageManagerImpl.java @@ -151,6 +151,7 @@ import com.cloud.user.dao.UserDao; import com.cloud.uservm.UserVm; import com.cloud.utils.NumbersUtil; import com.cloud.utils.Pair; +import com.cloud.utils.UriUtils; import com.cloud.utils.component.Adapters; import com.cloud.utils.component.ComponentLocator; import com.cloud.utils.component.Inject; @@ -3184,4 +3185,47 @@ public class StorageManagerImpl implements StorageManager, StorageService, Manag } } + @Override + public Host updateSecondaryStorage(long secStorageId, String newUrl) { + HostVO secHost = _hostDao.findById(secStorageId); + if (secHost == null) { + throw new InvalidParameterValueException("Can not find out the secondary storage id: " + secStorageId); + } + + if (secHost.getType() != Host.Type.SecondaryStorage) { + throw new InvalidParameterValueException("host: " + secStorageId + " is not a secondary storage"); + } + + URI uri = null; + try { + uri = new URI(UriUtils.encodeURIComponent(newUrl)); + if (uri.getScheme() == null) { + throw new InvalidParameterValueException("uri.scheme is null " + newUrl + ", add nfs:// as a prefix"); + } else if (uri.getScheme().equalsIgnoreCase("nfs")) { + if (uri.getHost() == null || uri.getHost().equalsIgnoreCase("") || uri.getPath() == null || uri.getPath().equalsIgnoreCase("")) { + throw new InvalidParameterValueException("Your host and/or path is wrong. Make sure it's of the format nfs://hostname/path"); + } + } + } catch (URISyntaxException e) { + throw new InvalidParameterValueException(newUrl + " is not a valid uri"); + } + + String oldUrl = secHost.getStorageUrl(); + + URI oldUri = null; + try { + oldUri = new URI(UriUtils.encodeURIComponent(oldUrl)); + if (!oldUri.getScheme().equalsIgnoreCase(uri.getScheme())) { + throw new InvalidParameterValueException("can not change old scheme:" + oldUri.getScheme() + " to " + uri.getScheme()); + } + } catch (URISyntaxException e) { + s_logger.debug("Failed to get uri from " + oldUrl); + } + + secHost.setStorageUrl(newUrl); + secHost.setGuid(newUrl); + secHost.setName(newUrl); + _hostDao.update(secHost.getId(), secHost); + return secHost; + } }