Added private_ip_google_access update support to google_compute_subnetwork. (#15125)
This commit is contained in:
parent
4e1eb9a856
commit
f5cccc6b7f
|
@ -14,6 +14,7 @@ func resourceComputeSubnetwork() *schema.Resource {
|
|||
return &schema.Resource{
|
||||
Create: resourceComputeSubnetworkCreate,
|
||||
Read: resourceComputeSubnetworkRead,
|
||||
Update: resourceComputeSubnetworkUpdate,
|
||||
Delete: resourceComputeSubnetworkDelete,
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
|
@ -61,7 +62,6 @@ func resourceComputeSubnetwork() *schema.Resource {
|
|||
"private_ip_google_access": &schema.Schema{
|
||||
Type: schema.TypeBool,
|
||||
Optional: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
|
||||
"self_link": &schema.Schema{
|
||||
|
@ -161,6 +161,46 @@ func resourceComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) err
|
|||
return nil
|
||||
}
|
||||
|
||||
func resourceComputeSubnetworkUpdate(d *schema.ResourceData, meta interface{}) error {
|
||||
config := meta.(*Config)
|
||||
|
||||
region, err := getRegion(d, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
project, err := getProject(d, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.Partial(true)
|
||||
|
||||
if d.HasChange("private_ip_google_access") {
|
||||
subnetworksSetPrivateIpGoogleAccessRequest := &compute.SubnetworksSetPrivateIpGoogleAccessRequest{
|
||||
PrivateIpGoogleAccess: d.Get("private_ip_google_access").(bool),
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Updating Subnetwork PrivateIpGoogleAccess %q: %#v", d.Id(), subnetworksSetPrivateIpGoogleAccessRequest)
|
||||
op, err := config.clientCompute.Subnetworks.SetPrivateIpGoogleAccess(
|
||||
project, region, d.Get("name").(string), subnetworksSetPrivateIpGoogleAccessRequest).Do()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error updating subnetwork PrivateIpGoogleAccess: %s", err)
|
||||
}
|
||||
|
||||
err = computeOperationWaitRegion(config, op, project, region, "Updating Subnetwork PrivateIpGoogleAccess")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.SetPartial("private_ip_google_access")
|
||||
}
|
||||
|
||||
d.Partial(false)
|
||||
|
||||
return resourceComputeSubnetworkRead(d, meta)
|
||||
}
|
||||
|
||||
func resourceComputeSubnetworkDelete(d *schema.ResourceData, meta interface{}) error {
|
||||
config := meta.(*Config)
|
||||
|
||||
|
|
|
@ -14,13 +14,18 @@ func TestAccComputeSubnetwork_basic(t *testing.T) {
|
|||
var subnetwork1 compute.Subnetwork
|
||||
var subnetwork2 compute.Subnetwork
|
||||
|
||||
cnName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
||||
subnetwork1Name := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
||||
subnetwork2Name := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
||||
subnetwork3Name := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckComputeSubnetworkDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccComputeSubnetwork_basic,
|
||||
Config: testAccComputeSubnetwork_basic(cnName, subnetwork1Name, subnetwork2Name, subnetwork3Name),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckComputeSubnetworkExists(
|
||||
"google_compute_subnetwork.network-ref-by-url", &subnetwork1),
|
||||
|
@ -32,6 +37,39 @@ func TestAccComputeSubnetwork_basic(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestAccComputeSubnetwork_update(t *testing.T) {
|
||||
var subnetwork compute.Subnetwork
|
||||
|
||||
cnName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
||||
subnetworkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckComputeSubnetworkDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccComputeSubnetwork_update1(cnName, subnetworkName),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckComputeSubnetworkExists(
|
||||
"google_compute_subnetwork.network-with-private-google-access", &subnetwork),
|
||||
),
|
||||
},
|
||||
resource.TestStep{
|
||||
Config: testAccComputeSubnetwork_update2(cnName, subnetworkName),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckComputeSubnetworkExists(
|
||||
"google_compute_subnetwork.network-with-private-google-access", &subnetwork),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if subnetwork.PrivateIpGoogleAccess {
|
||||
t.Errorf("Expected PrivateIpGoogleAccess to be false, got %v", subnetwork.PrivateIpGoogleAccess)
|
||||
}
|
||||
}
|
||||
|
||||
func testAccCheckComputeSubnetworkDestroy(s *terraform.State) error {
|
||||
config := testAccProvider.Meta().(*Config)
|
||||
|
||||
|
@ -81,14 +119,15 @@ func testAccCheckComputeSubnetworkExists(n string, subnetwork *compute.Subnetwor
|
|||
}
|
||||
}
|
||||
|
||||
var testAccComputeSubnetwork_basic = fmt.Sprintf(`
|
||||
func testAccComputeSubnetwork_basic(cnName, subnetwork1Name, subnetwork2Name, subnetwork3Name string) string {
|
||||
return fmt.Sprintf(`
|
||||
resource "google_compute_network" "custom-test" {
|
||||
name = "network-test-%s"
|
||||
name = "%s"
|
||||
auto_create_subnetworks = false
|
||||
}
|
||||
|
||||
resource "google_compute_subnetwork" "network-ref-by-url" {
|
||||
name = "subnetwork-test-%s"
|
||||
name = "%s"
|
||||
ip_cidr_range = "10.0.0.0/16"
|
||||
region = "us-central1"
|
||||
network = "${google_compute_network.custom-test.self_link}"
|
||||
|
@ -96,18 +135,51 @@ resource "google_compute_subnetwork" "network-ref-by-url" {
|
|||
|
||||
|
||||
resource "google_compute_subnetwork" "network-ref-by-name" {
|
||||
name = "subnetwork-test-%s"
|
||||
name = "%s"
|
||||
ip_cidr_range = "10.1.0.0/16"
|
||||
region = "us-central1"
|
||||
network = "${google_compute_network.custom-test.name}"
|
||||
}
|
||||
|
||||
resource "google_compute_subnetwork" "network-with-private-google-access" {
|
||||
name = "subnetwork-test-%s"
|
||||
name = "%s"
|
||||
ip_cidr_range = "10.2.0.0/16"
|
||||
region = "us-central1"
|
||||
network = "${google_compute_network.custom-test.self_link}"
|
||||
private_ip_google_access = true
|
||||
}
|
||||
`, cnName, subnetwork1Name, subnetwork2Name, subnetwork3Name)
|
||||
}
|
||||
|
||||
`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))
|
||||
func testAccComputeSubnetwork_update1(cnName, subnetworkName string) string {
|
||||
return fmt.Sprintf(`
|
||||
resource "google_compute_network" "custom-test" {
|
||||
name = "%s"
|
||||
auto_create_subnetworks = false
|
||||
}
|
||||
|
||||
resource "google_compute_subnetwork" "network-with-private-google-access" {
|
||||
name = "%s"
|
||||
ip_cidr_range = "10.2.0.0/16"
|
||||
region = "us-central1"
|
||||
network = "${google_compute_network.custom-test.self_link}"
|
||||
private_ip_google_access = true
|
||||
}
|
||||
`, cnName, subnetworkName)
|
||||
}
|
||||
|
||||
func testAccComputeSubnetwork_update2(cnName, subnetworkName string) string {
|
||||
return fmt.Sprintf(`
|
||||
resource "google_compute_network" "custom-test" {
|
||||
name = "%s"
|
||||
auto_create_subnetworks = false
|
||||
}
|
||||
|
||||
resource "google_compute_subnetwork" "network-with-private-google-access" {
|
||||
name = "%s"
|
||||
ip_cidr_range = "10.2.0.0/16"
|
||||
region = "us-central1"
|
||||
network = "${google_compute_network.custom-test.self_link}"
|
||||
}
|
||||
`, cnName, subnetworkName)
|
||||
}
|
||||
|
|
|
@ -8,7 +8,10 @@ description: |-
|
|||
|
||||
# google\_compute\_subnetwork
|
||||
|
||||
Manages a subnetwork within GCE.
|
||||
Manages a subnetwork within GCE. For more information see
|
||||
[the official documentation](https://cloud.google.com/compute/docs/vpc/#vpc_networks_and_subnets)
|
||||
and
|
||||
[API](https://cloud.google.com/compute/docs/reference/latest/subnetworks).
|
||||
|
||||
## Example Usage
|
||||
|
||||
|
@ -45,7 +48,7 @@ The following arguments are supported:
|
|||
* `region` - (Optional) The region this subnetwork will be created in. If
|
||||
unspecified, this defaults to the region configured in the provider.
|
||||
|
||||
* `private_ip_google_access` - Whether the VMs in this subnet
|
||||
* `private_ip_google_access` - (Optional) Whether the VMs in this subnet
|
||||
can access Google services without assigned external IP
|
||||
addresses.
|
||||
|
||||
|
|
Loading…
Reference in New Issue