diff --git a/aws/config.go b/aws/config.go index 19704ef4d52..c2efe7162e2 100644 --- a/aws/config.go +++ b/aws/config.go @@ -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" @@ -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 { @@ -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) diff --git a/aws/provider.go b/aws/provider.go index 623446d93b5..b578a7c08d4 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -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(), diff --git a/aws/resource_aws_budget.go b/aws/resource_aws_budget.go new file mode 100644 index 00000000000..fdd501e5e1e --- /dev/null +++ b/aws/resource_aws_budget.go @@ -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) +} diff --git a/aws/resource_aws_budget_test.go b/aws/resource_aws_budget_test.go new file mode 100644 index 00000000000..d4543f72483 --- /dev/null +++ b/aws/resource_aws_budget_test.go @@ -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) +}