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(cfn): delete CFN changeset if empty upon request #4101

Merged
merged 6 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2019 Adevinta
*
* 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.clouddriver.aws.deploy.converters;

import com.netflix.spinnaker.clouddriver.aws.AmazonOperation;
import com.netflix.spinnaker.clouddriver.aws.deploy.description.DeleteCloudFormationChangeSetDescription;
import com.netflix.spinnaker.clouddriver.aws.deploy.ops.DeleteCloudFormationChangeSetAtomicOperation;
import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation;
import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperations;
import com.netflix.spinnaker.clouddriver.security.AbstractAtomicOperationsCredentialsSupport;
import java.util.Map;
import org.springframework.stereotype.Component;

@AmazonOperation(AtomicOperations.DELETE_CLOUDFORMATION_CHANGESET)
@Component("deleteCloudFormationChangeSetDescription")
public class DeleteCloudFormationChangeSetAtomicOperationConverter
extends AbstractAtomicOperationsCredentialsSupport {
@Override
public AtomicOperation convertOperation(Map input) {
return new DeleteCloudFormationChangeSetAtomicOperation(convertDescription(input));
}

@Override
public DeleteCloudFormationChangeSetDescription convertDescription(Map input) {
DeleteCloudFormationChangeSetDescription converted =
getObjectMapper().convertValue(input, DeleteCloudFormationChangeSetDescription.class);
converted.setCredentials(getCredentialsObject((String) input.get("credentials")));
return converted;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2019 Adevinta
*
* 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.clouddriver.aws.deploy.description;

import lombok.Data;
import lombok.EqualsAndHashCode;

@EqualsAndHashCode(callSuper = false)
@Data
public class DeleteCloudFormationChangeSetDescription extends AbstractAmazonCredentialsDescription {

private String stackName;
private String changeSetName;
private String region;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2019 Adevinta
*
* 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.clouddriver.aws.deploy.ops;

import com.amazonaws.services.cloudformation.AmazonCloudFormation;
import com.amazonaws.services.cloudformation.model.*;
import com.netflix.spinnaker.clouddriver.aws.deploy.description.DeleteCloudFormationChangeSetDescription;
import com.netflix.spinnaker.clouddriver.aws.security.AmazonClientProvider;
import com.netflix.spinnaker.clouddriver.data.task.Task;
import com.netflix.spinnaker.clouddriver.data.task.TaskRepository;
import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
public class DeleteCloudFormationChangeSetAtomicOperation implements AtomicOperation<Map> {

private static final String BASE_PHASE = "DELETE_CLOUDFORMATION_CHANGESET";

@Autowired AmazonClientProvider amazonClientProvider;

private DeleteCloudFormationChangeSetDescription description;

public DeleteCloudFormationChangeSetAtomicOperation(
DeleteCloudFormationChangeSetDescription deployCloudFormationDescription) {
this.description = deployCloudFormationDescription;
}

@Override
public Map operate(List priorOutputs) {
Task task = TaskRepository.threadLocalTask.get();
AmazonCloudFormation amazonCloudFormation =
amazonClientProvider.getAmazonCloudFormation(
description.getCredentials(), description.getRegion());

DeleteChangeSetRequest deleteChangeSetRequest =
new DeleteChangeSetRequest()
.withStackName(description.getStackName())
.withChangeSetName(description.getChangeSetName());
try {
task.updateStatus(BASE_PHASE, "Deleting CloudFormation ChangeSet");
amazonCloudFormation.deleteChangeSet(deleteChangeSetRequest);
} catch (AmazonCloudFormationException e) {
log.error(
Copy link
Contributor

Choose a reason for hiding this comment

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

is it OK that this error is ignored?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean not returning any error back to orca? Fair comment, I'll return an error so orca can handle it appropiately, probably with a retryable task 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in c7cd134

"Error removing change set {} on stack {}",
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about including something from the exception in the message to give an idea why it failed?

Copy link
Contributor Author

@xavileon xavileon Oct 21, 2019

Choose a reason for hiding this comment

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

I think it's a good idea 🙂 I'll add the message.

description.getChangeSetName(),
description.getStackName());
return Collections.emptyMap();
}
return Collections.emptyMap();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2019 Adevinta
*
* 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.clouddriver.aws.deploy.converters

import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.spinnaker.clouddriver.aws.deploy.description.DeleteCloudFormationChangeSetDescription
import com.netflix.spinnaker.clouddriver.aws.deploy.ops.DeleteCloudFormationChangeSetAtomicOperation
import com.netflix.spinnaker.clouddriver.aws.security.NetflixAmazonCredentials
import com.netflix.spinnaker.clouddriver.security.AccountCredentialsProvider
import spock.lang.Shared
import spock.lang.Specification

class DeleteCloudFormationChangeSetAtomicOperationConverterSpec extends Specification {

@Shared
ObjectMapper mapper = new ObjectMapper()

@Shared
DeleteCloudFormationChangeSetAtomicOperationConverter converter

def setupSpec() {
this.converter = new DeleteCloudFormationChangeSetAtomicOperationConverter(objectMapper: mapper)
def accountCredentialsProvider = Mock(AccountCredentialsProvider)
def mockCredentials = Mock(NetflixAmazonCredentials)
accountCredentialsProvider.getCredentials(_) >> mockCredentials
converter.accountCredentialsProvider = accountCredentialsProvider
}

void "DeleteCloudFormationChangeSetConverter returns DeleteCloudFormationChangeSetDescription"() {
setup:
def input = [stackName : "stack",
changeSetName : "changeset",
region : "eu-west-1",
credentials : "credentials"]

when:
DeleteCloudFormationChangeSetDescription description = converter.convertDescription(input)

then:
description instanceof DeleteCloudFormationChangeSetDescription
description.stackName == "stack"
description.changeSetName == "changeset"
description.region == "eu-west-1"

when:
def operation = converter.convertOperation(input)

then:
operation instanceof DeleteCloudFormationChangeSetAtomicOperation
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2019 Adevinta, 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.clouddriver.aws.deploy.ops

import com.amazonaws.services.cloudformation.AmazonCloudFormation
import com.amazonaws.services.cloudformation.model.*
import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.spinnaker.clouddriver.aws.TestCredential
import com.netflix.spinnaker.clouddriver.aws.deploy.description.DeleteCloudFormationChangeSetDescription
import com.netflix.spinnaker.clouddriver.aws.deploy.description.DeployCloudFormationDescription
import com.netflix.spinnaker.clouddriver.aws.security.AmazonClientProvider
import com.netflix.spinnaker.clouddriver.data.task.Task
import com.netflix.spinnaker.clouddriver.data.task.TaskRepository
import spock.lang.Specification
import spock.lang.Unroll

class DeleteCloudFormationChangeSetAtomicOperationSpec extends Specification {
void setupSpec() {
TaskRepository.threadLocalTask.set(Mock(Task))
}

void "should build a DeleteChangeSetRequest and submit through aws client"() {
given:
def amazonClientProvider = Mock(AmazonClientProvider)
def amazonCloudFormation = Mock(AmazonCloudFormation)
def deleteChangeSetResult = Mock(DeleteChangeSetResult)
def op = new DeleteCloudFormationChangeSetAtomicOperation(
new DeleteCloudFormationChangeSetDescription(
[
stackName: "stackTest",
changeSetName: "changeSetName",
region: "eu-west-1",
credentials: TestCredential.named("test")
]
)
)
op.amazonClientProvider = amazonClientProvider

when:
op.operate([])

then:
1 * amazonClientProvider.getAmazonCloudFormation(_, _) >> amazonCloudFormation
1 * amazonCloudFormation.deleteChangeSet(_) >> { DeleteChangeSetRequest request ->
assert request.getStackName() == "stackTest"
assert request.getChangeSetName() == "changeSetName"
deleteChangeSetResult
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,5 @@ public final class AtomicOperations {

// CloudFormation operations
public static final String DEPLOY_CLOUDFORMATION_STACK = "deployCloudFormation";
public static final String DELETE_CLOUDFORMATION_CHANGESET = "deleteCloudFormationChangeSet";
}