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

add data source for aws_servicecatalog_provisioning_artifacts #25535

Merged
3 changes: 3 additions & 0 deletions .changelog/25535.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_servicecatalog_provisioning_artifacts
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package servicecatalog

import (
"context"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/servicecatalog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
)

// @SDKDataSource("aws_servicecatalog_provisioning_artifacts")
func DataSourceProvisioningArtifacts() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourceProvisioningArtifactsRead,

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(ConstraintReadTimeout),
},

Schema: map[string]*schema.Schema{
"accept_language": {
Type: schema.TypeString,
Default: AcceptLanguageEnglish,
Optional: true,
ValidateFunc: validation.StringInSlice(AcceptLanguage_Values(), false),
},
"product_id": {
Type: schema.TypeString,
Required: true,
},
"provisioning_artifact_details": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"active": {
Type: schema.TypeBool,
Computed: true,
},
"created_time": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"guidance": {
Type: schema.TypeString,
Computed: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceProvisioningArtifactsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ServiceCatalogConn()

productID := d.Get("product_id").(string)
input := &servicecatalog.ListProvisioningArtifactsInput{
AcceptLanguage: aws.String(d.Get("accept_language").(string)),
ProductId: aws.String(productID),
}

output, err := conn.ListProvisioningArtifactsWithContext(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "listing Service Catalog Provisioning Artifacts: %s", err)
}

d.SetId(productID)
if err := d.Set("provisioning_artifact_details", flattenProvisioningArtifactDetails(output.ProvisioningArtifactDetails)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting provisioning_artifact_details: %s", err)
}

return nil
}

func flattenProvisioningArtifactDetails(apiObjects []*servicecatalog.ProvisioningArtifactDetail) []interface{} {
if len(apiObjects) == 0 {
return nil
}

var tfList []interface{}

for _, apiObject := range apiObjects {
if apiObject == nil {
continue
}
tfList = append(tfList, flattenProvisioningArtifactDetail(apiObject))
}

return tfList
}

func flattenProvisioningArtifactDetail(apiObject *servicecatalog.ProvisioningArtifactDetail) map[string]interface{} {
if apiObject == nil {
return nil
}

tfMap := map[string]interface{}{}

if apiObject.Active != nil {
tfMap["active"] = aws.BoolValue(apiObject.Active)
}
if apiObject.CreatedTime != nil {
tfMap["created_time"] = aws.TimeValue(apiObject.CreatedTime).String()
}
if apiObject.Description != nil {
tfMap["description"] = aws.StringValue(apiObject.Description)
}
if apiObject.Guidance != nil {
tfMap["guidance"] = aws.StringValue(apiObject.Guidance)
}
if apiObject.Id != nil {
tfMap["id"] = aws.StringValue(apiObject.Id)
}
if apiObject.Name != nil {
tfMap["name"] = aws.StringValue(apiObject.Name)
}
if apiObject.Type != nil {
tfMap["type"] = aws.StringValue(apiObject.Type)
}

return tfMap
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package servicecatalog_test

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/servicecatalog"
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"
tfservicecatalog "github.com/hashicorp/terraform-provider-aws/internal/service/servicecatalog"
)

func TestAccServiceCatalogProvisioningArtifactsDataSource_basic(t *testing.T) {
dataSourceName := "data.aws_servicecatalog_provisioning_artifacts.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
domain := fmt.Sprintf("http://%s", acctest.RandomDomainName())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, servicecatalog.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccProvisioningArtifactsDataSourceConfig_basic(rName, domain),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "accept_language", tfservicecatalog.AcceptLanguageEnglish),
resource.TestCheckResourceAttrPair(dataSourceName, "product_id", "aws_servicecatalog_product.test", "id"),
resource.TestCheckResourceAttr(dataSourceName, "provisioning_artifact_details.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "provisioning_artifact_details.0.active", "true"),
resource.TestCheckResourceAttrSet(dataSourceName, "provisioning_artifact_details.0.description"),
resource.TestCheckResourceAttr(dataSourceName, "provisioning_artifact_details.0.guidance", servicecatalog.ProvisioningArtifactGuidanceDefault),
resource.TestCheckResourceAttr(dataSourceName, "provisioning_artifact_details.0.name", rName),
resource.TestCheckResourceAttr(dataSourceName, "provisioning_artifact_details.0.type", servicecatalog.ProductTypeCloudFormationTemplate),
),
},
},
})
}

func testAccProvisioningArtifactsDataSourceConfig_base(rName, domain string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "test" {
bucket = %[1]q
force_destroy = true
}

resource "aws_s3_bucket_acl" "test" {
bucket = aws_s3_bucket.test.id
acl = "private"
}

resource "aws_s3_object" "test" {
bucket = aws_s3_bucket.test.id
key = "%[1]s.json"

content = 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 = %[1]q
distributor = "distributör"
name = %[1]q
owner = "ägare"
type = "CLOUD_FORMATION_TEMPLATE"
support_description = %[1]q
support_email = %[3]q
support_url = %[2]q

provisioning_artifact_parameters {
description = "artefaktbeskrivning"
disable_template_validation = true
name = %[1]q
template_url = "https://${aws_s3_bucket.test.bucket_regional_domain_name}/${aws_s3_object.test.key}"
type = "CLOUD_FORMATION_TEMPLATE"
}

tags = {
Name = %[1]q
}
}

resource "aws_servicecatalog_provisioning_artifact" "test" {
accept_language = "en"
active = true
description = %[1]q
disable_template_validation = true
guidance = "DEFAULT"
name = "%[1]s-2"
product_id = aws_servicecatalog_product.test.id
template_url = "https://${aws_s3_bucket.test.bucket_regional_domain_name}/${aws_s3_object.test.key}"
type = "CLOUD_FORMATION_TEMPLATE"
}
`, rName, domain, acctest.DefaultEmailAddress)
}

func testAccProvisioningArtifactsDataSourceConfig_basic(rName, domain string) string {
return acctest.ConfigCompose(testAccProvisioningArtifactsDataSourceConfig_base(rName, domain), `
data "aws_servicecatalog_provisioning_artifacts" "test" {
product_id = aws_servicecatalog_product.test.id
}
`)
}
4 changes: 4 additions & 0 deletions internal/service/servicecatalog/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
subcategory: "Service Catalog"
layout: "aws"
page_title: "AWS: aws_servicecatalog_provisioning_artifacts"
description: |-
Provides information on Service Catalog Provisioning Artifacts
---

# Data Source: aws_servicecatalog_provisioning_artifacts

Lists the provisioning artifacts for the specified product.

## Example Usage

### Basic Usage

```terraform
data "aws_servicecatalog_provisioning_artifacts" "example" {
product_id = "prod-yakog5pdriver"
}
```

## Argument Reference

The following arguments are required:

* `product_id` - (Required) Product 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:

* `provisioning_artifact_details` - List with information about the provisioning artifacts. See details below.

### provisioning_artifact_details

* `active` - Indicates whether the product version is active.
* `created_time` - The UTC time stamp of the creation time.
* `description` - The description of the provisioning artifact.
* `guidance` - Information set by the administrator to provide guidance to end users about which provisioning artifacts to use.
* `id` - The identifier of the provisioning artifact.
* `name` - The name of the provisioning artifact.
* `type` - The type of provisioning artifact.