From 8818e37952d7aa639746ac4113eec8da9d259bcd Mon Sep 17 00:00:00 2001 From: Daman Arora Date: Wed, 25 Nov 2026 13:44:43 -0500 Subject: [PATCH] flasharray: skip hosts missing on the array and say so on attach --- .../adapter/flasharray/FlashArrayAdapter.java | 44 +++++++----- .../flasharray/FlashArrayAdapterTest.java | 70 +++++++++++++++++++ 2 files changed, 96 insertions(+), 18 deletions(-) create mode 100644 plugins/storage/volume/flasharray/src/test/java/org/apache/cloudstack/storage/datastore/adapter/flasharray/FlashArrayAdapterTest.java diff --git a/plugins/storage/volume/flasharray/src/main/java/org/apache/cloudstack/storage/datastore/adapter/flasharray/FlashArrayAdapter.java b/plugins/storage/volume/flasharray/src/main/java/org/apache/cloudstack/storage/datastore/adapter/flasharray/FlashArrayAdapter.java index fae94bb1bea0..920779377c93 100644 --- a/plugins/storage/volume/flasharray/src/main/java/org/apache/cloudstack/storage/datastore/adapter/flasharray/FlashArrayAdapter.java +++ b/plugins/storage/volume/flasharray/src/main/java/org/apache/cloudstack/storage/datastore/adapter/flasharray/FlashArrayAdapter.java @@ -153,11 +153,13 @@ public String attach(ProviderAdapterContext context, ProviderAdapterDataObject d try { FlashArrayList list = null; FlashArrayHost host = getHost(hostname); - if (host != null) { - list = POST("/connections?host_names=" + host.getName() + "&volume_names=" + volumeName, null, - new TypeReference>() { - }); + 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>() { + }); if (list == null || list.getItems() == null || list.getItems().size() == 0) { throw new RuntimeException("Volume attach did not return lun information"); @@ -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(); } @@ -571,25 +573,31 @@ public boolean canAccessHost(ProviderAdapterContext context, String hostname) { return false; } - private FlashArrayHost getHost(String hostname) { - FlashArrayList 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 list = GET("/hosts?names=" + name, new TypeReference>() { }); + 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>() { - }); - } - } - return (FlashArrayHost) getFlashArrayItem(list); + static String shortHostName(String hostname) { + int dot = hostname.indexOf('.'); + return dot > 0 ? hostname.substring(0, dot) : hostname; } private String getAccessToken() { @@ -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; diff --git a/plugins/storage/volume/flasharray/src/test/java/org/apache/cloudstack/storage/datastore/adapter/flasharray/FlashArrayAdapterTest.java b/plugins/storage/volume/flasharray/src/test/java/org/apache/cloudstack/storage/datastore/adapter/flasharray/FlashArrayAdapterTest.java new file mode 100644 index 000000000000..c967641f79f8 --- /dev/null +++ b/plugins/storage/volume/flasharray/src/test/java/org/apache/cloudstack/storage/datastore/adapter/flasharray/FlashArrayAdapterTest.java @@ -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"); + } +}