-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for percentage-based thresholds (#23)
* Add support for percentage-based thresholds * Add unit test and comments
- Loading branch information
Showing
17 changed files
with
196 additions
and
15 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
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
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
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
82 changes: 82 additions & 0 deletions
82
anomalysubscription/src/main/java/software/amazon/ce/anomalysubscription/Utils.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,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); | ||
} | ||
} | ||
} |
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
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
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