diff --git a/plugins/storage/volume/adaptive/src/main/java/org/apache/cloudstack/storage/datastore/lifecycle/AdaptiveDataStoreLifeCycleImpl.java b/plugins/storage/volume/adaptive/src/main/java/org/apache/cloudstack/storage/datastore/lifecycle/AdaptiveDataStoreLifeCycleImpl.java index c8efc08c2892..1f6f3926d61a 100644 --- a/plugins/storage/volume/adaptive/src/main/java/org/apache/cloudstack/storage/datastore/lifecycle/AdaptiveDataStoreLifeCycleImpl.java +++ b/plugins/storage/volume/adaptive/src/main/java/org/apache/cloudstack/storage/datastore/lifecycle/AdaptiveDataStoreLifeCycleImpl.java @@ -177,7 +177,7 @@ public DataStore initialize(Map dsInfos) { try { PrimaryDataStoreParameters parameters = new PrimaryDataStoreParameters(); parameters.setHost(uri.getHost()); - parameters.setPort(uri.getPort()); + parameters.setPort(portOrDefault(uri)); parameters.setPath(uri.getPath() + "?" + uri.getQuery()); parameters.setType(StoragePoolType.FiberChannel); parameters.setZoneId(zoneId); @@ -402,4 +402,9 @@ public void disableStoragePool(DataStore store) { logger.info("Disabling storage pool {}", store); _dataStoreHelper.disable(store); } + + // A URL without a port gives -1, which the storage_pool table cannot hold + static int portOrDefault(URL uri) { + return uri.getPort() > 0 ? uri.getPort() : uri.getDefaultPort(); + } } diff --git a/plugins/storage/volume/adaptive/src/test/java/org/apache/cloudstack/storage/datastore/lifecycle/AdaptiveDataStoreLifeCycleImplTest.java b/plugins/storage/volume/adaptive/src/test/java/org/apache/cloudstack/storage/datastore/lifecycle/AdaptiveDataStoreLifeCycleImplTest.java new file mode 100644 index 000000000000..1e89d09e9850 --- /dev/null +++ b/plugins/storage/volume/adaptive/src/test/java/org/apache/cloudstack/storage/datastore/lifecycle/AdaptiveDataStoreLifeCycleImplTest.java @@ -0,0 +1,36 @@ +// 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.cloudstack.storage.datastore.lifecycle; + +import java.net.URL; + +import org.junit.Assert; +import org.junit.Test; + +public class AdaptiveDataStoreLifeCycleImplTest { + + @Test + public void portOrDefaultUsesSchemeDefaultWhenUrlHasNoPort() throws Exception { + Assert.assertEquals(443, AdaptiveDataStoreLifeCycleImpl.portOrDefault(new URL("https://array/api?pod=p1"))); + Assert.assertEquals(80, AdaptiveDataStoreLifeCycleImpl.portOrDefault(new URL("http://array/api?pod=p1"))); + } + + @Test + public void portOrDefaultKeepsExplicitPort() throws Exception { + Assert.assertEquals(8443, AdaptiveDataStoreLifeCycleImpl.portOrDefault(new URL("https://array:8443/api?pod=p1"))); + } +}