3737import org .apache .logging .log4j .Logger ;
3838
3939import javax .inject .Inject ;
40+ import java .util .Map ;
4041import java .net .URI ;
4142import java .util .List ;
4243import java .util .Objects ;
@@ -69,42 +70,37 @@ public boolean connect() {
6970 s_logger .info ("Attempting to connect to ONTAP cluster at " + storage .getManagementLIF ());
7071 //Get AuthHeader
7172 String authHeader = utils .generateAuthHeader (storage .getUsername (), storage .getPassword ());
73+ String svmName = storage .getSvmName ();
7274 try {
7375 // Call the SVM API to check if the SVM exists
7476 Svm svm = null ;
75- URI url = URI .create (Constants .HTTPS + storage .getManagementLIF () + Constants .GETSVMs );
76- OntapResponse <Svm > svms = svmFeignClient .getSvms (url , authHeader );
77- for (Svm storageVM : svms .getRecords ()) {
78- if (storageVM .getName ().equals (storage .getSVM ())) {
79- svm = storageVM ;
80- s_logger .info ("Found SVM: " + storage .getSVM ());
81- break ;
82- }
77+ URI url = URI .create (Constants .HTTPS + storage .getManagementLIF () + Constants .GET_SVMs );
78+ Map <String , String > queryParams = Map .of ("name" , svmName );
79+ OntapResponse <Svm > svms = svmFeignClient .getSvmResponse (url , authHeader , queryParams );
80+ if (svms != null && svms .getRecords () != null && !svms .getRecords ().isEmpty ()) {
81+ svm = svms .getRecords ().get (0 );
82+ } else {
83+ throw new CloudRuntimeException ("No SVM found on the ONTAP cluster by the name" + svmName + "." );
8384 }
8485
8586 // Validations
86- if (svm == null ) {
87- s_logger .error ("SVM with name " + storage .getSVM () + " not found." );
88- throw new CloudRuntimeException ("SVM with name " + storage .getSVM () + " not found." );
89- } else {
90- if (svm .getState () != Constants .RUNNING ) {
91- s_logger .error ("SVM " + storage .getSVM () + " is not in running state." );
92- throw new CloudRuntimeException ("SVM " + storage .getSVM () + " is not in running state." );
93- }
94- if (Objects .equals (storage .getProtocol (), Constants .NFS ) && !svm .getNfsEnabled ()) {
95- s_logger .error ("NFS protocol is not enabled on SVM " + storage .getSVM ());
96- throw new CloudRuntimeException ("NFS protocol is not enabled on SVM " + storage .getSVM ());
97- } else if (Objects .equals (storage .getProtocol (), Constants .ISCSI ) && !svm .getIscsiEnabled ()) {
98- s_logger .error ("iSCSI protocol is not enabled on SVM " + storage .getSVM ());
99- throw new CloudRuntimeException ("iSCSI protocol is not enabled on SVM " + storage .getSVM ());
100- }
101- List <Aggregate > aggrs = svm .getAggregates ();
102- if (aggrs == null || aggrs .isEmpty ()) {
103- s_logger .error ("No aggregates are assigned to SVM " + storage .getSVM ());
104- throw new CloudRuntimeException ("No aggregates are assigned to SVM " + storage .getSVM ());
105- }
106- this .aggregates = aggrs ;
87+ if (!Objects .equals (svm .getState (), Constants .RUNNING )) {
88+ s_logger .error ("SVM " + svmName + " is not in running state." );
89+ throw new CloudRuntimeException ("SVM " + svmName + " is not in running state." );
90+ }
91+ if (Objects .equals (storage .getProtocol (), Constants .NFS ) && !svm .getNfsEnabled ()) {
92+ s_logger .error ("NFS protocol is not enabled on SVM " + svmName );
93+ throw new CloudRuntimeException ("NFS protocol is not enabled on SVM " + svmName );
94+ } else if (Objects .equals (storage .getProtocol (), Constants .ISCSI ) && !svm .getIscsiEnabled ()) {
95+ s_logger .error ("iSCSI protocol is not enabled on SVM " + svmName );
96+ throw new CloudRuntimeException ("iSCSI protocol is not enabled on SVM " + svmName );
10797 }
98+ List <Aggregate > aggrs = svm .getAggregates ();
99+ if (aggrs == null || aggrs .isEmpty ()) {
100+ s_logger .error ("No aggregates are assigned to SVM " + svmName );
101+ throw new CloudRuntimeException ("No aggregates are assigned to SVM " + svmName );
102+ }
103+ this .aggregates = aggrs ;
108104 s_logger .info ("Successfully connected to ONTAP cluster and validated ONTAP details provided" );
109105 } catch (Exception e ) {
110106 throw new CloudRuntimeException ("Failed to connect to ONTAP cluster: " + e .getMessage ());
@@ -116,17 +112,18 @@ public boolean connect() {
116112 public void createVolume (String volumeName , Long size ) {
117113 s_logger .info ("Creating volume: " + volumeName + " of size: " + size + " bytes" );
118114
115+ String svmName = storage .getSvmName ();
119116 if (aggregates == null || aggregates .isEmpty ()) {
120- s_logger .error ("No aggregates available to create volume on SVM " + storage . getSVM () );
121- throw new CloudRuntimeException ("No aggregates available to create volume on SVM " + storage . getSVM () );
117+ s_logger .error ("No aggregates available to create volume on SVM " + svmName );
118+ throw new CloudRuntimeException ("No aggregates available to create volume on SVM " + svmName );
122119 }
123120 // Get the AuthHeader
124121 String authHeader = utils .generateAuthHeader (storage .getUsername (), storage .getPassword ());
125122
126123 // Generate the Create Volume Request
127124 Volume volumeRequest = new Volume ();
128125 Svm svm = new Svm ();
129- svm .setName (storage . getSVM () );
126+ svm .setName (svmName );
130127
131128 volumeRequest .setName (volumeName );
132129 volumeRequest .setSvm (svm );
@@ -135,17 +132,20 @@ public void createVolume(String volumeName, Long size) {
135132 // Make the POST API call to create the volume
136133 try {
137134 // Create URI for POST CreateVolume API
138- URI url = utils .generateURI (Constants .CREATEVOLUME );
135+ URI url = utils .generateURI (Constants .CREATE_VOLUME );
139136 // Call the VolumeFeignClient to create the volume
140137 JobResponse jobResponse = volumeFeignClient .createVolumeWithJob (url , authHeader , volumeRequest );
138+ if (jobResponse == null || jobResponse .getJob () == null ) {
139+ throw new CloudRuntimeException ("Failed to initiate volume creation for " + volumeName );
140+ }
141141 String jobUUID = jobResponse .getJob ().getUuid ();
142142
143143 //Create URI for GET Job API
144- url = utils .generateURI (Constants .GETJOBBYUUID );
145- int jobRetryCount = 0 , maxJobRetries = Constants . JOBMAXRETRIES ;
144+ url = utils .generateURI (Constants .GET_JOB_BY_UUID );
145+ int jobRetryCount = 0 ;
146146 Job createVolumeJob = null ;
147- while (createVolumeJob == null || createVolumeJob .getState ().equals (Constants .JOBRUNNING ) || createVolumeJob . getState (). equals ( Constants . JOBQUEUE ) || createVolumeJob . getState (). equals ( Constants . JOBPAUSED )) {
148- if (jobRetryCount >= maxJobRetries ) {
147+ while (createVolumeJob == null || ! createVolumeJob .getState ().equals (Constants .JOB_SUCCESS )) {
148+ if (jobRetryCount >= Constants . JOB_MAX_RETRIES ) {
149149 s_logger .error ("Job to create volume " + volumeName + " did not complete within expected time." );
150150 throw new CloudRuntimeException ("Job to create volume " + volumeName + " did not complete within expected time." );
151151 }
@@ -154,15 +154,15 @@ public void createVolume(String volumeName, Long size) {
154154 createVolumeJob = jobFeignClient .getJobByUUID (url , authHeader , jobUUID );
155155 if (createVolumeJob == null ) {
156156 s_logger .warn ("Job with UUID " + jobUUID + " not found. Retrying..." );
157- } else if (createVolumeJob .getState ().equals (Constants .JOBFAILURE )) {
157+ } else if (createVolumeJob .getState ().equals (Constants .JOB_FAILURE )) {
158158 throw new CloudRuntimeException ("Job to create volume " + volumeName + " failed with error: " + createVolumeJob .getMessage ());
159159 }
160160 } catch (FeignException .FeignClientException e ) {
161161 throw new CloudRuntimeException ("Failed to fetch job status: " + e .getMessage ());
162162 }
163163
164164 jobRetryCount ++;
165- Thread .sleep (Constants .CREATEVOLUMECHECKSLEEPTIME ); // Sleep for 2 seconds before polling again
165+ Thread .sleep (Constants .CREATE_VOLUME_CHECK_SLEEP_TIME ); // Sleep for 2 seconds before polling again
166166 }
167167 } catch (Exception e ) {
168168 s_logger .error ("Exception while creating volume: " , e );
0 commit comments