forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 1
/
iam_kms_key_ring.go
112 lines (88 loc) · 2.99 KB
/
iam_kms_key_ring.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package google
import (
"fmt"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/cloudkms/v1"
"google.golang.org/api/cloudresourcemanager/v1"
)
var IamKmsKeyRingSchema = map[string]*schema.Schema{
"key_ring_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
}
type KmsKeyRingIamUpdater struct {
resourceId string
Config *Config
}
func NewKmsKeyRingIamUpdater(d *schema.ResourceData, config *Config) (ResourceIamUpdater, error) {
keyRing := d.Get("key_ring_id").(string)
keyRingId, err := parseKmsKeyRingId(keyRing, config)
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("Error parsing resource ID for for %s: {{err}}", keyRing), err)
}
return &KmsKeyRingIamUpdater{
resourceId: keyRingId.keyRingId(),
Config: config,
}, nil
}
func KeyRingIdParseFunc(d *schema.ResourceData, config *Config) error {
keyRingId, err := parseKmsKeyRingId(d.Id(), config)
if err != nil {
return err
}
d.Set("key_ring_id", keyRingId.keyRingId())
d.SetId(keyRingId.keyRingId())
return nil
}
func (u *KmsKeyRingIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) {
p, err := u.Config.clientKms.Projects.Locations.KeyRings.GetIamPolicy(u.resourceId).Do()
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err)
}
cloudResourcePolicy, err := kmsToResourceManagerPolicy(p)
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("Invalid IAM policy for %s: {{err}}", u.DescribeResource()), err)
}
return cloudResourcePolicy, nil
}
func (u *KmsKeyRingIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error {
kmsPolicy, err := resourceManagerToKmsPolicy(policy)
if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Invalid IAM policy for %s: {{err}}", u.DescribeResource()), err)
}
_, err = u.Config.clientKms.Projects.Locations.KeyRings.SetIamPolicy(u.resourceId, &cloudkms.SetIamPolicyRequest{
Policy: kmsPolicy,
}).Do()
if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err)
}
return nil
}
func (u *KmsKeyRingIamUpdater) GetResourceId() string {
return u.resourceId
}
func (u *KmsKeyRingIamUpdater) GetMutexKey() string {
return fmt.Sprintf("iam-kms-key-ring-%s", u.resourceId)
}
func (u *KmsKeyRingIamUpdater) DescribeResource() string {
return fmt.Sprintf("KMS KeyRing %q", u.resourceId)
}
func resourceManagerToKmsPolicy(p *cloudresourcemanager.Policy) (*cloudkms.Policy, error) {
out := &cloudkms.Policy{}
err := Convert(p, out)
if err != nil {
return nil, errwrap.Wrapf("Cannot convert a v1 policy to a kms policy: {{err}}", err)
}
return out, nil
}
func kmsToResourceManagerPolicy(p *cloudkms.Policy) (*cloudresourcemanager.Policy, error) {
out := &cloudresourcemanager.Policy{}
err := Convert(p, out)
if err != nil {
return nil, errwrap.Wrapf("Cannot convert a kms policy to a v1 policy: {{err}}", err)
}
return out, nil
}