diff --git a/google/import_compute_subnetwork_test.go b/google/import_compute_subnetwork_test.go new file mode 100644 index 00000000000..a0b300031ce --- /dev/null +++ b/google/import_compute_subnetwork_test.go @@ -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, + }, + }, + }) +} diff --git a/google/resource_compute_subnetwork.go b/google/resource_compute_subnetwork.go index 04db37cbd84..1d48f38e510 100644 --- a/google/resource_compute_subnetwork.go +++ b/google/resource_compute_subnetwork.go @@ -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{ @@ -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{ @@ -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) @@ -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 +} diff --git a/website/docs/r/compute_subnetwork.html.markdown b/website/docs/r/compute_subnetwork.html.markdown index 80f9b14b83e..55eed2b55e6 100644 --- a/website/docs/r/compute_subnetwork.html.markdown +++ b/website/docs/r/compute_subnetwork.html.markdown @@ -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 @@ -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 +```