@@ -42,19 +42,14 @@ public class OpenTofuResultPersistenceManage {
42
42
* @param result OpenTofuResult.
43
43
*/
44
44
public void persistOpenTofuResult (OpenTofuResult result ) {
45
- String filePath = getFilePath (result .getRequestId ());
46
- File file = new File (filePath );
47
- if (!file .exists () && !file .mkdirs ()) {
45
+ File filePath = getFilePath (result .getRequestId ());
46
+ if (!filePath .exists () && !filePath .mkdirs ()) {
48
47
log .error ("Failed to create directory {}" , filePath );
49
48
return ;
50
49
}
50
+ File file = new File (filePath , getFileName (result .getRequestId ()));
51
51
byte [] openTofuResultData = openTofuResultSerializer .serialize (result );
52
- try (FileOutputStream fos =
53
- new FileOutputStream (
54
- file .getPath ()
55
- + File .separator
56
- + result .getRequestId ()
57
- + TF_RESULT_FILE_SUFFIX )) {
52
+ try (FileOutputStream fos = new FileOutputStream (file )) {
58
53
fos .write (openTofuResultData );
59
54
log .info (
60
55
"openTofu result successfully stored to directoryName: {}" ,
@@ -74,10 +69,10 @@ public void persistOpenTofuResult(OpenTofuResult result) {
74
69
* @param requestId requestId.
75
70
* @return OpenTofuResult.
76
71
*/
77
- public ResponseEntity <OpenTofuResult > retrieveOpenTofuResultByRequestId (String requestId ) {
78
- String filePath = getFilePath (UUID .fromString (requestId ));
79
- File resultFile = new File (filePath + File .separator + requestId + TF_RESULT_FILE_SUFFIX );
72
+ public ResponseEntity <OpenTofuResult > retrieveOpenTofuResultByRequestId (UUID requestId ) {
73
+ File resultFile = new File (getFilePath (requestId ), getFileName (requestId ));
80
74
if (!resultFile .exists () && !resultFile .isFile ()) {
75
+ log .warn ("Result file does not exist: {}" , resultFile .getAbsolutePath ());
81
76
if (isDeployingInProgress (requestId )) {
82
77
return ResponseEntity .noContent ().build ();
83
78
}
@@ -89,7 +84,7 @@ public ResponseEntity<OpenTofuResult> retrieveOpenTofuResultByRequestId(String r
89
84
OpenTofuResult openTofuResult =
90
85
openTofuResultSerializer .deserialize (openTofuResultData );
91
86
fis .close ();
92
- deleteResultFileAndDirectory (new File ( filePath ) );
87
+ deleteResultFileAndDirectory (resultFile );
93
88
return ResponseEntity .ok (openTofuResult );
94
89
} catch (IOException e ) {
95
90
log .error ("Failed to retrieve OpenTofuResult for requestId: {}" , requestId , e );
@@ -98,8 +93,8 @@ public ResponseEntity<OpenTofuResult> retrieveOpenTofuResultByRequestId(String r
98
93
}
99
94
}
100
95
101
- private boolean isDeployingInProgress (String requestId ) {
102
- String workspace = scriptsHelper .buildTaskWorkspace (requestId );
96
+ private boolean isDeployingInProgress (UUID requestId ) {
97
+ String workspace = scriptsHelper .buildTaskWorkspace (requestId . toString () );
103
98
File targetFile ;
104
99
if (cleanWorkspaceAfterDeployment ) {
105
100
targetFile = new File (workspace );
@@ -113,10 +108,9 @@ private boolean isDeployingInProgress(String requestId) {
113
108
private void deleteResultFileAndDirectory (File resultFile ) {
114
109
try {
115
110
deleteRecursively (resultFile );
116
- log .info ("File folder deleted successfully: " + resultFile .getAbsolutePath ());
111
+ log .info ("File folder deleted successfully: {}" , resultFile .getAbsolutePath ());
117
112
} catch (Exception e ) {
118
- log .error ("An error occurred while deleting files: " + e .getMessage ());
119
- e .printStackTrace ();
113
+ log .error ("An error occurred while deleting files: {}" , e .getMessage ());
120
114
}
121
115
}
122
116
@@ -132,7 +126,11 @@ private void deleteRecursively(File file) {
132
126
file .delete ();
133
127
}
134
128
135
- private String getFilePath (UUID requestId ) {
136
- return failedCallbackStoreLocation + File .separator + requestId .toString ();
129
+ private File getFilePath (UUID requestId ) {
130
+ return new File (failedCallbackStoreLocation + File .separator + requestId );
131
+ }
132
+
133
+ private String getFileName (UUID requestId ) {
134
+ return requestId + TF_RESULT_FILE_SUFFIX ;
137
135
}
138
136
}
0 commit comments