mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-07 01:26:43 +00:00
![James Turley](/assets/img/avatar_default.png)
* Add IAM bindings and member resources for KMS KeyRings * Add IAM bindings and member resources for KMS CryptoKeys * Docs for key ring and crypto key IAM resources * Exctract KMS policy conversions to helper functions * Split iam_binding and iam_member tests for KMS * Docs for kms IAM member resources * Run KMS IAM tests in own project
82 lines
2.1 KiB
Go
82 lines
2.1 KiB
Go
package google
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
"google.golang.org/api/cloudkms/v1"
|
|
"google.golang.org/api/cloudresourcemanager/v1"
|
|
)
|
|
|
|
var IamKmsCryptoKeySchema = map[string]*schema.Schema{
|
|
"crypto_key_id": {
|
|
Type: schema.TypeString,
|
|
Required: true,
|
|
ForceNew: true,
|
|
},
|
|
}
|
|
|
|
type KmsCryptoKeyIamUpdater struct {
|
|
resourceId string
|
|
Config *Config
|
|
}
|
|
|
|
func NewKmsCryptoKeyIamUpdater(d *schema.ResourceData, config *Config) (ResourceIamUpdater, error) {
|
|
cryptoKey := d.Get("crypto_key_id").(string)
|
|
cryptoKeyId, err := parseKmsCryptoKeyId(cryptoKey, config)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Error parsing resource ID for for %s: %s", cryptoKey, err)
|
|
}
|
|
|
|
return &KmsCryptoKeyIamUpdater{
|
|
resourceId: cryptoKeyId.cryptoKeyId(),
|
|
Config: config,
|
|
}, nil
|
|
}
|
|
|
|
func (u *KmsCryptoKeyIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) {
|
|
p, err := u.Config.clientKms.Projects.Locations.KeyRings.CryptoKeys.GetIamPolicy(u.resourceId).Do()
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Error retrieving IAM policy for %s: %s", u.DescribeResource(), err)
|
|
}
|
|
|
|
cloudResourcePolicy, err := kmsToResourceManagerPolicy(p)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Invalid IAM policy for %s: %s", u.DescribeResource(), err)
|
|
}
|
|
|
|
return cloudResourcePolicy, nil
|
|
}
|
|
|
|
func (u *KmsCryptoKeyIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error {
|
|
kmsPolicy, err := resourceManagerToKmsPolicy(policy)
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("Invalid IAM policy for %s: %s", u.DescribeResource(), err)
|
|
}
|
|
|
|
_, err = u.Config.clientKms.Projects.Locations.KeyRings.CryptoKeys.SetIamPolicy(u.resourceId, &cloudkms.SetIamPolicyRequest{
|
|
Policy: kmsPolicy,
|
|
}).Do()
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("Error setting IAM policy for %s: %s", u.DescribeResource(), err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (u *KmsCryptoKeyIamUpdater) GetResourceId() string {
|
|
return u.resourceId
|
|
}
|
|
|
|
func (u *KmsCryptoKeyIamUpdater) GetMutexKey() string {
|
|
return fmt.Sprintf("iam-kms-crypto-key-%s", u.resourceId)
|
|
}
|
|
|
|
func (u *KmsCryptoKeyIamUpdater) DescribeResource() string {
|
|
return fmt.Sprintf("KMS CryptoKey %q", u.resourceId)
|
|
}
|