diff --git a/.changelog/26120.txt b/.changelog/26120.txt new file mode 100644 index 00000000000..7ab56e30a9a --- /dev/null +++ b/.changelog/26120.txt @@ -0,0 +1,3 @@ +```release-note:new-data-source +aws_prometheus_workspace +``` diff --git a/internal/provider/provider.go b/internal/provider/provider.go index c65e2386c81..175ec0b945f 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -793,6 +793,8 @@ func New(_ context.Context) (*schema.Provider, error) { "aws_pricing_product": pricing.DataSourceProduct(), + "aws_prometheus_workspace": amp.DataSourceWorkspace(), + "aws_qldb_ledger": qldb.DataSourceLedger(), "aws_ram_resource_share": ram.DataSourceResourceShare(), diff --git a/internal/service/amp/find.go b/internal/service/amp/find.go index 7b758d166c7..7892b72f888 100644 --- a/internal/service/amp/find.go +++ b/internal/service/amp/find.go @@ -75,3 +75,28 @@ func FindRuleGroupNamespaceByARN(ctx context.Context, conn *prometheusservice.Pr return output.RuleGroupsNamespace, nil } + +func FindWorkspaceByID(conn *prometheusservice.PrometheusService, id string) (*prometheusservice.WorkspaceDescription, error) { + input := &prometheusservice.DescribeWorkspaceInput{ + WorkspaceId: aws.String(id), + } + + output, err := conn.DescribeWorkspace(input) + + if tfawserr.ErrCodeEquals(err, prometheusservice.ErrCodeResourceNotFoundException) { + return nil, &resource.NotFoundError{ + LastError: err, + LastRequest: input, + } + } + + if err != nil { + return nil, err + } + + if output == nil || output.Workspace == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return output.Workspace, nil +} diff --git a/internal/service/amp/workspace_data_source.go b/internal/service/amp/workspace_data_source.go new file mode 100644 index 00000000000..30cc9157857 --- /dev/null +++ b/internal/service/amp/workspace_data_source.go @@ -0,0 +1,70 @@ +package amp + +import ( + "fmt" + "time" + + "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 DataSourceWorkspace() *schema.Resource { + return &schema.Resource{ + Read: dataSourceWorkspaceRead, + + Schema: map[string]*schema.Schema{ + "alias": { + Type: schema.TypeString, + Computed: true, + }, + "arn": { + Type: schema.TypeString, + Computed: true, + }, + "created_date": { + Type: schema.TypeString, + Computed: true, + }, + "prometheus_endpoint": { + Type: schema.TypeString, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + "tags": tftags.TagsSchemaComputed(), + "workspace_id": { + Type: schema.TypeString, + Required: true, + }, + }, + } +} + +func dataSourceWorkspaceRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*conns.AWSClient).AMPConn + ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig + + workspaceID := d.Get("workspace_id").(string) + workspace, err := FindWorkspaceByID(conn, workspaceID) + + if err != nil { + return fmt.Errorf("reading AMP Workspace (%s): %w", workspaceID, err) + } + + d.SetId(workspaceID) + + d.Set("alias", workspace.Alias) + d.Set("arn", workspace.Arn) + d.Set("created_date", workspace.CreatedAt.Format(time.RFC3339)) + d.Set("prometheus_endpoint", workspace.PrometheusEndpoint) + d.Set("status", workspace.Status.StatusCode) + + if err := d.Set("tags", KeyValueTags(workspace.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { + return fmt.Errorf("setting tags: %w", err) + } + + return nil +} diff --git a/internal/service/amp/workspace_data_source_test.go b/internal/service/amp/workspace_data_source_test.go new file mode 100644 index 00000000000..b1f9f93baa3 --- /dev/null +++ b/internal/service/amp/workspace_data_source_test.go @@ -0,0 +1,49 @@ +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 TestAccAMPWorkspaceDataSource_basic(t *testing.T) { + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_prometheus_workspace.test" + dataSourceName := "data.aws_prometheus_workspace.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t); acctest.PreCheckPartitionHasService(prometheusservice.EndpointsID, t) }, + ErrorCheck: acctest.ErrorCheck(t, prometheusservice.EndpointsID), + CheckDestroy: nil, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccWorkspaceDataSourceConfig_alias(rName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair(resourceName, "alias", dataSourceName, "alias"), + resource.TestCheckResourceAttrPair(resourceName, "arn", dataSourceName, "arn"), + resource.TestCheckResourceAttrSet(dataSourceName, "created_date"), + resource.TestCheckResourceAttrPair(resourceName, "prometheus_endpoint", dataSourceName, "prometheus_endpoint"), + resource.TestCheckResourceAttrSet(dataSourceName, "status"), + resource.TestCheckResourceAttrPair(resourceName, "tags.%", dataSourceName, "tags.%"), + ), + }, + }, + }) +} + +func testAccWorkspaceDataSourceConfig_alias(rName string) string { + return fmt.Sprintf(` +resource "aws_prometheus_workspace" "test" { + alias = %[1]q +} + +data "aws_prometheus_workspace" "test" { + workspace_id = aws_prometheus_workspace.test.id +} +`, rName) +} diff --git a/website/docs/d/prometheus_workspace.html.markdown b/website/docs/d/prometheus_workspace.html.markdown new file mode 100644 index 00000000000..c9db92ffb9f --- /dev/null +++ b/website/docs/d/prometheus_workspace.html.markdown @@ -0,0 +1,38 @@ +--- +subcategory: "AMP (Managed Prometheus)" +layout: "aws" +page_title: "AWS: aws_prometheus_workspace" +description: |- + Gets information on an Amazon Managed Prometheus workspace. +--- + +# Data Source: aws_amp_workspace + +Provides an Amazon Managed Prometheus workspace data source. + +## Example Usage + +### Basic configuration + +```terraform +data "aws_amp_workspace" "example" { + workspace_id = "ws-41det8a1-2c67-6a1a-9381-9b83d3d78ef7" +} +``` + +## Argument Reference + +The following arguments are required: + +* `workspace_id` - (Required) The Prometheus workspace ID. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `arn` - The Amazon Resource Name (ARN) of the Prometheus workspace. +* `created_date` - The creation date of the Prometheus workspace. +* `prometheus_endpoint` - The endpoint of the Prometheus workspace. +* `alias` - The Prometheus workspace alias. +* `status` - The status of the Prometheus workspace. +* `tags` - The tags assigned to the resource.