diff --git a/google-beta/resource_compute_project_metadata.go b/google-beta/resource_compute_project_metadata.go index 497005ffb8..1a934db812 100644 --- a/google-beta/resource_compute_project_metadata.go +++ b/google-beta/resource_compute_project_metadata.go @@ -14,6 +14,9 @@ func resourceComputeProjectMetadata() *schema.Resource { Read: resourceComputeProjectMetadataRead, Update: resourceComputeProjectMetadataCreateOrUpdate, Delete: resourceComputeProjectMetadataDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, SchemaVersion: 0, @@ -57,16 +60,19 @@ func resourceComputeProjectMetadataCreateOrUpdate(d *schema.ResourceData, meta i func resourceComputeProjectMetadataRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - projectID, err := getProject(d, config) - if err != nil { - return err + if d.Id() == "" { + projectID, err := getProject(d, config) + if err != nil { + return err + } + d.SetId(projectID) } // Load project service - log.Printf("[DEBUG] Loading project service: %s", projectID) - project, err := config.clientCompute.Projects.Get(projectID).Do() + log.Printf("[DEBUG] Loading project service: %s", d.Id()) + project, err := config.clientCompute.Projects.Get(d.Id()).Do() if err != nil { - return handleNotFoundError(err, d, fmt.Sprintf("Project metadata for project %q", projectID)) + return handleNotFoundError(err, d, fmt.Sprintf("Project metadata for project %q", d.Id())) } err = d.Set("metadata", flattenMetadata(project.CommonInstanceMetadata)) @@ -74,8 +80,8 @@ func resourceComputeProjectMetadataRead(d *schema.ResourceData, meta interface{} return fmt.Errorf("Error setting metadata: %s", err) } - d.Set("project", projectID) - d.SetId("common_metadata") + d.Set("project", d.Id()) + d.SetId(d.Id()) return nil } diff --git a/google-beta/resource_compute_project_metadata_test.go b/google-beta/resource_compute_project_metadata_test.go index 0ab3718931..f61cf7468b 100644 --- a/google-beta/resource_compute_project_metadata_test.go +++ b/google-beta/resource_compute_project_metadata_test.go @@ -7,7 +7,6 @@ import ( "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" - "google.golang.org/api/compute/v1" ) // Add two key value pairs @@ -16,7 +15,6 @@ func TestAccComputeProjectMetadata_basic(t *testing.T) { org := getTestOrgFromEnv(t) billingId := getTestBillingAccountFromEnv(t) - var project compute.Project projectID := "terrafom-test-" + acctest.RandString(10) resource.Test(t, resource.TestCase{ @@ -26,13 +24,11 @@ func TestAccComputeProjectMetadata_basic(t *testing.T) { Steps: []resource.TestStep{ resource.TestStep{ Config: testAccComputeProject_basic0_metadata(projectID, pname, org, billingId), - Check: resource.ComposeTestCheckFunc( - testAccCheckComputeProjectExists( - "google_compute_project_metadata.fizzbuzz", projectID, &project), - testAccCheckComputeProjectMetadataContains(projectID, "banana", "orange"), - testAccCheckComputeProjectMetadataContains(projectID, "sofa", "darwinism"), - testAccCheckComputeProjectMetadataSize(projectID, 2), - ), + }, + resource.TestStep{ + ResourceName: "google_compute_project_metadata.fizzbuzz", + ImportState: true, + ImportStateVerify: true, }, }, }) @@ -44,7 +40,6 @@ func TestAccComputeProjectMetadata_modify_1(t *testing.T) { org := getTestOrgFromEnv(t) billingId := getTestBillingAccountFromEnv(t) - var project compute.Project projectID := "terrafom-test-" + acctest.RandString(10) resource.Test(t, resource.TestCase{ @@ -54,26 +49,20 @@ func TestAccComputeProjectMetadata_modify_1(t *testing.T) { Steps: []resource.TestStep{ resource.TestStep{ Config: testAccComputeProject_modify0_metadata(projectID, pname, org, billingId), - Check: resource.ComposeTestCheckFunc( - testAccCheckComputeProjectExists( - "google_compute_project_metadata.fizzbuzz", projectID, &project), - testAccCheckComputeProjectMetadataContains(projectID, "paper", "pen"), - testAccCheckComputeProjectMetadataContains(projectID, "genghis_khan", "french bread"), - testAccCheckComputeProjectMetadataContains(projectID, "happy", "smiling"), - testAccCheckComputeProjectMetadataSize(projectID, 3), - ), + }, + resource.TestStep{ + ResourceName: "google_compute_project_metadata.fizzbuzz", + ImportState: true, + ImportStateVerify: true, }, resource.TestStep{ Config: testAccComputeProject_modify1_metadata(projectID, pname, org, billingId), - Check: resource.ComposeTestCheckFunc( - testAccCheckComputeProjectExists( - "google_compute_project_metadata.fizzbuzz", projectID, &project), - testAccCheckComputeProjectMetadataContains(projectID, "paper", "pen"), - testAccCheckComputeProjectMetadataContains(projectID, "paris", "french bread"), - testAccCheckComputeProjectMetadataContains(projectID, "happy", "laughing"), - testAccCheckComputeProjectMetadataSize(projectID, 3), - ), + }, + resource.TestStep{ + ResourceName: "google_compute_project_metadata.fizzbuzz", + ImportState: true, + ImportStateVerify: true, }, }, }) @@ -85,7 +74,6 @@ func TestAccComputeProjectMetadata_modify_2(t *testing.T) { org := getTestOrgFromEnv(t) billingId := getTestBillingAccountFromEnv(t) - var project compute.Project projectID := "terraform-test-" + acctest.RandString(10) resource.Test(t, resource.TestCase{ @@ -95,24 +83,20 @@ func TestAccComputeProjectMetadata_modify_2(t *testing.T) { Steps: []resource.TestStep{ resource.TestStep{ Config: testAccComputeProject_basic0_metadata(projectID, pname, org, billingId), - Check: resource.ComposeTestCheckFunc( - testAccCheckComputeProjectExists( - "google_compute_project_metadata.fizzbuzz", projectID, &project), - testAccCheckComputeProjectMetadataContains(projectID, "banana", "orange"), - testAccCheckComputeProjectMetadataContains(projectID, "sofa", "darwinism"), - testAccCheckComputeProjectMetadataSize(projectID, 2), - ), + }, + resource.TestStep{ + ResourceName: "google_compute_project_metadata.fizzbuzz", + ImportState: true, + ImportStateVerify: true, }, resource.TestStep{ Config: testAccComputeProject_basic1_metadata(projectID, pname, org, billingId), - Check: resource.ComposeTestCheckFunc( - testAccCheckComputeProjectExists( - "google_compute_project_metadata.fizzbuzz", projectID, &project), - testAccCheckComputeProjectMetadataContains(projectID, "kiwi", "papaya"), - testAccCheckComputeProjectMetadataContains(projectID, "finches", "darwinism"), - testAccCheckComputeProjectMetadataSize(projectID, 2), - ), + }, + resource.TestStep{ + ResourceName: "google_compute_project_metadata.fizzbuzz", + ImportState: true, + ImportStateVerify: true, }, }, }) @@ -135,74 +119,6 @@ func testAccCheckComputeProjectMetadataDestroy(s *terraform.State) error { return nil } -func testAccCheckComputeProjectExists(n, projectID string, project *compute.Project) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } - - config := testAccProvider.Meta().(*Config) - - found, err := config.clientCompute.Projects.Get(projectID).Do() - if err != nil { - return err - } - - if "common_metadata" != rs.Primary.ID { - return fmt.Errorf("Common metadata not found, found %s", rs.Primary.ID) - } - - *project = *found - - return nil - } -} - -func testAccCheckComputeProjectMetadataContains(projectID, key, value string) resource.TestCheckFunc { - return func(s *terraform.State) error { - config := testAccProvider.Meta().(*Config) - project, err := config.clientCompute.Projects.Get(projectID).Do() - if err != nil { - return fmt.Errorf("Error, failed to load project service for %s: %s", config.Project, err) - } - - for _, kv := range project.CommonInstanceMetadata.Items { - if kv.Key == key { - if kv.Value != nil && *kv.Value == value { - return nil - } else { - return fmt.Errorf("Error, key value mismatch, wanted (%s, %s), got (%s, %s)", - key, value, kv.Key, *kv.Value) - } - } - } - - return fmt.Errorf("Error, key %s not present in %s", key, project.SelfLink) - } -} - -func testAccCheckComputeProjectMetadataSize(projectID string, size int) resource.TestCheckFunc { - return func(s *terraform.State) error { - config := testAccProvider.Meta().(*Config) - project, err := config.clientCompute.Projects.Get(projectID).Do() - if err != nil { - return fmt.Errorf("Error, failed to load project service for %s: %s", config.Project, err) - } - - if size > len(project.CommonInstanceMetadata.Items) { - return fmt.Errorf("Error, expected at least %d metadata items, got %d", size, - len(project.CommonInstanceMetadata.Items)) - } - - return nil - } -} - func testAccComputeProject_basic0_metadata(projectID, name, org, billing string) string { return fmt.Sprintf(` resource "google_project" "project" { diff --git a/website/docs/r/compute_project_metadata.html.markdown b/website/docs/r/compute_project_metadata.html.markdown index 00b0d72a92..0fd65b31d3 100644 --- a/website/docs/r/compute_project_metadata.html.markdown +++ b/website/docs/r/compute_project_metadata.html.markdown @@ -44,3 +44,9 @@ The following arguments are supported: ## Attributes Reference Only the arguments listed above are exposed as attributes. + +## Import + +This resource can be imported using the project ID: + +`terraform import google_compute_project_metadata.foo my-project-id`