From 687319d4aba43a9b76d99f8dbfa97c6f098b0524 Mon Sep 17 00:00:00 2001 From: Nicolas Cohen Date: Tue, 6 Aug 2019 13:28:17 -0700 Subject: [PATCH] chore(manifest): Map service settings to key --- .../services/v1/RequestGenerateService.java | 45 +++++++++++++++---- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/halyard-deploy/src/main/java/com/netflix/spinnaker/halyard/deploy/services/v1/RequestGenerateService.java b/halyard-deploy/src/main/java/com/netflix/spinnaker/halyard/deploy/services/v1/RequestGenerateService.java index d8aa9297a8..f1543ce7fc 100644 --- a/halyard-deploy/src/main/java/com/netflix/spinnaker/halyard/deploy/services/v1/RequestGenerateService.java +++ b/halyard-deploy/src/main/java/com/netflix/spinnaker/halyard/deploy/services/v1/RequestGenerateService.java @@ -22,10 +22,7 @@ import com.netflix.spinnaker.halyard.config.config.v1.HalconfigDirectoryStructure; import com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentConfiguration; import com.netflix.spinnaker.halyard.config.model.v1.node.Halconfig; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; +import java.io.*; import java.util.*; import lombok.Getter; import org.springframework.web.multipart.MultipartFile; @@ -34,7 +31,10 @@ public class RequestGenerateService { private static final String CONFIG_KEY = "config"; + private static final String SERVICE_SETTINGS_KEY = "serviceSettings"; protected @Getter File baseDirectory; + private Yaml yaml = new Yaml(); + private ObjectMapper objectMapper = new ObjectMapper(); public RequestGenerateService() { this.baseDirectory = Files.createTempDir(); @@ -45,7 +45,10 @@ public void prepare(MultipartHttpServletRequest request) throws IOException { // Get the config first DeploymentConfiguration deploymentConfiguration = parseDeploymentConfiguration(request.getFile(CONFIG_KEY)); + // Write the main config writeHalConfig(deploymentConfiguration); + // Write service settings + writeServiceSettings(deploymentConfiguration, request); // Loop through all files request.getFileMap().entrySet().stream() @@ -81,8 +84,6 @@ protected File getTargetFile(DeploymentConfiguration deploymentConfiguration, St protected void writeHalConfig(DeploymentConfiguration deploymentConfiguration) throws IOException { - Yaml yaml = new Yaml(); - ObjectMapper objectMapper = new ObjectMapper(); Halconfig config = new Halconfig(); config.getDeploymentConfigurations().clear(); config.getDeploymentConfigurations().add(deploymentConfiguration); @@ -93,13 +94,41 @@ protected void writeHalConfig(DeploymentConfiguration deploymentConfiguration) outputStream.write(yaml.dump(objectMapper.convertValue(config, Map.class)).getBytes()); } + protected void writeServiceSettings( + DeploymentConfiguration deploymentConfiguration, MultipartHttpServletRequest request) + throws IOException { + MultipartFile file = request.getFile(SERVICE_SETTINGS_KEY); + if (file != null && file.getSize() > 0) { + // Read YAML and redispatch by service name (key), without checking service names + Object obj = yaml.load(new ByteArrayInputStream(file.getBytes())); + Map map = objectMapper.convertValue(obj, Map.class); + map.entrySet().stream() + .forEach( + e -> { + String filename = + new StringBuilder(deploymentConfiguration.getName()) + .append(File.separator) + .append("service-settings") + .append(File.separator) + .append(e.getKey()) + .append(".yml") + .toString(); + File targetFile = new File(baseDirectory, filename); + targetFile.getParentFile().mkdirs(); + try (BufferedWriter writer = new BufferedWriter(new FileWriter(targetFile))) { + writer.write(yaml.dump(e.getValue())); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + }); + } + } + protected DeploymentConfiguration parseDeploymentConfiguration(MultipartFile deploymentConfigFile) throws IOException { if (deploymentConfigFile == null) { throw new IllegalArgumentException("No deployment configuration file provided."); } - Yaml yaml = new Yaml(); - ObjectMapper objectMapper = new ObjectMapper(); Object obj = yaml.load(new ByteArrayInputStream(deploymentConfigFile.getBytes())); DeploymentConfiguration deploymentConfiguration = objectMapper.convertValue(obj, DeploymentConfiguration.class);