Skip to content

Commit

Permalink
Keep failed resources, add state output value (#331)
Browse files Browse the repository at this point in the history
* Fix storing of failed consul cluster/snapshot state

- there has been a longstanding issue where resources are leaked in our e2e tests. This is because we purge all failed clusters from state with d.SetId(""). This doesn't make sense for failed clusters/snapshots. Instead, we should store their state... in state

* Add acc testing

- and undo property sorting - it makes the PR bigger

* Make docs

* Change acc testing tier to development

- otherwise folks need to upload a credit card to help pr fixes

* Fix scale attr

- reduced the size, need to reduce the scale

* Fix size if acc testing of consul cluster

* Update Consul cluster data source to also store state

* Fix brace in cluster def

* go generate docs

* Use state vs cluster/snapshot_state

* Update internal/provider/resource_consul_cluster_test.go

Co-authored-by: Brenna Hewer-Darroch <[email protected]>

* Stop purging failed resources from state, export state output value

* go generate

Co-authored-by: Brenna Hewer-Darroch <[email protected]>
  • Loading branch information
Joshua Timmons and bcmdarroch authored Jun 28, 2022
1 parent 1c970d4 commit db2ef7b
Show file tree
Hide file tree
Showing 28 changed files with 98 additions and 43 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/aws_network_peering.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ data "hcp_aws_network_peering" "test" {
- `project_id` (String) The ID of the HCP project where the network peering is located. Always matches the HVN's project.
- `provider_peering_id` (String) The peering connection ID used by AWS.
- `self_link` (String) A unique URL identifying the network peering.
- `state` (String) The state of the network peering.

<a id="nestedblock--timeouts"></a>
### Nested Schema for `timeouts`
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/azure_peering_connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ data "hcp_azure_peering_connection" "test" {
- `peer_vnet_region` (String) The region of the peer VNet in Azure.
- `project_id` (String) The ID of the HCP project where the peering connection is located. Always matches the HVN's project.
- `self_link` (String) A unique URL identifying the peering connection
- `state` (String) The state of the Azure peering connection.

<a id="nestedblock--timeouts"></a>
### Nested Schema for `timeouts`
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/hvn.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ data "hcp_hvn" "example" {
- `provider_account_id` (String) The provider account ID where the HVN is located.
- `region` (String) The region where the HVN is located.
- `self_link` (String) A unique URL identifying the HVN.
- `state` (String) The state of the HVN route.

<a id="nestedblock--timeouts"></a>
### Nested Schema for `timeouts`
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/hvn_peering_connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ data "hcp_hvn_peering_connection" "test" {
- `organization_id` (String) The ID of the HCP organization where the peering connection is located. Always matches the HVNs' organization.
- `project_id` (String) The ID of the HCP project where the peering connection is located. Always matches the HVNs' project.
- `self_link` (String) A unique URL identifying the peering connection
- `state` (String) The state of the HVN peering connection.

<a id="nestedblock--timeouts"></a>
### Nested Schema for `timeouts`
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/vault_cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ data "hcp_vault_cluster" "example" {
- `public_endpoint` (Boolean) Denotes that the cluster has a public endpoint. Defaults to false.
- `region` (String) The region where the HCP Vault cluster is located.
- `self_link` (String) A unique URL identifying the Vault cluster.
- `state` (String) The state of the Vault cluster.
- `tier` (String) The tier that the HCP Vault cluster will be provisioned as. Only 'development' is available at this time.
- `vault_private_endpoint_url` (String) The private URL for the Vault cluster.
- `vault_public_endpoint_url` (String) The public URL for the Vault cluster. This will be empty if `public_endpoint` is `false`.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/aws_network_peering.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ resource "aws_vpc_peering_connection_accepter" "peer" {
- `project_id` (String) The ID of the HCP project where the network peering is located. Always matches the HVN's project.
- `provider_peering_id` (String) The peering connection ID used by AWS.
- `self_link` (String) A unique URL identifying the network peering.
- `state` (String) The state of the network peering.

<a id="nestedblock--timeouts"></a>
### Nested Schema for `timeouts`
Expand Down
1 change: 1 addition & 0 deletions docs/resources/azure_peering_connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ resource "azurerm_role_assignment" "assignment" {
- `organization_id` (String) The ID of the HCP organization where the peering connection is located. Always matches the HVN's organization.
- `project_id` (String) The ID of the HCP project where the peering connection is located. Always matches the HVN's project.
- `self_link` (String) A unique URL identifying the peering connection.
- `state` (String) The state of the Azure peering connection.

<a id="nestedblock--timeouts"></a>
### Nested Schema for `timeouts`
Expand Down
1 change: 1 addition & 0 deletions docs/resources/hvn.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ resource "hcp_hvn" "example" {
- `project_id` (String) The ID of the HCP project where the HVN is located.
- `provider_account_id` (String) The provider account ID where the HVN is located.
- `self_link` (String) A unique URL identifying the HVN.
- `state` (String) The state of the HVN.

<a id="nestedblock--timeouts"></a>
### Nested Schema for `timeouts`
Expand Down
1 change: 1 addition & 0 deletions docs/resources/hvn_peering_connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ resource "hcp_hvn_peering_connection" "peer_1" {
- `peering_id` (String) The ID of the peering connection.
- `project_id` (String) The ID of the HCP project where the peering connection is located. Always matches the HVNs' project.
- `self_link` (String) A unique URL identifying the peering connection
- `state` (String) The state of the HVN peering connection.

<a id="nestedblock--timeouts"></a>
### Nested Schema for `timeouts`
Expand Down
1 change: 1 addition & 0 deletions docs/resources/vault_cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ resource "hcp_vault_cluster" "example" {
- `project_id` (String) The ID of the project this HCP Vault cluster is located in.
- `region` (String) The region where the HCP Vault cluster is located.
- `self_link` (String) A unique URL identifying the Vault cluster.
- `state` (String) The state of the Vault cluster.
- `vault_private_endpoint_url` (String) The private URL for the Vault cluster.
- `vault_public_endpoint_url` (String) The public URL for the Vault cluster. This will be empty if `public_endpoint` is `false`.
- `vault_version` (String) The Vault version of the cluster.
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/data_source_aws_network_peering.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ func dataSourceAwsNetworkPeering() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"state": {
Description: "The state of the network peering.",
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/data_source_azure_peering_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ func dataSourceAzurePeeringConnection() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"state": {
Description: "The state of the Azure peering connection.",
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/data_source_hvn.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func dataSourceHvn() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"state": {
Description: "The state of the HVN route.",
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/data_source_hvn_peering_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func dataSourceHvnPeeringConnection() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"state": {
Description: "The state of the HVN peering connection.",
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/data_source_vault_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func dataSourceVaultCluster() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"state": {
Description: "The state of the Vault cluster.",
Type: schema.TypeString,
Computed: true,
},
"primary_link": {
Description: "The `self_link` of the HCP Vault Plus tier cluster which is the primary in the performance replication setup with this HCP Vault Plus tier cluster. If not specified, it is a standalone Plus tier HCP Vault cluster.",
Type: schema.TypeString,
Expand Down
16 changes: 8 additions & 8 deletions internal/provider/resource_aws_network_peering.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ func resourceAwsNetworkPeering() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"state": {
Description: "The state of the network peering.",
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -231,14 +236,6 @@ func resourceAwsNetworkPeeringRead(ctx context.Context, d *schema.ResourceData,
return diag.Errorf("unable to retrieve network peering (%s): %v", peeringID, err)
}

// The network peering failed to provision properly so we want to let the user know and
// remove it from state
if peering.State == networkmodels.HashicorpCloudNetwork20200907PeeringStateFAILED {
log.Printf("[WARN] Network peering (%s) failed to provision, removing from state", peering.ID)
d.SetId("")
return nil
}

// Network peering found, update resource data
if err := setAwsPeeringResourceData(d, peering); err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -321,6 +318,9 @@ func setAwsPeeringResourceData(d *schema.ResourceData, peering *networkmodels.Ha
if err := d.Set("expires_at", peering.ExpiresAt.String()); err != nil {
return err
}
if err := d.Set("state", peering.State); err != nil {
return err
}

link := newLink(peering.Hvn.Location, PeeringResourceType, peering.ID)
selfLink, err := linkURL(link)
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/resource_aws_network_peering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func TestAccAwsPeering(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "created_at"),
resource.TestCheckResourceAttrSet(resourceName, "expires_at"),
resource.TestCheckResourceAttrSet(resourceName, "state"),
testLink(resourceName, "self_link", "test-peering", PeeringResourceType, "hcp_hvn.test"),
),
},
Expand All @@ -130,6 +131,7 @@ func TestAccAwsPeering(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "created_at"),
resource.TestCheckResourceAttrSet(resourceName, "expires_at"),
resource.TestCheckResourceAttrSet(resourceName, "state"),
testLink(resourceName, "self_link", "test-peering", PeeringResourceType, "hcp_hvn.test"),
),
},
Expand Down
8 changes: 0 additions & 8 deletions internal/provider/resource_aws_transit_gateway_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,6 @@ func resourceAwsTransitGatewayAttachmentRead(ctx context.Context, d *schema.Reso
return diag.Errorf("unable to retrieve transit gateway attachment (%s): %v", tgwAttID, err)
}

// The TGW attachment failed to provision properly so we want to let the user know and
// remove it from state
if tgwAtt.State == networkmodels.HashicorpCloudNetwork20200907TGWAttachmentStateFAILED {
log.Printf("[WARN] Transit gateway attachment (%s) failed to provision, removing from state", tgwAtt.ID)
d.SetId("")
return nil
}

// TGW attachment has been found, update resource data
if err := setTransitGatewayAttachmentResourceData(d, tgwAtt); err != nil {
return diag.FromErr(err)
Expand Down
16 changes: 8 additions & 8 deletions internal/provider/resource_azure_peering_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ func resourceAzurePeeringConnection() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"state": {
Description: "The state of the Azure peering connection.",
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -254,14 +259,6 @@ func resourceAzurePeeringConnectionRead(ctx context.Context, d *schema.ResourceD
return diag.Errorf("unable to retrieve peering connection (%s): %v", peeringID, err)
}

// The peering connection failed to provision properly so we want to let the user know and
// remove it from state
if peering.State == networkmodels.HashicorpCloudNetwork20200907PeeringStateFAILED {
log.Printf("[WARN] peering connection (%s) failed to provision, removing from state", peering.ID)
d.SetId("")
return nil
}

// peering connection found, update resource data
if err := setAzurePeeringResourceData(d, peering); err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -352,6 +349,9 @@ func setAzurePeeringResourceData(d *schema.ResourceData, peering *networkmodels.
if err := d.Set("expires_at", peering.ExpiresAt.String()); err != nil {
return err
}
if err := d.Set("state", peering.State); err != nil {
return err
}

link := newLink(peering.Hvn.Location, PeeringResourceType, peering.ID)
selfLink, err := linkURL(link)
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/resource_azure_peering_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func TestAccAzurePeeringConnection(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "created_at"),
resource.TestCheckResourceAttrSet(resourceName, "expires_at"),
resource.TestCheckResourceAttrSet(resourceName, "state"),
testLink(resourceName, "self_link", uniqueAzurePeeringTestID, PeeringResourceType, "hcp_hvn.test"),
),
},
Expand All @@ -153,6 +154,7 @@ func TestAccAzurePeeringConnection(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "created_at"),
resource.TestCheckResourceAttrSet(resourceName, "expires_at"),
resource.TestCheckResourceAttrSet(resourceName, "state"),
testLink(resourceName, "self_link", uniqueAzurePeeringTestID, PeeringResourceType, "hcp_hvn.test"),
),
},
Expand Down
13 changes: 10 additions & 3 deletions internal/provider/resource_hvn.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ func resourceHvn() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"state": {
Description: "The state of the HVN.",
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -203,9 +208,8 @@ func resourceHvnRead(ctx context.Context, d *schema.ResourceData, meta interface
return diag.Errorf("unable to retrieve HVN (%s): %v", hvnID, err)
}

// The HVN failed to provision properly so we want to let the user know and remove it from
// state
if hvn.State == networkmodels.HashicorpCloudNetwork20200907NetworkStateFAILED {
// The HVN has already been deleted, remove from state.
if hvn.State == networkmodels.HashicorpCloudNetwork20200907NetworkStateDELETED {
log.Printf("[WARN] HVN (%s) failed to provision, removing from state", hvnID)
d.SetId("")
return nil
Expand Down Expand Up @@ -278,6 +282,9 @@ func setHvnResourceData(d *schema.ResourceData, hvn *networkmodels.HashicorpClou
if err := d.Set("created_at", hvn.CreatedAt.String()); err != nil {
return err
}
if err := d.Set("state", hvn.State); err != nil {
return err
}

var providerAccountID string
switch d.Get("cloud_provider") {
Expand Down
14 changes: 8 additions & 6 deletions internal/provider/resource_hvn_peering_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ func resourceHvnPeeringConnection() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"state": {
Description: "The state of the HVN peering connection.",
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -188,12 +193,6 @@ func resourceHvnPeeringConnectionRead(ctx context.Context, d *schema.ResourceDat
return diag.Errorf("unable to retrieve peering connection (%s): %v", peeringID, err)
}

if peering.State == networkmodels.HashicorpCloudNetwork20200907PeeringStateFAILED {
log.Printf("[WARN] Peering connection (%s) failed to provision, removing from state", peering.ID)
d.SetId("")
return nil
}

hvnLink2 := newLink(peering.Target.HvnTarget.Hvn.Location, HvnResourceType, peering.Target.HvnTarget.Hvn.ID)
hvnURL2, err := linkURL(hvnLink2)
if err != nil {
Expand Down Expand Up @@ -303,6 +302,9 @@ func setHvnPeeringResourceData(d *schema.ResourceData, peering *networkmodels.Ha
if err := d.Set("expires_at", peering.ExpiresAt.String()); err != nil {
return err
}
if err := d.Set("state", peering.ExpiresAt.String()); err != nil {
return err
}

link := newLink(peering.Hvn.Location, PeeringResourceType, peering.ID)
selfLink, err := linkURL(link)
Expand Down
3 changes: 3 additions & 0 deletions internal/provider/resource_hvn_peering_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func TestAccHvnPeeringConnection(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "created_at"),
resource.TestCheckResourceAttrSet(resourceName, "expires_at"),
resource.TestCheckResourceAttrSet(resourceName, "state"),
testLink(resourceName, "hvn_1", "test-1", HvnResourceType, resourceName),
testLink(resourceName, "hvn_2", "test-2", HvnResourceType, resourceName),
),
Expand Down Expand Up @@ -88,6 +89,7 @@ func TestAccHvnPeeringConnection(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "created_at"),
resource.TestCheckResourceAttrSet(resourceName, "expires_at"),
resource.TestCheckResourceAttrSet(resourceName, "state"),
testLink(resourceName, "hvn_1", "test-1", HvnResourceType, resourceName),
testLink(resourceName, "hvn_2", "test-2", HvnResourceType, resourceName),
),
Expand All @@ -101,6 +103,7 @@ func TestAccHvnPeeringConnection(t *testing.T) {
resource.TestCheckResourceAttrPair(resourceName, "project_id", dataSourceName, "project_id"),
resource.TestCheckResourceAttrPair(resourceName, "created_at", dataSourceName, "created_at"),
resource.TestCheckResourceAttrPair(resourceName, "expires_at", dataSourceName, "expires_at"),
resource.TestCheckResourceAttrPair(resourceName, "state", dataSourceName, "state"),
testLink(dataSourceName, "hvn_1", "test-1", HvnResourceType, dataSourceName),
testLink(dataSourceName, "hvn_2", "test-2", HvnResourceType, dataSourceName),
),
Expand Down
7 changes: 0 additions & 7 deletions internal/provider/resource_hvn_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,6 @@ func resourceHvnRouteRead(ctx context.Context, d *schema.ResourceData, meta inte
return diag.Errorf("unable to retrieve HVN route (%s): %v", idLink.ID, err)
}

// The HVN route failed to provision properly so we want to let the user know and remove it from state.
if route.State == networkmodels.HashicorpCloudNetwork20200907HVNRouteStateFAILED {
log.Printf("[WARN] HVN route (%s) failed to provision, removing from state", idLink.ID)
d.SetId("")
return nil
}

// HVN route found, update resource data.
if err := setHVNRouteResourceData(d, route, loc); err != nil {
return diag.FromErr(err)
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/resource_hvn_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func TestAccHvnRoute(t *testing.T) {
testAccCheckHvnRouteExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "hvn_route_id", "peering-route"),
resource.TestCheckResourceAttr(resourceName, "destination_cidr", "172.31.0.0/16"),
resource.TestCheckResourceAttr(resourceName, "state", "ACTIVE"),
testLink(resourceName, "self_link", "peering-route", HVNRouteResourceType, "hcp_hvn.test"),
testLink(resourceName, "target_link", "hcp-tf-provider-test", PeeringResourceType, "hcp_hvn.test"),
),
Expand All @@ -114,6 +115,7 @@ func TestAccHvnRoute(t *testing.T) {
testAccCheckHvnRouteExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "hvn_route_id", "peering-route"),
resource.TestCheckResourceAttr(resourceName, "destination_cidr", "172.31.0.0/16"),
resource.TestCheckResourceAttr(resourceName, "state", "ACTIVE"),
testLink(resourceName, "self_link", "peering-route", HVNRouteResourceType, "hcp_hvn.test"),
testLink(resourceName, "target_link", "hcp-tf-provider-test", PeeringResourceType, "hcp_hvn.test"),
),
Expand Down
Loading

0 comments on commit db2ef7b

Please sign in to comment.