Skip to content
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

r/budgets_budget - add arn attribute + plan time validations #13139

Merged
merged 7 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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