Skip to content

Commit

Permalink
compute: added numeric_id to google_compute_subnetwork
Browse files Browse the repository at this point in the history
Added `numeric_id` to `google_compute_subnetwork` resource and data
source.

Since Terraform uses `id` internally, follow the example of
`google_compute_network`, and make `numeric_id` contain the `id` field
from the API.

Part of hashicorp/terraform-provider-google#20223
  • Loading branch information
wyardley committed Nov 8, 2024
1 parent 069cde8 commit 3312a35
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 0 deletions.
7 changes: 7 additions & 0 deletions mmv1/products/compute/Subnetwork.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ iam_policy:
custom_code:
extra_schema_entry: 'templates/terraform/extra_schema_entry/subnetwork.tmpl'
constants: 'templates/terraform/constants/subnetwork.tmpl'
decoder: 'templates/terraform/decoders/compute_subnetwork.go.tmpl'
encoder: 'templates/terraform/encoders/compute_subnetwork.go.tmpl'
update_encoder: 'templates/terraform/update_encoder/compute_subnetwork.go.tmpl'
post_update: 'templates/terraform/post_update/compute_subnetwork.go.tmpl'
custom_diff:
- 'customdiff.ForceNewIfChange("ip_cidr_range", IsShrinkageIpCidr)'
Expand Down Expand Up @@ -203,6 +206,10 @@ properties:
custom_expand: 'templates/terraform/custom_expand/resourceref_with_validation.go.tmpl'
resource: 'Network'
imports: 'selfLink'
- name: 'numericId'
type: String
description: The numeric ID of the resource as a string.
output: true
- name: 'purpose'
type: String
description: |
Expand Down
2 changes: 2 additions & 0 deletions mmv1/templates/terraform/decoders/compute_subnetwork.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
res["numericId"] = res["id"] // stores unique id into numericId attribute before it's changed to path format
return res, nil
2 changes: 2 additions & 0 deletions mmv1/templates/terraform/encoders/compute_subnetwork.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
delete(obj, "numeric_id") // Field doesn't exist in the API
return obj, nil
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
delete(obj, "numeric_id") // Field doesn't exist in the API
return obj, nil
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package compute

import (
"fmt"
"strconv"

"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
Expand Down Expand Up @@ -31,6 +32,10 @@ func DataSourceGoogleComputeSubnetwork() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"numeric_id": {
Type: schema.TypeString,
Computed: true,
},
"ip_cidr_range": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -100,6 +105,10 @@ func dataSourceGoogleComputeSubnetworkRead(d *schema.ResourceData, meta interfac
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("Subnetwork Not Found : %s", name), id)
}

if err := d.Set("numeric_id", strconv.Itoa(int(subnetwork.Id))); err != nil {
return fmt.Errorf("Error setting numeric_id: %s", err)
}

if err := d.Set("ip_cidr_range", subnetwork.IpCidrRange); err != nil {
return fmt.Errorf("Error setting ip_cidr_range: %s", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func testAccDataSourceGoogleSubnetworkCheck(data_source_name string, resource_na
"id",
"name",
"description",
"numeric_id",
"ip_cidr_range",
"private_ip_google_access",
"internal_ipv6_prefix",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"
"fmt"
"testing"
"regexp"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-provider-google/google/acctest"
"github.com/hashicorp/terraform-provider-google/google/envvar"
tpgcompute "github.com/hashicorp/terraform-provider-google/google/services/compute"

{{ if eq $.TargetVersionName `ga` }}
Expand Down Expand Up @@ -94,6 +96,36 @@ func TestAccComputeSubnetwork_basic(t *testing.T) {
})
}

func TestAccComputeSubnetwork_numericId(t *testing.T) {
t.Parallel()

cnName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
subnetworkName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
projectId := envvar.GetTestProjectFromEnv()
subnetworkId := fmt.Sprintf("projects/%s/regions/us-central1/subnetworks/%s", projectId, subnetworkName)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeSubnetworkDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeSubnetwork_numericId(cnName, subnetworkName),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("google_compute_subnetwork.numeric_id_test", "numeric_id", regexp.MustCompile("^\\d{16,48}$")),
resource.TestCheckResourceAttr("google_compute_subnetwork.numeric_id_test", "id", subnetworkId),
),
},
{
ResourceName: "google_compute_subnetwork.numeric_id_test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}


func TestAccComputeSubnetwork_update(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -546,6 +578,23 @@ resource "google_compute_subnetwork" "network-with-private-google-access" {
`, cnName, subnetwork1Name, subnetwork2Name, subnetwork3Name)
}

func testAccComputeSubnetwork_numericId(cnName, subnetworkName string) string {
return fmt.Sprintf(`
resource "google_compute_network" "test" {
name = "%s"
auto_create_subnetworks = false
enable_ula_internal_ipv6 = true
}

resource "google_compute_subnetwork" "numeric_id_test" {
name = "%s"
ip_cidr_range = "10.2.0.0/16"
region = "us-central1"
network = google_compute_network.test.self_link
}
`, cnName, subnetworkName)
}

func testAccComputeSubnetwork_update1(cnName, cidrRange, subnetworkName string) string {
return fmt.Sprintf(`
resource "google_compute_network" "custom-test" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ In addition to the arguments listed above, the following attributes are exported
* `network` - The network name or resource link to the parent
network of this subnetwork.

* `numeric_id` - The numeric ID of the resource as a string.

* `description` - Description of this subnetwork.

* `ip_cidr_range` - The IP address range that machines in this
Expand Down

0 comments on commit 3312a35

Please sign in to comment.