Skip to content

Commit

Permalink
Create acceptance test for creating a KeyRing, fix read to use KeyRin…
Browse files Browse the repository at this point in the history
…g ID
  • Loading branch information
mrparkers committed Sep 29, 2017
1 parent 792a096 commit ca2e386
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 6 deletions.
21 changes: 15 additions & 6 deletions google/resource_kms_key_ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ func resourceKmsKeyRing() *schema.Resource {
}
}

func createKmsResourceParentString(project, location string) string {
return fmt.Sprintf("projects/%s/locations/%s", project, location)
}

func createKmsResourceKeyRingName(project, location, keyRingName string) string {
return fmt.Sprintf("%s/keyRings/%s", createKmsResourceParentString(project, location), keyRingName)
}

func resourceKmsKeyRingCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

Expand All @@ -39,7 +47,7 @@ func resourceKmsKeyRingCreate(d *schema.ResourceData, meta interface{}) error {
name := d.Get("name").(string)
location := d.Get("location").(string)

parent := fmt.Sprintf("projects/%s/locations/%s", project, location)
parent := createKmsResourceParentString(project, location)

_, err = config.clientKms.Projects.Locations.KeyRings.
Create(parent, &cloudkms.KeyRing{}).
Expand All @@ -50,21 +58,22 @@ func resourceKmsKeyRingCreate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error creating KeyRing: %s", err)
}

d.SetId(name)
d.Set("location", location)
keyRingName := createKmsResourceKeyRingName(project, location, name)

d.SetId(keyRingName)

return resourceKmsKeyRingRead(d, meta)
}

func resourceKmsKeyRingRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

name := d.Id()
keyRingName := d.Id()

log.Printf("[DEBUG] Executing read for KMS KeyRing %s", name)
log.Printf("[DEBUG] Executing read for KMS KeyRing %s", keyRingName)

keyRing, err := config.clientKms.Projects.Locations.KeyRings.
Get(name).
Get(keyRingName).
Do()

if err != nil {
Expand Down
78 changes: 78 additions & 0 deletions google/resource_kms_key_ring_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package google

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"log"
)

func TestAccGoogleKmsKeyRing_basic(t *testing.T) {
name := acctest.RandString(10)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testGoogleKmsKeyRing_recreate(name),
Steps: []resource.TestStep{
resource.TestStep{
Config: testGoogleKmsKeyRing_basic(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleKmsKeyRingExists("google_kms_key_ring.key_ring"),
),
},
},
})
}

func testGoogleKmsKeyRing_basic(name string) string {
return fmt.Sprintf(`
resource "google_kms_key_ring" "key_ring" {
name = "%s"
location = "us-central1"
}
`, name)
}

func testAccCheckGoogleKmsKeyRingExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)

rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Resource not found: %s", resourceName)
}

name := rs.Primary.Attributes["name"]
location := rs.Primary.Attributes["location"]

parent := createKmsResourceParentString(config.Project, location)
keyRingName := createKmsResourceKeyRingName(config.Project, location, name)

listKeyRingsResponse, err := config.clientKms.Projects.Locations.KeyRings.List(parent).Do()
if err != nil {
return fmt.Errorf("Error listing KeyRings: %s", err)
}

for _, keyRing := range listKeyRingsResponse.KeyRings {
log.Printf("[DEBUG] Found KeyRing: %s", keyRing.Name)

if keyRing.Name == keyRingName {
return nil
}
}

return fmt.Errorf("KeyRing not found: %s", keyRingName)
}
}

// TODO
// KMS KeyRings cannot be deleted. This will test if the resource can be added back to state after being removed
func testGoogleKmsKeyRing_recreate(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
return nil
}
}

0 comments on commit ca2e386

Please sign in to comment.