-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cb603f9
feat(cfn): delete CFN changeset if empty upon request
xavileon 37de452
Add some information about the error
xavileon 8f4d851
Update clouddriver-aws/src/main/groovy/com/netflix/spinnaker/clouddri…
xavileon c3bf766
Missing parenthesis
xavileon c7cd134
Rethrow exception so it can be properly handled by orca
xavileon 4fff890
Merge branch 'master' into deletechangeset
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
...uddriver/aws/deploy/converters/DeleteCloudFormationChangeSetAtomicOperationConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...pinnaker/clouddriver/aws/deploy/description/DeleteCloudFormationChangeSetDescription.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
71 changes: 71 additions & 0 deletions
71
...ix/spinnaker/clouddriver/aws/deploy/ops/DeleteCloudFormationChangeSetAtomicOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* 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.AmazonServiceException; | ||
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 (AmazonServiceException e) { | ||
log.error( | ||
"Error removing change set " | ||
+ description.getChangeSetName() | ||
+ " on stack " | ||
+ description.getStackName(), | ||
e); | ||
throw e; | ||
} | ||
return Collections.emptyMap(); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...er/aws/deploy/converters/DeleteCloudFormationChangeSetAtomicOperationConverterSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...nnaker/clouddriver/aws/deploy/ops/DeleteCloudFormationChangeSetAtomicOperationSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* 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.AmazonServiceException | ||
import com.amazonaws.services.cloudformation.AmazonCloudFormation | ||
import com.amazonaws.services.cloudformation.model.* | ||
import com.netflix.spinnaker.clouddriver.aws.TestCredential | ||
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 spock.lang.Specification | ||
|
||
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 | ||
} | ||
} | ||
|
||
void "should propagate exceptions when deleting the change set"() { | ||
given: | ||
def amazonClientProvider = Mock(AmazonClientProvider) | ||
def amazonCloudFormation = Mock(AmazonCloudFormation) | ||
def op = new DeleteCloudFormationChangeSetAtomicOperation( | ||
new DeleteCloudFormationChangeSetDescription( | ||
[ | ||
stackName: "stackTest", | ||
changeSetName: "changeSetName", | ||
region: "eu-west-1", | ||
credentials: TestCredential.named("test") | ||
] | ||
) | ||
) | ||
op.amazonClientProvider = amazonClientProvider | ||
def exception = new AmazonServiceException("error") | ||
|
||
when: | ||
try { | ||
op.operate([]) | ||
} | ||
catch (Exception e) { | ||
e instanceof AmazonServiceException | ||
} | ||
|
||
then: | ||
1 * amazonClientProvider.getAmazonCloudFormation(_, _) >> amazonCloudFormation | ||
1 * amazonCloudFormation.deleteChangeSet(_) >> { | ||
throw exception | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in c7cd134