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

Add support to show current state version Outputs #732

Merged
merged 7 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
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 @@ -14,6 +14,7 @@
import org.terrakube.api.plugin.state.model.configuration.ConfigurationData;
import org.terrakube.api.plugin.state.model.entitlement.EntitlementData;
import org.terrakube.api.plugin.state.model.organization.OrganizationData;
import org.terrakube.api.plugin.state.model.outputs.StateOutputs;
import org.terrakube.api.plugin.state.model.plan.PlanRunData;
import org.terrakube.api.plugin.state.model.apply.ApplyRunData;
import org.terrakube.api.plugin.state.model.runs.RunsData;
Expand Down Expand Up @@ -240,6 +241,13 @@ public ResponseEntity<String> getApplyLogs(@PathVariable("applyId") int planId)
return ResponseEntity.of(Optional.ofNullable(new String(remoteTfeService.getApplyLogs(planId), StandardCharsets.UTF_8)));
}

@Transactional
@GetMapping(produces = "application/vnd.api+json", path = "/workspaces/{workspaceId}/current-state-version-outputs")
public ResponseEntity<StateOutputs> getCurrentOutputs(@PathVariable("workspaceId") String workspaceId) {
log.info("Get current outputs for: {}", workspaceId);
return ResponseEntity.of(Optional.ofNullable(remoteTfeService.getCurrentOutputs(workspaceId)));
}

@GetMapping(
value = "configuration-versions/{planId}/terraformContent.tar.gz",
produces = MediaType.APPLICATION_OCTET_STREAM_VALUE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
import org.terrakube.api.plugin.state.model.generic.Resource;
import org.terrakube.api.plugin.state.model.organization.OrganizationData;
import org.terrakube.api.plugin.state.model.organization.OrganizationModel;
import org.terrakube.api.plugin.state.model.outputs.OutputData;
import org.terrakube.api.plugin.state.model.outputs.StateOutputs;
import org.terrakube.api.plugin.state.model.plan.PlanRunData;
import org.terrakube.api.plugin.state.model.plan.PlanRunModel;
import org.terrakube.api.plugin.state.model.runs.*;
import org.terrakube.api.plugin.state.model.state.StateData;
import org.terrakube.api.plugin.state.model.state.StateModel;
import org.terrakube.api.plugin.state.model.terraform.TerraformState;
import org.terrakube.api.plugin.state.model.workspace.WorkspaceData;
import org.terrakube.api.plugin.state.model.workspace.WorkspaceList;
import org.terrakube.api.plugin.state.model.workspace.WorkspaceModel;
Expand Down Expand Up @@ -969,4 +972,51 @@ byte[] getApplyLogs(int planId) {
}
return logs;
}

StateOutputs getCurrentOutputs(String workspaceId) {
StateOutputs stateOutputs = new StateOutputs();
stateOutputs.setData(new ArrayList());
try {
Optional<Workspace> searchWorkspace = workspaceRepository.findById(UUID.fromString(workspaceId));
if (searchWorkspace.isPresent()) {
Workspace workspace = searchWorkspace.get();
byte[] currentState = storageTypeService.getCurrentTerraformState(workspace.getOrganization().getId().toString(), workspaceId);
String state = new String(currentState, StandardCharsets.UTF_8);
TerraformState terraformState = new ObjectMapper().readValue(state, TerraformState.class);

terraformState.getOutputs().forEach((mapKey, mapValue) ->{
log.info("Processing Key output: {}", mapKey);

OutputData outputData = new OutputData();
outputData.setId(UUID.randomUUID().toString());
outputData.setType("state-version-outputs");
outputData.setAttributes(new HashMap());
outputData.getAttributes().put("name", mapKey);

LinkedHashMap linkedHashMap = (LinkedHashMap) mapValue;

outputData.getAttributes().put("value", linkedHashMap.get("value"));

if (linkedHashMap.get("sensitive") != null)
outputData.getAttributes().put("sensitive", linkedHashMap.get("sensitive") );
else
outputData.getAttributes().put("sensitive", false );

if (linkedHashMap.get("type") instanceof String ) {
outputData.getAttributes().put("type", "string");
outputData.getAttributes().put("detailed-type", "string");
} else {
outputData.getAttributes().put("type", "object");
outputData.getAttributes().put("detailed-type", linkedHashMap.get("type"));
}
stateOutputs.getData().add(outputData);

});
}
} catch (Exception ex) {
log.error(ex.getMessage());
}

return stateOutputs;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.terrakube.api.plugin.state.model.outputs;

import lombok.Getter;
import lombok.Setter;

import java.util.Map;

@Getter
@Setter
public class OutputData {

String id;
String type;
Map<String, Object> attributes;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.terrakube.api.plugin.state.model.outputs;

import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
public class StateOutputs {
List<OutputData> data;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.terrakube.api.plugin.state.model.terraform;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.Map;

@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
@ToString
public class TerraformState {
Map<String, Map<String, Object>> outputs;
}
18 changes: 18 additions & 0 deletions thunder-tests/thunderclient.json
Original file line number Diff line number Diff line change
Expand Up @@ -2789,5 +2789,23 @@
"value": "{{sshId}}"
}
]
},
{
"_id": "0638c31f-c560-4322-bb92-8465d95eb72d",
"colId": "59b30fd1-9a5b-4dab-aaf1-a6bfc4706ff2",
"containerId": "1e847596-c3bf-42d4-90d4-26e522a90d0d",
"name": "current outputs",
"url": "{{terrakubeApi}}/remote/tfe/v2/workspaces/{{workspaceId}}/current-state-version-outputs",
"method": "GET",
"sortNum": 220000,
"created": "2024-02-19T21:57:17.767Z",
"modified": "2024-02-19T22:04:20.313Z",
"headers": [],
"params": [],
"auth": {
"type": "bearer",
"bearer": "{{access_token}}"
},
"tests": []
}
]
Loading