Skip to content

Commit

Permalink
basic create budget functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
xchapter7x committed Mar 31, 2018
1 parent efa8cd4 commit 951d77a
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 0 deletions.
3 changes: 3 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/aws/aws-sdk-go/service/athena"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/batch"
"github.com/aws/aws-sdk-go/service/budgets"
"github.com/aws/aws-sdk-go/service/cloud9"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/cloudfront"
Expand Down Expand Up @@ -217,6 +218,7 @@ type AWSClient struct {
mediastoreconn *mediastore.MediaStore
appsyncconn *appsync.AppSync
lexmodelconn *lexmodelbuildingservice.LexModelBuildingService
budgetconn *budgets.Budgets
}

func (c *AWSClient) S3() *s3.S3 {
Expand Down Expand Up @@ -406,6 +408,7 @@ func (c *Config) Client() (interface{}, error) {
}
}

client.budgetconn = budgets.New(sess)
client.acmconn = acm.New(awsAcmSess)
client.apigateway = apigateway.New(awsApigatewaySess)
client.appautoscalingconn = applicationautoscaling.New(sess)
Expand Down
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ func Provider() terraform.ResourceProvider {
"aws_autoscaling_notification": resourceAwsAutoscalingNotification(),
"aws_autoscaling_policy": resourceAwsAutoscalingPolicy(),
"aws_autoscaling_schedule": resourceAwsAutoscalingSchedule(),
"aws_budget": resourceAwsBudget(),
"aws_cloud9_environment_ec2": resourceAwsCloud9EnvironmentEc2(),
"aws_cloudformation_stack": resourceAwsCloudFormationStack(),
"aws_cloudfront_distribution": resourceAwsCloudFrontDistribution(),
Expand Down
121 changes: 121 additions & 0 deletions aws/resource_aws_budget.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package aws

import (
"fmt"
"time"

"github.com/aws/aws-sdk-go/service/budgets"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsBudget() *schema.Resource {
return &schema.Resource{
Schema: resourceAwsBudgetSchema(),
Create: resourceAwsBudgetCreate,
Read: resourceAwsBudgetRead,
Update: resourceAwsBudgetUpdate,
Delete: resourceAwsBudgetDelete,
}
}

func resourceAwsBudgetSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Required: true,
},
"limit_amount": {
Type: schema.TypeString,
Required: true,
},
"limit_unit": {
Type: schema.TypeString,
Required: true,
},
"include_tax": {
Type: schema.TypeBool,
Required: true,
},
"include_subscriptions": {
Type: schema.TypeBool,
Required: true,
},
"include_blended": {
Type: schema.TypeBool,
Required: true,
},
"time_period_start": {
Type: schema.TypeInt,
Required: true,
},
"time_period_end": {
Type: schema.TypeInt,
Required: true,
},
"time_unit": {
Type: schema.TypeString,
Required: true,
},
}
}

func resourceAwsBudgetCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AWSClient).budgetconn
accountID := meta.(*AWSClient).accountid
budgetName := d.Get("name").(string)
budgetType := d.Get("type").(string)
budgetLimitAmount := d.Get("limit_amount").(string)
budgetLimitUnit := d.Get("limit_unit").(string)
budgetIncludeTax := d.Get("include_tax").(bool)
budgetIncludeSubscriptions := d.Get("include_subscriptions").(bool)
budgetIncludeBlended := d.Get("include_blended").(bool)
budgetTimePeriodStart := time.Unix(int64(d.Get("time_period_start").(int)), 0)
budgetTimePeriodEnd := time.Unix(int64(d.Get("time_period_end").(int)), 0)
budgetTimeUnit := d.Get("time_unit").(string)

budget := new(budgets.Budget)
budget.SetBudgetName(budgetName)
budget.SetBudgetType(budgetType)
budget.SetBudgetLimit(&budgets.Spend{
Amount: &budgetLimitAmount,
Unit: &budgetLimitUnit,
})
budget.SetCostTypes(&budgets.CostTypes{
IncludeSubscription: &budgetIncludeSubscriptions,
IncludeTax: &budgetIncludeTax,
UseBlended: &budgetIncludeBlended,
})
budget.SetTimePeriod(&budgets.TimePeriod{
End: &budgetTimePeriodEnd,
Start: &budgetTimePeriodStart,
})
budget.SetTimeUnit(budgetTimeUnit)
createBudgetInput := new(budgets.CreateBudgetInput)
createBudgetInput.SetAccountId(accountID)
createBudgetInput.SetBudget(budget)
_, err := client.CreateBudget(createBudgetInput)
if err != nil {
return fmt.Errorf("create budget failed: ", err)
}

return nil
}

func resourceAwsBudgetRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AWSClient).budgetconn
return fmt.Errorf("not yet implemented", client)
}

func resourceAwsBudgetUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AWSClient).budgetconn
return fmt.Errorf("not yet implemented", client)
}

func resourceAwsBudgetDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AWSClient).budgetconn
return fmt.Errorf("not yet implemented", client)
}
49 changes: 49 additions & 0 deletions aws/resource_aws_budget_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package aws

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAwsBudget_basic(t *testing.T) {
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
//CheckDestroy: testCheckBudgetDestroy,
Steps: []resource.TestStep{
{
Config: testBudgetConfig_basic(fmt.Sprintf("test-budget-%d", rInt)),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckOutput(
"aws_budget.foo", fmt.Sprintf("test-budget-%d", rInt)),
),
},
},
})
}

func testCheckBudgetDestroy(s *terraform.State) error {
return fmt.Errorf("not yet implemented")
}

func testBudgetConfig_basic(name string) string {
return fmt.Sprintf(`
resource "aws_budget" "foo" {
name = "%s"
type = "COST"
limit_amount = "100"
limit_unit = "USD"
include_tax = "true"
include_subscriptions = "false"
include_blended = "false"
time_period_start = 1477353600000
time_period_end = 1477958399000
time_unit = "MONTHLY"
}`, name)
}

0 comments on commit 951d77a

Please sign in to comment.