From 57ef042400431ab8277a39da58ad8a8c0206c1d8 Mon Sep 17 00:00:00 2001 From: GlennChia Date: Sat, 12 Feb 2022 05:53:13 +0800 Subject: [PATCH 1/7] feat(backup): ReportPlan datasrc schema --- .../service/backup/report_plan_data_source.go | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 internal/service/backup/report_plan_data_source.go diff --git a/internal/service/backup/report_plan_data_source.go b/internal/service/backup/report_plan_data_source.go new file mode 100644 index 00000000000..b24553a56b8 --- /dev/null +++ b/internal/service/backup/report_plan_data_source.go @@ -0,0 +1,81 @@ +package backup + +import ( + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" +) + +func DataSourceReportPlan() *schema.Resource { + return &schema.Resource{ + Schema: map[string]*schema.Schema{ + "arn": { + Type: schema.TypeString, + Computed: true, + }, + "creation_time": { + Type: schema.TypeString, + Computed: true, + }, + "deployment_status": { + Type: schema.TypeString, + Computed: true, + }, + "description": { + Type: schema.TypeString, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Required: true, + }, + "report_delivery_channel": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "formats": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "s3_bucket_name": { + Type: schema.TypeString, + Computed: true, + }, + "s3_key_prefix": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + "report_setting": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "framework_arns": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "number_of_frameworks": { + Type: schema.TypeInt, + Computed: true, + }, + "report_template": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + "tags": tftags.TagsSchemaComputed(), + }, + } +} + From e899e46f88f53d308894d2d45cd6d32d75870846 Mon Sep 17 00:00:00 2001 From: GlennChia Date: Sat, 12 Feb 2022 05:53:29 +0800 Subject: [PATCH 2/7] feat(backup): ReportPlan datasrc reads --- .../service/backup/report_plan_data_source.go | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/internal/service/backup/report_plan_data_source.go b/internal/service/backup/report_plan_data_source.go index b24553a56b8..07243ffd89d 100644 --- a/internal/service/backup/report_plan_data_source.go +++ b/internal/service/backup/report_plan_data_source.go @@ -1,12 +1,20 @@ package backup import ( + "fmt" + "time" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/backup" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-provider-aws/internal/conns" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" ) func DataSourceReportPlan() *schema.Resource { return &schema.Resource{ + Read: dataSourceReportPlanRead, + Schema: map[string]*schema.Schema{ "arn": { Type: schema.TypeString, @@ -79,3 +87,47 @@ func DataSourceReportPlan() *schema.Resource { } } +func dataSourceReportPlanRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*conns.AWSClient).BackupConn + ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig + + name := d.Get("name").(string) + + resp, err := conn.DescribeReportPlan(&backup.DescribeReportPlanInput{ + ReportPlanName: aws.String(name), + }) + if err != nil { + return fmt.Errorf("Error getting Backup Report Plan: %w", err) + } + + d.SetId(aws.StringValue(resp.ReportPlan.ReportPlanName)) + + d.Set("arn", resp.ReportPlan.ReportPlanArn) + d.Set("deployment_status", resp.ReportPlan.DeploymentStatus) + d.Set("description", resp.ReportPlan.ReportPlanDescription) + d.Set("name", resp.ReportPlan.ReportPlanName) + + if err := d.Set("creation_time", resp.ReportPlan.CreationTime.Format(time.RFC3339)); err != nil { + return fmt.Errorf("error setting creation_time: %s", err) + } + + if err := d.Set("report_delivery_channel", flattenReportDeliveryChannel(resp.ReportPlan.ReportDeliveryChannel)); err != nil { + return fmt.Errorf("error setting report_delivery_channel: %w", err) + } + + if err := d.Set("report_setting", flattenReportSetting(resp.ReportPlan.ReportSetting)); err != nil { + return fmt.Errorf("error setting report_delivery_channel: %w", err) + } + + tags, err := ListTags(conn, aws.StringValue(resp.ReportPlan.ReportPlanArn)) + + if err != nil { + return fmt.Errorf("error listing tags for Backup Report Plan (%s): %w", d.Id(), err) + } + + if err := d.Set("tags", tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { + return fmt.Errorf("error setting tags: %w", err) + } + + return nil +} From e3e0cd60bb5ee38059d333bd34040472e201e02f Mon Sep 17 00:00:00 2001 From: GlennChia Date: Sat, 12 Feb 2022 05:54:58 +0800 Subject: [PATCH 3/7] feat(backup): aws_backup_report_plan provider --- internal/provider/provider.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/internal/provider/provider.go b/internal/provider/provider.go index ae26a00af4d..1035cd865cf 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -417,9 +417,10 @@ func Provider() *schema.Provider { "aws_autoscaling_groups": autoscaling.DataSourceGroups(), "aws_launch_configuration": autoscaling.DataSourceLaunchConfiguration(), - "aws_backup_plan": backup.DataSourcePlan(), - "aws_backup_selection": backup.DataSourceSelection(), - "aws_backup_vault": backup.DataSourceVault(), + "aws_backup_plan": backup.DataSourcePlan(), + "aws_backup_report_plan": backup.DataSourceReportPlan(), + "aws_backup_selection": backup.DataSourceSelection(), + "aws_backup_vault": backup.DataSourceVault(), "aws_batch_compute_environment": batch.DataSourceComputeEnvironment(), "aws_batch_job_queue": batch.DataSourceJobQueue(), From 418e2d75c1e713bb12c133e38f5f2e772123357b Mon Sep 17 00:00:00 2001 From: GlennChia Date: Sat, 12 Feb 2022 05:55:25 +0800 Subject: [PATCH 4/7] test(backup): name for ReportPlan datasrc --- .../backup/report_plan_data_source_test.go | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 internal/service/backup/report_plan_data_source_test.go diff --git a/internal/service/backup/report_plan_data_source_test.go b/internal/service/backup/report_plan_data_source_test.go new file mode 100644 index 00000000000..9586d4519d8 --- /dev/null +++ b/internal/service/backup/report_plan_data_source_test.go @@ -0,0 +1,96 @@ +package backup_test + +import ( + "fmt" + "regexp" + "testing" + + "github.com/aws/aws-sdk-go/service/backup" + sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" +) + +func TestAccBackupReportPlanDataSource_basic(t *testing.T) { + datasourceName := "data.aws_backup_report_plan.test" + resourceName := "aws_backup_report_plan.test" + rName := sdkacctest.RandomWithPrefix("tf-test-bucket") + rName2 := fmt.Sprintf("tf_acc_test_%s", sdkacctest.RandString(7)) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, backup.EndpointsID), + Providers: acctest.Providers, + Steps: []resource.TestStep{ + { + Config: testAccReportPlanDataSourceConfig_nonExistent, + ExpectError: regexp.MustCompile(`Error getting Backup Report Plan`), + }, + { + Config: testAccReportPlanDataSourceConfig_basic(rName, rName2), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair(datasourceName, "arn", resourceName, "arn"), + resource.TestCheckResourceAttrPair(datasourceName, "creation_time", resourceName, "creation_time"), + resource.TestCheckResourceAttrSet(datasourceName, "deployment_status"), // CREATE_IN_PROGRESS | UPDATE_IN_PROGRESS | DELETE_IN_PROGRESS | COMPLETED + resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"), + resource.TestCheckResourceAttrPair(datasourceName, "report_delivery_channel.#", resourceName, "report_delivery_channel.#"), + resource.TestCheckResourceAttrPair(datasourceName, "report_delivery_channel.0.formats.#", resourceName, "report_delivery_channel.0.formats.#"), + resource.TestCheckResourceAttrPair(datasourceName, "report_delivery_channel.0.formats.0", resourceName, "report_delivery_channel.0.formats.0"), + resource.TestCheckResourceAttrPair(datasourceName, "report_delivery_channel.0.s3_bucket_name", resourceName, "report_delivery_channel.0.s3_bucket_name"), + resource.TestCheckResourceAttrPair(datasourceName, "report_setting.#", resourceName, "report_setting.#"), + resource.TestCheckResourceAttrPair(datasourceName, "report_setting.0.report_template", resourceName, "report_setting.0.report_template"), + resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"), + resource.TestCheckResourceAttrPair(datasourceName, "tags.Name", resourceName, "tags.Name"), + resource.TestCheckResourceAttrPair(datasourceName, "tags.Key2", resourceName, "tags.Key2"), + ), + }, + }, + }) +} + +const testAccReportPlanDataSourceConfig_nonExistent = ` +data "aws_backup_report_plan" "test" { + name = "tf_acc_test_does_not_exist" +} +` + +func testAccReportPlanDataSourceConfig_basic(rName, rName2 string) string { + return fmt.Sprintf(` +resource "aws_s3_bucket" "test" { + bucket = %[1]q +} + +resource "aws_s3_bucket_public_access_block" "test" { + bucket = aws_s3_bucket.test.id + block_public_acls = true + block_public_policy = true + ignore_public_acls = true + restrict_public_buckets = true +} + +resource "aws_backup_report_plan" "test" { + name = %[2]q + description = "Test report plan data source" + + report_delivery_channel { + formats = [ + "CSV" + ] + s3_bucket_name = aws_s3_bucket.test.id + } + + report_setting { + report_template = "RESTORE_JOB_REPORT" + } + + tags = { + "Name" = "Test Report Plan Data Source" + "Key2" = "Value2a" + } +} + +data "aws_backup_report_plan" "test" { + name = aws_backup_report_plan.test.name +} +`, rName, rName2) +} From 2ee2555aeea1ec2958029a6e914bda563eabd83d Mon Sep 17 00:00:00 2001 From: GlennChia Date: Sat, 12 Feb 2022 05:58:47 +0800 Subject: [PATCH 5/7] docs(backup): data source for ReportPlan --- .../docs/d/backup_report_plan.html.markdown | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 website/docs/d/backup_report_plan.html.markdown diff --git a/website/docs/d/backup_report_plan.html.markdown b/website/docs/d/backup_report_plan.html.markdown new file mode 100644 index 00000000000..3b619edfe48 --- /dev/null +++ b/website/docs/d/backup_report_plan.html.markdown @@ -0,0 +1,52 @@ +--- +subcategory: "Backup" +layout: "aws" +page_title: "AWS: aws_backup_report_plan" +description: |- + Provides details about an AWS Backup Report Plan. +--- + +# Data Source: aws_backup_report_plan + +Use this data source to get information on an existing backup report plan. + +## Example Usage + +```terraform +data "aws_backup_report_plan" "example" { + name = "tf_example_backup_report_plan_name" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The backup report plan name. + +## Attributes Reference + +In addition to the arguments above, the following attributes are exported: + +* `arn` - The ARN of the backup report plan. +* `creation_time` - The date and time that a report plan is created, in Unix format and Coordinated Universal Time (UTC). +* `deployment_status` - The deployment status of a report plan. The statuses are: `CREATE_IN_PROGRESS` | `UPDATE_IN_PROGRESS` | `DELETE_IN_PROGRESS` | `COMPLETED`. +* `description` - The description of the report plan. +* `id` - The id of the report plan. +* `report_delivery_channel` - An object that contains information about where and how to deliver your reports, specifically your Amazon S3 bucket name, S3 key prefix, and the formats of your reports. Detailed below. +* `report_setting` - An object that identifies the report template for the report. Reports are built using a report template. Detailed below. +* `tags` - Metadata that you can assign to help organize the report plans you create. + +### Report Delivery Channel Attributes +For **report_delivery_channel** the following attributes are supported: + +* `formats` - A list of the format of your reports: CSV, JSON, or both. +* `s3_bucket_name` - The unique name of the S3 bucket that receives your reports. +* `s3_key_prefix` - The prefix for where Backup Audit Manager delivers your reports to Amazon S3. The prefix is this part of the following path: s3://your-bucket-name/prefix/Backup/us-west-2/year/month/day/report-name. + +### Report Setting Attributes +For **report_setting** the following attributes are supported: + +* `framework_arns` - Specifies the Amazon Resource Names (ARNs) of the frameworks a report covers. +* `number_of_frameworks` - Specifies the number of frameworks a report covers. +* `report_template` - Identifies the report template for the report. Reports are built using a report template. \ No newline at end of file From 4266f17b37b7e4361b77a0d97445a031b549c6cb Mon Sep 17 00:00:00 2001 From: GlennChia Date: Sat, 12 Feb 2022 06:04:36 +0800 Subject: [PATCH 6/7] ci(backup): changelog backup_report_plan datasrc --- .changelog/23146.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/23146.txt diff --git a/.changelog/23146.txt b/.changelog/23146.txt new file mode 100644 index 00000000000..01b1b5476d9 --- /dev/null +++ b/.changelog/23146.txt @@ -0,0 +1,3 @@ +```release-note:new-data-source +aws_backup_report_plan +``` \ No newline at end of file From 5b9a34e095c5023a83f90388c9d77f2d80408797 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Sun, 13 Feb 2022 15:22:04 -0500 Subject: [PATCH 7/7] Add call to 'testAccReportPlanPreCheck'. --- internal/service/backup/report_plan_data_source_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/backup/report_plan_data_source_test.go b/internal/service/backup/report_plan_data_source_test.go index 9586d4519d8..2f889474a2a 100644 --- a/internal/service/backup/report_plan_data_source_test.go +++ b/internal/service/backup/report_plan_data_source_test.go @@ -18,7 +18,7 @@ func TestAccBackupReportPlanDataSource_basic(t *testing.T) { rName2 := fmt.Sprintf("tf_acc_test_%s", sdkacctest.RandString(7)) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acctest.PreCheck(t) }, + PreCheck: func() { acctest.PreCheck(t); testAccReportPlanPreCheck(t) }, ErrorCheck: acctest.ErrorCheck(t, backup.EndpointsID), Providers: acctest.Providers, Steps: []resource.TestStep{