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

Provide user with BOM when asked #199

Merged
merged 1 commit into from
Mar 4, 2017
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 @@ -17,6 +17,7 @@
package com.netflix.spinnaker.halyard.cli.command.v1;

import com.beust.jcommander.Parameters;
import com.netflix.spinnaker.halyard.cli.command.v1.versions.BomVersionCommand;
import com.netflix.spinnaker.halyard.cli.command.v1.versions.LatestVersionCommand;
import com.netflix.spinnaker.halyard.cli.services.v1.Daemon;
import com.netflix.spinnaker.halyard.cli.ui.v1.AnsiUi;
Expand All @@ -34,6 +35,7 @@ public class VersionsCommand extends NestableCommand {

public VersionsCommand() {
registerSubcommand(new LatestVersionCommand());
registerSubcommand(new BomVersionCommand());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2017 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.netflix.spinnaker.halyard.cli.command.v1.versions;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.netflix.spinnaker.halyard.cli.command.v1.NestableCommand;
import com.netflix.spinnaker.halyard.cli.services.v1.Daemon;
import com.netflix.spinnaker.halyard.cli.ui.v1.AnsiFormatUtils;
import com.netflix.spinnaker.halyard.cli.ui.v1.AnsiUi;
import lombok.AccessLevel;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

@Parameters()
public class BomVersionCommand extends NestableCommand {
@Parameter(description = "The version whose Bill of Materials (BOM) to lookup.", arity = 1)
List<String> versions = new ArrayList<>();

@Getter(AccessLevel.PUBLIC)
private String commandName = "bom";

@Getter(AccessLevel.PUBLIC)
private String description = "Get the Bill of Materials (BOM) for the specified version.";

@Override
public String getMainParameter() {
return "VERSION";
}

public String getVersion() {
switch (versions.size()) {
case 0:
throw new IllegalArgumentException("No version name supplied");
case 1:
return versions.get(0);
default:
throw new IllegalArgumentException("More than one version supplied");
}
}

@Override
protected void executeThis() {
AnsiUi.raw(AnsiFormatUtils.formatYaml(Daemon.getBillOfMaterials(getVersion())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.netflix.spinnaker.halyard.config.model.v1.security.Security;
import com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTask;
import com.netflix.spinnaker.halyard.deploy.deployment.v1.Deployment;
import com.netflix.spinnaker.halyard.deploy.spinnaker.v1.BillOfMaterials;
import com.netflix.spinnaker.halyard.deploy.spinnaker.v1.Versions;
import retrofit.RestAdapter;
import retrofit.client.OkClient;
Expand Down Expand Up @@ -181,6 +182,11 @@ public static String getLatest() {
return ResponseUnwrapper.get(getService().getLatest());
}

public static BillOfMaterials getBillOfMaterials(String version) {
Object rawBillOfMaterials = ResponseUnwrapper.get(getService().getBillOfMaterials(version));
return getObjectMapper().convertValue(rawBillOfMaterials, BillOfMaterials.class);
}

public static void publishProfile(String bomPath, String artifactName, String profilePath) {
ResponseUnwrapper.get(getService().publishProfile(bomPath, artifactName, profilePath, ""));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.netflix.spinnaker.halyard.config.model.v1.security.Security;
import com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTask;
import com.netflix.spinnaker.halyard.deploy.deployment.v1.Deployment;
import com.netflix.spinnaker.halyard.deploy.spinnaker.v1.BillOfMaterials;
import com.netflix.spinnaker.halyard.deploy.spinnaker.v1.Versions;
import retrofit.http.*;

Expand Down Expand Up @@ -258,6 +259,9 @@ DaemonTask<Halconfig, Void> deleteMaster(
@GET("/v1/versions/latest/")
DaemonTask<Halconfig, String> getLatest();

@GET("/v1/versions/bom/{version}")
DaemonTask<Halconfig, BillOfMaterials> getBillOfMaterials(@Path("version") String version);

@PUT("/v1/admin/publishProfile/{artifactName}")
DaemonTask<Halconfig, Void> publishProfile(
@Query("bomPath") String bomPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,43 @@

package com.netflix.spinnaker.halyard.cli.ui.v1;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.spinnaker.halyard.config.model.v1.node.Account;
import com.netflix.spinnaker.halyard.config.model.v1.node.Provider;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

import java.util.List;
import java.util.Map;

public class AnsiFormatUtils {
private static Yaml yamlParser = null;
private static ObjectMapper objectMapper = null;

private static Yaml getYamlParser() {
if (yamlParser == null) {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);

yamlParser = new Yaml(options);
}

return yamlParser;
}

private static ObjectMapper getObjectMapper() {
if (objectMapper == null) {
objectMapper = new ObjectMapper();
}

return objectMapper;
}

public static String formatYaml(Object yaml) {
return getYamlParser().dump(getObjectMapper().convertValue(yaml, Map.class));
}

public static String format(Account account) {
AnsiStoryBuilder resultBuilder = new AnsiStoryBuilder();
AnsiParagraphBuilder paragraph = resultBuilder.addParagraph();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.amazonaws.util.IOUtils;
import com.netflix.spinnaker.halyard.config.config.v1.StrictObjectMapper;
import com.netflix.spinnaker.halyard.config.error.v1.IllegalConfigException;
import com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentConfiguration;
import com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemBuilder;
import com.netflix.spinnaker.halyard.config.services.v1.DeploymentService;
Expand All @@ -30,7 +29,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;
import retrofit.RetrofitError;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -41,9 +39,6 @@

@Component
public class ArtifactService {
@Autowired
ProfileRegistry profileRegistry;

@Autowired(required = false)
WriteableProfileRegistry writeableProfileRegistry;

Expand All @@ -56,33 +51,13 @@ public class ArtifactService {
@Autowired
DeploymentService deploymentService;

@Autowired
VersionsService versionsService;

public BillOfMaterials getBillOfMaterials(String deploymentName) {
DeploymentConfiguration deploymentConfiguration = deploymentService.getDeploymentConfiguration(deploymentName);
String version = deploymentConfiguration.getVersion();
if (version == null || version.isEmpty()) {
throw new IllegalConfigException(
new ConfigProblemBuilder(FATAL,
"In order to load a Spinnaker Component's profile, you must specify a version of Spinnaker in your halconfig.")
.build()
);
}

try {
String bomName = ProfileRegistry.bomPath(version);

BillOfMaterials bom = strictObjectMapper.convertValue(
yaml.load(profileRegistry.getObjectContents(bomName)),
BillOfMaterials.class
);

return bom;
} catch (RetrofitError | IOException e) {
throw new HalException(
new ConfigProblemBuilder(FATAL,
"Unable to retrieve the Spinnaker bill of materials: " + e.getMessage())
.build()
);
}
return versionsService.getBillOfMaterials(version);
}

public String getArtifactVersion(String deploymentName, SpinnakerArtifact artifact) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@

package com.netflix.spinnaker.halyard.deploy.services.v1;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.spinnaker.halyard.config.config.v1.StrictObjectMapper;
import com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemBuilder;
import com.netflix.spinnaker.halyard.core.error.v1.HalException;
import com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity;
import com.netflix.spinnaker.halyard.deploy.spinnaker.v1.BillOfMaterials;
import com.netflix.spinnaker.halyard.deploy.spinnaker.v1.Versions;
import com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.registry.ProfileRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;
import retrofit.RetrofitError;

import java.io.IOException;

import static com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity.FATAL;

@Component
public class VersionsService {
@Autowired
Expand All @@ -37,17 +40,45 @@ public class VersionsService {
Yaml yamlParser;

@Autowired
ObjectMapper objectMapper;
StrictObjectMapper strictObjectMapper;


public Versions getVersions() {
try {
return objectMapper.convertValue(
return strictObjectMapper.convertValue(
yamlParser.load(profileRegistry.getObjectContents("versions.yml")),
Versions.class
);
} catch (IOException e) {
throw new HalException(
new ConfigProblemBuilder(Severity.FATAL, "Could not load \"versions.yml\" from config bucket: " + e.getMessage() + ".").build());
new ConfigProblemBuilder(FATAL, "Could not load \"versions.yml\" from config bucket: " + e.getMessage() + ".").build());
}
}

public BillOfMaterials getBillOfMaterials(String version) {
if (version == null || version.isEmpty()) {
throw new HalException(
new ConfigProblemBuilder(FATAL,
"No version specified to load.")
.build()
);
}

try {
String bomName = ProfileRegistry.bomPath(version);

BillOfMaterials bom = strictObjectMapper.convertValue(
yamlParser.load(profileRegistry.getObjectContents(bomName)),
BillOfMaterials.class
);

return bom;
} catch (RetrofitError | IOException e) {
throw new HalException(
new ConfigProblemBuilder(FATAL,
"Unable to retrieve the Spinnaker bill of materials: " + e.getMessage())
.build()
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTask;
import com.netflix.spinnaker.halyard.core.tasks.v1.TaskRepository;
import com.netflix.spinnaker.halyard.deploy.services.v1.VersionsService;
import com.netflix.spinnaker.halyard.deploy.spinnaker.v1.BillOfMaterials;
import com.netflix.spinnaker.halyard.deploy.spinnaker.v1.Versions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -46,4 +48,11 @@ DaemonTask<Halconfig, String> latest() {
builder.setBuildResponse(() -> versionsService.getLatest());
return TaskRepository.submitTask(builder::build);
}

@RequestMapping(value = "/bom/{version:.+}", method = RequestMethod.GET)
DaemonTask<Halconfig, BillOfMaterials> bom(@PathVariable String version) {
DaemonResponse.StaticRequestBuilder<BillOfMaterials> builder = new DaemonResponse.StaticRequestBuilder<>();
builder.setBuildResponse(() -> versionsService.getBillOfMaterials(version));
return TaskRepository.submitTask(builder::build);
}
}