Skip to content

Commit

Permalink
Add import support to project metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
nat-henderson authored and modular-magician committed Nov 7, 2018
1 parent 8a921f6 commit 89d07b2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 117 deletions.
22 changes: 14 additions & 8 deletions google-beta/resource_compute_project_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ func resourceComputeProjectMetadata() *schema.Resource {
Read: resourceComputeProjectMetadataRead,
Update: resourceComputeProjectMetadataCreateOrUpdate,
Delete: resourceComputeProjectMetadataDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

SchemaVersion: 0,

Expand Down Expand Up @@ -57,25 +60,28 @@ 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))
if err != nil {
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
}

Expand Down
134 changes: 25 additions & 109 deletions google-beta/resource_compute_project_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{
Expand All @@ -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,
},
},
})
Expand All @@ -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{
Expand All @@ -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,
},
},
})
Expand All @@ -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{
Expand All @@ -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,
},
},
})
Expand All @@ -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" {
Expand Down
6 changes: 6 additions & 0 deletions website/docs/r/compute_project_metadata.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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`

0 comments on commit 89d07b2

Please sign in to comment.