Skip to content

Commit 95a88be

Browse files
authored
Tagging Specific Error Code (#419)
1 parent a8f97aa commit 95a88be

File tree

6 files changed

+114
-18
lines changed

6 files changed

+114
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package software.amazon.cloudformation.exceptions;
16+
17+
import software.amazon.cloudformation.proxy.HandlerErrorCode;
18+
19+
public class CfnUnauthorizedTaggingOperationException extends BaseHandlerException {
20+
21+
private static final long serialVersionUID = -1646136434112354328L;
22+
private static final HandlerErrorCode ERROR_CODE = HandlerErrorCode.UnauthorizedTaggingOperation;
23+
24+
public CfnUnauthorizedTaggingOperationException() {
25+
this(null);
26+
}
27+
28+
public CfnUnauthorizedTaggingOperationException(final Throwable cause) {
29+
super(ERROR_CODE.getMessage(), cause, ERROR_CODE);
30+
}
31+
}

src/main/java/software/amazon/cloudformation/proxy/ExceptionMessages.java

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
final class ExceptionMessages {
1818
static final String ACCESS_DENIED = "Access denied for operation '%s'.";
19+
static final String UNAUTHORIZED_TAGGING_OPERATION = "Unauthorized tagging operation";
1920
static final String ALREADY_EXISTS = "Resource of type '%s' with identifier '%s' already exists.";
2021
static final String GENERAL_SERVICE_EXCEPTION = "Error occurred during operation '%s'.";
2122
static final String INTERNAL_FAILURE = "Internal error occurred.";

src/main/java/software/amazon/cloudformation/proxy/HandlerErrorCode.java

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public enum HandlerErrorCode {
3535
* the customer has insufficient permissions to perform this action (Terminal)
3636
*/
3737
AccessDenied(ExceptionMessages.ACCESS_DENIED),
38+
/**
39+
* the customer has insufficient permissions to perform tagging
40+
* create/update/delete/read action (Terminal)
41+
*/
42+
UnauthorizedTaggingOperation(ExceptionMessages.UNAUTHORIZED_TAGGING_OPERATION),
3843

3944
/**
4045
* the customer's provided credentials were invalid (Terminal)

src/main/java/software/amazon/cloudformation/proxy/delay/CappedExponential.java

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1+
/*
2+
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
115
package software.amazon.cloudformation.proxy.delay;
216

317
import com.google.common.base.Preconditions;
4-
518
import java.time.Duration;
619
import java.util.concurrent.TimeUnit;
720

@@ -13,7 +26,10 @@ public class CappedExponential extends MinDelayAbstractBase {
1326

1427
final Duration maxDelay;
1528

16-
CappedExponential(Duration timeout, Duration minDelay, Double powerBy, Duration maxDelay) {
29+
CappedExponential(Duration timeout,
30+
Duration minDelay,
31+
Double powerBy,
32+
Duration maxDelay) {
1733
super(timeout, minDelay);
1834
Preconditions.checkArgument(powerBy >= 1.0, "powerBy >= 1.0");
1935
Preconditions.checkArgument(maxDelay != null && maxDelay.toMillis() >= 0, "maxDelay must be > 0");
@@ -29,7 +45,7 @@ public static Builder of() {
2945

3046
public static final class Builder extends MinDelayBasedBuilder<CappedExponential, Builder> {
3147
private double powerBy = 2;
32-
private Duration maxDelay =Duration.ofSeconds(20);
48+
private Duration maxDelay = Duration.ofSeconds(20);
3349

3450
public CappedExponential.Builder powerBy(Double powerBy) {
3551
this.powerBy = powerBy;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package software.amazon.cloudformation.exceptions;
16+
17+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
18+
import static org.junit.Assert.assertEquals;
19+
import org.junit.jupiter.api.Test;
20+
import software.amazon.cloudformation.proxy.HandlerErrorCode;
21+
22+
public class CfnUnauthorizedTaggingOperationExceptionTest {
23+
@Test
24+
public void cfnUnauthorizedTaggingOperation_isBaseHandlerException() {
25+
assertThatExceptionOfType(BaseHandlerException.class).isThrownBy(() -> {
26+
throw new CfnUnauthorizedTaggingOperationException(new RuntimeException());
27+
});
28+
}
29+
30+
@Test
31+
public void cfnUnauthorizedTaggingOperationException_singleArgConstructorHasMessage() {
32+
assertThatExceptionOfType(BaseHandlerException.class).isThrownBy(() -> {
33+
throw new CfnUnauthorizedTaggingOperationException(new RuntimeException());
34+
}).withCauseInstanceOf(RuntimeException.class).withMessageContaining("Unauthorized tagging operation");
35+
}
36+
37+
@Test
38+
public void cfnUnauthorizedTaggingOperationException_noCauseGiven() {
39+
assertThatExceptionOfType(CfnUnauthorizedTaggingOperationException.class).isThrownBy(() -> {
40+
throw new CfnUnauthorizedTaggingOperationException();
41+
}).withNoCause().withMessageContaining("Unauthorized tagging operation");
42+
}
43+
44+
@Test
45+
public void cfnUnauthorizedTaggingOperationException_errorCodeIsAppropriate() {
46+
assertThatExceptionOfType(CfnUnauthorizedTaggingOperationException.class).isThrownBy(() -> {
47+
throw new CfnUnauthorizedTaggingOperationException(new RuntimeException());
48+
}).satisfies(exception -> assertEquals(HandlerErrorCode.UnauthorizedTaggingOperation, exception.getErrorCode()));
49+
}
50+
}

src/test/java/software/amazon/cloudformation/proxy/DelayTest.java

+8-15
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,9 @@ public void exponentialDelays() {
203203
@Test
204204
public void cappedExponentialDelays() {
205205
Duration MAX_DELAY = Duration.ofSeconds(15);
206-
final Delay cappedExponential = CappedExponential.of()
207-
.timeout(Duration.ofMinutes(20))
208-
.maxDelay(MAX_DELAY)
209-
.powerBy(1.3)
210-
.minDelay(Duration.ofSeconds(1))
211-
.build();
212-
int[] results = {1, 1, 1, 1, 2, 2, 3, 4, 6, 8, 10, 13, 15, 15, 15, 15};
206+
final Delay cappedExponential = CappedExponential.of().timeout(Duration.ofMinutes(20)).maxDelay(MAX_DELAY).powerBy(1.3)
207+
.minDelay(Duration.ofSeconds(1)).build();
208+
int[] results = { 1, 1, 1, 1, 2, 2, 3, 4, 6, 8, 10, 13, 15, 15, 15, 15 };
213209
for (int tries = 0; tries <= 15; tries++) {
214210
Duration delay = cappedExponential.nextDelay(tries);
215211
assertThat(results[tries]).isEqualTo((int) delay.getSeconds());
@@ -218,7 +214,7 @@ public void cappedExponentialDelays() {
218214
}
219215
}
220216

221-
//If minDelay is not set, the retry is without delay.
217+
// If minDelay is not set, the retry is without delay.
222218
final Delay cappedExponentialNoDelay = CappedExponential.of().timeout(Duration.ofSeconds(12)).build();
223219
for (int tries = 0; tries <= 15; tries++) {
224220
Duration delay = cappedExponentialNoDelay.nextDelay(tries);
@@ -228,14 +224,11 @@ public void cappedExponentialDelays() {
228224
}
229225
}
230226

231-
//If powerBy is not passed, it's set to default 2.
232-
final Delay cappedExponentialNoPower = CappedExponential.of()
233-
.timeout(Duration.ofMinutes(20))
234-
.maxDelay(MAX_DELAY)
235-
.minDelay(Duration.ofSeconds(1))
236-
.build();
227+
// If powerBy is not passed, it's set to default 2.
228+
final Delay cappedExponentialNoPower = CappedExponential.of().timeout(Duration.ofMinutes(20)).maxDelay(MAX_DELAY)
229+
.minDelay(Duration.ofSeconds(1)).build();
237230

238-
int[] resultsNoPower = {1, 1, 2, 4, 8, 15, 15, 15, 15, 15};
231+
int[] resultsNoPower = { 1, 1, 2, 4, 8, 15, 15, 15, 15, 15 };
239232
for (int tries = 0; tries <= 6; tries++) {
240233
Duration delay = cappedExponentialNoPower.nextDelay(tries);
241234
assertThat(resultsNoPower[tries]).isEqualTo((int) delay.getSeconds());

0 commit comments

Comments
 (0)