@@ -133,18 +133,15 @@ private SolidFireConnection getSolidFireConnection(long storagePoolId) {
133133 return new SolidFireConnection (mVip , mPort , clusterAdminUsername , clusterAdminPassword );
134134 }
135135
136- private SolidFireUtil .SolidFireAccount createSolidFireAccount (String sfAccountName ,
137- SolidFireConnection sfConnection ) {
136+ private SolidFireUtil .SolidFireAccount createSolidFireAccount (String sfAccountName , SolidFireConnection sfConnection ) {
138137 String mVip = sfConnection .getManagementVip ();
139138 int mPort = sfConnection .getManagementPort ();
140139 String clusterAdminUsername = sfConnection .getClusterAdminUsername ();
141140 String clusterAdminPassword = sfConnection .getClusterAdminPassword ();
142141
143- long accountNumber = SolidFireUtil .createSolidFireAccount (mVip , mPort ,
144- clusterAdminUsername , clusterAdminPassword , sfAccountName );
142+ long accountNumber = SolidFireUtil .createSolidFireAccount (mVip , mPort , clusterAdminUsername , clusterAdminPassword , sfAccountName );
145143
146- return SolidFireUtil .getSolidFireAccountById (mVip , mPort ,
147- clusterAdminUsername , clusterAdminPassword , accountNumber );
144+ return SolidFireUtil .getSolidFireAccountById (mVip , mPort , clusterAdminUsername , clusterAdminPassword , accountNumber );
148145 }
149146
150147 private void updateCsDbWithAccountInfo (long csAccountId , SolidFireUtil .SolidFireAccount sfAccount ) {
@@ -185,8 +182,7 @@ private class ChapInfoImpl implements ChapInfo {
185182 private final String _targetUsername ;
186183 private final String _targetSecret ;
187184
188- public ChapInfoImpl (String initiatorUsername , String initiatorSecret ,
189- String targetUsername , String targetSecret ) {
185+ public ChapInfoImpl (String initiatorUsername , String initiatorSecret , String targetUsername , String targetSecret ) {
190186 _initiatorUsername = initiatorUsername ;
191187 _initiatorSecret = initiatorSecret ;
192188 _targetUsername = targetUsername ;
@@ -269,16 +265,39 @@ public boolean connectVolumeToHost(VolumeInfo volumeInfo, Host host, DataStore d
269265 SolidFireUtil .SolidFireVag sfVag = SolidFireUtil .getSolidFireVag (sfConnection .getManagementVip (), sfConnection .getManagementPort (),
270266 sfConnection .getClusterAdminUsername (), sfConnection .getClusterAdminPassword (), Long .parseLong (vagId ));
271267
268+ String [] hostIqns = getNewHostIqns (sfVag .getInitiators (), getIqnsFromHosts (hosts ));
272269 long [] volumeIds = getNewVolumeIds (sfVag .getVolumeIds (), sfVolumeId , true );
273270
274271 SolidFireUtil .modifySolidFireVag (sfConnection .getManagementVip (), sfConnection .getManagementPort (),
275272 sfConnection .getClusterAdminUsername (), sfConnection .getClusterAdminPassword (), sfVag .getId (),
276- getIqnsFromHosts ( hosts ) , volumeIds );
273+ hostIqns , volumeIds );
277274 }
278275 else {
279- long lVagId = SolidFireUtil .createSolidFireVag (sfConnection .getManagementVip (), sfConnection .getManagementPort (),
280- sfConnection .getClusterAdminUsername (), sfConnection .getClusterAdminPassword (), "CloudStack-" + UUID .randomUUID ().toString (),
281- getIqnsFromHosts (hosts ), new long [] { sfVolumeId });
276+ long lVagId ;
277+
278+ try {
279+ lVagId = SolidFireUtil .createSolidFireVag (sfConnection .getManagementVip (), sfConnection .getManagementPort (),
280+ sfConnection .getClusterAdminUsername (), sfConnection .getClusterAdminPassword (), "CloudStack-" + UUID .randomUUID ().toString (),
281+ getIqnsFromHosts (hosts ), new long [] { sfVolumeId });
282+ }
283+ catch (Exception ex ) {
284+ String iqnInVagAlready = "Exceeded maximum number of Volume Access Groups per initiator" ;
285+
286+ if (!ex .getMessage ().contains (iqnInVagAlready )) {
287+ throw new CloudRuntimeException (ex .getMessage ());
288+ }
289+
290+ // getCompatibleVag throws an exception if an existing VAG can't be located
291+ SolidFireUtil .SolidFireVag sfVag = getCompatibleVag (hosts , sfConnection );
292+
293+ long [] volumeIds = getNewVolumeIds (sfVag .getVolumeIds (), sfVolumeId , true );
294+
295+ SolidFireUtil .modifySolidFireVag (sfConnection .getManagementVip (), sfConnection .getManagementPort (),
296+ sfConnection .getClusterAdminUsername (), sfConnection .getClusterAdminPassword (), sfVag .getId (),
297+ sfVag .getInitiators (), volumeIds );
298+
299+ lVagId = sfVag .getId ();
300+ }
282301
283302 clusterDetail = new ClusterDetailsVO (clusterId , getVagKey (storagePoolId ), String .valueOf (lVagId ));
284303
@@ -288,6 +307,48 @@ public boolean connectVolumeToHost(VolumeInfo volumeInfo, Host host, DataStore d
288307 return true ;
289308 }
290309
310+ // this method takes in a collection of hosts and tries to find an existing VAG that has all three of them in it
311+ // if successful, the VAG is returned; else, a CloudRuntimeException is thrown and this issue should be corrected by an admin
312+ private SolidFireUtil .SolidFireVag getCompatibleVag (List <HostVO > hosts , SolidFireConnection sfConnection ) {
313+ List <SolidFireUtil .SolidFireVag > sfVags = SolidFireUtil .getAllSolidFireVags (sfConnection .getManagementVip (), sfConnection .getManagementPort (),
314+ sfConnection .getClusterAdminUsername (), sfConnection .getClusterAdminPassword ());
315+
316+ if (sfVags != null ) {
317+ List <String > hostIqns = new ArrayList <String >();
318+
319+ // where the method we're in is called, hosts should not be null
320+ for (HostVO host : hosts ) {
321+ // where the method we're in is called, host.getStorageUrl() should not be null (it actually should start with "iqn")
322+ hostIqns .add (host .getStorageUrl ().toLowerCase ());
323+ }
324+
325+ for (SolidFireUtil .SolidFireVag sfVag : sfVags ) {
326+ List <String > lstInitiators = getStringArrayAsLowerCaseStringList (sfVag .getInitiators ());
327+
328+ // lstInitiators should not be returned from getStringArrayAsLowerCaseStringList as null
329+ if (lstInitiators .containsAll (hostIqns )) {
330+ return sfVag ;
331+ }
332+ }
333+ }
334+
335+ throw new CloudRuntimeException ("Unable to locate the appropriate SolidFire Volume Access Group" );
336+ }
337+
338+ private List <String > getStringArrayAsLowerCaseStringList (String [] aString ) {
339+ List <String > lstLowerCaseString = new ArrayList <String >();
340+
341+ if (aString != null ) {
342+ for (String str : aString ) {
343+ if (str != null ) {
344+ lstLowerCaseString .add (str .toLowerCase ());
345+ }
346+ }
347+ }
348+
349+ return lstLowerCaseString ;
350+ }
351+
291352 // get the VAG associated with volumeInfo's cluster, if any (ListVolumeAccessGroups) // might not exist if using CHAP
292353 // if the VAG exists
293354 // remove the ID of volumeInfo from the VAG (ModifyVolumeAccessGroup)
@@ -314,11 +375,12 @@ public void disconnectVolumeFromHost(VolumeInfo volumeInfo, Host host, DataStore
314375 SolidFireUtil .SolidFireVag sfVag = SolidFireUtil .getSolidFireVag (sfConnection .getManagementVip (), sfConnection .getManagementPort (),
315376 sfConnection .getClusterAdminUsername (), sfConnection .getClusterAdminPassword (), Long .parseLong (vagId ));
316377
378+ String [] hostIqns = getNewHostIqns (sfVag .getInitiators (), getIqnsFromHosts (hosts ));
317379 long [] volumeIds = getNewVolumeIds (sfVag .getVolumeIds (), sfVolumeId , false );
318380
319381 SolidFireUtil .modifySolidFireVag (sfConnection .getManagementVip (), sfConnection .getManagementPort (),
320382 sfConnection .getClusterAdminUsername (), sfConnection .getClusterAdminPassword (), sfVag .getId (),
321- getIqnsFromHosts ( hosts ) , volumeIds );
383+ hostIqns , volumeIds );
322384 }
323385 }
324386
@@ -336,6 +398,26 @@ private boolean hostsSupport_iScsi(List<HostVO> hosts) {
336398 return true ;
337399 }
338400
401+ private String [] getNewHostIqns (String [] currentIqns , String [] newIqns ) {
402+ List <String > lstIqns = new ArrayList <String >();
403+
404+ if (currentIqns != null ) {
405+ for (String currentIqn : currentIqns ) {
406+ lstIqns .add (currentIqn );
407+ }
408+ }
409+
410+ if (newIqns != null ) {
411+ for (String newIqn : newIqns ) {
412+ if (!lstIqns .contains (newIqn )) {
413+ lstIqns .add (newIqn );
414+ }
415+ }
416+ }
417+
418+ return lstIqns .toArray (new String [0 ]);
419+ }
420+
339421 private long [] getNewVolumeIds (long [] volumeIds , long volumeIdToAddOrRemove , boolean add ) {
340422 if (add ) {
341423 return getNewVolumeIdsAdd (volumeIds , volumeIdToAddOrRemove );
0 commit comments