Skip to content

Commit

Permalink
feat(artifacts): add artifact download endpoint (#2282)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwander authored Jan 9, 2018
1 parent a267bd0 commit a496273
Showing 1 changed file with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@
package com.netflix.spinnaker.clouddriver.controllers;

import com.netflix.spinnaker.clouddriver.artifacts.ArtifactCredentialsRepository;
import com.netflix.spinnaker.clouddriver.artifacts.ArtifactDownloader;
import com.netflix.spinnaker.clouddriver.artifacts.config.ArtifactCredentials;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand All @@ -34,10 +40,13 @@
@RequestMapping("/artifacts")
public class ArtifactController {
private ArtifactCredentialsRepository artifactCredentialsRepository;
private ArtifactDownloader artifactDownloader;

@Autowired
public ArtifactController(Optional<ArtifactCredentialsRepository> artifactCredentialsRepository) {
public ArtifactController(Optional<ArtifactCredentialsRepository> artifactCredentialsRepository,
Optional<ArtifactDownloader> artifactDownloader) {
this.artifactCredentialsRepository = artifactCredentialsRepository.orElse(null);
this.artifactDownloader = artifactDownloader.orElse(null);
}

@RequestMapping(method = RequestMethod.GET, value = "/credentials")
Expand All @@ -48,4 +57,25 @@ List<ArtifactCredentials> list() {
return artifactCredentialsRepository.getAllCredentials();
}
}

@RequestMapping(method = RequestMethod.GET, value = "/fetch")
String fetch(@RequestParam("artifactAccount") String artifactAccount,
@RequestParam("type") String type,
@RequestParam("reference") String reference) {
if (artifactDownloader == null) {
throw new IllegalStateException("Artifacts have not been enabled. Enable them using 'artifacts.enabled' in clouddriver");
}

Artifact artifact = Artifact.builder()
.type(type)
.artifactAccount(artifactAccount)
.reference(reference)
.build();

try {
return IOUtils.toString(artifactDownloader.download(artifact));
} catch (IOException e) {
throw new RuntimeException("Failure fetching '" + artifact + "': " + e.getMessage(), e);
}
}
}

0 comments on commit a496273

Please sign in to comment.