2020import javax .inject .Inject ;
2121
2222import com .cloud .agent .api .ModifyStoragePoolCommand ;
23+ import com .cloud .agent .api .ModifyStoragePoolAnswer ;
24+ import com .cloud .agent .api .StoragePoolInfo ;
2325import com .cloud .alert .AlertManager ;
2426import com .cloud .storage .StoragePoolHostVO ;
2527import com .cloud .storage .dao .StoragePoolHostDao ;
3234import com .cloud .storage .StoragePool ;
3335import com .cloud .utils .exception .CloudRuntimeException ;
3436import org .apache .cloudstack .storage .datastore .db .PrimaryDataStoreDao ;
37+ import org .apache .cloudstack .storage .datastore .db .StoragePoolVO ;
3538import org .apache .cloudstack .engine .subsystem .api .storage .HypervisorHostListener ;
3639import com .cloud .host .dao .HostDao ;
3740
@@ -67,25 +70,8 @@ public boolean hostConnect(long hostId, long poolId) {
6770 logger .info ("Connecting host {} to ONTAP storage pool {}" , host .getName (), pool .getName ());
6871
6972
70- // incase host was not added by cloudstack , we will add it
71- StoragePoolHostVO storagePoolHost = storagePoolHostDao .findByPoolHost (poolId , hostId );
72-
73- if (storagePoolHost == null ) {
74- storagePoolHost = new StoragePoolHostVO (poolId , hostId , "" );
75-
76- storagePoolHostDao .persist (storagePoolHost );
77- }
78-
79- // Validate pool type - ONTAP supports NFS and iSCSI
80- // StoragePoolType poolType = pool.getPoolType();
81- // // TODO add iscsi also here
82- // if (poolType != StoragePoolType.NetworkFilesystem) {
83- // logger.error("Unsupported pool type {} for ONTAP storage", poolType);
84- // return false;
85- // }
86-
8773 try {
88- // Create the CreateStoragePoolCommand to send to the agent
74+ // Create the ModifyStoragePoolCommand to send to the agent
8975 ModifyStoragePoolCommand cmd = new ModifyStoragePoolCommand (true , pool );
9076
9177 Answer answer = _agentMgr .easySend (hostId , cmd );
@@ -102,6 +88,39 @@ public boolean hostConnect(long hostId, long poolId) {
10288 throw new CloudRuntimeException (String .format (
10389 "Unable to establish a connection from agent to storage pool %s due to %s" , pool , answer .getDetails ()));
10490 }
91+
92+ // Get the mount path from the answer
93+ ModifyStoragePoolAnswer mspAnswer = (ModifyStoragePoolAnswer ) answer ;
94+ StoragePoolInfo poolInfo = mspAnswer .getPoolInfo ();
95+ if (poolInfo == null ) {
96+ throw new CloudRuntimeException ("ModifyStoragePoolAnswer returned null poolInfo" );
97+ }
98+
99+ String localPath = poolInfo .getLocalPath ();
100+ logger .info ("Storage pool {} successfully mounted at: {}" , pool .getName (), localPath );
101+
102+ // Update or create the storage_pool_host_ref entry with the correct local_path
103+ StoragePoolHostVO storagePoolHost = storagePoolHostDao .findByPoolHost (poolId , hostId );
104+
105+ if (storagePoolHost == null ) {
106+ storagePoolHost = new StoragePoolHostVO (poolId , hostId , localPath );
107+ storagePoolHostDao .persist (storagePoolHost );
108+ logger .info ("Created storage_pool_host_ref entry for pool {} and host {}" , pool .getName (), host .getName ());
109+ } else {
110+ storagePoolHost .setLocalPath (localPath );
111+ storagePoolHostDao .update (storagePoolHost .getId (), storagePoolHost );
112+ logger .info ("Updated storage_pool_host_ref entry with local_path: {}" , localPath );
113+ }
114+
115+ // Update pool capacity/usage information
116+ StoragePoolVO poolVO = _storagePoolDao .findById (poolId );
117+ if (poolVO != null && poolInfo .getCapacityBytes () > 0 ) {
118+ poolVO .setCapacityBytes (poolInfo .getCapacityBytes ());
119+ poolVO .setUsedBytes (poolInfo .getCapacityBytes () - poolInfo .getAvailableBytes ());
120+ _storagePoolDao .update (poolVO .getId (), poolVO );
121+ logger .info ("Updated storage pool capacity: {} GB, used: {} GB" , poolInfo .getCapacityBytes () / (1024 * 1024 * 1024 ), (poolInfo .getCapacityBytes () - poolInfo .getAvailableBytes ()) / (1024 * 1024 * 1024 ));
122+ }
123+
105124 } catch (Exception e ) {
106125 logger .error ("Exception while connecting host {} to storage pool {}" , host .getName (), pool .getName (), e );
107126 throw new CloudRuntimeException ("Failed to connect host to storage pool: " + e .getMessage (), e );
0 commit comments