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

Add Resource for Cost and Usage Report Definitions #7432

Merged
merged 16 commits into from
Feb 13, 2019
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
3 changes: 3 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"github.com/aws/aws-sdk-go/service/cognitoidentity"
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
"github.com/aws/aws-sdk-go/service/configservice"
"github.com/aws/aws-sdk-go/service/costandusagereportservice"
"github.com/aws/aws-sdk-go/service/databasemigrationservice"
"github.com/aws/aws-sdk-go/service/datapipeline"
"github.com/aws/aws-sdk-go/service/datasync"
Expand Down Expand Up @@ -215,6 +216,7 @@ type AWSClient struct {
cognitoconn *cognitoidentity.CognitoIdentity
cognitoidpconn *cognitoidentityprovider.CognitoIdentityProvider
configconn *configservice.ConfigService
costandusagereportconn *costandusagereportservice.CostandUsageReportService
datapipelineconn *datapipeline.DataPipeline
datasyncconn *datasync.DataSync
daxconn *dax.DAX
Expand Down Expand Up @@ -576,6 +578,7 @@ func (c *Config) Client() (interface{}, error) {
client.cognitoconn = cognitoidentity.New(sess)
client.cognitoidpconn = cognitoidentityprovider.New(sess)
client.configconn = configservice.New(sess)
client.costandusagereportconn = costandusagereportservice.New(sess)
client.datapipelineconn = datapipeline.New(sess)
client.datasyncconn = datasync.New(sess)
client.daxconn = dax.New(awsDynamoSess)
Expand Down
59 changes: 59 additions & 0 deletions aws/data_source_aws_cur_report_definition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package aws

import (
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceAwsCurReportDefinition() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsCurReportDefinitionRead,

Schema: map[string]*schema.Schema{
"report_name": {
Type: schema.TypeString,
Required: true,
},
"time_unit": {
Type: schema.TypeString,
Computed: true,
},
"format": {
Type: schema.TypeString,
Computed: true,
},
"compression": {
Type: schema.TypeString,
Computed: true,
},
"additional_schema_elements": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Computed: true,
},
"s3_bucket": {
Type: schema.TypeString,
Computed: true,
},
"s3_prefix": {
Type: schema.TypeString,
Computed: true,
},
"s3_region": {
Type: schema.TypeString,
Computed: true,
},
"additional_artifacts": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Computed: true,
},
},
}
}

func dataSourceAwsCurReportDefinitionRead(d *schema.ResourceData, meta interface{}) error {
d.SetId(d.Get("report_name").(string))
return resourceAwsCurReportDefinitionRead(d, meta)
}
122 changes: 122 additions & 0 deletions aws/data_source_aws_cur_report_definition_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package aws

import (
"fmt"
"testing"

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

func TestAccDataSourceAwsCurReportDefinition_basic(t *testing.T) {
resourceName := "aws_cur_report_definition.test"
datasourceName := "data.aws_cur_report_definition.test"

reportName := acctest.RandomWithPrefix("tf_acc_test")
bucketName := fmt.Sprintf("tf-test-bucket-%d", acctest.RandInt())
bucketRegion := "us-east-1"

resource.ParallelTest(t, resource.TestCase{
jbmchuck marked this conversation as resolved.
Show resolved Hide resolved
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsCurReportDefinitionDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsCurReportDefinitionConfig_basic(reportName, bucketName, bucketRegion),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsCurReportDefinitionCheckExists(datasourceName, resourceName),
resource.TestCheckResourceAttrPair(datasourceName, "report_name", resourceName, "report_name"),
resource.TestCheckResourceAttrPair(datasourceName, "time_unit", resourceName, "time_unit"),
resource.TestCheckResourceAttrPair(datasourceName, "compression", resourceName, "compression"),
resource.TestCheckResourceAttrPair(datasourceName, "additional_schema_elements.#", resourceName, "additional_schema_elements.#"),
resource.TestCheckResourceAttrPair(datasourceName, "s3_bucket", resourceName, "s3_bucket"),
resource.TestCheckResourceAttrPair(datasourceName, "s3_prefix", resourceName, "s3_prefix"),
resource.TestCheckResourceAttrPair(datasourceName, "s3_region", resourceName, "s3_region"),
resource.TestCheckResourceAttrPair(datasourceName, "additional_artifacts.#", resourceName, "additional_artifacts.#"),
),
},
},
})
}

func testAccDataSourceAwsCurReportDefinitionCheckExists(datasourceName, resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, ok := s.RootModule().Resources[datasourceName]
if !ok {
return fmt.Errorf("root module has no data source called %s", datasourceName)
}
_, ok = s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("root module has no resource called %s", resourceName)
}
return nil
}
}

// note: cur report definitions are currently only supported in us-east-1
func testAccDataSourceAwsCurReportDefinitionConfig_basic(reportName string, bucketName string, bucketRegion string) string {
return fmt.Sprintf(`
provider "aws" {
region = "us-east-1"
}

data "aws_billing_service_account" "test" {}

resource "aws_s3_bucket" "test" {
bucket = "%[2]s"
acl = "private"
force_destroy = true
region = "%[3]s"
}

resource "aws_s3_bucket_policy" "test" {
bucket = "${aws_s3_bucket.test.id}"
policy = <<POLICY
{
"Version": "2008-10-17",
"Id": "s3policy",
"Statement": [
{
"Sid": "AllowCURBillingACLPolicy",
"Effect": "Allow",
"Principal": {
"AWS": "${data.aws_billing_service_account.test.arn}"
},
"Action": [
"s3:GetBucketAcl",
"s3:GetBucketPolicy"
],
"Resource": "${aws_s3_bucket.test.arn}"
},
{
"Sid": "AllowCURPutObject",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::386209384616:root"
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::${aws_s3_bucket.test.id}/*"
}
]
}
POLICY
}

resource "aws_cur_report_definition" "test" {
report_name = "%[1]s"
time_unit = "DAILY"
format = "textORcsv"
compression = "GZIP"
additional_schema_elements = ["RESOURCES"]
s3_bucket = "${aws_s3_bucket.test.id}"
s3_prefix = ""
s3_region = "${aws_s3_bucket.test.region}"
additional_artifacts = ["REDSHIFT", "QUICKSIGHT"]
}

data "aws_cur_report_definition" "test" {
report_name = "${aws_cur_report_definition.test.report_name}"
}
`, reportName, bucketName, bucketRegion)
}
2 changes: 2 additions & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func Provider() terraform.ResourceProvider {
"aws_cloudwatch_log_group": dataSourceAwsCloudwatchLogGroup(),
"aws_cognito_user_pools": dataSourceAwsCognitoUserPools(),
"aws_codecommit_repository": dataSourceAwsCodeCommitRepository(),
"aws_cur_report_definition": dataSourceAwsCurReportDefinition(),
"aws_db_cluster_snapshot": dataSourceAwsDbClusterSnapshot(),
"aws_db_event_categories": dataSourceAwsDbEventCategories(),
"aws_db_instance": dataSourceAwsDbInstance(),
Expand Down Expand Up @@ -381,6 +382,7 @@ func Provider() terraform.ResourceProvider {
"aws_codebuild_webhook": resourceAwsCodeBuildWebhook(),
"aws_codepipeline": resourceAwsCodePipeline(),
"aws_codepipeline_webhook": resourceAwsCodePipelineWebhook(),
"aws_cur_report_definition": resourceAwsCurReportDefinition(),
"aws_customer_gateway": resourceAwsCustomerGateway(),
"aws_datasync_agent": resourceAwsDataSyncAgent(),
"aws_datasync_location_efs": resourceAwsDataSyncLocationEfs(),
Expand Down
Loading