Skip to content

Commit

Permalink
Merge pull request #3 from tragiclifestories/kms-cryptokeys
Browse files Browse the repository at this point in the history
Inherit project, location etc from KeyRing in CryptoKey
  • Loading branch information
amfarrell authored Nov 7, 2017
2 parents 1378591 + c0a967f commit 900fe2e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 45 deletions.
65 changes: 27 additions & 38 deletions google/resource_kms_crypto_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,62 +24,53 @@ func resourceKmsCryptoKey() *schema.Resource {
Required: true,
ForceNew: true,
},
"location": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"key_ring": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}

type kmsCryptoKeyId struct {
Project string
Location string
KeyRing string
Name string
KeyRingId kmsKeyRingId
Name string
}

// TODO: Add the info about rotation frequency and start time.

func (s *kmsCryptoKeyId) cryptoKeyId() string {
return fmt.Sprintf("projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", s.Project, s.Location, s.KeyRing, s.Name)
return fmt.Sprintf("%s/cryptoKeys/%s", s.KeyRingId.keyRingId(), s.Name)
}

func (s *kmsCryptoKeyId) parentId() string {
return fmt.Sprintf("projects/%s/locations/%s/keyRings/%s", s.Project, s.Location, s.KeyRing)
return s.KeyRingId.keyRingId()
}

func (s *kmsCryptoKeyId) terraformId() string {
return fmt.Sprintf("%s/%s/%s/%s", s.Project, s.Location, s.KeyRing, s.Name)
return fmt.Sprintf("%s/%s", s.KeyRingId.terraformId(), s.Name)
}

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

project, err := getProject(d, config)
if err != nil {
return err
}

keyRingId, err := parseKmsKeyRingId(d.Get("key_ring").(string), config)

if err != nil {
return err
}

cryptoKeyId := &kmsCryptoKeyId{
Project: project,
Location: d.Get("location").(string),
KeyRing: d.Get("key_ring").(string),
Name: d.Get("name").(string),
KeyRingId: *keyRingId,
Name: d.Get("name").(string),
}

cryptoKey, err := config.clientKms.Projects.Locations.KeyRings.CryptoKeys.Create(cryptoKeyId.parentId(), &cloudkms.CryptoKey{Purpose: "ENCRYPT_DECRYPT"}).CryptoKeyId(cryptoKeyId.Name).Do()
cryptoKey, err := config.clientKms.Projects.Locations.KeyRings.CryptoKeys.Create(cryptoKeyId.KeyRingId.keyRingId(), &cloudkms.CryptoKey{Purpose: "ENCRYPT_DECRYPT"}).CryptoKeyId(cryptoKeyId.Name).Do()

if err != nil {
return fmt.Errorf("Error creating CryptoKey: %s", err)
Expand Down Expand Up @@ -167,10 +158,12 @@ func parseKmsCryptoKeyId(id string, config *Config) (*kmsCryptoKeyId, error) {

if cryptoKeyIdRegex.MatchString(id) {
return &kmsCryptoKeyId{
Project: parts[0],
Location: parts[1],
KeyRing: parts[2],
Name: parts[3],
KeyRingId: kmsKeyRingId{
Project: parts[0],
Location: parts[1],
Name: parts[2],
},
Name: parts[3],
}, nil
}

Expand All @@ -180,10 +173,12 @@ func parseKmsCryptoKeyId(id string, config *Config) (*kmsCryptoKeyId, error) {
}

return &kmsCryptoKeyId{
Project: config.Project,
Location: parts[0],
KeyRing: parts[1],
Name: parts[2],
KeyRingId: kmsKeyRingId{
Project: config.Project,
Location: parts[0],
Name: parts[1],
},
Name: parts[2],
}, nil
}

Expand All @@ -198,13 +193,7 @@ func resourceKmsCryptoKeyImportState(d *schema.ResourceData, meta interface{}) (
return nil, err
}

d.Set("name", cryptoKeyId.Name)
d.Set("location", cryptoKeyId.Location)
d.Set("key_ring", cryptoKeyId.KeyRing)

if config.Project != cryptoKeyId.Project {
d.Set("project", cryptoKeyId.Project)
}
d.Set("key_ring", cryptoKeyId.KeyRingId.keyRingId())

if d.Get("purpose") == "" {
d.Set("purpose", "ENCRYPT_DECRYPT")
Expand Down
16 changes: 9 additions & 7 deletions google/resource_kms_crypto_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,15 @@ func testAccCheckGoogleKmsCryptoKeyExists(resourceName string) resource.TestChec
return fmt.Errorf("Resource not found: %s", resourceName)
}

keyRingId, err := parseKmsKeyRingId(rs.Primary.Attributes["key_ring"], config)

if err != nil {
return err
}

cryptoKeyId := &kmsCryptoKeyId{
Project: rs.Primary.Attributes["project"],
Location: rs.Primary.Attributes["location"],
KeyRing: rs.Primary.Attributes["key_ring"],
Name: rs.Primary.Attributes["name"],
KeyRingId: *keyRingId,
Name: rs.Primary.Attributes["name"],
}

listCryptoKeysResponse, err := config.clientKms.Projects.Locations.KeyRings.CryptoKeys.List(cryptoKeyId.parentId()).Do()
Expand Down Expand Up @@ -204,10 +208,8 @@ resource "google_kms_key_ring" "key_ring" {
}
resource "google_kms_crypto_key" "crypto_key" {
project = "${google_project_services.acceptance.project}"
name = "%s"
location = "us-central1"
key_ring = "${google_kms_key_ring.key_ring.name}"
key_ring = "${google_kms_key_ring.key_ring.id}"
}
`, projectId, projectId, projectOrg, projectBillingAccount, keyRingName, cryptoKeyName)
}
Expand Down

0 comments on commit 900fe2e

Please sign in to comment.