Skip to content

Commit c84079b

Browse files
Srivastava, PiyushSrivastava, Piyush
authored andcommitted
file specific operation changes
1 parent 63e0f2f commit c84079b

File tree

2 files changed

+152
-11
lines changed

2 files changed

+152
-11
lines changed

plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/feign/client/NASFeignClient.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ public interface NASFeignClient {
4343
//File Operations
4444

4545
@RequestMapping(method = RequestMethod.GET, value="/{volume.uuid}/files/{path}")
46-
OntapResponse<FileInfo> getFileResponse(URI uri, @RequestHeader("Authorization") String header, @PathVariable(name = "volume.uuid", required = true) String volumeUUID,
47-
@PathVariable(name = "path", required = true) String filePath);
46+
OntapResponse<FileInfo> getFileResponse(URI uri, @RequestHeader("Authorization") String header, @PathVariable(name = "volume.uuid") String volumeUUID,
47+
@PathVariable(name = "path") String filePath);
4848
@RequestMapping(method = RequestMethod.DELETE, value="/{volume.uuid}/files/{path}")
49-
void deleteFile(URI uri, @RequestHeader("Authorization") String header, @PathVariable(name = "volume.uuid", required = true) String volumeUUID,
50-
@PathVariable(name = "path", required = true) String filePath);
49+
void deleteFile(URI uri, @RequestHeader("Authorization") String header, @PathVariable(name = "volume.uuid") String volumeUUID,
50+
@PathVariable(name = "path") String filePath);
5151
@RequestMapping(method = RequestMethod.PATCH, value="/{volume.uuid}/files/{path}")
52-
void updateFile(URI uri, @RequestHeader("Authorization") String header, @PathVariable(name = "volume.uuid", required = true) String volumeUUID,
53-
@PathVariable(name = "path", required = true) String filePath, @RequestBody FileInfo fileInfo);
52+
void updateFile(URI uri, @RequestHeader("Authorization") String header, @PathVariable(name = "volume.uuid") String volumeUUID,
53+
@PathVariable(name = "path") String filePath, @RequestBody FileInfo fileInfo);
5454
@RequestMapping(method = RequestMethod.POST, value="/{volume.uuid}/files/{path}")
55-
void createFile(URI uri, @RequestHeader("Authorization") String header, @PathVariable(name = "volume.uuid", required = true) String volumeUUID,
56-
@PathVariable(name = "path", required = true) String filePath, @RequestBody FileInfo file);
55+
void createFile(URI uri, @RequestHeader("Authorization") String header, @PathVariable(name = "volume.uuid") String volumeUUID,
56+
@PathVariable(name = "path") String filePath, @RequestBody FileInfo file);
5757

5858

5959

@@ -68,12 +68,12 @@ ExportPolicy createExportPolicy(URI uri, @RequestHeader("Authorization") String
6868
OntapResponse<ExportPolicy> getExportPolicyResponse(URI baseURL, @RequestHeader("Authorization") String header);
6969

7070
@RequestMapping(method = RequestMethod.GET, value="/{id}")
71-
OntapResponse<ExportPolicy> getExportPolicyById(URI baseURL, @RequestHeader("Authorization") String header, @PathVariable(name = "id", required = true) String id);
71+
OntapResponse<ExportPolicy> getExportPolicyById(URI baseURL, @RequestHeader("Authorization") String header, @PathVariable(name = "id") String id);
7272

7373
@RequestMapping(method = RequestMethod.DELETE, value="/{id}")
74-
void deleteExportPolicyById(URI baseURL, @RequestHeader("Authorization") String header, @PathVariable(name = "id", required = true) String id);
74+
void deleteExportPolicyById(URI baseURL, @RequestHeader("Authorization") String header, @PathVariable(name = "id") String id);
7575

7676
@RequestMapping(method = RequestMethod.PATCH, value="/{id}")
77-
OntapResponse<ExportPolicy> updateExportPolicy(URI baseURL, @RequestHeader("Authorization") String header, @PathVariable(name = "id", required = true) String id,
77+
OntapResponse<ExportPolicy> updateExportPolicy(URI baseURL, @RequestHeader("Authorization") String header, @PathVariable(name = "id") String id,
7878
@RequestBody ExportPolicy request);
7979
}

plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/service/UnifiedNASStrategy.java

100644100755
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,145 @@ public String enableNFS(String svmUuid) {
219219
throw new CloudRuntimeException("Failed to enable NFS: " + e.getMessage());
220220
}
221221
}
222+
223+
// TODO should we return boolean or string ?
224+
private boolean createFile(String volumeUuid, String filePath, Long fileSize) {
225+
s_logger.info("Creating file: {} in volume: {}", filePath, volumeUuid);
226+
227+
try {
228+
String authHeader = utils.generateAuthHeader(OntapStorage.Username, OntapStorage.Password);
229+
230+
FileInfo fileInfo = new FileInfo();
231+
fileInfo.setPath(filePath);
232+
fileInfo.setType(FileInfo.TypeEnum.FILE);
233+
234+
if (fileSize != null && fileSize > 0) {
235+
fileInfo.setSize(fileSize);
236+
}
237+
238+
URI createFileUrl = URI.create(Constants.HTTPS + OntapStorage.ManagementLIF +
239+
"/api/storage/volumes/" + volumeUuid + "/files" + filePath);
240+
241+
nasFeignClient.createFile(createFileUrl, authHeader, volumeUuid, filePath, fileInfo);
242+
243+
s_logger.info("File created successfully: {} in volume: {}", filePath, volumeUuid);
244+
return true;
245+
246+
} catch (FeignException e) {
247+
s_logger.error("Failed to create file: {} in volume: {}", filePath, volumeUuid, e);
248+
return false;
249+
} catch (Exception e) {
250+
s_logger.error("Exception while creating file: {} in volume: {}", filePath, volumeUuid, e);
251+
return false;
252+
}
253+
}
254+
255+
private boolean deleteFile(String volumeUuid, String filePath) {
256+
s_logger.info("Deleting file: {} from volume: {}", filePath, volumeUuid);
257+
258+
try {
259+
String authHeader = utils.generateAuthHeader(OntapStorage.Username, OntapStorage.Password);
260+
261+
// Check if file exists first
262+
if (!fileExists(volumeUuid, filePath)) {
263+
s_logger.warn("File does not exist: {} in volume: {}", filePath, volumeUuid);
264+
return false;
265+
}
266+
267+
URI deleteFileUrl = URI.create(Constants.HTTPS + OntapStorage.ManagementLIF +
268+
"/api/storage/volumes/" + volumeUuid + "/files" + filePath);
269+
270+
nasFeignClient.deleteFile(deleteFileUrl, authHeader, volumeUuid, filePath);
271+
272+
s_logger.info("File deleted successfully: {} from volume: {}", filePath, volumeUuid);
273+
return true;
274+
275+
} catch (FeignException e) {
276+
s_logger.error("Failed to delete file: {} from volume: {}", filePath, volumeUuid, e);
277+
return false;
278+
} catch (Exception e) {
279+
s_logger.error("Exception while deleting file: {} from volume: {}", filePath, volumeUuid, e);
280+
return false;
281+
}
282+
}
283+
284+
private boolean fileExists(String volumeUuid, String filePath) {
285+
s_logger.debug("Checking if file exists: {} in volume: {}", filePath, volumeUuid);
286+
287+
try {
288+
String authHeader = utils.generateAuthHeader(OntapStorage.Username, OntapStorage.Password);
289+
290+
// Build URI for file info retrieval - volume-specific endpoint
291+
URI getFileUrl = URI.create(Constants.HTTPS + OntapStorage.ManagementLIF +
292+
"/api/storage/volumes/" + volumeUuid + "/files" + filePath);
293+
294+
nasFeignClient.getFileResponse(getFileUrl, authHeader, volumeUuid, filePath);
295+
296+
s_logger.debug("File exists: {} in volume: {}", filePath, volumeUuid);
297+
return true;
298+
299+
} catch (FeignException e) {
300+
// TODO check the status code while testing for file not found error
301+
if (e.status() == 404) {
302+
s_logger.debug("File does not exist: {} in volume: {}", filePath, volumeUuid);
303+
return false;
304+
}
305+
s_logger.error("Error checking file existence: {} in volume: {}", filePath, volumeUuid, e);
306+
return false;
307+
} catch (Exception e) {
308+
s_logger.error("Exception while checking file existence: {} in volume: {}", filePath, volumeUuid, e);
309+
return false;
310+
}
311+
}
312+
313+
private OntapResponse<FileInfo> getFileInfo(String volumeUuid, String filePath) {
314+
s_logger.debug("Getting file info for: {} in volume: {}", filePath, volumeUuid);
315+
316+
try {
317+
String authHeader = utils.generateAuthHeader(OntapStorage.Username, OntapStorage.Password);
318+
319+
URI getFileUrl = URI.create(Constants.HTTPS + OntapStorage.ManagementLIF +
320+
"/api/storage/volumes/" + volumeUuid + "/files" + filePath);
321+
322+
OntapResponse<FileInfo> response = nasFeignClient.getFileResponse(getFileUrl, authHeader, volumeUuid, filePath);
323+
324+
s_logger.debug("Retrieved file info for: {} in volume: {}", filePath, volumeUuid);
325+
return response;
326+
327+
} catch (FeignException e) {
328+
if (e.status() == 404) {
329+
s_logger.debug("File not found: {} in volume: {}", filePath, volumeUuid);
330+
return null;
331+
}
332+
s_logger.error("Failed to get file info: {} in volume: {}", filePath, volumeUuid, e);
333+
throw new CloudRuntimeException("Failed to get file info: " + e.getMessage());
334+
} catch (Exception e) {
335+
s_logger.error("Exception while getting file info: {} in volume: {}", filePath, volumeUuid, e);
336+
throw new CloudRuntimeException("Failed to get file info: " + e.getMessage());
337+
}
338+
}
339+
340+
private boolean updateFile(String volumeUuid, String filePath, FileInfo fileInfo) {
341+
s_logger.info("Updating file: {} in volume: {}", filePath, volumeUuid);
342+
343+
try {
344+
String authHeader = utils.generateAuthHeader(OntapStorage.Username, OntapStorage.Password);
345+
346+
URI updateFileUrl = URI.create(Constants.HTTPS + OntapStorage.ManagementLIF +
347+
"/api/storage/volumes/" + volumeUuid + "/files" + filePath);
348+
349+
nasFeignClient.updateFile(updateFileUrl, authHeader, volumeUuid, filePath, fileInfo);
350+
351+
s_logger.info("File updated successfully: {} in volume: {}", filePath, volumeUuid);
352+
return true;
353+
354+
} catch (FeignException e) {
355+
s_logger.error("Failed to update file: {} in volume: {}", filePath, volumeUuid, e);
356+
return false;
357+
} catch (Exception e) {
358+
s_logger.error("Exception while updating file: {} in volume: {}", filePath, volumeUuid, e);
359+
return false;
360+
}
361+
}
362+
222363
}

0 commit comments

Comments
 (0)