Skip to content

Commit

Permalink
Improved workspace importer validation for workspaces that doesn't ha…
Browse files Browse the repository at this point in the history
…ve JSON State (#745)
  • Loading branch information
jcanizalez authored Feb 22, 2024
1 parent d65730b commit 951d0a3
Showing 1 changed file with 31 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public String importWorkspace(String apiToken, String apiUrl, WorkspaceImportReq
String result = "";

// Create the workspace
log.info("Importing Workspace: {}" , workspaceImportRequest.getName());
log.info("Importing Workspace: {}", workspaceImportRequest.getName());
Workspace workspace = createWorkspaceFromRequest(workspaceImportRequest);
try {
workspace = workspaceRepository.save(workspace);
Expand All @@ -229,10 +229,10 @@ public String importWorkspace(String apiToken, String apiUrl, WorkspaceImportReq

importVariables(variablesImporter, workspace);
log.info("Variables imported: {}", variablesImporter.size());
if(variablesImporter.size() > 0)
result += "<li>Variables imported successfully.</li>";
if (variablesImporter.size() > 0)
result += "<li>Variables imported successfully.</li>";
else
result += "<li>No variables to import.</li>";
result += "<li>No variables to import.</li>";
} catch (Exception e) {
log.error(e.getMessage());
result += "<li>There was an error importing the variables:" + e.getMessage() + "</li>";
Expand All @@ -243,10 +243,10 @@ public String importWorkspace(String apiToken, String apiUrl, WorkspaceImportReq
List<TagAttributes> tags = getTags(apiToken, apiUrl, workspaceImportRequest.getId());
importTags(tags, workspace);
log.info("Tags imported: {}", tags.size());
if(tags.size() > 0)
result += "<li>Tags imported successfully.</li>";
if (tags.size() > 0)
result += "<li>Tags imported successfully.</li>";
else
result += "<li>No tags to import.</li>";
result += "<li>No tags to import.</li>";
} catch (Exception e) {
log.error(e.getMessage());
result += "<li>There was an error importing the tags:" + e.getMessage() + "</li>";
Expand All @@ -262,7 +262,6 @@ public String importWorkspace(String apiToken, String apiUrl, WorkspaceImportReq
result += "<li>No state to import.</li>";
return result;
}


String stateDownloadUrl = lastState.getHostedStateDownloadUrl();
String stateDownloadJsonUrl = lastState.getHostedJsonStateDownloadUrl();
Expand All @@ -284,42 +283,44 @@ public String importWorkspace(String apiToken, String apiUrl, WorkspaceImportReq
workspace.getId().toString(),
history.getId().toString()));

try{
historyRepository.save(history);
log.info("History created: {}", history.getId());
try {
historyRepository.save(history);
log.info("History created: {}", history.getId());
} catch (Exception e) {
log.error(e.getMessage());
result += "<li>There was an error importing the state:" + e.getMessage() + "</li>";
}
catch(Exception e){

// Download state
try {
Resource state = downloadState(apiToken, stateDownloadUrl);
String terraformState = "";
terraformState = readResourceToString(state);
log.info("State downloaded: {}", terraformState.length());
storageTypeService.uploadState(workspace.getOrganization().getId().toString(),
workspace.getId().toString(), terraformState);
result += "<li>State imported successfully.</li>";

} catch (IOException e) {
log.error(e.getMessage());
result += "<li>There was an error importing the state:" + e.getMessage() + "</li>";
return result;
}

// Download JSON state
try {
Resource stateJson = downloadState(apiToken, stateDownloadJsonUrl);
Resource state = downloadState(apiToken, stateDownloadUrl);
String terraformStateJson = "";
String terraformState = "";
try {
terraformStateJson = readResourceToString(stateJson);
terraformState = readResourceToString(state);
log.info("State downloaded: {}", terraformState.length());
log.info("State JSON downloaded: {}", terraformStateJson.length());

} catch (IOException e) {
log.error(e.getMessage());
result += "<li>There was an error importing the state:" + e.getMessage() + "</li>";
return result;
}
terraformStateJson = readResourceToString(stateJson);
log.info("State JSON downloaded: {}", terraformStateJson.length());
storageTypeService.uploadTerraformStateJson(workspace.getOrganization().getId().toString(),
workspace.getId().toString(), terraformStateJson, history.getId().toString());

storageTypeService.uploadState(workspace.getOrganization().getId().toString(),
workspace.getId().toString(), terraformState);
} catch (Exception e) {
log.error(e.getMessage());
result += "<li>There was an error importing the state:" + e.getMessage() + "</li>";
result += "<li><b>Warning:</b> The JSON state file was not available. This means you can still execute plan, apply, and destroy operations, but you will not be able to view the JSON output in the Terrakube UI. <a href='https://developer.hashicorp.com/terraform/cloud-docs/api-docs/state-versions' >This feature is accessible for workspaces utilizing Terraform v1.3.0 or later.</a> Error:" + e.getMessage() + "</li>";
return result;
}
result += "<li>State imported successfully.</li>";

return result;
}

Expand Down

0 comments on commit 951d0a3

Please sign in to comment.