1919
2020package org .apache .cloudstack .storage .service ;
2121
22- import org .apache .cloudstack .storage .feign .model .OntapStorage ;
22+ import com .cloud .utils .exception .CloudRuntimeException ;
23+ import feign .FeignException ;
24+ import org .apache .cloudstack .storage .feign .client .NASFeignClient ;
25+ import org .apache .cloudstack .storage .feign .client .SvmFeignClient ;
26+ import org .apache .cloudstack .storage .feign .client .VolumeFeignClient ;
27+ import org .apache .cloudstack .storage .feign .model .*;
28+ import org .apache .cloudstack .storage .feign .model .response .OntapResponse ;
29+ import org .apache .cloudstack .storage .utils .Constants ;
30+ import org .apache .cloudstack .storage .utils .Utility ;
31+ import org .apache .logging .log4j .LogManager ;
32+ import org .apache .logging .log4j .Logger ;
33+
34+ import javax .inject .Inject ;
35+ import java .net .URI ;
2336
2437public class UnifiedNASStrategy extends NASStrategy {
38+
39+ @ Inject
40+ private Utility utils ;
41+
42+ @ Inject
43+ private NASFeignClient nasFeignClient ;
44+
45+ @ Inject
46+ private SvmFeignClient svmFeignClient ;
47+
48+ @ Inject
49+ private VolumeFeignClient volumeFeignClient ;
50+
51+ private static final Logger s_logger = LogManager .getLogger (NASStrategy .class );
2552 public UnifiedNASStrategy (OntapStorage ontapStorage ) {
2653 super (ontapStorage );
2754 }
2855
2956 @ Override
3057 public String createExportPolicy (String svmName , String policyName ) {
31- return "" ;
58+ s_logger .info ("Creating export policy: {} for SVM: {}" , policyName , svmName );
59+
60+ try {
61+ // Get AuthHeader
62+ String authHeader = utils .generateAuthHeader (OntapStorage .Username , OntapStorage .Password ); // TODO change these once ontapStorage is made singleton
63+
64+ // Create ExportPolicy object
65+ ExportPolicy exportPolicy = new ExportPolicy ();
66+ exportPolicy .setName (policyName );
67+
68+ // Set SVM
69+ Svm svm = new Svm ();
70+ svm .setName (svmName );
71+ exportPolicy .setSvm (svm );
72+
73+ // Create URI for export policy creation
74+ URI url = URI .create (Constants .HTTPS + OntapStorage .ManagementLIF + "/api/protocols/nfs/export-policies" ); // TODO move this to constants ?
75+
76+ // Create export policy
77+ ExportPolicy createdPolicy = nasFeignClient .createExportPolicy (url , authHeader , true , exportPolicy );
78+
79+ if (createdPolicy != null && createdPolicy .getId () != null ) {
80+ s_logger .info ("Export policy created successfully with ID: {}" , createdPolicy .getId ());
81+ return createdPolicy .getId ().toString ();
82+ } else {
83+ throw new CloudRuntimeException ("Failed to create export policy: " + policyName );
84+ }
85+
86+ } catch (FeignException e ) {
87+ s_logger .error ("Failed to create export policy: {}" , policyName , e );
88+ throw new CloudRuntimeException ("Failed to create export policy: " + e .getMessage ());
89+ } catch (Exception e ) {
90+ s_logger .error ("Exception while creating export policy: {}" , policyName , e );
91+ throw new CloudRuntimeException ("Failed to create export policy: " + e .getMessage ());
92+ }
93+ }
94+
95+ @ Override
96+ public boolean deleteExportPolicy (String svmName , String policyName ) {
97+ try {
98+ String authHeader = utils .generateAuthHeader (OntapStorage .Username , OntapStorage .Password );
99+
100+ // Get policy ID first
101+ URI getUrl = URI .create (Constants .HTTPS + OntapStorage .ManagementLIF +
102+ "/api/protocols/nfs/export-policies?name=" + policyName + "&svm.name=" + svmName ); // TODO move this to constants and how to dynamic pass params later ?
103+
104+ OntapResponse <ExportPolicy > policiesResponse = nasFeignClient .getExportPolicyResponse (getUrl , authHeader );
105+
106+ if (policiesResponse .getRecords () == null || policiesResponse .getRecords ().isEmpty ()) {
107+ s_logger .warn ("Export policy not found for deletion: {}" , policyName );
108+ return false ;
109+ }
110+
111+ String policyId = policiesResponse .getRecords ().get (0 ).getId ().toString ();
112+
113+ // Delete the policy
114+ URI deleteUrl = URI .create (Constants .HTTPS + OntapStorage .ManagementLIF +
115+ "/api/protocols/nfs/export-policies/" + policyId );
116+
117+ nasFeignClient .deleteExportPolicyById (deleteUrl , authHeader , policyId );
118+
119+ s_logger .info ("Export policy deleted successfully: {}" , policyName );
120+ return true ;
121+
122+ } catch (Exception e ) {
123+ s_logger .error ("Failed to delete export policy: {}" , policyName , e );
124+ return false ;
125+ }
126+ }
127+
128+ @ Override
129+ public boolean exportPolicyExists (String svmName , String policyName ) {
130+ try {
131+ String authHeader = utils .generateAuthHeader (OntapStorage .Username , OntapStorage .Password );
132+ URI url = URI .create (Constants .HTTPS + OntapStorage .ManagementLIF +
133+ "/api/protocols/nfs/export-policies?name=" + policyName + "&svm.name=" + svmName );
134+
135+ OntapResponse <ExportPolicy > response = nasFeignClient .getExportPolicyResponse (url , authHeader );
136+ return response .getRecords () != null && !response .getRecords ().isEmpty ();
137+
138+ } catch (Exception e ) {
139+ s_logger .warn ("Error checking export policy existence: {}" , e .getMessage ());
140+ return false ;
141+ }
32142 }
33143
34144 @ Override
@@ -38,11 +148,75 @@ public String addExportRule(String policyName, String clientMatch, String[] prot
38148
39149 @ Override
40150 public String assignExportPolicyToVolume (String volumeUuid , String policyName ) {
41- return "" ;
151+ s_logger .info ("Assigning export policy: {} to volume: {}" , policyName , volumeUuid );
152+
153+ try {
154+ // Get AuthHeader
155+ String authHeader = utils .generateAuthHeader (OntapStorage .Username , OntapStorage .Password );
156+
157+ // First, get the export policy by name
158+ URI getPolicyUrl = URI .create (Constants .HTTPS + OntapStorage .ManagementLIF +
159+ "/api/protocols/nfs/export-policies?name=" + policyName + "&svm.name=" + OntapStorage .Svm );
160+
161+ OntapResponse <ExportPolicy > policiesResponse = nasFeignClient .getExportPolicyResponse (getPolicyUrl , authHeader );
162+
163+ if (policiesResponse .getRecords () == null || policiesResponse .getRecords ().isEmpty ()) {
164+ throw new CloudRuntimeException ("Export policy not found: " + policyName );
165+ }
166+
167+ ExportPolicy exportPolicy = policiesResponse .getRecords ().get (0 );
168+
169+ // Create Volume update object with NAS configuration
170+ Volume volumeUpdate = new Volume ();
171+ Nas nas = new Nas ();
172+ nas .setExportPolicy (exportPolicy );
173+ volumeUpdate .setNas (nas );
174+
175+ // Update the volume
176+ URI updateVolumeUrl = URI .create (Constants .HTTPS + OntapStorage .ManagementLIF +
177+ "/api/storage/volumes/" + volumeUuid );
178+
179+ volumeFeignClient .updateVolumeRebalancing (updateVolumeUrl , authHeader , volumeUuid , volumeUpdate );
180+
181+ s_logger .info ("Export policy successfully assigned to volume: {}" , volumeUuid );
182+ return "Export policy " + policyName + " assigned to volume " + volumeUuid ;
183+
184+ } catch (FeignException e ) {
185+ s_logger .error ("Failed to assign export policy to volume: {}" , volumeUuid , e );
186+ throw new CloudRuntimeException ("Failed to assign export policy: " + e .getMessage ());
187+ } catch (Exception e ) {
188+ s_logger .error ("Exception while assigning export policy to volume: {}" , volumeUuid , e );
189+ throw new CloudRuntimeException ("Failed to assign export policy: " + e .getMessage ());
190+ }
42191 }
43192
44193 @ Override
45194 public String enableNFS (String svmUuid ) {
46- return "" ;
195+ s_logger .info ("Enabling NFS on SVM: {}" , svmUuid );
196+
197+ try {
198+ // Get AuthHeader
199+ String authHeader = utils .generateAuthHeader (OntapStorage .Username , OntapStorage .Password );
200+
201+ // Create SVM update object to enable NFS
202+ Svm svmUpdate = new Svm ();
203+ svmUpdate .setNfsEnabled (true );
204+
205+ // Update the SVM to enable NFS
206+ URI updateSvmUrl = URI .create (Constants .HTTPS + OntapStorage .ManagementLIF +
207+ "/api/svm/svms/" + svmUuid );
208+
209+ svmFeignClient .updateSVM (updateSvmUrl , authHeader , svmUpdate );
210+
211+ s_logger .info ("NFS successfully enabled on SVM: {}" , svmUuid );
212+ return "NFS enabled on SVM: " + svmUuid ;
213+
214+ } catch (FeignException e ) {
215+ s_logger .error ("Failed to enable NFS on SVM: {}" , svmUuid , e );
216+ throw new CloudRuntimeException ("Failed to enable NFS: " + e .getMessage ());
217+ } catch (Exception e ) {
218+ s_logger .error ("Exception while enabling NFS on SVM: {}" , svmUuid , e );
219+ throw new CloudRuntimeException ("Failed to enable NFS: " + e .getMessage ());
220+ }
47221 }
48222}
0 commit comments