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,36 @@ 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
74- Svm svm = null ;
75- URI url = URI .create (Constants .HTTPS + storage .getManagementLIF () + Constants .GET_SVMs );
76+ Svm svm = new Svm () ;
77+ URI url = URI .create (Constants .HTTPS + storage .getManagementLIF () + Constants .GET_SVMs + "?name=" + svmName );
7678 OntapResponse <Svm > svms = svmFeignClient .getSvmResponse (url , authHeader );
77- for (Svm storageVM : svms .getRecords ()) {
78- if (storageVM .getName ().equals (storage .getSvmName ())) {
79- svm = storageVM ;
80- s_logger .info ("Found SVM: " + storage .getSvmName ());
81- break ;
82- }
79+ if (svms != null && svms .getRecords () != null && !svms .getRecords ().isEmpty ()) {
80+ svm = svms .getRecords ().get (0 );
81+ } else {
82+ throw new CloudRuntimeException ("No SVM found on the ONTAP cluster by the name" + svmName + "." );
8383 }
8484
8585 // Validations
86- if (svm == null ) {
87- s_logger .error ("SVM with name " + storage .getSvmName () + " not found." );
88- throw new CloudRuntimeException ("SVM with name " + storage .getSvmName () + " not found." );
89- } else {
90- if (svm .getState () != Constants .RUNNING ) {
91- s_logger .error ("SVM " + storage .getSvmName () + " is not in running state." );
92- throw new CloudRuntimeException ("SVM " + storage .getSvmName () + " 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 .getSvmName ());
96- throw new CloudRuntimeException ("NFS protocol is not enabled on SVM " + storage .getSvmName ());
97- } else if (Objects .equals (storage .getProtocol (), Constants .ISCSI ) && !svm .getIscsiEnabled ()) {
98- s_logger .error ("iSCSI protocol is not enabled on SVM " + storage .getSvmName ());
99- throw new CloudRuntimeException ("iSCSI protocol is not enabled on SVM " + storage .getSvmName ());
100- }
101- List <Aggregate > aggrs = svm .getAggregates ();
102- if (aggrs == null || aggrs .isEmpty ()) {
103- s_logger .error ("No aggregates are assigned to SVM " + storage .getSvmName ());
104- throw new CloudRuntimeException ("No aggregates are assigned to SVM " + storage .getSvmName ());
105- }
106- this .aggregates = aggrs ;
86+ if (!Objects .equals (svm .getState (), Constants .RUNNING )) {
87+ s_logger .error ("SVM " + svmName + " is not in running state." );
88+ throw new CloudRuntimeException ("SVM " + svmName + " is not in running state." );
89+ }
90+ if (Objects .equals (storage .getProtocol (), Constants .NFS ) && !svm .getNfsEnabled ()) {
91+ s_logger .error ("NFS protocol is not enabled on SVM " + svmName );
92+ throw new CloudRuntimeException ("NFS protocol is not enabled on SVM " + svmName );
93+ } else if (Objects .equals (storage .getProtocol (), Constants .ISCSI ) && !svm .getIscsiEnabled ()) {
94+ s_logger .error ("iSCSI protocol is not enabled on SVM " + svmName );
95+ throw new CloudRuntimeException ("iSCSI protocol is not enabled on SVM " + svmName );
10796 }
97+ List <Aggregate > aggrs = svm .getAggregates ();
98+ if (aggrs == null || aggrs .isEmpty ()) {
99+ s_logger .error ("No aggregates are assigned to SVM " + svmName );
100+ throw new CloudRuntimeException ("No aggregates are assigned to SVM " + svmName );
101+ }
102+ this .aggregates = aggrs ;
108103 s_logger .info ("Successfully connected to ONTAP cluster and validated ONTAP details provided" );
109104 } catch (Exception e ) {
110105 throw new CloudRuntimeException ("Failed to connect to ONTAP cluster: " + e .getMessage ());
@@ -116,17 +111,18 @@ public boolean connect() {
116111 public void createVolume (String volumeName , Long size ) {
117112 s_logger .info ("Creating volume: " + volumeName + " of size: " + size + " bytes" );
118113
114+ String svmName = storage .getSvmName ();
119115 if (aggregates == null || aggregates .isEmpty ()) {
120- s_logger .error ("No aggregates available to create volume on SVM " + storage . getSvmName () );
121- throw new CloudRuntimeException ("No aggregates available to create volume on SVM " + storage . getSvmName () );
116+ s_logger .error ("No aggregates available to create volume on SVM " + svmName );
117+ throw new CloudRuntimeException ("No aggregates available to create volume on SVM " + svmName );
122118 }
123119 // Get the AuthHeader
124120 String authHeader = utils .generateAuthHeader (storage .getUsername (), storage .getPassword ());
125121
126122 // Generate the Create Volume Request
127123 Volume volumeRequest = new Volume ();
128124 Svm svm = new Svm ();
129- svm .setName (storage . getSvmName () );
125+ svm .setName (svmName );
130126
131127 volumeRequest .setName (volumeName );
132128 volumeRequest .setSvm (svm );
@@ -138,14 +134,17 @@ public void createVolume(String volumeName, Long size) {
138134 URI url = utils .generateURI (Constants .CREATE_VOLUME );
139135 // Call the VolumeFeignClient to create the volume
140136 JobResponse jobResponse = volumeFeignClient .createVolumeWithJob (url , authHeader , volumeRequest );
137+ if (jobResponse == null || jobResponse .getJob () == null ) {
138+ throw new CloudRuntimeException ("Failed to initiate volume creation for " + volumeName );
139+ }
141140 String jobUUID = jobResponse .getJob ().getUuid ();
142141
143142 //Create URI for GET Job API
144143 url = utils .generateURI (Constants .GET_JOB_BY_UUID );
145- int jobRetryCount = 0 , maxJobRetries = Constants . JOB_MAX_RETRIES ;
144+ int jobRetryCount = 0 ;
146145 Job createVolumeJob = null ;
147- while (createVolumeJob == null || createVolumeJob .getState ().equals (Constants .JOB_RUNNING ) || createVolumeJob . getState (). equals ( Constants . JOB_QUEUE ) || createVolumeJob . getState (). equals ( Constants . JOB_PAUSED )) {
148- if (jobRetryCount >= maxJobRetries ) {
146+ while (createVolumeJob == null || ! createVolumeJob .getState ().equals (Constants .JOB_SUCCESS )) {
147+ if (jobRetryCount >= Constants . JOB_MAX_RETRIES ) {
149148 s_logger .error ("Job to create volume " + volumeName + " did not complete within expected time." );
150149 throw new CloudRuntimeException ("Job to create volume " + volumeName + " did not complete within expected time." );
151150 }
@@ -170,4 +169,4 @@ public void createVolume(String volumeName, Long size) {
170169 }
171170 s_logger .info ("Volume created successfully: " + volumeName );
172171 }
173- }
172+ }
0 commit comments