diff --git a/CHANGELOG.md b/CHANGELOG.md index df4c651810..d9839129aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/internal/aws/ec2/vpc_resource_test.go b/internal/aws/ec2/vpc_resource_test.go new file mode 100644 index 0000000000..2016dc0cb7 --- /dev/null +++ b/internal/aws/ec2/vpc_resource_test.go @@ -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) +} diff --git a/internal/generic/resource.go b/internal/generic/resource.go index ae9a363e29..edee186474 100644 --- a/internal/generic/resource.go +++ b/internal/generic/resource.go @@ -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)) @@ -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...) @@ -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...) @@ -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) @@ -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) {