Skip to content

Commit

Permalink
feat(provider/appengine): add deploy global configuration stage (#3929)
Browse files Browse the repository at this point in the history
* feat(provider/appengine): add deploy global configuration stage

* feat(appengine): deploy app engine config refactoring

* feat(appengine): deploy app engine config refactoring

* feat(appengine): deploy app engine config refactoring
  • Loading branch information
zachsmith1 committed Sep 28, 2020
1 parent 3610721 commit a9c4309
Show file tree
Hide file tree
Showing 3 changed files with 292 additions and 0 deletions.
@@ -0,0 +1,36 @@
/*
* Copyright 2020 Armory, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.orca.clouddriver.pipeline.providers.appengine;

import com.netflix.spinnaker.orca.api.pipeline.graph.StageDefinitionBuilder;
import com.netflix.spinnaker.orca.api.pipeline.graph.TaskNode;
import com.netflix.spinnaker.orca.api.pipeline.models.StageExecution;
import com.netflix.spinnaker.orca.clouddriver.tasks.MonitorKatoTask;
import com.netflix.spinnaker.orca.clouddriver.tasks.providers.appengine.DeployAppEngineConfigurationTask;
import javax.annotation.Nonnull;
import org.springframework.stereotype.Component;

@Component
public class DeployAppEngineConfigurationStage implements StageDefinitionBuilder {

@Override
public void taskGraph(@Nonnull StageExecution stage, @Nonnull TaskNode.Builder builder) {
builder
.withTask("deployAppengineConfiguration", DeployAppEngineConfigurationTask.class)
.withTask("monitorTask", MonitorKatoTask.class);
}
}
@@ -0,0 +1,134 @@
/*
* Copyright 2020 Armory, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.orca.clouddriver.tasks.providers.appengine;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import com.netflix.spinnaker.orca.api.pipeline.RetryableTask;
import com.netflix.spinnaker.orca.api.pipeline.TaskResult;
import com.netflix.spinnaker.orca.api.pipeline.models.ExecutionStatus;
import com.netflix.spinnaker.orca.api.pipeline.models.StageExecution;
import com.netflix.spinnaker.orca.clouddriver.KatoService;
import com.netflix.spinnaker.orca.clouddriver.model.TaskId;
import com.netflix.spinnaker.orca.clouddriver.tasks.AbstractCloudProviderAwareTask;
import com.netflix.spinnaker.orca.pipeline.util.ArtifactUtils;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class DeployAppEngineConfigurationTask extends AbstractCloudProviderAwareTask
implements RetryableTask {

private final ObjectMapper objectMapper;
private final KatoService kato;
private final String CLOUD_OPERATION_TYPE = "deployAppengineConfiguration";
private final String CLOUD_PROVIDER = "appengine";
private final ArtifactUtils artifactUtils;

@Override
public long getBackoffPeriod() {
return 30000;
}

@Override
public long getTimeout() {
return 300000;
}

@Nonnull
@Override
public TaskResult execute(@Nonnull StageExecution stage) {
Map<String, Object> context = stage.getContext();
Map<String, Object> operationDescription = new HashMap<>();
String account = getCredentials(stage);
operationDescription.put("accountName", account);

if (context.get("cronArtifact") == null
&& context.get("dispatchArtifact") == null
&& context.get("indexArtifact") == null
&& context.get("queueArtifact") == null) {
throw new IllegalArgumentException("At least one configuration artifact must be supplied.");
}

if (context.get("cronArtifact") != null) {
ArtifactAccountPair artifactAccountPair =
objectMapper.convertValue(context.get("cronArtifact"), ArtifactAccountPair.class);
Artifact artifact =
artifactUtils.getBoundArtifactForStage(
stage,
artifactAccountPair.getId(),
objectMapper.convertValue(artifactAccountPair.getArtifact(), Artifact.class));
operationDescription.put("cronArtifact", artifact);
}

if (context.get("dispatchArtifact") != null) {
ArtifactAccountPair artifactAccountPair =
objectMapper.convertValue(context.get("dispatchArtifact"), ArtifactAccountPair.class);
Artifact artifact =
artifactUtils.getBoundArtifactForStage(
stage,
artifactAccountPair.getId(),
objectMapper.convertValue(artifactAccountPair.getArtifact(), Artifact.class));
operationDescription.put("dispatchArtifact", artifact);
}

if (context.get("indexArtifact") != null) {
ArtifactAccountPair artifactAccountPair =
objectMapper.convertValue(context.get("indexArtifact"), ArtifactAccountPair.class);
Artifact artifact =
artifactUtils.getBoundArtifactForStage(
stage,
artifactAccountPair.getId(),
objectMapper.convertValue(artifactAccountPair.getArtifact(), Artifact.class));
operationDescription.put("indexArtifact", artifact);
}

if (context.get("queueArtifact") != null) {
ArtifactAccountPair artifactAccountPair =
objectMapper.convertValue(context.get("queueArtifact"), ArtifactAccountPair.class);
Artifact artifact =
artifactUtils.getBoundArtifactForStage(
stage,
artifactAccountPair.getId(),
objectMapper.convertValue(artifactAccountPair.getArtifact(), Artifact.class));
operationDescription.put("queueArtifact", artifact);
}

Map<String, Map> operation =
new ImmutableMap.Builder<String, Map>()
.put(CLOUD_OPERATION_TYPE, operationDescription)
.build();

TaskId taskId = kato.requestOperations(CLOUD_PROVIDER, Collections.singletonList(operation));

Map<String, Object> outputs =
new ImmutableMap.Builder<String, Object>()
.put("notification.type", CLOUD_OPERATION_TYPE)
.put("kato.last.task.id", taskId)
.put("service.region", Optional.ofNullable(stage.getContext().get("region")).orElse(""))
.put("service.account", account)
.build();
return TaskResult.builder(ExecutionStatus.SUCCEEDED).context(outputs).build();
}
}
@@ -0,0 +1,122 @@
/*
* Copyright 2020 Armory, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.orca.clouddriver.tasks.providers.appengine;

import static com.netflix.spinnaker.orca.api.pipeline.models.ExecutionType.PIPELINE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import com.netflix.spinnaker.orca.api.pipeline.TaskResult;
import com.netflix.spinnaker.orca.api.pipeline.models.ExecutionStatus;
import com.netflix.spinnaker.orca.api.pipeline.models.StageExecution;
import com.netflix.spinnaker.orca.clouddriver.KatoService;
import com.netflix.spinnaker.orca.clouddriver.model.TaskId;
import com.netflix.spinnaker.orca.pipeline.model.PipelineExecutionImpl;
import com.netflix.spinnaker.orca.pipeline.model.StageExecutionImpl;
import com.netflix.spinnaker.orca.pipeline.util.ArtifactUtils;
import java.util.Map;
import org.junit.Test;

public class DeployAppEngineConfigurationTaskTest {
private final String CLOUD_OPERATION_TYPE = "deployAppengineConfiguration";

ObjectMapper mapper = new ObjectMapper();
KatoService katoService = mock(KatoService.class);
ArtifactUtils artifactUtils = mock(ArtifactUtils.class);
DeployAppEngineConfigurationTask deployAppEngineConfigurationTask =
new DeployAppEngineConfigurationTask(mapper, katoService, artifactUtils);

@Test
public void shouldMapAndDeploy() throws JsonProcessingException {
String json =
"{\n"
+ " \"account\": \"my-appengine-account\",\n"
+ " \"cronArtifact\": {\n"
+ " \"account\": \"embedded-artifact\",\n"
+ " \"artifact\": {\n"
+ " \"artifactAccount\": \"embedded-artifact\",\n"
+ " \"id\": \"04fa7b32-fd17-4536-87b5-2a0d7b58c87c\",\n"
+ " \"name\": \"blah\",\n"
+ " \"reference\": \"ZG9zb21ldGhpbmc=\",\n"
+ " \"type\": \"embedded/base64\"\n"
+ " },\n"
+ " \"id\": null\n"
+ " },\n"
+ " \"name\": \"Deploy Global AppEngine Configuration\",\n"
+ " \"region\": \"us-east1\",\n"
+ " \"type\": \"deployAppengineConfig\"\n"
+ "}";

Map<String, Object> input = mapper.readValue(json, Map.class);
StageExecution stageExecution = new StageExecutionImpl();
stageExecution.setContext(input);

when(katoService.requestOperations(any(), any())).thenReturn(new TaskId("taskid"));

Map<String, Object> expectedContext =
new ImmutableMap.Builder<String, Object>()
.put("notification.type", CLOUD_OPERATION_TYPE)
.put("kato.last.task.id", new TaskId("taskid"))
.put("service.region", "us-east1")
.put("service.account", deployAppEngineConfigurationTask.getCredentials(stageExecution))
.build();

TaskResult expected =
TaskResult.builder(ExecutionStatus.SUCCEEDED).context(expectedContext).build();

TaskResult result =
deployAppEngineConfigurationTask.execute(
new StageExecutionImpl(
new PipelineExecutionImpl(PIPELINE, "orca"),
CLOUD_OPERATION_TYPE,
stageExecution.getContext()));

assertThat(result).isEqualToComparingFieldByFieldRecursively(expected);
}

@Test
public void shouldThrowWhenNoArtifactsSelected() throws JsonProcessingException {
String json =
"{\n"
+ " \"account\": \"my-appengine-account\",\n"
+ " \"name\": \"Deploy Global AppEngine Configuration\",\n"
+ " \"region\": \"us-east1\",\n"
+ " \"type\": \"deployAppengineConfig\"\n"
+ "}";

Map<String, Object> input = mapper.readValue(json, Map.class);
StageExecution stageExecution = new StageExecutionImpl();
stageExecution.setContext(input);

Exception exception =
assertThrows(
IllegalArgumentException.class,
() -> {
deployAppEngineConfigurationTask.execute(stageExecution);
});

String message = exception.getMessage();
assertTrue(message.equals("At least one configuration artifact must be supplied."));
}
}

0 comments on commit a9c4309

Please sign in to comment.