Skip to content

Commit

Permalink
Fetch task to get bucket info from material plugin or env var
Browse files Browse the repository at this point in the history
  • Loading branch information
manojlds committed Jul 13, 2017
1 parent f78879b commit 7dc98bd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
20 changes: 17 additions & 3 deletions fetch/src/main/java/com/indix/gocd/s3fetch/FetchExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ public class FetchExecutor {
private static Logger logger = Logger.getLoggerFor(FetchTask.class);

public TaskExecutionResult execute(Config config, final Context context) {
final GoEnvironment env = new GoEnvironment(context.getEnvironmentVariables());
if (env.isAbsent(GO_ARTIFACTS_S3_BUCKET)) return envNotFound(GO_ARTIFACTS_S3_BUCKET);

try {
final GoEnvironment env = new GoEnvironment(context.getEnvironmentVariables());
String artifactPathOnS3 = getArtifactsLocationTemplate(config, env);
final String bucket = env.get(GO_ARTIFACTS_S3_BUCKET);
final String bucket = getBucket(config, env);
final S3ArtifactStore store = getS3ArtifactStore(env, bucket);

context.printMessage(String.format("Getting artifacts from %s", store.pathString(artifactPathOnS3)));
Expand Down Expand Up @@ -54,6 +53,21 @@ private void setupDestinationDirectory(String destination) {
}
}

private String getBucket(Config config, GoEnvironment env) {
String repoName = config.getRepo();
String packageName = config.getPkg();
String bucketFromMaterial = env.get(String.format("GO_REPO_%s_%s_S3_BUCKET", repoName, packageName));
if(bucketFromMaterial != null) {
return bucketFromMaterial;
}

if(env.isAbsent(GO_ARTIFACTS_S3_BUCKET)) {
throw new RuntimeException("S3 bucket to fetch from should be from material plugin or GO_ARTIFACTS_S3_BUCKET env var.");
}

return env.get(GO_ARTIFACTS_S3_BUCKET);
}

private String getArtifactsLocationTemplate(Config config, GoEnvironment env) {
String repoName = config.getRepo();
String packageName = config.getPkg();
Expand Down
3 changes: 0 additions & 3 deletions fetch/src/main/resources/views/task.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,3 @@
<input type="text" ng-model="Destination" ng-required="true">
<span class="form_error" ng-show="GOINPUTNAME[Destination].$error.server">{{ GOINPUTNAME[Destination].$error.server }}</span>
</div>
<div class="form_item_block">
<p class="required">Make sure GO_ARTIFACTS_S3_BUCKET environment variables is present with appropriate values on any of pipeline / Go Environments / on all agent machines. </p>
</div>
22 changes: 18 additions & 4 deletions fetch/src/test/java/com/indix/gocd/s3fetch/FetchExecutorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void setUp() throws Exception {
.with(GO_ARTIFACTS_S3_BUCKET, bucket)
.with(GO_SERVER_DASHBOARD_URL, "http://go.server:8153")
.with("GO_PACKAGE_GOCD_TESTPUBLISHS3ARTIFACTS_LABEL", "20.1")
.with("GO_REPO_GOCD_TESTPUBLISHS3ARTIFACTS_S3_BUCKET", bucket)

.with("GO_PACKAGE_GOCD_TESTPUBLISHS3ARTIFACTS_PIPELINE_NAME", "TestPublish")
.with("GO_PACKAGE_GOCD_TESTPUBLISHS3ARTIFACTS_STAGE_NAME", "defaultStage")
.with("GO_PACKAGE_GOCD_TESTPUBLISHS3ARTIFACTS_JOB_NAME", "defaultJob");
Expand Down Expand Up @@ -85,9 +85,6 @@ public void shouldBeFailureIfUnableToFetchArtifacts() {
@Test
public void shouldBeSuccessResultOnSuccessfulFetch() {
Map<String, String> mockVariables = mockEnvironmentVariables.build();
AmazonS3Client mockClient = mockClient();
S3ArtifactStore store = new S3ArtifactStore(mockClient, bucket);
doReturn(store).when(fetchExecutor).getS3ArtifactStore(any(GoEnvironment.class), eq(bucket));
S3ArtifactStore mockStore = mockStore();

doReturn(mockStore).when(fetchExecutor).getS3ArtifactStore(any(GoEnvironment.class), any(String.class));
Expand All @@ -99,6 +96,23 @@ public void shouldBeSuccessResultOnSuccessfulFetch() {

}

@Test
public void shouldGetBucketInfoFromMaterialEnvVars() {
Map<String, String> mockVariables = mockEnvironmentVariables
.with("GO_REPO_GOCD_TESTPUBLISHS3ARTIFACTS_S3_BUCKET", bucket)
.with(GO_ARTIFACTS_S3_BUCKET, "")
.build();
S3ArtifactStore mockStore = mockStore();

doReturn(mockStore).when(fetchExecutor).getS3ArtifactStore(any(GoEnvironment.class), eq(bucket));
TaskExecutionResult result = fetchExecutor.execute(config, mockContext(mockVariables));

assertTrue(result.isSuccessful());
assertThat(result.message(), is("Fetched all artifacts"));
verify(mockStore, times(1)).getPrefix("TestPublish/defaultStage/defaultJob/20.1", "here/artifacts");

}

@Test
public void shouldBeAbleToHandleTaskConfigEntriesWithDashesInTheName() {
Map<String, String> mockVariables = mockEnvironmentVariables
Expand Down

0 comments on commit 7dc98bd

Please sign in to comment.