Skip to content

Add reservedInternalRange fields to the google_compute_subnetwork resource #7318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 78 additions & 20 deletions google-beta/services/compute/resource_compute_subnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,6 @@ func ResourceComputeSubnetwork() *schema.Resource {
),

Schema: map[string]*schema.Schema{
"ip_cidr_range": {
Type: schema.TypeString,
Required: true,
ValidateFunc: verify.ValidateIpCidrRange,
Description: `The range of internal addresses that are owned by this subnetwork.
Provide this property when you create the subnetwork. For example,
10.0.0.0/8 or 192.168.0.0/16. Ranges must be unique and
non-overlapping within a network. Only IPv4 is supported.`,
},
"name": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -133,6 +124,17 @@ creation time.`,
ForceNew: true,
Description: `The range of external IPv6 addresses that are owned by this subnetwork.`,
},
"ip_cidr_range": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ValidateFunc: verify.ValidateIpCidrRange,
Description: `The range of internal addresses that are owned by this subnetwork.
Provide this property when you create the subnetwork. For example,
10.0.0.0/8 or 192.168.0.0/16. Ranges must be unique and
non-overlapping within a network. Only IPv4 is supported.
Field is optional when 'reserved_internal_range' is defined, otherwise required.`,
},
"ipv6_access_type": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -239,6 +241,14 @@ If unspecified, the purpose defaults to 'PRIVATE_RFC_1918'.`,
DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName,
Description: `The GCP region for this subnetwork.`,
},
"reserved_internal_range": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName,
Description: `The ID of the reserved internal range. Must be prefixed with 'networkconnectivity.googleapis.com'
E.g. 'networkconnectivity.googleapis.com/projects/{project}/locations/global/internalRanges/{rangeId}'`,
},
"role": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -266,15 +276,6 @@ of zero objects you must use the following syntax:
For more details about this behavior, see [this section](https://www.terraform.io/docs/configuration/attr-as-blocks.html#defining-a-fixed-object-collection-value).`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip_cidr_range": {
Type: schema.TypeString,
Required: true,
ValidateFunc: verify.ValidateIpCidrRange,
Description: `The range of IP addresses belonging to this subnetwork secondary
range. Provide this property when you create the subnetwork.
Ranges must be unique and non-overlapping with all primary and
secondary IP ranges within a network. Only IPv4 is supported.`,
},
"range_name": {
Type: schema.TypeString,
Required: true,
Expand All @@ -284,6 +285,24 @@ when adding an alias IP range to a VM instance. The name must
be 1-63 characters long, and comply with RFC1035. The name
must be unique within the subnetwork.`,
},
"ip_cidr_range": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ValidateFunc: verify.ValidateIpCidrRange,
Description: `The range of IP addresses belonging to this subnetwork secondary
range. Provide this property when you create the subnetwork.
Ranges must be unique and non-overlapping with all primary and
secondary IP ranges within a network. Only IPv4 is supported.
Field is optional when 'reserved_internal_range' is defined, otherwise required.`,
},
"reserved_internal_range": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName,
Description: `The ID of the reserved internal range. Must be prefixed with 'networkconnectivity.googleapis.com'
E.g. 'networkconnectivity.googleapis.com/projects/{project}/locations/global/internalRanges/{rangeId}'`,
},
},
},
},
Expand Down Expand Up @@ -399,6 +418,12 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e
} else if v, ok := d.GetOkExists("ip_cidr_range"); !tpgresource.IsEmptyValue(reflect.ValueOf(ipCidrRangeProp)) && (ok || !reflect.DeepEqual(v, ipCidrRangeProp)) {
obj["ipCidrRange"] = ipCidrRangeProp
}
reservedInternalRangeProp, err := expandComputeSubnetworkReservedInternalRange(d.Get("reserved_internal_range"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("reserved_internal_range"); !tpgresource.IsEmptyValue(reflect.ValueOf(reservedInternalRangeProp)) && (ok || !reflect.DeepEqual(v, reservedInternalRangeProp)) {
obj["reservedInternalRange"] = reservedInternalRangeProp
}
nameProp, err := expandComputeSubnetworkName(d.Get("name"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -588,6 +613,9 @@ func resourceComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) err
if err := d.Set("ip_cidr_range", flattenComputeSubnetworkIpCidrRange(res["ipCidrRange"], d, config)); err != nil {
return fmt.Errorf("Error reading Subnetwork: %s", err)
}
if err := d.Set("reserved_internal_range", flattenComputeSubnetworkReservedInternalRange(res["reservedInternalRange"], d, config)); err != nil {
return fmt.Errorf("Error reading Subnetwork: %s", err)
}
if err := d.Set("name", flattenComputeSubnetworkName(res["name"], d, config)); err != nil {
return fmt.Errorf("Error reading Subnetwork: %s", err)
}
Expand Down Expand Up @@ -1136,6 +1164,13 @@ func flattenComputeSubnetworkIpCidrRange(v interface{}, d *schema.ResourceData,
return v
}

func flattenComputeSubnetworkReservedInternalRange(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return v
}
return tpgresource.ConvertSelfLinkToV1(v.(string))
}

func flattenComputeSubnetworkName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
Expand Down Expand Up @@ -1168,8 +1203,9 @@ func flattenComputeSubnetworkSecondaryIpRange(v interface{}, d *schema.ResourceD
continue
}
transformed = append(transformed, map[string]interface{}{
"range_name": flattenComputeSubnetworkSecondaryIpRangeRangeName(original["rangeName"], d, config),
"ip_cidr_range": flattenComputeSubnetworkSecondaryIpRangeIpCidrRange(original["ipCidrRange"], d, config),
"range_name": flattenComputeSubnetworkSecondaryIpRangeRangeName(original["rangeName"], d, config),
"ip_cidr_range": flattenComputeSubnetworkSecondaryIpRangeIpCidrRange(original["ipCidrRange"], d, config),
"reserved_internal_range": flattenComputeSubnetworkSecondaryIpRangeReservedInternalRange(original["reservedInternalRange"], d, config),
})
}
return transformed
Expand All @@ -1182,6 +1218,13 @@ func flattenComputeSubnetworkSecondaryIpRangeIpCidrRange(v interface{}, d *schem
return v
}

func flattenComputeSubnetworkSecondaryIpRangeReservedInternalRange(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return v
}
return tpgresource.ConvertSelfLinkToV1(v.(string))
}

func flattenComputeSubnetworkPrivateIpGoogleAccess(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
Expand Down Expand Up @@ -1262,6 +1305,10 @@ func expandComputeSubnetworkIpCidrRange(v interface{}, d tpgresource.TerraformRe
return v, nil
}

func expandComputeSubnetworkReservedInternalRange(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandComputeSubnetworkName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -1306,6 +1353,13 @@ func expandComputeSubnetworkSecondaryIpRange(v interface{}, d tpgresource.Terraf
transformed["ipCidrRange"] = transformedIpCidrRange
}

transformedReservedInternalRange, err := expandComputeSubnetworkSecondaryIpRangeReservedInternalRange(original["reserved_internal_range"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedReservedInternalRange); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["reservedInternalRange"] = transformedReservedInternalRange
}

req = append(req, transformed)
}
return req, nil
Expand All @@ -1319,6 +1373,10 @@ func expandComputeSubnetworkSecondaryIpRangeIpCidrRange(v interface{}, d tpgreso
return v, nil
}

func expandComputeSubnetworkSecondaryIpRangeReservedInternalRange(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandComputeSubnetworkPrivateIpGoogleAccess(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestAccComputeSubnetwork_subnetworkBasicExample(t *testing.T) {
ResourceName: "google_compute_subnetwork.network-with-private-secondary-ip-ranges",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"network", "region"},
ImportStateVerifyIgnore: []string{"reserved_internal_range", "network", "region"},
},
},
})
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestAccComputeSubnetwork_subnetworkLoggingConfigExample(t *testing.T) {
ResourceName: "google_compute_subnetwork.subnet-with-logging",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"network", "region"},
ImportStateVerifyIgnore: []string{"reserved_internal_range", "network", "region"},
},
},
})
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestAccComputeSubnetwork_subnetworkInternalL7lbExample(t *testing.T) {
ResourceName: "google_compute_subnetwork.network-for-l7lb",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"network", "region"},
ImportStateVerifyIgnore: []string{"reserved_internal_range", "network", "region"},
},
},
})
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestAccComputeSubnetwork_subnetworkIpv6Example(t *testing.T) {
ResourceName: "google_compute_subnetwork.subnetwork-ipv6",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"network", "region"},
ImportStateVerifyIgnore: []string{"reserved_internal_range", "network", "region"},
},
},
})
Expand Down Expand Up @@ -234,7 +234,7 @@ func TestAccComputeSubnetwork_subnetworkInternalIpv6Example(t *testing.T) {
ResourceName: "google_compute_subnetwork.subnetwork-internal-ipv6",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"network", "region"},
ImportStateVerifyIgnore: []string{"reserved_internal_range", "network", "region"},
},
},
})
Expand Down Expand Up @@ -281,7 +281,7 @@ func TestAccComputeSubnetwork_subnetworkPurposePrivateNatExample(t *testing.T) {
ResourceName: "google_compute_subnetwork.subnetwork-purpose-private-nat",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"network", "region"},
ImportStateVerifyIgnore: []string{"reserved_internal_range", "network", "region"},
},
},
})
Expand Down Expand Up @@ -327,7 +327,7 @@ func TestAccComputeSubnetwork_subnetworkCidrOverlapExample(t *testing.T) {
ResourceName: "google_compute_subnetwork.subnetwork-cidr-overlap",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"network", "region"},
ImportStateVerifyIgnore: []string{"reserved_internal_range", "network", "region"},
},
},
})
Expand All @@ -354,6 +354,61 @@ resource "google_compute_network" "net-cidr-overlap" {
`, context)
}

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

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
CheckDestroy: testAccCheckComputeSubnetworkDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeSubnetwork_subnetworkReservedInternalRangeExample(context),
},
{
ResourceName: "google_compute_subnetwork.subnetwork-reserved-internal-range",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"reserved_internal_range", "network", "region"},
},
},
})
}

func testAccComputeSubnetwork_subnetworkReservedInternalRangeExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_compute_subnetwork" "subnetwork-reserved-internal-range" {
provider = google-beta
name = "tf-test-subnetwork-reserved-internal-range%{random_suffix}"
region = "us-central1"
network = google_compute_network.default.id
reserved_internal_range = "networkconnectivity.googleapis.com/${google_network_connectivity_internal_range.reserved.id}"
}

resource "google_compute_network" "default" {
provider = google-beta
name = "tf-test-network-reserved-internal-range%{random_suffix}"
auto_create_subnetworks = false
}

resource "google_network_connectivity_internal_range" "reserved" {
provider = google-beta
name = "reserved"
network = google_compute_network.default.id
usage = "FOR_VPC"
peering = "FOR_SELF"
prefix_length = 24
target_cidr_range = [
"10.0.0.0/8"
]
}
`, context)
}

func testAccCheckComputeSubnetworkDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
Loading