@@ -18,6 +18,7 @@ import (
18
18
"fmt"
19
19
"log"
20
20
"reflect"
21
+ "strings"
21
22
"time"
22
23
23
24
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -27,6 +28,7 @@ func resourceCloudIdsEndpoint() *schema.Resource {
27
28
return & schema.Resource {
28
29
Create : resourceCloudIdsEndpointCreate ,
29
30
Read : resourceCloudIdsEndpointRead ,
31
+ Update : resourceCloudIdsEndpointUpdate ,
30
32
Delete : resourceCloudIdsEndpointDelete ,
31
33
32
34
Importer : & schema.ResourceImporter {
@@ -35,6 +37,7 @@ func resourceCloudIdsEndpoint() *schema.Resource {
35
37
36
38
Timeouts : & schema.ResourceTimeout {
37
39
Create : schema .DefaultTimeout (20 * time .Minute ),
40
+ Update : schema .DefaultTimeout (20 * time .Minute ),
38
41
Delete : schema .DefaultTimeout (20 * time .Minute ),
39
42
},
40
43
@@ -70,6 +73,14 @@ func resourceCloudIdsEndpoint() *schema.Resource {
70
73
ForceNew : true ,
71
74
Description : `An optional description of the endpoint.` ,
72
75
},
76
+ "threat_exceptions" : {
77
+ Type : schema .TypeList ,
78
+ Optional : true ,
79
+ Description : `Configuration for threat IDs excluded from generating alerts. Limit: 99 IDs.` ,
80
+ Elem : & schema.Schema {
81
+ Type : schema .TypeString ,
82
+ },
83
+ },
73
84
"create_time" : {
74
85
Type : schema .TypeString ,
75
86
Computed : true ,
@@ -133,6 +144,12 @@ func resourceCloudIdsEndpointCreate(d *schema.ResourceData, meta interface{}) er
133
144
} else if v , ok := d .GetOkExists ("severity" ); ! isEmptyValue (reflect .ValueOf (severityProp )) && (ok || ! reflect .DeepEqual (v , severityProp )) {
134
145
obj ["severity" ] = severityProp
135
146
}
147
+ threatExceptionsProp , err := expandCloudIdsEndpointThreatExceptions (d .Get ("threat_exceptions" ), d , config )
148
+ if err != nil {
149
+ return err
150
+ } else if v , ok := d .GetOkExists ("threat_exceptions" ); ! isEmptyValue (reflect .ValueOf (threatExceptionsProp )) && (ok || ! reflect .DeepEqual (v , threatExceptionsProp )) {
151
+ obj ["threatExceptions" ] = threatExceptionsProp
152
+ }
136
153
137
154
url , err := replaceVars (d , config , "{{CloudIdsBasePath}}projects/{{project}}/locations/{{location}}/endpoints?endpointId={{name}}" )
138
155
if err != nil {
@@ -252,10 +269,78 @@ func resourceCloudIdsEndpointRead(d *schema.ResourceData, meta interface{}) erro
252
269
if err := d .Set ("severity" , flattenCloudIdsEndpointSeverity (res ["severity" ], d , config )); err != nil {
253
270
return fmt .Errorf ("Error reading Endpoint: %s" , err )
254
271
}
272
+ if err := d .Set ("threat_exceptions" , flattenCloudIdsEndpointThreatExceptions (res ["threatExceptions" ], d , config )); err != nil {
273
+ return fmt .Errorf ("Error reading Endpoint: %s" , err )
274
+ }
255
275
256
276
return nil
257
277
}
258
278
279
+ func resourceCloudIdsEndpointUpdate (d * schema.ResourceData , meta interface {}) error {
280
+ config := meta .(* Config )
281
+ userAgent , err := generateUserAgentString (d , config .userAgent )
282
+ if err != nil {
283
+ return err
284
+ }
285
+
286
+ billingProject := ""
287
+
288
+ project , err := getProject (d , config )
289
+ if err != nil {
290
+ return fmt .Errorf ("Error fetching project for Endpoint: %s" , err )
291
+ }
292
+ billingProject = project
293
+
294
+ obj := make (map [string ]interface {})
295
+ threatExceptionsProp , err := expandCloudIdsEndpointThreatExceptions (d .Get ("threat_exceptions" ), d , config )
296
+ if err != nil {
297
+ return err
298
+ } else if v , ok := d .GetOkExists ("threat_exceptions" ); ! isEmptyValue (reflect .ValueOf (v )) && (ok || ! reflect .DeepEqual (v , threatExceptionsProp )) {
299
+ obj ["threatExceptions" ] = threatExceptionsProp
300
+ }
301
+
302
+ url , err := replaceVars (d , config , "{{CloudIdsBasePath}}projects/{{project}}/locations/{{location}}/endpoints/{{name}}" )
303
+ if err != nil {
304
+ return err
305
+ }
306
+
307
+ log .Printf ("[DEBUG] Updating Endpoint %q: %#v" , d .Id (), obj )
308
+ updateMask := []string {}
309
+
310
+ if d .HasChange ("threat_exceptions" ) {
311
+ updateMask = append (updateMask , "threatExceptions" )
312
+ }
313
+ // updateMask is a URL parameter but not present in the schema, so replaceVars
314
+ // won't set it
315
+ url , err = addQueryParams (url , map [string ]string {"updateMask" : strings .Join (updateMask , "," )})
316
+ if err != nil {
317
+ return err
318
+ }
319
+
320
+ // err == nil indicates that the billing_project value was found
321
+ if bp , err := getBillingProject (d , config ); err == nil {
322
+ billingProject = bp
323
+ }
324
+
325
+ res , err := sendRequestWithTimeout (config , "PATCH" , billingProject , url , userAgent , obj , d .Timeout (schema .TimeoutUpdate ))
326
+
327
+ if err != nil {
328
+ return fmt .Errorf ("Error updating Endpoint %q: %s" , d .Id (), err )
329
+ } else {
330
+ log .Printf ("[DEBUG] Finished updating Endpoint %q: %#v" , d .Id (), res )
331
+ }
332
+
333
+ err = cloudIdsOperationWaitTime (
334
+ config , res , project , "Updating Endpoint" , userAgent ,
335
+ d .Timeout (schema .TimeoutUpdate ))
336
+
337
+ if err != nil {
338
+ return err
339
+ }
340
+
341
+ return resourceCloudIdsEndpointRead (d , meta )
342
+ }
343
+
259
344
func resourceCloudIdsEndpointDelete (d * schema.ResourceData , meta interface {}) error {
260
345
config := meta .(* Config )
261
346
userAgent , err := generateUserAgentString (d , config .userAgent )
@@ -322,10 +407,8 @@ func resourceCloudIdsEndpointImport(d *schema.ResourceData, meta interface{}) ([
322
407
}
323
408
324
409
func flattenCloudIdsEndpointName (v interface {}, d * schema.ResourceData , config * Config ) interface {} {
325
- if v == nil {
326
- return v
327
- }
328
- return NameFromSelfLinkStateFunc (v )
410
+ parts := strings .Split (d .Get ("name" ).(string ), "/" )
411
+ return parts [len (parts )- 1 ]
329
412
}
330
413
331
414
func flattenCloudIdsEndpointCreateTime (v interface {}, d * schema.ResourceData , config * Config ) interface {} {
@@ -356,6 +439,10 @@ func flattenCloudIdsEndpointSeverity(v interface{}, d *schema.ResourceData, conf
356
439
return v
357
440
}
358
441
442
+ func flattenCloudIdsEndpointThreatExceptions (v interface {}, d * schema.ResourceData , config * Config ) interface {} {
443
+ return v
444
+ }
445
+
359
446
func expandCloudIdsEndpointName (v interface {}, d TerraformResourceData , config * Config ) (interface {}, error ) {
360
447
return replaceVars (d , config , "projects/{{project}}/locations/{{location}}/endpoints/{{name}}" )
361
448
}
@@ -371,3 +458,7 @@ func expandCloudIdsEndpointDescription(v interface{}, d TerraformResourceData, c
371
458
func expandCloudIdsEndpointSeverity (v interface {}, d TerraformResourceData , config * Config ) (interface {}, error ) {
372
459
return v , nil
373
460
}
461
+
462
+ func expandCloudIdsEndpointThreatExceptions (v interface {}, d TerraformResourceData , config * Config ) (interface {}, error ) {
463
+ return v , nil
464
+ }
0 commit comments