Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,13 @@ public String attach(ProviderAdapterContext context, ProviderAdapterDataObject d
try {
FlashArrayList<FlashArrayConnection> list = null;
FlashArrayHost host = getHost(hostname);
if (host != null) {
list = POST("/connections?host_names=" + host.getName() + "&volume_names=" + volumeName, null,
new TypeReference<FlashArrayList<FlashArrayConnection>>() {
});
if (host == null) {
throw new RuntimeException("Host [" + hostname + "] not found in FlashArray at [" + url
+ "]. The host object on the array must have the same name as the CloudStack host");
}
list = POST("/connections?host_names=" + host.getName() + "&volume_names=" + volumeName, null,
new TypeReference<FlashArrayList<FlashArrayConnection>>() {
});

if (list == null || list.getItems() == null || list.getItems().size() == 0) {
throw new RuntimeException("Volume attach did not return lun information");
Expand All @@ -180,7 +182,7 @@ public String attach(ProviderAdapterContext context, ProviderAdapterDataObject d
if (list != null && list.getItems() != null) {
for (FlashArrayConnection conn : list.getItems()) {
if (conn.getHost() != null && conn.getHost().getName() != null &&
(conn.getHost().getName().equals(hostname) || conn.getHost().getName().equals(hostname.substring(0, hostname.indexOf('.')))) &&
(conn.getHost().getName().equals(hostname) || conn.getHost().getName().equals(shortHostName(hostname))) &&
conn.getLun() != null) {
return "" + conn.getLun();
}
Expand Down Expand Up @@ -571,25 +573,31 @@ public boolean canAccessHost(ProviderAdapterContext context, String hostname) {
return false;
}

private FlashArrayHost getHost(String hostname) {
FlashArrayList<FlashArrayHost> list = null;
FlashArrayHost getHost(String hostname) {
FlashArrayHost host = findHost(hostname);
String shortName = shortHostName(hostname);
if (host == null && !shortName.equals(hostname)) {
host = findHost(shortName);
}
return host;
}

// The array answers 400 for an unknown host name, which is a normal "not found" here
FlashArrayHost findHost(String name) {
try {
list = GET("/hosts?names=" + hostname,
FlashArrayList<FlashArrayHost> list = GET("/hosts?names=" + name,
new TypeReference<FlashArrayList<FlashArrayHost>>() {
});
return (FlashArrayHost) getFlashArrayItem(list);
} catch (Exception e) {

logger.debug("Host [" + name + "] not found in FlashArray at [" + url + "]: " + e.getMessage());
return null;
}
}

if (list == null) {
if (hostname.indexOf('.') > 0) {
list = GET("/hosts?names=" + hostname.substring(0, (hostname.indexOf('.'))),
new TypeReference<FlashArrayList<FlashArrayHost>>() {
});
}
}
return (FlashArrayHost) getFlashArrayItem(list);
static String shortHostName(String hostname) {
int dot = hostname.indexOf('.');
return dot > 0 ? hostname.substring(0, dot) : hostname;
}

private String getAccessToken() {
Expand Down Expand Up @@ -941,7 +949,7 @@ private FlashArrayVolume getSnapshot(String snapshotName) {
}

private Object getFlashArrayItem(FlashArrayList<?> list) {
if (list.getItems() != null && list.getItems().size() > 0) {
if (list != null && list.getItems() != null && list.getItems().size() > 0) {
return list.getItems().get(0);
} else {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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.adapter.flasharray;

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

public class FlashArrayAdapterTest {

private FlashArrayAdapter adapter() {
return mock(FlashArrayAdapter.class, Mockito.CALLS_REAL_METHODS);
}

@Test
public void shortHostNameStripsDomain() {
Assert.assertEquals("b08-36", FlashArrayAdapter.shortHostName("b08-36.lab.local"));
Assert.assertEquals("b08-36", FlashArrayAdapter.shortHostName("b08-36"));
}

@Test
public void getHostReturnsNullWhenHostIsMissing() {
FlashArrayAdapter adapter = adapter();
doReturn(null).when(adapter).findHost("b08-36");

Assert.assertNull(adapter.getHost("b08-36"));
Assert.assertFalse(adapter.canAccessHost(null, "b08-36"));
}

@Test
public void getHostFallsBackToShortName() {
FlashArrayAdapter adapter = adapter();
FlashArrayHost host = new FlashArrayHost();
doReturn(null).when(adapter).findHost("b08-36.lab.local");
doReturn(host).when(adapter).findHost("b08-36");

Assert.assertSame(host, adapter.getHost("b08-36.lab.local"));
Assert.assertTrue(adapter.canAccessHost(null, "b08-36.lab.local"));
}

@Test
public void getHostDoesNotRetryShortNameWithoutDomain() {
FlashArrayAdapter adapter = adapter();
FlashArrayHost host = new FlashArrayHost();
doReturn(host).when(adapter).findHost("b08-36");

Assert.assertSame(host, adapter.getHost("b08-36"));
verify(adapter, Mockito.times(1)).findHost("b08-36");
verify(adapter, never()).findHost("b08-36.lab.local");
}
}
Loading