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

update terraform-boot healthcheck method #9

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 @@ -11,7 +11,6 @@
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.xpanse.terraform.boot.models.SystemStatus;
import org.eclipse.xpanse.terraform.boot.models.enums.HealthStatus;
import org.eclipse.xpanse.terraform.boot.models.request.TerraformDeployRequest;
import org.eclipse.xpanse.terraform.boot.models.request.TerraformDestroyRequest;
import org.eclipse.xpanse.terraform.boot.models.request.async.TerraformAsyncDeployRequest;
Expand Down Expand Up @@ -58,9 +57,7 @@ public TerraformApiController(TerraformExecutor terraformExecutor) {
@GetMapping(value = "/health", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public SystemStatus healthCheck() {
SystemStatus systemStatus = new SystemStatus();
systemStatus.setHealthStatus(HealthStatus.OK);
return systemStatus;
return this.terraformExecutor.tfHealthCheck();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,16 @@ public Response handleHttpMessageConversionException(HttpMessageConversionExcept
return Response.errorResponse(ResultType.BAD_PARAMETERS,
Collections.singletonList(failMessage));
}

/**
* Exception handler for TerraformHealthCheckException.
*/
@ExceptionHandler({TerraformHealthCheckException.class})
@ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
public Response handleTerraformHealthCheckException(TerraformHealthCheckException ex) {
log.error("TerraformHealthCheckException: ", ex);
String failMessage = ex.getMessage();
return Response.errorResponse(ResultType.SERVICE_UNAVAILABLE,
Collections.singletonList(failMessage));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*/

package org.eclipse.xpanse.terraform.boot.models.exceptions;

/**
* Used to indicate Terraform health check anomalies.
*/
public class TerraformHealthCheckException extends RuntimeException {

public TerraformHealthCheckException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public enum ResultType {
ACCESS_DENIED("Access Denied"),
SENSITIVE_FIELD_ENCRYPTION_DECRYPTION_EXCEPTION("Sensitive "
+ "Field Encryption Or Decryption Failed Exception"),
UNSUPPORTED_ENUM_VALUE("Unsupported Enum Value");
UNSUPPORTED_ENUM_VALUE("Unsupported Enum Value"),
SERVICE_UNAVAILABLE("Service Unavailable");

private final String value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.xpanse.terraform.boot.models.SystemStatus;
import org.eclipse.xpanse.terraform.boot.models.enums.HealthStatus;
import org.eclipse.xpanse.terraform.boot.models.exceptions.TerraformExecutorException;
import org.eclipse.xpanse.terraform.boot.models.exceptions.TerraformHealthCheckException;
import org.eclipse.xpanse.terraform.boot.models.request.TerraformDeployRequest;
import org.eclipse.xpanse.terraform.boot.models.request.TerraformDestroyRequest;
import org.eclipse.xpanse.terraform.boot.models.request.async.TerraformAsyncDeployRequest;
Expand All @@ -41,6 +46,13 @@
public class TerraformExecutor {
private static final String VARS_FILE_NAME = "variables.tfvars.json";
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final String TEST_FILE_NAME = "hello-world.tf";
private static final String HEALTH_CHECK_DIR = UUID.randomUUID().toString();
private static final String HELLO_WORLD_TEMPLATE = """
output "hello_world" {
value = "Hello, World!"
}
""";

static {
OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
Expand Down Expand Up @@ -254,6 +266,37 @@ public TerraformValidationResult tfValidate(String moduleDirectory) {
}
}

/**
* Perform Terraform health checks by creating a Terraform test configuration file.
*
* @return SystemStatus.
*/
public SystemStatus tfHealthCheck() {
String filePath = getModuleFullPath(HEALTH_CHECK_DIR) + File.separator
+ TEST_FILE_NAME;
try {
File file = new File(filePath);
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
FileWriter writer = new FileWriter(filePath);
writer.write(HELLO_WORLD_TEMPLATE);
writer.close();
} catch (IOException e) {
throw new TerraformHealthCheckException(
"Error creating or writing to file '" + filePath + "': " + e.getMessage());
}
TerraformValidationResult terraformValidationResult = tfValidate(HEALTH_CHECK_DIR);
SystemStatus systemStatus = new SystemStatus();
if (terraformValidationResult.isValid()) {
systemStatus.setHealthStatus(HealthStatus.OK);
return systemStatus;
}
systemStatus.setHealthStatus(HealthStatus.NOK);
return systemStatus;
}

private String getModuleFullPath(String moduleDirectory) {
return this.moduleParentDirectoryPath + File.separator + moduleDirectory;
}
Expand Down