Skip to content

Commit

Permalink
Merge branch 'r/budgets_budget_validations' of ssh://github.com/DrFau…
Browse files Browse the repository at this point in the history
…st92/terraform-provider-aws into DrFaust92-r/budgets_budget_validations
  • Loading branch information
bflad committed Feb 11, 2021
2 parents 8f04481 + 575df9f commit fe44198
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 94 deletions.
62 changes: 35 additions & 27 deletions aws/resource_aws_budgets_budget.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/budgets"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -16,6 +17,10 @@ import (
func resourceAwsBudgetsBudget() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"account_id": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -37,8 +42,9 @@ func resourceAwsBudgetsBudget() *schema.Resource {
ForceNew: true,
},
"budget_type": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(budgets.BudgetType_Values(), false),
},
"limit_amount": {
Type: schema.TypeString,
Expand Down Expand Up @@ -123,8 +129,9 @@ func resourceAwsBudgetsBudget() *schema.Resource {
Default: "2087-06-15_00:00",
},
"time_unit": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(budgets.TimeUnit_Values(), false),
},
"cost_filters": {
Type: schema.TypeMap,
Expand All @@ -138,33 +145,23 @@ func resourceAwsBudgetsBudget() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"comparison_operator": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
budgets.ComparisonOperatorEqualTo,
budgets.ComparisonOperatorGreaterThan,
budgets.ComparisonOperatorLessThan,
}, false),
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(budgets.ComparisonOperator_Values(), false),
},
"threshold": {
Type: schema.TypeFloat,
Required: true,
},
"threshold_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
budgets.ThresholdTypeAbsoluteValue,
budgets.ThresholdTypePercentage,
}, false),
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(budgets.ThresholdType_Values(), false),
},
"notification_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
budgets.NotificationTypeActual,
budgets.NotificationTypeForecasted,
}, false),
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(budgets.NotificationType_Values(), false),
},
"subscriber_email_addresses": {
Type: schema.TypeSet,
Expand All @@ -174,7 +171,10 @@ func resourceAwsBudgetsBudget() *schema.Resource {
"subscriber_sns_topic_arns": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validateArn,
},
},
},
},
Expand Down Expand Up @@ -222,7 +222,7 @@ func resourceAwsBudgetsBudgetCreate(d *schema.ResourceData, meta interface{}) er
return fmt.Errorf("create budget failed: %v", err)
}

d.SetId(fmt.Sprintf("%s:%s", accountID, *budget.BudgetName))
d.SetId(fmt.Sprintf("%s:%s", accountID, aws.StringValue(budget.BudgetName)))

notificationsRaw := d.Get("notification").(*schema.Set).List()
notifications, subscribers := expandBudgetNotificationsUnmarshal(notificationsRaw)
Expand Down Expand Up @@ -350,6 +350,14 @@ func resourceAwsBudgetsBudgetRead(d *schema.ResourceData, meta interface{}) erro

d.Set("time_unit", budget.TimeUnit)

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "budgetservice",
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("budget/%s", aws.StringValue(budget.BudgetName)),
}
d.Set("arn", arn.String())

return resourceAwsBudgetsBudgetNotificationRead(d, meta)
}

Expand Down Expand Up @@ -402,9 +410,9 @@ func resourceAwsBudgetsBudgetNotificationRead(d *schema.ResourceData, meta inter
emailSubscribers := make([]interface{}, 0)

for _, subscriberOutput := range subscribersOutput.Subscribers {
if *subscriberOutput.SubscriptionType == budgets.SubscriptionTypeSns {
if aws.StringValue(subscriberOutput.SubscriptionType) == budgets.SubscriptionTypeSns {
snsSubscribers = append(snsSubscribers, *subscriberOutput.Address)
} else if *subscriberOutput.SubscriptionType == budgets.SubscriptionTypeEmail {
} else if aws.StringValue(subscriberOutput.SubscriptionType) == budgets.SubscriptionTypeEmail {
emailSubscribers = append(emailSubscribers, *subscriberOutput.Address)
}
}
Expand Down
Loading

0 comments on commit fe44198

Please sign in to comment.