diff --git a/google/resource_compute_shared_vpc_host_project.go b/google/resource_compute_shared_vpc_host_project.go index 31c5b066609..07f00857dad 100644 --- a/google/resource_compute_shared_vpc_host_project.go +++ b/google/resource_compute_shared_vpc_host_project.go @@ -12,6 +12,9 @@ func resourceComputeSharedVpcHostProject() *schema.Resource { Create: resourceComputeSharedVpcHostProjectCreate, Read: resourceComputeSharedVpcHostProjectRead, Delete: resourceComputeSharedVpcHostProjectDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: map[string]*schema.Schema{ "project": &schema.Schema{ @@ -46,7 +49,7 @@ func resourceComputeSharedVpcHostProjectCreate(d *schema.ResourceData, meta inte func resourceComputeSharedVpcHostProjectRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - hostProject := d.Get("project").(string) + hostProject := d.Id() project, err := config.clientCompute.Projects.Get(hostProject).Do() if err != nil { @@ -58,6 +61,8 @@ func resourceComputeSharedVpcHostProjectRead(d *schema.ResourceData, meta interf d.SetId("") } + d.Set("project", hostProject) + return nil } diff --git a/google/resource_compute_shared_vpc_service_project.go b/google/resource_compute_shared_vpc_service_project.go index 10d6e640cd1..8ddba80e8e5 100644 --- a/google/resource_compute_shared_vpc_service_project.go +++ b/google/resource_compute_shared_vpc_service_project.go @@ -2,12 +2,14 @@ package google import ( "fmt" + "strings" "google.golang.org/api/compute/v1" + "log" + "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/googleapi" - "log" ) func resourceComputeSharedVpcServiceProject() *schema.Resource { @@ -15,6 +17,9 @@ func resourceComputeSharedVpcServiceProject() *schema.Resource { Create: resourceComputeSharedVpcServiceProjectCreate, Read: resourceComputeSharedVpcServiceProjectRead, Delete: resourceComputeSharedVpcServiceProjectDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: map[string]*schema.Schema{ "host_project": &schema.Schema{ @@ -59,8 +64,12 @@ func resourceComputeSharedVpcServiceProjectCreate(d *schema.ResourceData, meta i func resourceComputeSharedVpcServiceProjectRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - hostProject := d.Get("host_project").(string) - serviceProject := d.Get("service_project").(string) + split := strings.Split(d.Id(), "/") + if len(split) != 2 { + return fmt.Errorf("Error parsing resource ID %s", d.Id()) + } + hostProject := split[0] + serviceProject := split[1] associatedHostProject, err := config.clientCompute.Projects.GetXpnHost(serviceProject).Do() if err != nil { @@ -76,6 +85,9 @@ func resourceComputeSharedVpcServiceProjectRead(d *schema.ResourceData, meta int return nil } + d.Set("host_project", hostProject) + d.Set("service_project", serviceProject) + return nil } diff --git a/google/resource_compute_shared_vpc_test.go b/google/resource_compute_shared_vpc_test.go index 7839c6ab01d..4937fda8c50 100644 --- a/google/resource_compute_shared_vpc_test.go +++ b/google/resource_compute_shared_vpc_test.go @@ -16,6 +16,9 @@ func TestAccComputeSharedVpc_basic(t *testing.T) { hostProject := "xpn-host-" + acctest.RandString(10) serviceProject := "xpn-service-" + acctest.RandString(10) + hostProjectResourceName := "google_compute_shared_vpc_host_project.host" + serviceProjectResourceName := "google_compute_shared_vpc_service_project.service" + resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -27,6 +30,17 @@ func TestAccComputeSharedVpc_basic(t *testing.T) { testAccCheckComputeSharedVpcServiceProject(hostProject, serviceProject, true), ), }, + // Test import. + resource.TestStep{ + ResourceName: hostProjectResourceName, + ImportState: true, + ImportStateVerify: true, + }, + resource.TestStep{ + ResourceName: serviceProjectResourceName, + ImportState: true, + ImportStateVerify: true, + }, // Use a separate TestStep rather than a CheckDestroy because we need the project to still exist. resource.TestStep{ Config: testAccComputeSharedVpc_disabled(hostProject, serviceProject, org, billingId), diff --git a/website/docs/r/compute_shared_vpc_host_project.html.markdown b/website/docs/r/compute_shared_vpc_host_project.html.markdown index 5b011677b74..cdec57c2cb5 100644 --- a/website/docs/r/compute_shared_vpc_host_project.html.markdown +++ b/website/docs/r/compute_shared_vpc_host_project.html.markdown @@ -42,3 +42,10 @@ The following arguments are expected: * `project` - (Required) The ID of the project that will serve as a Shared VPC host project +## Import + +Google Compute Engine Shared VPC host project feature can be imported using the `project`, e.g. + +``` +$ terraform import google_compute_shared_vpc_host_project.host host-project-id +``` diff --git a/website/docs/r/compute_shared_vpc_service_project.html.markdown b/website/docs/r/compute_shared_vpc_service_project.html.markdown index 76dcd42beac..6744f3dd9b0 100644 --- a/website/docs/r/compute_shared_vpc_service_project.html.markdown +++ b/website/docs/r/compute_shared_vpc_service_project.html.markdown @@ -36,3 +36,11 @@ The following arguments are expected: * `host_project` - (Required) The ID of a host project to associate. * `service_project` - (Required) The ID of the project that will serve as a Shared VPC service project. + +## Import + +Google Compute Engine Shared VPC service project feature can be imported using the `host_project` and `service_project`, e.g. + +``` +$ terraform import google_compute_shared_vpc_service_project.service1 host-project-id/service-project-id-1 +```