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

Adding amp workspace datasource #26120

Merged
merged 13 commits into from
Aug 8, 2022
3 changes: 3 additions & 0 deletions .changelog/26120.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_amp_workspace
lewinkedrs marked this conversation as resolved.
Show resolved Hide resolved
```
2 changes: 2 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,8 @@ func New(_ context.Context) (*schema.Provider, error) {
"aws_acmpca_certificate_authority": acmpca.DataSourceCertificateAuthority(),
"aws_acmpca_certificate": acmpca.DataSourceCertificate(),

"aws_amp_workspace": amp.DataSourceWorkspace(),
lewinkedrs marked this conversation as resolved.
Show resolved Hide resolved

"aws_api_gateway_api_key": apigateway.DataSourceAPIKey(),
"aws_api_gateway_domain_name": apigateway.DataSourceDomainName(),
"aws_api_gateway_export": apigateway.DataSourceExport(),
Expand Down
25 changes: 25 additions & 0 deletions internal/service/amp/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
70 changes: 70 additions & 0 deletions internal/service/amp/workspace_data_source.go
Original file line number Diff line number Diff line change
@@ -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{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"workspace_id": {
Type: schema.TypeString,
Required: true,
},
"prometheus_endpoint": {
Type: schema.TypeString,
Computed: true,
},
"alias": {
Type: schema.TypeString,
Computed: true,
},
"created_date": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"tags": tftags.TagsSchemaComputed(),
},
}
}

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("error reading AMP Workspace (%s): %w", workspaceID, err)
}

d.SetId(workspaceID)

d.Set("arn", workspace.Arn)
d.Set("prometheus_endpoint", workspace.PrometheusEndpoint)
d.Set("alias", workspace.Alias)
d.Set("created_date", workspace.CreatedAt.Format(time.RFC3339))
d.Set("status", workspace.Status.StatusCode)

if err := d.Set("tags", KeyValueTags(workspace.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

return nil
}
46 changes: 46 additions & 0 deletions internal/service/amp/workspace_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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 testAccWorkspaceDataSource_basic(t *testing.T) {
randName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_amp_workspace.test"
dataSourceName := "data.aws_amp_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(randName),
Check: resource.ComposeTestCheckFunc(
testAccCheckWorkspaceExists(dataSourceName),
resource.TestCheckResourceAttrPair(resourceName, "arn", dataSourceName, "arn"),
resource.TestCheckResourceAttrSet(dataSourceName, "created_date"),
resource.TestCheckResourceAttrPair(resourceName, "prometheus_endpoint", dataSourceName, "prometheus_endpoint"),
resource.TestCheckResourceAttrPair(resourceName, "tags.%", dataSourceName, "tags.%"),
resource.TestCheckResourceAttrSet(dataSourceName, "status"),
resource.TestCheckResourceAttrPair(resourceName, "alias", dataSourceName, "alias"),
),
},
},
})
}

func testAccWorkspaceDataSourceConfig_alias(randName string) string {
return fmt.Sprintf(`
resource "aws_prometheus_workspace" "test" {
alias = %q
}
`, randName)
}
38 changes: 38 additions & 0 deletions website/docs/d/amp_workspace.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
subcategory: "AMP (Managed Prometheus)"
layout: "aws"
page_title: "AWS: aws_amp_workspace"
lewinkedrs marked this conversation as resolved.
Show resolved Hide resolved
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.