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

aws_servicecatalog_product: import latest provisioning_artifact_parameters #33448

Merged
merged 5 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/33448.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_servicecatalog_product: Allow import on `provisioning_artifact_parameters` attribute
```
21 changes: 21 additions & 0 deletions internal/service/servicecatalog/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,24 @@ func FindTagOptionResourceAssociation(ctx context.Context, conn *servicecatalog.

return result, err
}

func findProductByID(ctx context.Context, conn *servicecatalog.ServiceCatalog, productID string) (*servicecatalog.DescribeProductAsAdminOutput, error) {
in := &servicecatalog.DescribeProductAsAdminInput{
Id: aws.String(productID),
}

out, err := conn.DescribeProductAsAdminWithContext(ctx, in)

if tfawserr.ErrCodeEquals(err, servicecatalog.ErrCodeResourceNotFoundException) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: in,
}
}

if err != nil {
return nil, err
}

return out, nil
}
62 changes: 61 additions & 1 deletion internal/service/servicecatalog/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package servicecatalog
import (
"context"
"log"
"sort"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -34,7 +35,7 @@ func ResourceProduct() *schema.Resource {
DeleteWithoutTimeout: resourceProductDelete,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
StateContext: resourceProductImport,
},

Timeouts: &schema.ResourceTimeout{
Expand Down Expand Up @@ -163,6 +164,40 @@ func ResourceProduct() *schema.Resource {
}
}

func resourceProductImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
conn := meta.(*conns.AWSClient).ServiceCatalogConn(ctx)

productData, err := findProductByID(ctx, conn, d.Id())

if err != nil {
return []*schema.ResourceData{d}, err
}

// import the last entry in the summary
if len(productData.ProvisioningArtifactSummaries) > 0 {
sort.Slice(productData.ProvisioningArtifactSummaries, func(i, j int) bool {
return aws.TimeValue(productData.ProvisioningArtifactSummaries[i].CreatedTime).Before(aws.TimeValue(productData.ProvisioningArtifactSummaries[j].CreatedTime))
})

provisioningArtifact := productData.ProvisioningArtifactSummaries[len(productData.ProvisioningArtifactSummaries)-1]
in := &servicecatalog.DescribeProvisioningArtifactInput{
ProductId: aws.String(d.Id()),
ProvisioningArtifactId: provisioningArtifact.Id,
}

// Find additional artifact details.
artifactData, err := conn.DescribeProvisioningArtifactWithContext(ctx, in)

if err != nil {
return []*schema.ResourceData{d}, err
}

d.Set("provisioning_artifact_parameters", flattenProvisioningArtifactParameters(artifactData))
}

return []*schema.ResourceData{d}, nil
}

func resourceProductCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ServiceCatalogConn(ctx)
Expand Down Expand Up @@ -433,3 +468,28 @@ func expandProvisioningArtifactParameters(tfMap map[string]interface{}) *service

return apiObject
}

func flattenProvisioningArtifactParameters(apiObject *servicecatalog.DescribeProvisioningArtifactOutput) []interface{} {
if apiObject == nil {
return nil
}

m := map[string]interface{}{
"description": aws.StringValue(apiObject.ProvisioningArtifactDetail.Description),
"disable_template_validation": false, // set default because it cannot be read
"name": aws.StringValue(apiObject.ProvisioningArtifactDetail.Name),
"type": aws.StringValue(apiObject.ProvisioningArtifactDetail.Type),
}

if apiObject.Info != nil {
if v, ok := apiObject.Info["TemplateUrl"]; ok {
m["template_url"] = aws.StringValue(v)
}

if v, ok := apiObject.Info["PhysicalId"]; ok {
m["template_physical_id"] = aws.StringValue(v)
}
}

return []interface{}{m}
}
4 changes: 2 additions & 2 deletions internal/service/servicecatalog/product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestAccServiceCatalogProduct_basic(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"accept_language",
"provisioning_artifact_parameters",
"provisioning_artifact_parameters.0.disable_template_validation",
},
},
},
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestAccServiceCatalogProduct_physicalID(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"accept_language",
"provisioning_artifact_parameters",
"provisioning_artifact_parameters.0.disable_template_validation",
},
},
},
Expand Down