-
Notifications
You must be signed in to change notification settings - Fork 9
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
Add support for percentage-based thresholds #23
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package software.amazon.ce.anomalysubscription; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAutoDetect; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.PropertyAccessor; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.ObjectWriter; | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategy; | ||
import com.fasterxml.jackson.databind.introspect.AnnotatedClass; | ||
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector; | ||
import com.google.common.collect.ImmutableMap; | ||
|
||
import lombok.experimental.UtilityClass; | ||
import software.amazon.awssdk.services.costexplorer.model.CostCategoryValues; | ||
import software.amazon.awssdk.services.costexplorer.model.DimensionValues; | ||
import software.amazon.awssdk.services.costexplorer.model.Expression; | ||
import software.amazon.awssdk.services.costexplorer.model.TagValues; | ||
import software.amazon.cloudformation.exceptions.CfnInvalidRequestException; | ||
|
||
import java.util.Map; | ||
|
||
@UtilityClass | ||
public class Utils { | ||
static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
static ObjectWriter objectWriter; | ||
|
||
static { | ||
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true); | ||
|
||
// Use upper case in JSON key | ||
OBJECT_MAPPER.setPropertyNamingStrategy(new PropertyNamingStrategy.UpperCamelCaseStrategy()); | ||
|
||
// SDK model has private field without getter/setter, make Jackson to use field directly | ||
OBJECT_MAPPER.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); | ||
|
||
// Skip null or empty values when serializing to JSON | ||
OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL); | ||
OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); | ||
|
||
// SDK model has no default constructor, and build class is private. | ||
// Add custom builder so Jackson can deserialize the class. | ||
final Map<Class<?>, Class<?>> buildersMap = ImmutableMap.of( | ||
Expression.class, Expression.serializableBuilderClass(), | ||
DimensionValues.class, DimensionValues.serializableBuilderClass(), | ||
TagValues.class, TagValues.serializableBuilderClass(), | ||
CostCategoryValues.class, CostCategoryValues.serializableBuilderClass() | ||
); | ||
OBJECT_MAPPER.setAnnotationIntrospector(new JacksonAnnotationIntrospector() { | ||
private static final long serialVersionUID = 1L; | ||
|
||
@Override | ||
public Class<?> findPOJOBuilder(AnnotatedClass ac) { | ||
if (buildersMap.containsKey(ac.getRawType())) { | ||
return buildersMap.get(ac.getRawType()); | ||
} | ||
return super.findPOJOBuilder(ac); | ||
} | ||
}); | ||
objectWriter = OBJECT_MAPPER.writerWithDefaultPrettyPrinter(); | ||
} | ||
|
||
public static Expression toExpressionFromJson(String expressionJson) { | ||
try { | ||
return OBJECT_MAPPER.readValue(expressionJson, Expression.class); | ||
} catch (JsonMappingException e) { | ||
throw new CfnInvalidRequestException(String.format("Unsupported JSON '%s' for Expression", expressionJson), e); | ||
} catch (Exception e) { | ||
throw new CfnInvalidRequestException(String.format("Invalid JSON '%s' for Expression", expressionJson), e); | ||
} | ||
} | ||
|
||
public static String toJson(Expression expression) { | ||
try { | ||
return objectWriter.writeValueAsString(expression); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,40 @@ public void handleRequest_Success() { | |
assertThat(response.getErrorCode()).isNull(); | ||
} | ||
|
||
@Test | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need a similar test under UpdateHandler? Could that be a good place to document the nuances of Update for Threshold/ThresholdExpression? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, can add a similar unit test Update: Added 👍 |
||
public void handleRequest_Success_thresholdExpression() { | ||
final ResourceModel model = ResourceModel.builder() | ||
.subscriptionName(TestFixtures.SUBSCRIPTION_NAME) | ||
.thresholdExpression(TestFixtures.THRESHOLD_EXPRESSION) | ||
.subscribers(TestFixtures.CFN_MODEL_SUBSCRIBERS) | ||
.frequency(TestFixtures.FREQUENCY) | ||
.monitorArnList(TestFixtures.MONITOR_ARNS) | ||
.build(); | ||
|
||
final ResourceHandlerRequest<ResourceModel> request = ResourceHandlerRequest.<ResourceModel>builder() | ||
.desiredResourceState(model) | ||
.build(); | ||
|
||
final CreateAnomalySubscriptionResponse mockResponse = CreateAnomalySubscriptionResponse.builder() | ||
.subscriptionArn(TestFixtures.SUBSCRIPTION_ARN) | ||
.build(); | ||
|
||
doReturn(mockResponse) | ||
.when(proxy).injectCredentialsAndInvokeV2(any(), any()); | ||
|
||
final ProgressEvent<ResourceModel, CallbackContext> response | ||
= handler.handleRequest(proxy, request, null, logger); | ||
|
||
assertThat(response).isNotNull(); | ||
assertThat(response.getStatus()).isEqualTo(OperationStatus.SUCCESS); | ||
assertThat(response.getCallbackContext()).isNull(); | ||
assertThat(response.getCallbackDelaySeconds()).isEqualTo(0); | ||
assertThat(response.getResourceModel()).isEqualTo(request.getDesiredResourceState()); | ||
assertThat(response.getResourceModels()).isNull(); | ||
assertThat(response.getMessage()).isNull(); | ||
assertThat(response.getErrorCode()).isNull(); | ||
} | ||
|
||
@Test | ||
public void handleRequest_Success_tagsSubscription() { | ||
final ResourceModel model = ResourceModel.builder() | ||
|
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.
Could we add a comment to this method about acceptable inputs for Threshold and ThresholdExpression for updates
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.
Yep, will add
Update: Added 👍