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

d/aws_prometheus_workspaces: Add New Data Source #28574

Merged
3 changes: 3 additions & 0 deletions .changelog/28574.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_prometheus_workspaces
```
16 changes: 16 additions & 0 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
29 changes: 29 additions & 0 deletions internal/service/amp/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,32 @@ func FindLoggingConfigurationByWorkspaceID(ctx context.Context, conn *prometheus

return output.LoggingConfiguration, nil
}

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)
}
var output []*prometheusservice.WorkspaceSummary

err := conn.ListWorkspacesPagesWithContext(ctx, input, func(page *prometheusservice.ListWorkspacesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, v := range page.Workspaces {
if v == nil {
continue
}
output = append(output, v)
}

return !lastPage
})

if err != nil {
return nil, err
}

return output, nil
}
4 changes: 4 additions & 0 deletions internal/service/amp/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions internal/service/amp/workspaces_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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"
)

// @SDKDataSource("aws_prometheus_workspaces")
func DataSourceWorkspaces() *schema.Resource { // nosemgrep:ci.caps0-in-func-name
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 { // nosemgrep:ci.caps0-in-func-name
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("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("aliases", aliases)
d.Set("arns", arns)
d.Set("workspace_ids", workspace_ids)

return nil
}
91 changes: 91 additions & 0 deletions internal/service/amp/workspaces_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package amp_test

import (
"fmt"
"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-provider-aws/internal/acctest"
)

func TestAccAMPWorkspacesDataSource_basic(t *testing.T) { // nosemgrep:ci.caps0-in-func-name
ctx := acctest.Context(t)
rCount := sdkacctest.RandIntRange(1, 4)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
dataSourceName := "data.aws_prometheus_workspaces.test"

resource.Test(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckPartitionHasService(t, prometheusservice.EndpointsID)
},
ErrorCheck: acctest.ErrorCheck(t, prometheusservice.EndpointsID),
PreventPostDestroyRefresh: true,
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccWorkspacesDataSourceConfig_base(rName, rCount),
},
{
Config: testAccWorkspacesDataSourceConfig_basic(rName, rCount),
Check: resource.ComposeTestCheckFunc(
acctest.CheckResourceAttrGreaterThanOrEqualValue(dataSourceName, "aliases.#", rCount),
acctest.CheckResourceAttrGreaterThanOrEqualValue(dataSourceName, "arns.#", rCount),
acctest.CheckResourceAttrGreaterThanOrEqualValue(dataSourceName, "workspace_ids.#", rCount),
),
},
},
})
}

func TestAccAMPWorkspacesDataSource_aliasPrefix(t *testing.T) { // nosemgrep:ci.caps0-in-func-name
ctx := acctest.Context(t)
rCount := sdkacctest.RandIntRange(1, 4)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
dataSourceName := "data.aws_prometheus_workspaces.test"

resource.Test(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckPartitionHasService(t, prometheusservice.EndpointsID)
},
ErrorCheck: acctest.ErrorCheck(t, prometheusservice.EndpointsID),
PreventPostDestroyRefresh: true,
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccWorkspacesDataSourceConfig_aliasPrefix(rName, rCount),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "aliases.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "arns.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "workspace_ids.#", "1"),
),
},
},
})
}

func testAccWorkspacesDataSourceConfig_base(rName string, rCount int) string { // nosemgrep:ci.caps0-in-func-name
return fmt.Sprintf(`
resource "aws_prometheus_workspace" "test" {
count = %[2]d
alias = "%[1]s-${count.index}"
}
`, rName, rCount)
}

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(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
}
`)
}
42 changes: 42 additions & 0 deletions website/docs/d/prometheus_workspaces.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
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.