Skip to content

Commit

Permalink
Support import for google_compute_subnetwork (hashicorp#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
rosbo authored and Dmitry Vlasov committed Aug 15, 2017
1 parent 3dfd9bc commit c18e331
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
36 changes: 36 additions & 0 deletions google/import_compute_subnetwork_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package google

import (
"fmt"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"testing"
)

func TestAccComputeSubnetwork_importBasic(t *testing.T) {
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(cnName, subnetwork1Name, subnetwork2Name, subnetwork3Name),
},
resource.TestStep{
ResourceName: "google_compute_subnetwork.network-ref-by-url",
ImportState: true,
ImportStateVerify: true,
},
resource.TestStep{
ResourceName: "google_compute_subnetwork.network-with-private-google-access",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
33 changes: 30 additions & 3 deletions google/resource_compute_subnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ func resourceComputeSubnetwork() *schema.Resource {
Read: resourceComputeSubnetworkRead,
Update: resourceComputeSubnetworkUpdate,
Delete: resourceComputeSubnetworkDelete,
Importer: &schema.ResourceImporter{
State: resourceComputeSubnetworkImportState,
},

Schema: map[string]*schema.Schema{
"ip_cidr_range": &schema.Schema{
Expand All @@ -31,9 +34,10 @@ func resourceComputeSubnetwork() *schema.Resource {
},

"network": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: linkDiffSuppress,
},

"description": &schema.Schema{
Expand Down Expand Up @@ -155,6 +159,11 @@ func resourceComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) err
return handleNotFoundError(err, d, fmt.Sprintf("Subnetwork %q", name))
}

d.Set("name", subnetwork.Name)
d.Set("ip_cidr_range", subnetwork.IpCidrRange)
d.Set("network", subnetwork.Network)
d.Set("description", subnetwork.Description)
d.Set("private_ip_google_access", subnetwork.PrivateIpGoogleAccess)
d.Set("gateway_address", subnetwork.GatewayAddress)
d.Set("self_link", subnetwork.SelfLink)

Expand Down Expand Up @@ -229,3 +238,21 @@ func resourceComputeSubnetworkDelete(d *schema.ResourceData, meta interface{}) e
d.SetId("")
return nil
}

func resourceComputeSubnetworkImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 2 {
return nil, fmt.Errorf("Invalid compute subnetwork specifier. Expecting {region}/{name}")
}

region, name := parts[0], parts[1]
d.Set("region", region)
d.Set("name", name)

d.SetId(createSubnetID(&compute.Subnetwork{
Region: region,
Name: name,
}))

return []*schema.ResourceData{d}, nil
}
12 changes: 12 additions & 0 deletions website/docs/r/compute_subnetwork.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ resource "google_compute_subnetwork" "default-us-east1" {
network = "${google_compute_network.default.self_link}"
region = "us-east1"
}
resource "google_compute_network" "default" {
name = "test"
}
```

## Argument Reference
Expand Down Expand Up @@ -60,3 +64,11 @@ exported:
* `gateway_address` - The IP address of the gateway.

* `self_link` - The URI of the created resource.

## Import

Subnetwork can be imported using the `region` and `name`, e.g.

```
$ terraform import google_compute_subnetwork.default-us-east1 us-east1/default-us-east1
```

0 comments on commit c18e331

Please sign in to comment.