Skip to content

Commit

Permalink
feat(aws): Support new artifact model for deploy cloudformation (#3022)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jammy Louie committed Jul 9, 2019
1 parent 2e4d4ca commit 2ff80ae
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.netflix.spinnaker.orca.clouddriver.tasks.providers.aws.cloudformation;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.CharStreams;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
Expand Down Expand Up @@ -47,6 +48,8 @@ public class DeployCloudFormationTask extends AbstractCloudProviderAwareTask imp

@Autowired OortService oortService;

@Autowired ObjectMapper objectMapper;

@Autowired ArtifactResolver artifactResolver;

public static final String TASK_NAME = "deployCloudFormation";
Expand All @@ -63,17 +66,26 @@ public TaskResult execute(@Nonnull Stage stage) {
}

if (task.get("source").equals("artifact")) {
if (!StringUtils.isNotBlank((String) task.get("stackArtifactId"))) {
if (!StringUtils.isNotBlank((String) task.get("stackArtifactId"))
&& task.get("stackArtifact") == null) {
throw new IllegalArgumentException(
"Invalid stage format, no stack template artifact was specified.");
}
if (!StringUtils.isNotBlank((String) task.get("stackArtifactAccount"))) {
if (!StringUtils.isNotBlank((String) task.get("stackArtifactAccount"))
&& task.get("stackArtifact") == null) {
throw new IllegalArgumentException(
"Invalid stage format, no artifact account was specified.");
}
String stackArtifactId =
Optional.ofNullable(task.get("stackArtifactId")).map(Object::toString).orElse(null);
Artifact stackArtifact =
Optional.ofNullable(task.get("stackArtifact"))
.map(m -> objectMapper.convertValue(m, Artifact.class))
.orElse(null);
Artifact artifact =
artifactResolver.getBoundArtifactForId(stage, task.get("stackArtifactId").toString());
artifact.setArtifactAccount(task.get("stackArtifactAccount").toString());
artifactResolver.getBoundArtifactForStage(stage, stackArtifactId, stackArtifact);
Optional.ofNullable(task.get("stackArtifactAccount"))
.ifPresent(account -> artifact.setArtifactAccount(account.toString()));
Response response = oortService.fetchArtifact(artifact);
try {
String template = CharStreams.toString(new InputStreamReader(response.getBody().in()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class DeployCloudFormationTaskSpec extends Specification {
def artifactResolver = Mock(ArtifactResolver)

@Subject
def deployCloudFormationTask = new DeployCloudFormationTask(katoService: katoService, oortService: oortService, artifactResolver: artifactResolver)
def deployCloudFormationTask = new DeployCloudFormationTask(katoService: katoService, oortService: oortService, artifactResolver: artifactResolver, objectMapper: objectMapper)

def "should put kato task information as output"() {
given:
Expand Down Expand Up @@ -81,7 +81,7 @@ class DeployCloudFormationTaskSpec extends Specification {
def result = deployCloudFormationTask.execute(stage)

then:
(_..1) * artifactResolver.getBoundArtifactForId(stage, 'id') >> new Artifact()
(_..1) * artifactResolver.getBoundArtifactForStage(stage, 'id', null) >> new Artifact()
(_..1) * oortService.fetchArtifact(_) >> new Response("url", 200, "reason", Collections.emptyList(), template)
thrown(expectedException)

Expand All @@ -105,6 +105,7 @@ class DeployCloudFormationTaskSpec extends Specification {
cloudProvider: 'aws',
source: source,
stackArtifactId: stackArtifactId,
stackArtifact: stackArtifact,
stackArtifactAccount: stackArtifactAccount,
regions: ['eu-west-1'],
templateBody: [key: 'value']]
Expand All @@ -114,7 +115,7 @@ class DeployCloudFormationTaskSpec extends Specification {
def result = deployCloudFormationTask.execute(stage)

then:
1 * artifactResolver.getBoundArtifactForId(stage, 'id') >> new Artifact()
1 * artifactResolver.getBoundArtifactForStage(stage, stackArtifactId, _) >> new Artifact()
1 * oortService.fetchArtifact(_) >> new Response("url", 200, "reason", Collections.emptyList(), new TypedString(template))
1 * katoService.requestOperations("aws", {
it.get(0).get("deployCloudFormation").containsKey("templateBody")
Expand All @@ -123,9 +124,11 @@ class DeployCloudFormationTaskSpec extends Specification {
result.context.'kato.last.task.id' == taskId

where:
source | stackArtifactId | stackArtifactAccount | template
'artifact' | 'id' | 'account' | '{"key": "value"}'
'artifact' | 'id' | 'account' | 'key: value'
source | stackArtifactId | stackArtifactAccount | stackArtifact | template
'artifact' | 'id' | 'account' | null | '{"key": "value"}'
'artifact' | 'id' | 'account' | null | 'key: value'
'artifact' | null | null | Collections.singletonMap("id", "id") | 'key: value'

}

}

0 comments on commit 2ff80ae

Please sign in to comment.