Skip to content

Commit e7f98fd

Browse files
wyardleymelinath
authored andcommitted
compute: added numeric_id to google_compute_network data source (GoogleCloudPlatform#12339)
1 parent 6f75b8b commit e7f98fd

7 files changed

+45
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: "Common resource patterns"
3+
weight: 40
4+
---
5+
6+
# Common resource patterns
7+
8+
## Singletons
9+
10+
Singletons are resources – often config or settings objects – that can only exist once. In some cases, it may be possible to create and delete the resource (but only one can exist at a time); in other cases the resource _always_ exists and can only be read and updated.
11+
12+
Implementing resources like this may require some or all of the following:
13+
14+
1. If there _isn't_ a create endpoint, set the [create_url]({{< ref "/develop/resource-reference/#create_url" >}}) to point to the update endpoint.
15+
1. If there _is_ a create endpoint, add [pre-create custom code]({{< ref "/develop/custom-code/#pre_post_injection" >}}) that implements "acquire-on-create" logic. The custom code should check whether the resource already exists with a read request, and if it does, run the update logic and return early. For example, see [mmv1/templates/terraform/pre_create/firebasehosting_site.go.tmpl](https://github.com/GoogleCloudPlatform/magic-modules/blob/dc4d9755cb9288177e0996c1c3b3fa9738ebdf89/mmv1/templates/terraform/pre_create/firebasehosting_site.go.tmpl).
16+
* Note: The main disadvantage of "acquire-on-create" logic is that users will not be presented with a diff between the resource's old and new states – because from the terraform perspective, the resource is only being created. Please upvote https://github.com/hashicorp/terraform/issues/19017 to request better support for this workflow.
17+
1. If there is no delete endpoint, set [`exclude_delete: true`]({{< ref "/develop/resource-reference/#create_url" >}}) at the top level of the resource.

docs/content/best-practices/deletion-behaviors.md

+4
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ Some resources need to let users control the actions taken add deletion time. Fo
3333
One common example is `ABANDON`, which is useful if the resource is safe to delete from Terraform but could cause problems if deleted from the API - for example, `google_bigtable_gc_policy` deletion can fail in replicated instances. `ABANDON` indicates that attempts to delete the resource should remove it from state without actually deleting it.
3434

3535
See [Client-side fields]({{< ref "/develop/client-side-fields" >}}) for information about adding `deletion_policy` fields.
36+
37+
## Exclude deletion {#exclude_delete}
38+
39+
Some resources do not support deletion in the API and can only be removed from state. For these resources, the best practice is to set [`exclude_delete: true`]({{< ref "/develop/resource-reference#exclude_delete" >}}) on the resource.

docs/content/develop/resource-reference.md

+8
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ Example:
205205
delete_verb: 'POST'
206206
```
207207

208+
### `exclude_delete`
209+
If true, deleting the resource will only remove it from the Terraform state and will not call an API. If false, deleting the resource will call the [`delete_url`]({{< ref "#delete_url" >}}) using the HTTP [`delete_verb`]({{< ref "#delete_verb" >}}).
210+
This should be used if the resource can never be deleted in the API, and there is no other reasonable action to take on deletion. See [Deletion behaviors]({{< ref "/best-practices/deletion-behaviors" >}}) for more information.
211+
212+
```yaml
213+
exclude_delete: true
214+
```
215+
208216
### `autogen_async`
209217

210218
If true, code for handling long-running operations is generated along with

mmv1/third_party/terraform/services/compute/data_source_google_compute_network.go.tmpl

+12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package compute
22

33
import (
44
"fmt"
5+
"strconv"
56

67
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
78
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
@@ -23,6 +24,14 @@ func DataSourceGoogleComputeNetwork() *schema.Resource {
2324
Computed: true,
2425
},
2526

27+
// TODO: this should eventually be TypeInt, but leaving as
28+
// string for now to match the resource and to avoid a
29+
// breaking change.
30+
"numeric_id": {
31+
Type: schema.TypeString,
32+
Computed: true,
33+
},
34+
2635
"gateway_ipv4": {
2736
Type: schema.TypeString,
2837
Computed: true,
@@ -95,6 +104,9 @@ func dataSourceGoogleComputeNetworkRead(d *schema.ResourceData, meta interface{}
95104
if err := d.Set("description", network.Description); err != nil {
96105
return fmt.Errorf("Error setting description: %s", err)
97106
}
107+
if err := d.Set("numeric_id", strconv.Itoa(int(network.Id))); err != nil {
108+
return fmt.Errorf("Error setting numeric_id: %s", err)
109+
}
98110
if err := d.Set("subnetworks_self_links", network.Subnetworks); err != nil {
99111
return fmt.Errorf("Error setting subnetworks_self_links: %s", err)
100112
}

mmv1/third_party/terraform/services/compute/data_source_google_compute_network_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func testAccDataSourceGoogleNetworkCheck(data_source_name string, resource_name
4545
network_attrs_to_test := []string{
4646
"id",
4747
"name",
48+
"numeric_id",
4849
"description",
4950
"internal_ipv6_range",
5051
}

mmv1/third_party/terraform/services/compute/resource_compute_network_test.go.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func TestAccComputeNetwork_numericId(t *testing.T) {
262262
{
263263
Config: testAccComputeNetwork_basic(networkName),
264264
Check: resource.ComposeTestCheckFunc(
265-
resource.TestMatchResourceAttr("google_compute_network.bar", "numeric_id",regexp.MustCompile("^\\d{1,}$")),
265+
resource.TestMatchResourceAttr("google_compute_network.bar", "numeric_id",regexp.MustCompile("^\\d{16,48}$")),
266266
resource.TestCheckResourceAttr("google_compute_network.bar", "id", networkId),
267267
),
268268
},

mmv1/third_party/terraform/website/docs/d/compute_network.html.markdown

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ In addition to the arguments listed above, the following attributes are exported
3636

3737
* `description` - Description of this network.
3838

39+
* `numeric_id` - The numeric unique identifier for the resource.
40+
3941
* `gateway_ipv4` - The IP address of the gateway.
4042

4143
* `internal_ipv6_range` - The ula internal ipv6 range assigned to this network.

0 commit comments

Comments
 (0)