diff --git a/clouddriver-web/src/main/groovy/com/netflix/spinnaker/clouddriver/controllers/ArtifactController.java b/clouddriver-web/src/main/groovy/com/netflix/spinnaker/clouddriver/controllers/ArtifactController.java index 48031b5b031..4e8d4000590 100644 --- a/clouddriver-web/src/main/groovy/com/netflix/spinnaker/clouddriver/controllers/ArtifactController.java +++ b/clouddriver-web/src/main/groovy/com/netflix/spinnaker/clouddriver/controllers/ArtifactController.java @@ -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; @@ -34,10 +40,13 @@ @RequestMapping("/artifacts") public class ArtifactController { private ArtifactCredentialsRepository artifactCredentialsRepository; + private ArtifactDownloader artifactDownloader; @Autowired - public ArtifactController(Optional artifactCredentialsRepository) { + public ArtifactController(Optional artifactCredentialsRepository, + Optional artifactDownloader) { this.artifactCredentialsRepository = artifactCredentialsRepository.orElse(null); + this.artifactDownloader = artifactDownloader.orElse(null); } @RequestMapping(method = RequestMethod.GET, value = "/credentials") @@ -48,4 +57,25 @@ List 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); + } + } }