Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved workspace importer validation for workspaces that don't have JSON State #745

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading