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

feat(artifacts): add artifact download endpoint #2282

Merged
merged 1 commit into from
Jan 9, 2018
Merged
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 @@ -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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(just noticed this while cutting a release)

Thoughts on just sending the underlying stream all the way across to the caller without first converting to a String?

Similar to what was done here: https://github.com/spinnaker/clouddriver/blob/master/clouddriver-web/src/main/groovy/com/netflix/spinnaker/clouddriver/controllers/DataController.groovy#L55

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sweet, didn't know that was possible - I'll switch that over.

} catch (IOException e) {
throw new RuntimeException("Failure fetching '" + artifact + "': " + e.getMessage(), e);
}
}
}