Skip to content

Commit

Permalink
Merge pull request #19500 from hashicorp/f-servicecat-ds-portfolio
Browse files Browse the repository at this point in the history
ds/servicecatalog_portfolio: New data source
  • Loading branch information
YakDriver authored Jun 17, 2021
2 parents 0eade2b + d6c006a commit 2eb601b
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/19500.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_servicecatalog_portfolio
```
98 changes: 98 additions & 0 deletions aws/data_source_aws_servicecatalog_portfolio.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package aws

import (
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/servicecatalog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
tfservicecatalog "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/servicecatalog"
)

func dataSourceAwsServiceCatalogPortfolio() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsServiceCatalogPortfolioRead,

Schema: map[string]*schema.Schema{
"accept_language": {
Type: schema.TypeString,
Optional: true,
Default: "en",
ValidateFunc: validation.StringInSlice(tfservicecatalog.AcceptLanguage_Values(), false),
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"created_time": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"id": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"provider_name": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchemaComputed(),
},
}
}

func dataSourceAwsServiceCatalogPortfolioRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).scconn

input := &servicecatalog.DescribePortfolioInput{
Id: aws.String(d.Get("id").(string)),
}

if v, ok := d.GetOk("accept_language"); ok {
input.AcceptLanguage = aws.String(v.(string))
}

output, err := conn.DescribePortfolio(input)

if err != nil {
return fmt.Errorf("error getting Service Catalog Portfolio (%s): %w", d.Get("id").(string), err)
}

if output == nil || output.PortfolioDetail == nil {
return fmt.Errorf("error getting Service Catalog Portfolio (%s): empty response", d.Get("id").(string))
}

detail := output.PortfolioDetail

d.SetId(aws.StringValue(detail.Id))

if err := d.Set("created_time", aws.TimeValue(detail.CreatedTime).Format(time.RFC3339)); err != nil {
log.Printf("[DEBUG] Error setting created_time: %s", err)
}

d.Set("arn", detail.ARN)
d.Set("description", detail.Description)
d.Set("name", detail.DisplayName)
d.Set("provider_name", detail.ProviderName)

ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig
tags := keyvaluetags.ServicecatalogKeyValueTags(output.Tags)

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

return nil
}
44 changes: 44 additions & 0 deletions aws/data_source_aws_servicecatalog_portfolio_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package aws

import (
"testing"

"github.com/aws/aws-sdk-go/service/servicecatalog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccAWSServiceCatalogPortfolioDataSource_basic(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
dataSourceName := "data.aws_servicecatalog_portfolio.test"
resourceName := "aws_servicecatalog_portfolio.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, servicecatalog.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckServiceCatlaogPortfolioDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckAwsServiceCatalogPortfolioDataSourceConfigBasic(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName, "arn", dataSourceName, "arn"),
resource.TestCheckResourceAttrPair(resourceName, "created_time", dataSourceName, "created_time"),
resource.TestCheckResourceAttrPair(resourceName, "description", dataSourceName, "description"),
resource.TestCheckResourceAttrPair(resourceName, "name", dataSourceName, "name"),
resource.TestCheckResourceAttrPair(resourceName, "provider_name", dataSourceName, "provider_name"),
resource.TestCheckResourceAttrPair(resourceName, "tags.%", dataSourceName, "tags.%"),
resource.TestCheckResourceAttrPair(resourceName, "tags.Chicane", dataSourceName, "tags.Chicane"),
),
},
},
})
}

func testAccCheckAwsServiceCatalogPortfolioDataSourceConfigBasic(rName string) string {
return composeConfig(testAccCheckAwsServiceCatalogPortfolioResourceConfigTags1(rName, "Chicane", "Nick"), `
data "aws_servicecatalog_portfolio" "test" {
id = aws_servicecatalog_portfolio.test.id
}
`)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ func Provider() *schema.Provider {
"aws_secretsmanager_secret_rotation": dataSourceAwsSecretsManagerSecretRotation(),
"aws_secretsmanager_secret_version": dataSourceAwsSecretsManagerSecretVersion(),
"aws_servicecatalog_constraint": dataSourceAwsServiceCatalogConstraint(),
"aws_servicecatalog_portfolio": dataSourceAwsServiceCatalogPortfolio(),
"aws_servicequotas_service": dataSourceAwsServiceQuotasService(),
"aws_servicequotas_service_quota": dataSourceAwsServiceQuotasServiceQuota(),
"aws_service_discovery_dns_namespace": dataSourceServiceDiscoveryDnsNamespace(),
Expand Down
40 changes: 40 additions & 0 deletions website/docs/d/servicecatalog_portfolio.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
subcategory: "Service Catalog"
layout: "aws"
page_title: "AWS: aws_servicecatalog_portfolio"
description: |-
Provides information for a Service Catalog Portfolio.
---

# Data Source: aws_servicecatalog_portfolio

Provides information for a Service Catalog Portfolio.

## Example Usage

```terraform
data "aws_servicecatalog_portfolio" "portfolio" {
id = "port-07052002"
}
```

## Argument Reference

The following arguments are required:

* `id` - (Required) Portfolio identifier.

The following arguments are optional:

* `accept_language` - (Optional) Language code. Valid values: `en` (English), `jp` (Japanese), `zh` (Chinese). Default value is `en`.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `arn` - Portfolio ARN.
* `created_time` - Time the portfolio was created.
* `description` - Description of the portfolio
* `name` - Portfolio name.
* `provider_name` - Name of the person or organization who owns the portfolio.
* `tags` - Tags applied to the portfolio.

0 comments on commit 2eb601b

Please sign in to comment.