From 3fd1a084dc23d04922e739d5e22858250ca9f696 Mon Sep 17 00:00:00 2001 From: Cody Bond Date: Sat, 24 Dec 2022 12:15:58 -0500 Subject: [PATCH 01/12] Add func to FindWorkspaces --- internal/service/amp/find.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/internal/service/amp/find.go b/internal/service/amp/find.go index f3eb5f6cb10..e794cacdb0c 100644 --- a/internal/service/amp/find.go +++ b/internal/service/amp/find.go @@ -125,3 +125,36 @@ func FindLoggingConfigurationByWorkspaceID(ctx context.Context, conn *prometheus return output.LoggingConfiguration, nil } + +func FindWorkspaces(ctx context.Context, conn *prometheusservice.PrometheusService, alias string) ([]*prometheusservice.WorkspaceSummary, error) { + input := &prometheusservice.ListWorkspacesInput{} + + // ListWorkspaces returns all workspaces that begin with the given alias. + if alias != "" { + input.Alias = aws.String(alias) + } + + var results []*prometheusservice.WorkspaceSummary + err := conn.ListWorkspacesPages(input, func(page *prometheusservice.ListWorkspacesOutput, lastPage bool) bool { + if page == nil { + return !lastPage + } + + for _, w := range page.Workspaces { + if w == nil { + continue + } + results = append(results, w) + } + + return !lastPage + }) + + if err != nil { + return nil, err + } + if len(results) == 0 { + return nil, tfresource.NewEmptyResultError(nil) + } + return results, nil +} From 27ebd3ffb7b82d074dfb6ab5d6ae4d975dbe5860 Mon Sep 17 00:00:00 2001 From: Cody Bond Date: Sat, 24 Dec 2022 12:17:01 -0500 Subject: [PATCH 02/12] Add aws_prometheus_workspaces data source --- internal/provider/provider.go | 3 +- .../service/amp/workspaces_data_source.go | 63 +++++++++++ .../amp/workspaces_data_source_test.go | 106 ++++++++++++++++++ 3 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 internal/service/amp/workspaces_data_source.go create mode 100644 internal/service/amp/workspaces_data_source_test.go diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 71027843189..2362b915174 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -813,7 +813,8 @@ func New(ctx context.Context) (*schema.Provider, error) { "aws_pricing_product": pricing.DataSourceProduct(), - "aws_prometheus_workspace": amp.DataSourceWorkspace(), + "aws_prometheus_workspace": amp.DataSourceWorkspace(), + "aws_prometheus_workspaces": amp.DataSourceWorkspaces(), "aws_qldb_ledger": qldb.DataSourceLedger(), diff --git a/internal/service/amp/workspaces_data_source.go b/internal/service/amp/workspaces_data_source.go new file mode 100644 index 00000000000..7efb0bb4206 --- /dev/null +++ b/internal/service/amp/workspaces_data_source.go @@ -0,0 +1,63 @@ +package amp + +import ( + "context" + + "github.com/aws/aws-sdk-go/aws" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-provider-aws/internal/conns" +) + +func DataSourceWorkspaces() *schema.Resource { + return &schema.Resource{ + ReadWithoutTimeout: dataSourceWorkspacesRead, + + Schema: map[string]*schema.Schema{ + "alias_prefix": { + Type: schema.TypeString, + Optional: true, + }, + "aliases": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "arns": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "workspace_ids": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + } +} + +func dataSourceWorkspacesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).AMPConn + + alias_prefix := d.Get("alias_prefix").(string) + workspaces, err := FindWorkspaces(ctx, conn, alias_prefix) + + if err != nil { + return diag.Errorf("Error reading AMP Workspaces: %s", err) + } + + var arns, aliases, workspace_ids []string + for _, w := range workspaces { + arns = append(arns, aws.StringValue(w.Arn)) + aliases = append(aliases, aws.StringValue(w.Alias)) + workspace_ids = append(workspace_ids, aws.StringValue(w.WorkspaceId)) + } + + d.SetId(meta.(*conns.AWSClient).Region) + d.Set("arns", arns) + d.Set("aliases", aliases) + d.Set("workspace_ids", workspace_ids) + + return nil +} diff --git a/internal/service/amp/workspaces_data_source_test.go b/internal/service/amp/workspaces_data_source_test.go new file mode 100644 index 00000000000..988f0e00116 --- /dev/null +++ b/internal/service/amp/workspaces_data_source_test.go @@ -0,0 +1,106 @@ +package amp_test + +import ( + "fmt" + "strconv" + "testing" + + "github.com/aws/aws-sdk-go/service/prometheusservice" + sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" +) + +// TODO: Figure out why I'm returning one more alias than expected. +func TestAccAMPWorkspacesDataSource_basic(t *testing.T) { + rCount := strconv.Itoa(sdkacctest.RandIntRange(1, 4)) + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + dataSourceName := "data.aws_prometheus_workspaces.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t); acctest.PreCheckPartitionHasService(prometheusservice.EndpointsID, t) }, + ErrorCheck: acctest.ErrorCheck(t, prometheusservice.EndpointsID), + PreventPostDestroyRefresh: true, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccWorkspacesDataSourceConfig_resources(rCount, rName), + }, + { + Config: testAccWorkspacesDataSourceConfig_all(rCount, rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckWorkspacesExistsDataSource(dataSourceName), + resource.TestCheckResourceAttr(dataSourceName, "aliases.#", rCount), + resource.TestCheckResourceAttr(dataSourceName, "arns.#", rCount), + resource.TestCheckResourceAttr(dataSourceName, "workspace_ids.#", rCount), + ), + }, + }, + }) +} + +func TestAccAMPWorkspacesDataSource_aliasPrefix(t *testing.T) { + rCount := strconv.Itoa(sdkacctest.RandIntRange(1, 4)) + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + dataSourceName := "data.aws_prometheus_workspaces.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t); acctest.PreCheckPartitionHasService(prometheusservice.EndpointsID, t) }, + ErrorCheck: acctest.ErrorCheck(t, prometheusservice.EndpointsID), + PreventPostDestroyRefresh: true, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccWorkspacesDataSourceConfig_aliasPrefix(rCount, rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckWorkspacesExistsDataSource(dataSourceName), + resource.TestCheckResourceAttr(dataSourceName, "aliases.#", "1"), + resource.TestCheckResourceAttr(dataSourceName, "arns.#", "1"), + resource.TestCheckResourceAttr(dataSourceName, "workspace_ids.#", "1"), + ), + }, + }, + }) +} + +func testAccWorkspacesDataSourceConfig_resources(rCount, rName string) string { + return fmt.Sprintf(` +resource "aws_prometheus_workspace" "test" { + count = %[1]s + alias = "%[2]s-${count.index}" +} +`, rCount, rName) +} + +func testAccWorkspacesDataSourceConfig_all(rCount, rName string) string { + return fmt.Sprintf(` +%s +data "aws_prometheus_workspaces" "test" { +} +`, testAccWorkspacesDataSourceConfig_resources(rCount, rName)) +} + +func testAccWorkspacesDataSourceConfig_aliasPrefix(rCount, rName string) string { + return fmt.Sprintf(` +%s +data "aws_prometheus_workspaces" "test" { + alias_prefix = aws_prometheus_workspace.test[0].alias +} +`, testAccWorkspacesDataSourceConfig_resources(rCount, rName)) +} + +func testAccCheckWorkspacesExistsDataSource(addr string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[addr] + if !ok { + return fmt.Errorf("Can't find AMP workspaces data source: %s", addr) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("AMP workspaces data source ID not set") + } + + return nil + } +} From 6caef163578b1758c3d9ad6e25b775d69c1adc6a Mon Sep 17 00:00:00 2001 From: Cody Bond Date: Sat, 24 Dec 2022 12:18:38 -0500 Subject: [PATCH 03/12] Remove TODO comment --- internal/service/amp/workspaces_data_source_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/service/amp/workspaces_data_source_test.go b/internal/service/amp/workspaces_data_source_test.go index 988f0e00116..27f2a553cd0 100644 --- a/internal/service/amp/workspaces_data_source_test.go +++ b/internal/service/amp/workspaces_data_source_test.go @@ -12,7 +12,6 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/acctest" ) -// TODO: Figure out why I'm returning one more alias than expected. func TestAccAMPWorkspacesDataSource_basic(t *testing.T) { rCount := strconv.Itoa(sdkacctest.RandIntRange(1, 4)) rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) From 3171b99404fd47f377f0517dc74e3e88a0f938ca Mon Sep 17 00:00:00 2001 From: Cody Bond Date: Sat, 24 Dec 2022 12:38:33 -0500 Subject: [PATCH 04/12] Don't return an EmptyResults error when there's no workspaces found --- internal/service/amp/find.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/internal/service/amp/find.go b/internal/service/amp/find.go index e794cacdb0c..5e1f796ec76 100644 --- a/internal/service/amp/find.go +++ b/internal/service/amp/find.go @@ -150,11 +150,5 @@ func FindWorkspaces(ctx context.Context, conn *prometheusservice.PrometheusServi return !lastPage }) - if err != nil { - return nil, err - } - if len(results) == 0 { - return nil, tfresource.NewEmptyResultError(nil) - } - return results, nil + return results, err } From 32cf6e1084d122db8a0793119ced0c13e2b122a6 Mon Sep 17 00:00:00 2001 From: Cody Bond Date: Sat, 24 Dec 2022 12:55:20 -0500 Subject: [PATCH 05/12] Add documentation --- .../d/prometheus_workspaces.html.markdown | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 website/docs/d/prometheus_workspaces.html.markdown diff --git a/website/docs/d/prometheus_workspaces.html.markdown b/website/docs/d/prometheus_workspaces.html.markdown new file mode 100644 index 00000000000..dfe4ae1160d --- /dev/null +++ b/website/docs/d/prometheus_workspaces.html.markdown @@ -0,0 +1,43 @@ +--- +subcategory: "AMP (Managed Prometheus)" +layout: "aws" +page_title: "AWS: aws_prometheus_workspaces" +description: |- + Gets the aliases, ARNs, and workspace IDs of Amazon Prometheus workspaces. +--- + +# Data Source: aws_prometheus_workspaces + +Provides the aliases, ARNs, and workspace IDs of Amazon Prometheus workspaces. + +## Example Usage + +The following example returns all of the workspaces in a region: + +```terraform +data "aws_prometheus_workspaces" "example" { +} +``` + +The following example filters the workspaces by alias. Only the workspaces with +aliases that begin with the value of `alias_prefix` will be returned: + +```terraform +data "aws_prometheus_workspaces" "example" { + alias_prefix = "example" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `alias_prefix` - (Optional) Limits results to workspaces with aliases that begin with this value. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `aliases` - List of aliases of the matched Prometheus workspaces. +* `arns` - List of ARNs of the matched Prometheus workspaces. +* `workspace_ids` - List of workspace IDs of the matched Prometheus workspaces. From f1476317a8e6dc68336909e508f2b2d1c0c8b715 Mon Sep 17 00:00:00 2001 From: Cody Bond Date: Sat, 24 Dec 2022 13:19:12 -0500 Subject: [PATCH 06/12] Add changelog --- .changelog/28574.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/28574.txt diff --git a/.changelog/28574.txt b/.changelog/28574.txt new file mode 100644 index 00000000000..46f68b38932 --- /dev/null +++ b/.changelog/28574.txt @@ -0,0 +1,3 @@ +```release-note:new-data-source +aws_prometheus_workspaces +``` From 52b18e254a81df5387f1a35c57a9ecc9161373e9 Mon Sep 17 00:00:00 2001 From: Cody Bond Date: Sat, 24 Dec 2022 14:29:55 -0500 Subject: [PATCH 07/12] sync test and config names --- internal/service/amp/workspaces_data_source_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/amp/workspaces_data_source_test.go b/internal/service/amp/workspaces_data_source_test.go index 27f2a553cd0..62da303bad3 100644 --- a/internal/service/amp/workspaces_data_source_test.go +++ b/internal/service/amp/workspaces_data_source_test.go @@ -27,7 +27,7 @@ func TestAccAMPWorkspacesDataSource_basic(t *testing.T) { Config: testAccWorkspacesDataSourceConfig_resources(rCount, rName), }, { - Config: testAccWorkspacesDataSourceConfig_all(rCount, rName), + Config: testAccWorkspacesDataSourceConfig_basic(rCount, rName), Check: resource.ComposeTestCheckFunc( testAccCheckWorkspacesExistsDataSource(dataSourceName), resource.TestCheckResourceAttr(dataSourceName, "aliases.#", rCount), @@ -72,7 +72,7 @@ resource "aws_prometheus_workspace" "test" { `, rCount, rName) } -func testAccWorkspacesDataSourceConfig_all(rCount, rName string) string { +func testAccWorkspacesDataSourceConfig_basic(rCount, rName string) string { return fmt.Sprintf(` %s data "aws_prometheus_workspaces" "test" { From f45fb93f5dc187c211e8601c1c6363d3e668c86d Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 17 Apr 2023 12:17:09 -0400 Subject: [PATCH 08/12] d/aws_prometheus_workspaces: Cosmetics. --- internal/service/amp/workspaces_data_source.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/amp/workspaces_data_source.go b/internal/service/amp/workspaces_data_source.go index 600806e34a4..237f3495b6a 100644 --- a/internal/service/amp/workspaces_data_source.go +++ b/internal/service/amp/workspaces_data_source.go @@ -45,7 +45,7 @@ func dataSourceWorkspacesRead(ctx context.Context, d *schema.ResourceData, meta workspaces, err := FindWorkspaces(ctx, conn, alias_prefix) if err != nil { - return diag.Errorf("Error reading AMP Workspaces: %s", err) + return diag.Errorf("reading AMP Workspaces: %s", err) } var arns, aliases, workspace_ids []string @@ -56,8 +56,8 @@ func dataSourceWorkspacesRead(ctx context.Context, d *schema.ResourceData, meta } d.SetId(meta.(*conns.AWSClient).Region) - d.Set("arns", arns) d.Set("aliases", aliases) + d.Set("arns", arns) d.Set("workspace_ids", workspace_ids) return nil From 29a6f80cc81f5552a3cf8e7cc89aeabdad760a92 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 17 Apr 2023 12:22:40 -0400 Subject: [PATCH 09/12] Fix semgrep 'ci.semgrep.migrate.aws-api-context'. --- internal/service/amp/find.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/internal/service/amp/find.go b/internal/service/amp/find.go index d0117b03a09..a29a0b0059b 100644 --- a/internal/service/amp/find.go +++ b/internal/service/amp/find.go @@ -128,27 +128,29 @@ func FindLoggingConfigurationByWorkspaceID(ctx context.Context, conn *prometheus func FindWorkspaces(ctx context.Context, conn *prometheusservice.PrometheusService, alias string) ([]*prometheusservice.WorkspaceSummary, error) { input := &prometheusservice.ListWorkspacesInput{} - - // ListWorkspaces returns all workspaces that begin with the given alias. if alias != "" { input.Alias = aws.String(alias) } + var output []*prometheusservice.WorkspaceSummary - var results []*prometheusservice.WorkspaceSummary - err := conn.ListWorkspacesPages(input, func(page *prometheusservice.ListWorkspacesOutput, lastPage bool) bool { + err := conn.ListWorkspacesPagesWithContext(ctx, input, func(page *prometheusservice.ListWorkspacesOutput, lastPage bool) bool { if page == nil { return !lastPage } - for _, w := range page.Workspaces { - if w == nil { + for _, v := range page.Workspaces { + if v == nil { continue } - results = append(results, w) + output = append(output, v) } return !lastPage }) - return results, err + if err != nil { + return nil, err + } + + return output, nil } From 4b4845f82474267a61eeca487d251c76329a0a5e Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 17 Apr 2023 12:24:28 -0400 Subject: [PATCH 10/12] Fix terrafmt website errors. --- website/docs/d/prometheus_workspaces.html.markdown | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/website/docs/d/prometheus_workspaces.html.markdown b/website/docs/d/prometheus_workspaces.html.markdown index dfe4ae1160d..bcddb9cfc89 100644 --- a/website/docs/d/prometheus_workspaces.html.markdown +++ b/website/docs/d/prometheus_workspaces.html.markdown @@ -15,8 +15,7 @@ Provides the aliases, ARNs, and workspace IDs of Amazon Prometheus workspaces. The following example returns all of the workspaces in a region: ```terraform -data "aws_prometheus_workspaces" "example" { -} +data "aws_prometheus_workspaces" "example" {} ``` The following example filters the workspaces by alias. Only the workspaces with @@ -24,7 +23,7 @@ aliases that begin with the value of `alias_prefix` will be returned: ```terraform data "aws_prometheus_workspaces" "example" { - alias_prefix = "example" + alias_prefix = "example" } ``` From 2bc7acc092620150d4144f7e830c71e033425762 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 17 Apr 2023 13:19:18 -0400 Subject: [PATCH 11/12] Fix semgrep 'ci.caps0-in-func-name'. --- internal/service/amp/find.go | 2 +- .../service/amp/workspaces_data_source.go | 4 +- .../amp/workspaces_data_source_test.go | 43 +++++-------------- 3 files changed, 14 insertions(+), 35 deletions(-) diff --git a/internal/service/amp/find.go b/internal/service/amp/find.go index a29a0b0059b..38fb205307b 100644 --- a/internal/service/amp/find.go +++ b/internal/service/amp/find.go @@ -126,7 +126,7 @@ func FindLoggingConfigurationByWorkspaceID(ctx context.Context, conn *prometheus return output.LoggingConfiguration, nil } -func FindWorkspaces(ctx context.Context, conn *prometheusservice.PrometheusService, alias string) ([]*prometheusservice.WorkspaceSummary, error) { +func FindWorkspaces(ctx context.Context, conn *prometheusservice.PrometheusService, alias string) ([]*prometheusservice.WorkspaceSummary, error) { // nosemgrep:ci.caps0-in-func-name input := &prometheusservice.ListWorkspacesInput{} if alias != "" { input.Alias = aws.String(alias) diff --git a/internal/service/amp/workspaces_data_source.go b/internal/service/amp/workspaces_data_source.go index 237f3495b6a..5ce2e03ac5a 100644 --- a/internal/service/amp/workspaces_data_source.go +++ b/internal/service/amp/workspaces_data_source.go @@ -10,7 +10,7 @@ import ( ) // @SDKDataSource("aws_prometheus_workspaces") -func DataSourceWorkspaces() *schema.Resource { +func DataSourceWorkspaces() *schema.Resource { // nosemgrep:ci.caps0-in-func-name return &schema.Resource{ ReadWithoutTimeout: dataSourceWorkspacesRead, @@ -38,7 +38,7 @@ func DataSourceWorkspaces() *schema.Resource { } } -func dataSourceWorkspacesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { +func dataSourceWorkspacesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { // nosemgrep:ci.caps0-in-func-name conn := meta.(*conns.AWSClient).AMPConn() alias_prefix := d.Get("alias_prefix").(string) diff --git a/internal/service/amp/workspaces_data_source_test.go b/internal/service/amp/workspaces_data_source_test.go index 569e3d4fc4d..425e27c9172 100644 --- a/internal/service/amp/workspaces_data_source_test.go +++ b/internal/service/amp/workspaces_data_source_test.go @@ -8,11 +8,10 @@ import ( "github.com/aws/aws-sdk-go/service/prometheusservice" sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "github.com/hashicorp/terraform-provider-aws/internal/acctest" ) -func TestAccAMPWorkspacesDataSource_basic(t *testing.T) { +func TestAccAMPWorkspacesDataSource_basic(t *testing.T) { // nosemgrep:ci.caps0-in-func-name ctx := acctest.Context(t) rCount := strconv.Itoa(sdkacctest.RandIntRange(1, 4)) rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -28,12 +27,11 @@ func TestAccAMPWorkspacesDataSource_basic(t *testing.T) { ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, Steps: []resource.TestStep{ { - Config: testAccWorkspacesDataSourceConfig_resources(rCount, rName), + Config: testAccWorkspacesDataSourceConfig_base(rCount, rName), }, { Config: testAccWorkspacesDataSourceConfig_basic(rCount, rName), Check: resource.ComposeTestCheckFunc( - testAccCheckWorkspacesExistsDataSource(dataSourceName), resource.TestCheckResourceAttr(dataSourceName, "aliases.#", rCount), resource.TestCheckResourceAttr(dataSourceName, "arns.#", rCount), resource.TestCheckResourceAttr(dataSourceName, "workspace_ids.#", rCount), @@ -43,7 +41,7 @@ func TestAccAMPWorkspacesDataSource_basic(t *testing.T) { }) } -func TestAccAMPWorkspacesDataSource_aliasPrefix(t *testing.T) { +func TestAccAMPWorkspacesDataSource_aliasPrefix(t *testing.T) { // nosemgrep:ci.caps0-in-func-name ctx := acctest.Context(t) rCount := strconv.Itoa(sdkacctest.RandIntRange(1, 4)) rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -61,7 +59,6 @@ func TestAccAMPWorkspacesDataSource_aliasPrefix(t *testing.T) { { Config: testAccWorkspacesDataSourceConfig_aliasPrefix(rCount, rName), Check: resource.ComposeTestCheckFunc( - testAccCheckWorkspacesExistsDataSource(dataSourceName), resource.TestCheckResourceAttr(dataSourceName, "aliases.#", "1"), resource.TestCheckResourceAttr(dataSourceName, "arns.#", "1"), resource.TestCheckResourceAttr(dataSourceName, "workspace_ids.#", "1"), @@ -71,7 +68,7 @@ func TestAccAMPWorkspacesDataSource_aliasPrefix(t *testing.T) { }) } -func testAccWorkspacesDataSourceConfig_resources(rCount, rName string) string { +func testAccWorkspacesDataSourceConfig_base(rCount, rName string) string { // nosemgrep:ci.caps0-in-func-name return fmt.Sprintf(` resource "aws_prometheus_workspace" "test" { count = %[1]s @@ -80,34 +77,16 @@ resource "aws_prometheus_workspace" "test" { `, rCount, rName) } -func testAccWorkspacesDataSourceConfig_basic(rCount, rName string) string { - return fmt.Sprintf(` -%s -data "aws_prometheus_workspaces" "test" { -} -`, testAccWorkspacesDataSourceConfig_resources(rCount, rName)) +func testAccWorkspacesDataSourceConfig_basic(rCount, rName string) string { // nosemgrep:ci.caps0-in-func-name + return acctest.ConfigCompose(testAccWorkspacesDataSourceConfig_base(rCount, rName), ` +data "aws_prometheus_workspaces" "test" {} +`) } -func testAccWorkspacesDataSourceConfig_aliasPrefix(rCount, rName string) string { - return fmt.Sprintf(` -%s +func testAccWorkspacesDataSourceConfig_aliasPrefix(rCount, rName string) string { // nosemgrep:ci.caps0-in-func-name + return acctest.ConfigCompose(testAccWorkspacesDataSourceConfig_base(rCount, rName), ` data "aws_prometheus_workspaces" "test" { alias_prefix = aws_prometheus_workspace.test[0].alias } -`, testAccWorkspacesDataSourceConfig_resources(rCount, rName)) -} - -func testAccCheckWorkspacesExistsDataSource(addr string) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[addr] - if !ok { - return fmt.Errorf("Can't find AMP workspaces data source: %s", addr) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("AMP workspaces data source ID not set") - } - - return nil - } +`) } From 53304950bd89ef08f11ac9dbdf3f2a6decde46cb Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 17 Apr 2023 13:29:07 -0400 Subject: [PATCH 12/12] Add 'acctest.CheckResourceAttrGreaterThanOrEqualValue'. --- internal/acctest/acctest.go | 16 +++++++++ .../amp/workspaces_data_source_test.go | 33 +++++++++---------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/internal/acctest/acctest.go b/internal/acctest/acctest.go index e290a437524..24d3d3b6f37 100644 --- a/internal/acctest/acctest.go +++ b/internal/acctest/acctest.go @@ -2193,6 +2193,22 @@ func CheckResourceAttrGreaterThanValue(n, key, value string) resource.TestCheckF } } +func CheckResourceAttrGreaterThanOrEqualValue(n, key string, val int) resource.TestCheckFunc { + return resource.TestCheckResourceAttrWith(n, key, func(value string) error { + v, err := strconv.Atoi(value) + + if err != nil { + return err + } + + if v < val { + return fmt.Errorf("%s: Attribute %q is not greater than or equal to %d, got %d", n, key, val, v) + } + + return nil + }) +} + // RunSerialTests1Level runs test cases in parallel, optionally sleeping between each. func RunSerialTests1Level(t *testing.T, testCases map[string]func(t *testing.T), d time.Duration) { t.Helper() diff --git a/internal/service/amp/workspaces_data_source_test.go b/internal/service/amp/workspaces_data_source_test.go index 425e27c9172..8a390bb716a 100644 --- a/internal/service/amp/workspaces_data_source_test.go +++ b/internal/service/amp/workspaces_data_source_test.go @@ -2,7 +2,6 @@ package amp_test import ( "fmt" - "strconv" "testing" "github.com/aws/aws-sdk-go/service/prometheusservice" @@ -13,7 +12,7 @@ import ( func TestAccAMPWorkspacesDataSource_basic(t *testing.T) { // nosemgrep:ci.caps0-in-func-name ctx := acctest.Context(t) - rCount := strconv.Itoa(sdkacctest.RandIntRange(1, 4)) + rCount := sdkacctest.RandIntRange(1, 4) rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) dataSourceName := "data.aws_prometheus_workspaces.test" @@ -27,14 +26,14 @@ func TestAccAMPWorkspacesDataSource_basic(t *testing.T) { // nosemgrep:ci.caps0- ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, Steps: []resource.TestStep{ { - Config: testAccWorkspacesDataSourceConfig_base(rCount, rName), + Config: testAccWorkspacesDataSourceConfig_base(rName, rCount), }, { - Config: testAccWorkspacesDataSourceConfig_basic(rCount, rName), + Config: testAccWorkspacesDataSourceConfig_basic(rName, rCount), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(dataSourceName, "aliases.#", rCount), - resource.TestCheckResourceAttr(dataSourceName, "arns.#", rCount), - resource.TestCheckResourceAttr(dataSourceName, "workspace_ids.#", rCount), + acctest.CheckResourceAttrGreaterThanOrEqualValue(dataSourceName, "aliases.#", rCount), + acctest.CheckResourceAttrGreaterThanOrEqualValue(dataSourceName, "arns.#", rCount), + acctest.CheckResourceAttrGreaterThanOrEqualValue(dataSourceName, "workspace_ids.#", rCount), ), }, }, @@ -43,7 +42,7 @@ func TestAccAMPWorkspacesDataSource_basic(t *testing.T) { // nosemgrep:ci.caps0- func TestAccAMPWorkspacesDataSource_aliasPrefix(t *testing.T) { // nosemgrep:ci.caps0-in-func-name ctx := acctest.Context(t) - rCount := strconv.Itoa(sdkacctest.RandIntRange(1, 4)) + rCount := sdkacctest.RandIntRange(1, 4) rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) dataSourceName := "data.aws_prometheus_workspaces.test" @@ -57,7 +56,7 @@ func TestAccAMPWorkspacesDataSource_aliasPrefix(t *testing.T) { // nosemgrep:ci. ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, Steps: []resource.TestStep{ { - Config: testAccWorkspacesDataSourceConfig_aliasPrefix(rCount, rName), + Config: testAccWorkspacesDataSourceConfig_aliasPrefix(rName, rCount), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(dataSourceName, "aliases.#", "1"), resource.TestCheckResourceAttr(dataSourceName, "arns.#", "1"), @@ -68,23 +67,23 @@ func TestAccAMPWorkspacesDataSource_aliasPrefix(t *testing.T) { // nosemgrep:ci. }) } -func testAccWorkspacesDataSourceConfig_base(rCount, rName string) string { // nosemgrep:ci.caps0-in-func-name +func testAccWorkspacesDataSourceConfig_base(rName string, rCount int) string { // nosemgrep:ci.caps0-in-func-name return fmt.Sprintf(` resource "aws_prometheus_workspace" "test" { - count = %[1]s - alias = "%[2]s-${count.index}" + count = %[2]d + alias = "%[1]s-${count.index}" } -`, rCount, rName) +`, rName, rCount) } -func testAccWorkspacesDataSourceConfig_basic(rCount, rName string) string { // nosemgrep:ci.caps0-in-func-name - return acctest.ConfigCompose(testAccWorkspacesDataSourceConfig_base(rCount, rName), ` +func testAccWorkspacesDataSourceConfig_basic(rName string, rCount int) string { // nosemgrep:ci.caps0-in-func-name + return acctest.ConfigCompose(testAccWorkspacesDataSourceConfig_base(rName, rCount), ` data "aws_prometheus_workspaces" "test" {} `) } -func testAccWorkspacesDataSourceConfig_aliasPrefix(rCount, rName string) string { // nosemgrep:ci.caps0-in-func-name - return acctest.ConfigCompose(testAccWorkspacesDataSourceConfig_base(rCount, rName), ` +func testAccWorkspacesDataSourceConfig_aliasPrefix(rName string, rCount int) string { // nosemgrep:ci.caps0-in-func-name + return acctest.ConfigCompose(testAccWorkspacesDataSourceConfig_base(rName, rCount), ` data "aws_prometheus_workspaces" "test" { alias_prefix = aws_prometheus_workspace.test[0].alias }