Skip to content

Commit

Permalink
COSTCATEGORY: dynamically be able to allow more nesting based on aws …
Browse files Browse the repository at this point in the history
…docs
  • Loading branch information
porqueoutai committed Jun 30, 2023
1 parent 58c8420 commit 80648a6
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .changelog/30862.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_ce_cost_category: Allow operand nesting like aws cost_category api does to be able create sophisticated cost_category rules
```
124 changes: 121 additions & 3 deletions internal/service/ce/cost_category.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"github.com/hashicorp/terraform-provider-aws/names"
)

const costCategoryRuleMaxNesting int = 1

// @SDKResource("aws_ce_cost_category", name="Cost Category")
// @Tags(identifierAttribute="id")
func ResourceCostCategory() *schema.Resource {
Expand Down Expand Up @@ -171,7 +173,123 @@ func schemaCostCategoryRule() *schema.Resource {
"and": {
Type: schema.TypeSet,
Optional: true,
Elem: schemaCostCategoryRuleExpression(),
Elem: schemaCostCategoryRuleLevel(1),
},
"cost_category": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 50),
},
"match_options": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(costexplorer.MatchOption_Values(), false),
},
},
"values": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringLenBetween(0, 1024),
},
},
},
},
},
"dimension": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(costexplorer.Dimension_Values(), false),
},
"match_options": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(costexplorer.MatchOption_Values(), false),
},
},
"values": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringLenBetween(0, 1024),
},
},
},
},
},
"not": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: schemaCostCategoryRuleLevel(1),
},
"or": {
Type: schema.TypeSet,
Optional: true,
Elem: schemaCostCategoryRuleLevel(1),
},
"tags": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Optional: true,
},
"match_options": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(costexplorer.MatchOption_Values(), false),
},
},
"values": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringLenBetween(0, 1024),
},
},
},
},
},
},
}
}

func schemaCostCategoryRuleLevel(level int) *schema.Resource {
var elem interface{} = schemaCostCategoryRuleLevel(level + 1)
if level == costCategoryRuleMaxNesting {
elem = schemaCostCategoryRuleExpression()
}
return &schema.Resource{
Schema: map[string]*schema.Schema{
"and": {
Type: schema.TypeSet,
Optional: true,
Elem: elem,
},
"cost_category": {
Type: schema.TypeList,
Expand Down Expand Up @@ -237,12 +355,12 @@ func schemaCostCategoryRule() *schema.Resource {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: schemaCostCategoryRuleExpression(),
Elem: elem,
},
"or": {
Type: schema.TypeSet,
Optional: true,
Elem: schemaCostCategoryRuleExpression(),
Elem: elem,
},
"tags": {
Type: schema.TypeList,
Expand Down
8 changes: 4 additions & 4 deletions internal/service/ce/cost_category_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,10 +593,10 @@ resource "aws_ce_cost_category" "test" {
func testAccCostCategoryConfig_operandNot(rName string) string {
return fmt.Sprintf(`
resource "aws_ce_cost_category" "test" {
name = %[1]q
rule_version = "CostCategoryExpression.v1"
name = %[1]q
rule_version = "CostCategoryExpression.v1"
rule {
value = "production"
value = "notax"
rule {
and {
dimension {
Expand All @@ -613,7 +613,7 @@ resource "aws_ce_cost_category" "test" {
match_options = ["EQUALS"]
}
}
}
}
}
type = "REGULAR"
}
Expand Down

0 comments on commit 80648a6

Please sign in to comment.