forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_iam_policy.go
166 lines (140 loc) · 4.32 KB
/
resource_iam_policy.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package google
import (
"github.com/hashicorp/terraform/helper/schema"
"encoding/json"
"errors"
"fmt"
"google.golang.org/api/cloudresourcemanager/v1"
)
var IamPolicyBaseSchema = map[string]*schema.Schema{
"policy_data": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: jsonPolicyDiffSuppress,
ValidateFunc: validateIamPolicy,
},
"etag": {
Type: schema.TypeString,
Computed: true,
},
}
func iamPolicyImport(resourceIdParser resourceIdParserFunc) schema.StateFunc {
return func(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
if resourceIdParser == nil {
return nil, errors.New("Import not supported for this IAM resource.")
}
config := m.(*Config)
err := resourceIdParser(d, config)
if err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}
}
func ResourceIamPolicy(parentSpecificSchema map[string]*schema.Schema, newUpdaterFunc newResourceIamUpdaterFunc) *schema.Resource {
return &schema.Resource{
Create: ResourceIamPolicyCreate(newUpdaterFunc),
Read: ResourceIamPolicyRead(newUpdaterFunc),
Update: ResourceIamPolicyUpdate(newUpdaterFunc),
Delete: ResourceIamPolicyDelete(newUpdaterFunc),
Schema: mergeSchemas(IamPolicyBaseSchema, parentSpecificSchema),
}
}
func ResourceIamPolicyWithImport(parentSpecificSchema map[string]*schema.Schema, newUpdaterFunc newResourceIamUpdaterFunc, resourceIdParser resourceIdParserFunc) *schema.Resource {
r := ResourceIamPolicy(parentSpecificSchema, newUpdaterFunc)
r.Importer = &schema.ResourceImporter{
State: iamPolicyImport(resourceIdParser),
}
return r
}
func ResourceIamPolicyCreate(newUpdaterFunc newResourceIamUpdaterFunc) schema.CreateFunc {
return func(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
updater, err := newUpdaterFunc(d, config)
if err != nil {
return err
}
if err := setIamPolicyData(d, updater); err != nil {
return err
}
d.SetId(updater.GetResourceId())
return ResourceIamPolicyRead(newUpdaterFunc)(d, meta)
}
}
func ResourceIamPolicyRead(newUpdaterFunc newResourceIamUpdaterFunc) schema.ReadFunc {
return func(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
updater, err := newUpdaterFunc(d, config)
if err != nil {
return err
}
policy, err := updater.GetResourceIamPolicy()
if err != nil {
return err
}
d.Set("etag", policy.Etag)
d.Set("policy_data", marshalIamPolicy(policy))
return nil
}
}
func ResourceIamPolicyUpdate(newUpdaterFunc newResourceIamUpdaterFunc) schema.UpdateFunc {
return func(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
updater, err := newUpdaterFunc(d, config)
if err != nil {
return err
}
if d.HasChange("policy_data") {
if err := setIamPolicyData(d, updater); err != nil {
return err
}
}
return ResourceIamPolicyRead(newUpdaterFunc)(d, meta)
}
}
func ResourceIamPolicyDelete(newUpdaterFunc newResourceIamUpdaterFunc) schema.DeleteFunc {
return func(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
updater, err := newUpdaterFunc(d, config)
if err != nil {
return err
}
// Set an empty policy to delete the attached policy.
err = updater.SetResourceIamPolicy(&cloudresourcemanager.Policy{})
if err != nil {
return err
}
return nil
}
}
func setIamPolicyData(d *schema.ResourceData, updater ResourceIamUpdater) error {
policy, err := unmarshalIamPolicy(d.Get("policy_data").(string))
if err != nil {
return fmt.Errorf("'policy_data' is not valid for %s: %s", updater.DescribeResource(), err)
}
err = updater.SetResourceIamPolicy(policy)
if err != nil {
return err
}
return nil
}
func marshalIamPolicy(policy *cloudresourcemanager.Policy) string {
pdBytes, _ := json.Marshal(&cloudresourcemanager.Policy{
Bindings: policy.Bindings,
})
return string(pdBytes)
}
func unmarshalIamPolicy(policyData string) (*cloudresourcemanager.Policy, error) {
policy := &cloudresourcemanager.Policy{}
if err := json.Unmarshal([]byte(policyData), policy); err != nil {
return nil, fmt.Errorf("Could not unmarshal policy data %s:\n%s", policyData, err)
}
return policy, nil
}
func validateIamPolicy(i interface{}, k string) (s []string, es []error) {
_, err := unmarshalIamPolicy(i.(string))
if err != nil {
es = append(es, err)
}
return
}