From ef7d0e072fe93251c9244d8dd3140094f7a4159a Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Mon, 24 May 2021 12:46:35 -0400 Subject: [PATCH 1/9] provider: Add new data source --- aws/provider.go | 1 + 1 file changed, 1 insertion(+) diff --git a/aws/provider.go b/aws/provider.go index 09bb2c0de5d..5e91aefcac2 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -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(), From 9cdfcfa47cffcee17f87808f2ea75602c3149e35 Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Mon, 24 May 2021 12:47:08 -0400 Subject: [PATCH 2/9] ds/servicecat_portfolio: New data source --- ...ata_source_aws_servicecatalog_portfolio.go | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 aws/data_source_aws_servicecatalog_portfolio.go diff --git a/aws/data_source_aws_servicecatalog_portfolio.go b/aws/data_source_aws_servicecatalog_portfolio.go new file mode 100644 index 00000000000..e1ea1cff1f4 --- /dev/null +++ b/aws/data_source_aws_servicecatalog_portfolio.go @@ -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, + }, + "id": { + Type: schema.TypeString, + Required: true, + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + "description": { + Type: schema.TypeString, + Computed: true, + }, + "provider_name": { + Type: schema.TypeString, + Computed: true, + }, + "tags": tagsSchema(), + }, + } +} + +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: %w", err) + } + + if output == nil || output.PortfolioDetail == nil { + return fmt.Errorf("error getting Service Catalog Portfolio: empty response") + } + + detail := output.PortfolioDetail + + d.SetId(aws.StringValue(detail.Id)) + + if err := d.Set("created_time", 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 +} From af5495497ee59beaa560d6cde42393cb6ecea493 Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Mon, 24 May 2021 12:47:52 -0400 Subject: [PATCH 3/9] tests/ds/servicecat_portfolio: New data source --- ...ource_aws_servicecatalog_portfolio_test.go | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 aws/data_source_aws_servicecatalog_portfolio_test.go diff --git a/aws/data_source_aws_servicecatalog_portfolio_test.go b/aws/data_source_aws_servicecatalog_portfolio_test.go new file mode 100644 index 00000000000..4306faa9df1 --- /dev/null +++ b/aws/data_source_aws_servicecatalog_portfolio_test.go @@ -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 +} +`) +} From 0a66ba485093297d5ab2a6398112ba6f999d3920 Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Mon, 24 May 2021 12:48:08 -0400 Subject: [PATCH 4/9] docs/ds/servicecat_portfolio: New data source --- .../d/servicecatalog_portfolio.html.markdown | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 website/docs/d/servicecatalog_portfolio.html.markdown diff --git a/website/docs/d/servicecatalog_portfolio.html.markdown b/website/docs/d/servicecatalog_portfolio.html.markdown new file mode 100644 index 00000000000..5bab83d5d4b --- /dev/null +++ b/website/docs/d/servicecatalog_portfolio.html.markdown @@ -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` - (Optional) 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. From 52222ef730907491027dd4ffe3f8d682445af9c9 Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Mon, 24 May 2021 12:49:49 -0400 Subject: [PATCH 5/9] ds/servicecat_portfolio: Add changelog --- .changelog/19500.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/19500.txt diff --git a/.changelog/19500.txt b/.changelog/19500.txt new file mode 100644 index 00000000000..d6a9282b976 --- /dev/null +++ b/.changelog/19500.txt @@ -0,0 +1,3 @@ +```release-notes:new-data-source + aws_servicecatalog_portfolio + ``` \ No newline at end of file From ecf8c67d95d3bddf3f04c3a3c1697b2dabaa7287 Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Mon, 24 May 2021 14:26:05 -0400 Subject: [PATCH 6/9] ds/servicecat_portfolio: Make tags computed --- aws/data_source_aws_servicecatalog_portfolio.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/aws/data_source_aws_servicecatalog_portfolio.go b/aws/data_source_aws_servicecatalog_portfolio.go index e1ea1cff1f4..abfda971b04 100644 --- a/aws/data_source_aws_servicecatalog_portfolio.go +++ b/aws/data_source_aws_servicecatalog_portfolio.go @@ -32,6 +32,10 @@ func dataSourceAwsServiceCatalogPortfolio() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "description": { + Type: schema.TypeString, + Computed: true, + }, "id": { Type: schema.TypeString, Required: true, @@ -40,15 +44,11 @@ func dataSourceAwsServiceCatalogPortfolio() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "description": { - Type: schema.TypeString, - Computed: true, - }, "provider_name": { Type: schema.TypeString, Computed: true, }, - "tags": tagsSchema(), + "tags": tagsSchemaComputed(), }, } } From e62f3abbf3c63cc5070f8d2fb8ac7c759ec09a0b Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Thu, 17 Jun 2021 14:48:38 -0400 Subject: [PATCH 7/9] ds/servicecat_port: Fix changelog --- .changelog/19500.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changelog/19500.txt b/.changelog/19500.txt index d6a9282b976..46d3a1e003a 100644 --- a/.changelog/19500.txt +++ b/.changelog/19500.txt @@ -1,3 +1,3 @@ -```release-notes:new-data-source +```release-note:new-data-source aws_servicecatalog_portfolio ``` \ No newline at end of file From 63118a4a76289033880acb6984cc37550dabb09b Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Thu, 17 Jun 2021 14:49:21 -0400 Subject: [PATCH 8/9] ds/servicecat_port: Minor fixes --- aws/data_source_aws_servicecatalog_portfolio.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/aws/data_source_aws_servicecatalog_portfolio.go b/aws/data_source_aws_servicecatalog_portfolio.go index abfda971b04..8ad11c0924e 100644 --- a/aws/data_source_aws_servicecatalog_portfolio.go +++ b/aws/data_source_aws_servicecatalog_portfolio.go @@ -67,18 +67,18 @@ func dataSourceAwsServiceCatalogPortfolioRead(d *schema.ResourceData, meta inter output, err := conn.DescribePortfolio(input) if err != nil { - return fmt.Errorf("error getting Service Catalog Portfolio: %w", err) + 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: empty response") + 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", detail.CreatedTime.Format(time.RFC3339)); err != nil { + if err := d.Set("created_time", aws.TimeValue(detail.CreatedTime).Format(time.RFC3339)); err != nil { log.Printf("[DEBUG] Error setting created_time: %s", err) } From d6c006a602dc1cfa23e5388b6c35c17991c2f946 Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Thu, 17 Jun 2021 14:49:50 -0400 Subject: [PATCH 9/9] docs/ds/servicecat_port: Fix docs --- website/docs/d/servicecatalog_portfolio.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/d/servicecatalog_portfolio.html.markdown b/website/docs/d/servicecatalog_portfolio.html.markdown index 5bab83d5d4b..438c25fd3f4 100644 --- a/website/docs/d/servicecatalog_portfolio.html.markdown +++ b/website/docs/d/servicecatalog_portfolio.html.markdown @@ -6,7 +6,7 @@ description: |- Provides information for a Service Catalog Portfolio. --- -# Data source: aws_servicecatalog_portfolio +# Data Source: aws_servicecatalog_portfolio Provides information for a Service Catalog Portfolio. @@ -22,7 +22,7 @@ data "aws_servicecatalog_portfolio" "portfolio" { The following arguments are required: -* `id` - (Optional) Portfolio identifier. +* `id` - (Required) Portfolio identifier. The following arguments are optional: