Skip to content

Commit

Permalink
Merge pull request #229 from hashicorp/b-non-synthetic-id-create
Browse files Browse the repository at this point in the history
Ensure that resource ID is specified when populating Unknown values
  • Loading branch information
ewbankkit authored Oct 5, 2021
2 parents 15d43b5 + 3d65a57 commit 91d9263
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ FEATURES:
* **New Resource:** `awscc_ec2_host`
* **New Resource:** `awscc_lightsail_disk`

BUG FIXES:

* Correctly create resource with CloudFormation schema defined top-level `id` attribute ([#229](https://github.com/hashicorp/terraform-provider-awscc/issues/229))

## [0.1.0](https://github.com/hashicorp/terraform-provider-awscc/releases/tag/v0.1.0) (September 30, 2021)

Public Technical Preview.
Expand Down
49 changes: 49 additions & 0 deletions internal/aws/ec2/vpc_resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ec2_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-awscc/internal/acctest"
)

func TestAccAWSEC2VPC_CidrBlock(t *testing.T) {
td := acctest.NewTestData(t, "AWS::EC2::VPC", "awscc_ec2_vpc", "test")
resourceName := td.ResourceName
rName := td.RandomName()
cidrBlock := "10.0.0.0/16"

td.ResourceTest(t, []resource.TestStep{
{
Config: testAccAWSEC2VPCCidrBlockConfig(&td, rName, cidrBlock),
Check: resource.ComposeTestCheckFunc(
td.CheckExistsInAWS(),
resource.TestCheckResourceAttr(resourceName, "cidr_block", cidrBlock),
resource.TestCheckResourceAttr(resourceName, "tags.#", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.0.key", "Name"),
resource.TestCheckResourceAttr(resourceName, "tags.0.value", rName),
),
},
{
ResourceName: td.ResourceName,
ImportState: true,
ImportStateVerify: true,
},
})
}

func testAccAWSEC2VPCCidrBlockConfig(td *acctest.TestData, rName, cidrBlock string) string {
return fmt.Sprintf(`
resource %[1]q %[2]q {
cidr_block = %[4]q
tags = [
{
key = "Name"
value = %[3]q
}
]
}
`, td.TerraformResourceType, td.ResourceLabel, rName, cidrBlock)
}
18 changes: 6 additions & 12 deletions internal/generic/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,14 @@ func (r *resource) Create(ctx context.Context, request tfsdk.CreateResourceReque
return
}

id := aws.ToString(progressEvent.Identifier)

// Produce a wholly-known new State by determining the final values for any attributes left unknown in the planned state.
response.State.Raw = request.Plan.Raw

// Set the synthetic "id" attribute.
if r.resourceType.syntheticIDAttribute {
err = r.setId(ctx, aws.ToString(progressEvent.Identifier), &response.State)
err = r.setId(ctx, id, &response.State)

if err != nil {
response.Diagnostics = append(response.Diagnostics, ResourceIdentifierNotSetDiag(err))
Expand All @@ -469,7 +471,7 @@ func (r *resource) Create(ctx context.Context, request tfsdk.CreateResourceReque
}
}

diags := r.populateUnknownValues(ctx, &response.State)
diags := r.populateUnknownValues(ctx, id, &response.State)

if diags.HasError() {
response.Diagnostics.Append(diags...)
Expand Down Expand Up @@ -656,7 +658,7 @@ func (r *resource) Update(ctx context.Context, request tfsdk.UpdateResourceReque
// Produce a wholly-known new State by determining the final values for any attributes left unknown in the planned state.
response.State.Raw = request.Plan.Raw

diags := r.populateUnknownValues(ctx, &response.State)
diags := r.populateUnknownValues(ctx, id, &response.State)

if diags.HasError() {
response.Diagnostics.Append(diags...)
Expand Down Expand Up @@ -796,7 +798,7 @@ func (r *resource) setEmptyAttributes(ctx context.Context, state *tfsdk.State) e
}

// populateUnknownValues populates and unknown values in State with values from the current resource description.
func (r *resource) populateUnknownValues(ctx context.Context, state *tfsdk.State) diag.Diagnostics {
func (r *resource) populateUnknownValues(ctx context.Context, id string, state *tfsdk.State) diag.Diagnostics {
var diags diag.Diagnostics

unknowns, err := Unknowns(ctx, state.Raw, r.resourceType.tfToCfNameMap)
Expand All @@ -814,14 +816,6 @@ func (r *resource) populateUnknownValues(ctx context.Context, state *tfsdk.State
return nil
}

id, err := r.getId(ctx, state)

if err != nil {
diags.Append(ResourceIdentifierNotFoundDiag(err))

return diags
}

description, err := r.describe(ctx, r.provider.CloudControlApiClient(ctx), id)

if tfresource.NotFound(err) {
Expand Down

0 comments on commit 91d9263

Please sign in to comment.