-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19572 from hashicorp/f-servicecat-ds-launch-paths
ds/servicecatalog_launch_paths: New data source
- Loading branch information
Showing
7 changed files
with
405 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-notes:new-data-source | ||
aws_servicecatalog_launch_paths | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
|
||
"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" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/servicecatalog/waiter" | ||
) | ||
|
||
func dataSourceAwsServiceCatalogLaunchPaths() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsServiceCatalogLaunchPathsRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"accept_language": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: "en", | ||
ValidateFunc: validation.StringInSlice(tfservicecatalog.AcceptLanguage_Values(), false), | ||
}, | ||
"product_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"summaries": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"constraint_summaries": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"path_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"tags": tagsSchemaComputed(), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsServiceCatalogLaunchPathsRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).scconn | ||
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig | ||
|
||
summaries, err := waiter.LaunchPathsReady(conn, d.Get("accept_language").(string), d.Get("product_id").(string)) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error describing Service Catalog Launch Paths: %w", err) | ||
} | ||
|
||
if err := d.Set("summaries", flattenServiceCatalogLaunchPathSummaries(summaries, ignoreTagsConfig)); err != nil { | ||
return fmt.Errorf("error setting summaries: %w", err) | ||
} | ||
|
||
d.SetId(d.Get("product_id").(string)) | ||
|
||
return nil | ||
} | ||
|
||
func flattenServiceCatalogLaunchPathSummary(apiObject *servicecatalog.LaunchPathSummary, ignoreTagsConfig *keyvaluetags.IgnoreConfig) map[string]interface{} { | ||
if apiObject == nil { | ||
return nil | ||
} | ||
|
||
tfMap := map[string]interface{}{} | ||
|
||
if len(apiObject.ConstraintSummaries) > 0 { | ||
tfMap["constraint_summaries"] = flattenServiceCatalogConstraintSummaries(apiObject.ConstraintSummaries) | ||
} | ||
|
||
if apiObject.Id != nil { | ||
tfMap["path_id"] = aws.StringValue(apiObject.Id) | ||
} | ||
|
||
if apiObject.Name != nil { | ||
tfMap["name"] = aws.StringValue(apiObject.Name) | ||
} | ||
|
||
tags := keyvaluetags.ServicecatalogKeyValueTags(apiObject.Tags) | ||
|
||
tfMap["tags"] = tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map() | ||
|
||
return tfMap | ||
} | ||
|
||
func flattenServiceCatalogLaunchPathSummaries(apiObjects []*servicecatalog.LaunchPathSummary, ignoreTagsConfig *keyvaluetags.IgnoreConfig) []interface{} { | ||
if len(apiObjects) == 0 { | ||
return nil | ||
} | ||
|
||
var tfList []interface{} | ||
|
||
for _, apiObject := range apiObjects { | ||
if apiObject == nil { | ||
continue | ||
} | ||
|
||
tfList = append(tfList, flattenServiceCatalogLaunchPathSummary(apiObject, ignoreTagsConfig)) | ||
} | ||
|
||
return tfList | ||
} | ||
|
||
func flattenServiceCatalogConstraintSummary(apiObject *servicecatalog.ConstraintSummary) map[string]interface{} { | ||
if apiObject == nil { | ||
return nil | ||
} | ||
|
||
tfMap := map[string]interface{}{} | ||
|
||
if apiObject.Description != nil { | ||
tfMap["description"] = aws.StringValue(apiObject.Description) | ||
} | ||
|
||
if apiObject.Type != nil { | ||
tfMap["type"] = aws.StringValue(apiObject.Type) | ||
} | ||
|
||
return tfMap | ||
} | ||
|
||
func flattenServiceCatalogConstraintSummaries(apiObjects []*servicecatalog.ConstraintSummary) []interface{} { | ||
if len(apiObjects) == 0 { | ||
return nil | ||
} | ||
|
||
var tfList []interface{} | ||
|
||
for _, apiObject := range apiObjects { | ||
if apiObject == nil { | ||
continue | ||
} | ||
|
||
tfList = append(tfList, flattenServiceCatalogConstraintSummary(apiObject)) | ||
} | ||
|
||
return tfList | ||
} |
132 changes: 132 additions & 0 deletions
132
aws/data_source_aws_servicecatalog_launch_paths_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"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 TestAccAWSServiceCatalogLaunchPathsDataSource_basic(t *testing.T) { | ||
dataSourceName := "data.aws_servicecatalog_launch_paths.test" | ||
resourceNameProduct := "aws_servicecatalog_product.test" | ||
resourceNamePortfolio := "aws_servicecatalog_portfolio.test" | ||
rName := acctest.RandomWithPrefix("tf-acc-test") | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ErrorCheck: testAccErrorCheck(t, servicecatalog.EndpointsID), | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSServiceCatalogLaunchPathsDataSourceConfig_basic(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(dataSourceName, "accept_language", "en"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "product_id", resourceNameProduct, "id"), | ||
resource.TestCheckResourceAttr(dataSourceName, "summaries.#", "1"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "summaries.0.name", resourceNamePortfolio, "name"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "summaries.0.path_id"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccAWSServiceCatalogLaunchPathsDataSourceConfig_base(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_cloudformation_stack" "test" { | ||
name = %[1]q | ||
template_body = jsonencode({ | ||
AWSTemplateFormatVersion = "2010-09-09" | ||
Resources = { | ||
MyVPC = { | ||
Type = "AWS::EC2::VPC" | ||
Properties = { | ||
CidrBlock = "10.1.0.0/16" | ||
} | ||
} | ||
} | ||
Outputs = { | ||
VpcID = { | ||
Description = "VPC ID" | ||
Value = { | ||
Ref = "MyVPC" | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
resource "aws_servicecatalog_product" "test" { | ||
description = "beskrivning" | ||
distributor = "distributör" | ||
name = %[1]q | ||
owner = "ägare" | ||
type = "CLOUD_FORMATION_TEMPLATE" | ||
support_description = "supportbeskrivning" | ||
support_email = "[email protected]" | ||
support_url = "http://example.com" | ||
provisioning_artifact_parameters { | ||
description = "artefaktbeskrivning" | ||
name = %[1]q | ||
template_physical_id = aws_cloudformation_stack.test.id | ||
type = "CLOUD_FORMATION_TEMPLATE" | ||
} | ||
tags = { | ||
Name = %[1]q | ||
} | ||
} | ||
resource "aws_servicecatalog_portfolio" "test" { | ||
name = %[1]q | ||
provider_name = %[1]q | ||
} | ||
resource "aws_servicecatalog_product_portfolio_association" "test" { | ||
portfolio_id = aws_servicecatalog_portfolio.test.id | ||
product_id = aws_servicecatalog_product.test.id | ||
} | ||
data "aws_partition" "current" {} | ||
resource "aws_iam_role" "test" { | ||
name = %[1]q | ||
assume_role_policy = jsonencode({ | ||
Version = "2012-10-17" | ||
Statement = [{ | ||
Action = "sts:AssumeRole" | ||
Effect = "Allow" | ||
Principal = { | ||
Service = "servicecatalog.${data.aws_partition.current.dns_suffix}" | ||
} | ||
Sid = "" | ||
}] | ||
}) | ||
} | ||
data "aws_caller_identity" "current" {} | ||
resource "aws_servicecatalog_principal_portfolio_association" "test" { | ||
portfolio_id = aws_servicecatalog_portfolio.test.id | ||
principal_arn = data.aws_caller_identity.current.arn | ||
} | ||
`, rName) | ||
} | ||
|
||
func testAccAWSServiceCatalogLaunchPathsDataSourceConfig_basic(rName string) string { | ||
return composeConfig(testAccAWSServiceCatalogLaunchPathsDataSourceConfig_base(rName), ` | ||
data "aws_servicecatalog_launch_paths" "test" { | ||
product_id = aws_servicecatalog_product_portfolio_association.test.product_id | ||
depends_on = [aws_servicecatalog_principal_portfolio_association.test] | ||
} | ||
`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.