Skip to content

Commit

Permalink
handle still in-progress requests in getStoredTaskResultByRequestId (#86
Browse files Browse the repository at this point in the history
)
  • Loading branch information
WangLiNaruto authored Nov 27, 2024
1 parent 7988fac commit b2b9d88
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.eclipse.xpanse.tofu.maker.opentofu.service.OpenTofuResultPersistenceManage;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -41,7 +42,7 @@ public class OpenTofuMakerTaskResultApi {
@GetMapping(value = "/result/{requestId}", produces =
MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public OpenTofuResult getStoredTaskResultByRequestId(
public ResponseEntity<OpenTofuResult> getStoredTaskResultByRequestId(
@Parameter(name = "requestId",
description = "id of the request")
@PathVariable("requestId") String requestId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.eclipse.xpanse.tofu.maker.models.response.OpenTofuResult;
import org.eclipse.xpanse.tofu.maker.utils.OpenTofuResultSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

/**
Expand All @@ -26,10 +27,16 @@
public class OpenTofuResultPersistenceManage {

private static final String TF_RESULT_FILE_SUFFIX = ".dat";
private static final String TF_LOCK_FILE_NAME = ".terraform.tfstate.lock.info";

@Value("${failed.callback.response.store.location}")
private String failedCallbackStoreLocation;

@Value("${clean.workspace.after.deployment.enabled:true}")
private Boolean cleanWorkspaceAfterDeployment;

@Resource
private OpenTofuScriptsHelper scriptsHelper;
@Resource
private OpenTofuResultSerializer openTofuResultSerializer;

Expand Down Expand Up @@ -64,10 +71,13 @@ public void persistOpenTofuResult(OpenTofuResult result) {
* @param requestId requestId.
* @return OpenTofuResult.
*/
public OpenTofuResult retrieveOpenTofuResultByRequestId(String requestId) {
public ResponseEntity<OpenTofuResult> retrieveOpenTofuResultByRequestId(String requestId) {
String filePath = getFilePath(UUID.fromString(requestId));
File resultFile = new File(filePath + File.separator + requestId + TF_RESULT_FILE_SUFFIX);
if (!resultFile.exists() && !resultFile.isFile()) {
if (isDeployingInProgress(requestId)) {
return ResponseEntity.noContent().build();
}
throw new ResultAlreadyReturnedOrRequestIdInvalidException(
"Result file does not exist: " + resultFile.getAbsolutePath());
}
Expand All @@ -77,14 +87,26 @@ public OpenTofuResult retrieveOpenTofuResultByRequestId(String requestId) {
.deserialize(openTofuResultData);
fis.close();
deleteResultFileAndDirectory(new File(filePath));
return openTofuResult;
return ResponseEntity.ok(openTofuResult);
} catch (IOException e) {
log.error("Failed to retrieve OpenTofuResult for requestId: {}", requestId, e);
throw new ResultAlreadyReturnedOrRequestIdInvalidException(
"Failed to retrieve OpenTofuResult for requestId: " + requestId);
}
}

private boolean isDeployingInProgress(String requestId) {
String workspace = scriptsHelper.buildTaskWorkspace(requestId);
File targetFile;
if (cleanWorkspaceAfterDeployment) {
targetFile = new File(workspace);
return targetFile.exists() && targetFile.isDirectory();
} else {
targetFile = new File(workspace, TF_LOCK_FILE_NAME);
return targetFile.exists() && targetFile.isFile();
}
}

private void deleteResultFileAndDirectory(File resultFile) {
try {
deleteRecursively(resultFile);
Expand Down

0 comments on commit b2b9d88

Please sign in to comment.