forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_logging_bucket_config.go
375 lines (320 loc) · 12.9 KB
/
resource_logging_bucket_config.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package google
import (
"fmt"
"log"
"regexp"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
var loggingBucketConfigSchema = map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The resource name of the bucket`,
},
"location": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The location of the bucket.`,
},
"bucket_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The name of the logging bucket. Logging automatically creates two log buckets: _Required and _Default.`,
},
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `An optional description for this bucket.`,
},
"retention_days": {
Type: schema.TypeInt,
Optional: true,
Default: 30,
Description: `Logs will be retained by default for this amount of time, after which they will automatically be deleted. The minimum retention period is 1 day. If this value is set to zero at bucket creation time, the default time of 30 days will be used.`,
},
"lifecycle_state": {
Type: schema.TypeString,
Computed: true,
Description: `The bucket's lifecycle such as active or deleted.`,
},
"cmek_settings": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: `The CMEK settings of the log bucket. If present, new log entries written to this log bucket are encrypted using the CMEK key provided in this configuration. If a log bucket has CMEK settings, the CMEK settings cannot be disabled later by updating the log bucket. Changing the KMS key is allowed.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The resource name of the CMEK settings.`,
},
"kms_key_name": {
Type: schema.TypeString,
Required: true,
Description: `The resource name for the configured Cloud KMS key.
KMS key name format:
"projects/[PROJECT_ID]/locations/[LOCATION]/keyRings/[KEYRING]/cryptoKeys/[KEY]"
To enable CMEK for the bucket, set this field to a valid kmsKeyName for which the associated service account has the required cloudkms.cryptoKeyEncrypterDecrypter roles assigned for the key.
The Cloud KMS key used by the bucket can be updated by changing the kmsKeyName to a new valid key name. Encryption operations that are in progress will be completed with the key that was in use when they started. Decryption operations will be completed using the key that was used at the time of encryption unless access to that key has been revoked.
See [Enabling CMEK for Logging Buckets](https://cloud.google.com/logging/docs/routing/managed-encryption-storage) for more information.`,
},
"kms_key_version_name": {
Type: schema.TypeString,
Computed: true,
Description: `The CryptoKeyVersion resource name for the configured Cloud KMS key.
KMS key name format:
"projects/[PROJECT_ID]/locations/[LOCATION]/keyRings/[KEYRING]/cryptoKeys/[KEY]/cryptoKeyVersions/[VERSION]"
For example:
"projects/my-project/locations/us-central1/keyRings/my-ring/cryptoKeys/my-key/cryptoKeyVersions/1"
This is a read-only field used to convey the specific configured CryptoKeyVersion of kms_key that has been configured. It will be populated in cases where the CMEK settings are bound to a single key version.`,
},
"service_account_id": {
Type: schema.TypeString,
Computed: true,
Description: `The service account associated with a project for which CMEK will apply.
Before enabling CMEK for a logging bucket, you must first assign the cloudkms.cryptoKeyEncrypterDecrypter role to the service account associated with the project for which CMEK will apply. Use [v2.getCmekSettings](https://cloud.google.com/logging/docs/reference/v2/rest/v2/TopLevel/getCmekSettings#google.logging.v2.ConfigServiceV2.GetCmekSettings) to obtain the service account ID.
See [Enabling CMEK for Logging Buckets](https://cloud.google.com/logging/docs/routing/managed-encryption-storage) for more information.`,
},
},
},
},
}
type loggingBucketConfigIDFunc func(d *schema.ResourceData, config *Config) (string, error)
// ResourceLoggingBucketConfig creates a resource definition by merging a unique field (eg: folder) to a generic logging bucket
// config resource. In practice the only difference between these resources is the url location.
func ResourceLoggingBucketConfig(parentType string, parentSpecificSchema map[string]*schema.Schema, iDFunc loggingBucketConfigIDFunc) *schema.Resource {
return &schema.Resource{
Create: resourceLoggingBucketConfigAcquireOrCreate(parentType, iDFunc),
Read: resourceLoggingBucketConfigRead,
Update: resourceLoggingBucketConfigUpdate,
Delete: resourceLoggingBucketConfigDelete,
Importer: &schema.ResourceImporter{
State: resourceLoggingBucketConfigImportState(parentType),
},
Schema: mergeSchemas(loggingBucketConfigSchema, parentSpecificSchema),
UseJSONNumber: true,
}
}
var loggingBucketConfigIDRegex = regexp.MustCompile("(.+)/(.+)/locations/(.+)/buckets/(.+)")
func resourceLoggingBucketConfigImportState(parent string) schema.StateFunc {
return func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := loggingBucketConfigIDRegex.FindStringSubmatch(d.Id())
if parts == nil {
return nil, fmt.Errorf("unable to parse logging sink id %#v", d.Id())
}
if len(parts) != 5 {
return nil, fmt.Errorf("Invalid id format. Format should be '{{parent}}/{{parent_id}}/locations/{{location}}/buckets/{{bucket_id}} with parent in %s", loggingSinkResourceTypes)
}
validLoggingType := false
for _, v := range loggingSinkResourceTypes {
if v == parts[1] {
validLoggingType = true
break
}
}
if !validLoggingType {
return nil, fmt.Errorf("Logging parent type %s is not valid. Valid resource types: %#v", parts[1],
loggingSinkResourceTypes)
}
if err := d.Set(parent, parts[1]+"/"+parts[2]); err != nil {
return nil, fmt.Errorf("Error setting parent: %s", err)
}
if err := d.Set("location", parts[3]); err != nil {
return nil, fmt.Errorf("Error setting location: %s", err)
}
if err := d.Set("bucket_id", parts[4]); err != nil {
return nil, fmt.Errorf("Error setting bucket_id: %s", err)
}
return []*schema.ResourceData{d}, nil
}
}
func resourceLoggingBucketConfigAcquireOrCreate(parentType string, iDFunc loggingBucketConfigIDFunc) func(*schema.ResourceData, interface{}) error {
return func(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
id, err := iDFunc(d, config)
if err != nil {
return err
}
if parentType == "project" {
//logging bucket can be created only at the project level, in future api may allow for folder, org and other parent resources
log.Printf("[DEBUG] Fetching logging bucket config: %#v", id)
url, err := replaceVars(d, config, fmt.Sprintf("{{LoggingBasePath}}%s", id))
if err != nil {
return err
}
res, _ := sendRequest(config, "GET", "", url, userAgent, nil)
if res == nil {
log.Printf("[DEGUG] Loggin Bucket not exist %s", id)
// we need to pass the id in here because we don't want to set it in state
// until we know there won't be any errors on create
return resourceLoggingBucketConfigCreate(d, meta, id)
}
}
d.SetId(id)
return resourceLoggingBucketConfigUpdate(d, meta)
}
}
func resourceLoggingBucketConfigCreate(d *schema.ResourceData, meta interface{}, id string) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
obj := make(map[string]interface{})
obj["name"] = d.Get("name")
obj["description"] = d.Get("description")
obj["retentionDays"] = d.Get("retention_days")
obj["locked"] = d.Get("locked")
obj["cmekSettings"] = expandCmekSettings(d.Get("cmek_settings"))
url, err := replaceVars(d, config, "{{LoggingBasePath}}projects/{{project}}/locations/{{location}}/buckets?bucketId={{bucket_id}}")
if err != nil {
return err
}
log.Printf("[DEBUG] Creating new Bucket: %#v", obj)
billingProject := ""
project, err := getProject(d, config)
if err != nil {
return err
}
billingProject = project
// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}
res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate))
if err != nil {
return fmt.Errorf("Error creating Bucket: %s", err)
}
d.SetId(id)
log.Printf("[DEBUG] Finished creating Bucket %q: %#v", d.Id(), res)
return resourceLoggingBucketConfigRead(d, meta)
}
func resourceLoggingBucketConfigRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
log.Printf("[DEBUG] Fetching logging bucket config: %#v", d.Id())
url, err := replaceVars(d, config, fmt.Sprintf("{{LoggingBasePath}}%s", d.Id()))
if err != nil {
return err
}
res, err := sendRequest(config, "GET", "", url, userAgent, nil)
if err != nil {
log.Printf("[WARN] Unable to acquire logging bucket config at %s", d.Id())
d.SetId("")
return err
}
if err := d.Set("name", res["name"]); err != nil {
return fmt.Errorf("Error setting name: %s", err)
}
if err := d.Set("description", res["description"]); err != nil {
return fmt.Errorf("Error setting description: %s", err)
}
if err := d.Set("lifecycle_state", res["lifecycleState"]); err != nil {
return fmt.Errorf("Error setting lifecycle_state: %s", err)
}
if err := d.Set("retention_days", res["retentionDays"]); err != nil {
return fmt.Errorf("Error setting retention_days: %s", err)
}
if err := d.Set("cmek_settings", flattenCmekSettings(res["cmekSettings"])); err != nil {
return fmt.Errorf("Error setting cmek_settings: %s", err)
}
return nil
}
func resourceLoggingBucketConfigUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
obj := make(map[string]interface{})
url, err := replaceVars(d, config, fmt.Sprintf("{{LoggingBasePath}}%s", d.Id()))
if err != nil {
return err
}
obj["retentionDays"] = d.Get("retention_days")
obj["description"] = d.Get("description")
obj["cmekSettings"] = expandCmekSettings(d.Get("cmek_settings"))
updateMask := []string{}
if d.HasChange("retention_days") {
updateMask = append(updateMask, "retentionDays")
}
if d.HasChange("description") {
updateMask = append(updateMask, "description")
}
if d.HasChange("cmek_settings") {
updateMask = append(updateMask, "cmekSettings")
}
url, err = addQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
if err != nil {
return err
}
_, err = sendRequestWithTimeout(config, "PATCH", "", url, userAgent, obj, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return fmt.Errorf("Error updating Logging Bucket Config %q: %s", d.Id(), err)
}
return resourceLoggingBucketConfigRead(d, meta)
}
func resourceLoggingBucketConfigDelete(d *schema.ResourceData, meta interface{}) error {
name := d.Get("bucket_id")
for _, restrictedName := range []string{"_Required", "_Default"} {
if name == restrictedName {
log.Printf("[WARN] Default logging bucket configs cannot be deleted. Removing logging bucket config from state: %#v", d.Id())
return nil
}
}
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
url, err := replaceVars(d, config, fmt.Sprintf("{{LoggingBasePath}}%s", d.Id()))
if err != nil {
return err
}
if _, err := sendRequestWithTimeout(config, "DELETE", "", url, userAgent, nil, d.Timeout(schema.TimeoutUpdate)); err != nil {
return fmt.Errorf("Error deleting Logging Bucket Config %q: %s", d.Id(), err)
}
return nil
}
func expandCmekSettings(v interface{}) interface{} {
if v == nil {
return nil
}
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil
}
original := l[0].(map[string]interface{})
transformed := map[string]interface{}{
"name": original["name"],
"kmsKeyName": original["kms_key_name"],
"kmsKeyVersionName": original["kms_key_version_name"],
"serviceAccountId": original["service_account_id"],
}
return transformed
}
func flattenCmekSettings(cmekSettings interface{}) []map[string]interface{} {
if cmekSettings == nil {
return nil
}
cmekSettingsData := cmekSettings.(map[string]interface{})
data := map[string]interface{}{
"name": cmekSettingsData["name"],
"kms_key_name": cmekSettingsData["kmsKeyName"],
"kms_key_version_name": cmekSettingsData["kmsKeyVersionName"],
"service_account_id": cmekSettingsData["serviceAccountId"],
}
return []map[string]interface{}{data}
}