Skip to content

Commit 82e6268

Browse files
Add threat_exception field, add update test (#7077) (#13442)
* cloudids: added `threat_exception` field to `google_cloud_ids_endpoint ` resource. Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
1 parent ac80bac commit 82e6268

File tree

4 files changed

+147
-9
lines changed

4 files changed

+147
-9
lines changed

.changelog/7077.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
```release-note:enhancement
2+
cloudids: added `threat_exception` field to `google_cloud_ids_endpoint ` resource.
3+
4+
```

google/resource_cloud_ids_endpoint.go

+95-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"log"
2020
"reflect"
21+
"strings"
2122
"time"
2223

2324
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -27,6 +28,7 @@ func resourceCloudIdsEndpoint() *schema.Resource {
2728
return &schema.Resource{
2829
Create: resourceCloudIdsEndpointCreate,
2930
Read: resourceCloudIdsEndpointRead,
31+
Update: resourceCloudIdsEndpointUpdate,
3032
Delete: resourceCloudIdsEndpointDelete,
3133

3234
Importer: &schema.ResourceImporter{
@@ -35,6 +37,7 @@ func resourceCloudIdsEndpoint() *schema.Resource {
3537

3638
Timeouts: &schema.ResourceTimeout{
3739
Create: schema.DefaultTimeout(20 * time.Minute),
40+
Update: schema.DefaultTimeout(20 * time.Minute),
3841
Delete: schema.DefaultTimeout(20 * time.Minute),
3942
},
4043

@@ -70,6 +73,14 @@ func resourceCloudIdsEndpoint() *schema.Resource {
7073
ForceNew: true,
7174
Description: `An optional description of the endpoint.`,
7275
},
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+
},
7384
"create_time": {
7485
Type: schema.TypeString,
7586
Computed: true,
@@ -133,6 +144,12 @@ func resourceCloudIdsEndpointCreate(d *schema.ResourceData, meta interface{}) er
133144
} else if v, ok := d.GetOkExists("severity"); !isEmptyValue(reflect.ValueOf(severityProp)) && (ok || !reflect.DeepEqual(v, severityProp)) {
134145
obj["severity"] = severityProp
135146
}
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+
}
136153

137154
url, err := replaceVars(d, config, "{{CloudIdsBasePath}}projects/{{project}}/locations/{{location}}/endpoints?endpointId={{name}}")
138155
if err != nil {
@@ -252,10 +269,78 @@ func resourceCloudIdsEndpointRead(d *schema.ResourceData, meta interface{}) erro
252269
if err := d.Set("severity", flattenCloudIdsEndpointSeverity(res["severity"], d, config)); err != nil {
253270
return fmt.Errorf("Error reading Endpoint: %s", err)
254271
}
272+
if err := d.Set("threat_exceptions", flattenCloudIdsEndpointThreatExceptions(res["threatExceptions"], d, config)); err != nil {
273+
return fmt.Errorf("Error reading Endpoint: %s", err)
274+
}
255275

256276
return nil
257277
}
258278

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+
259344
func resourceCloudIdsEndpointDelete(d *schema.ResourceData, meta interface{}) error {
260345
config := meta.(*Config)
261346
userAgent, err := generateUserAgentString(d, config.userAgent)
@@ -322,10 +407,8 @@ func resourceCloudIdsEndpointImport(d *schema.ResourceData, meta interface{}) ([
322407
}
323408

324409
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]
329412
}
330413

331414
func flattenCloudIdsEndpointCreateTime(v interface{}, d *schema.ResourceData, config *Config) interface{} {
@@ -356,6 +439,10 @@ func flattenCloudIdsEndpointSeverity(v interface{}, d *schema.ResourceData, conf
356439
return v
357440
}
358441

442+
func flattenCloudIdsEndpointThreatExceptions(v interface{}, d *schema.ResourceData, config *Config) interface{} {
443+
return v
444+
}
445+
359446
func expandCloudIdsEndpointName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
360447
return replaceVars(d, config, "projects/{{project}}/locations/{{location}}/endpoints/{{name}}")
361448
}
@@ -371,3 +458,7 @@ func expandCloudIdsEndpointDescription(v interface{}, d TerraformResourceData, c
371458
func expandCloudIdsEndpointSeverity(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
372459
return v, nil
373460
}
461+
462+
func expandCloudIdsEndpointThreatExceptions(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
463+
return v, nil
464+
}

google/resource_cloudids_endpoint_test.go

+43-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ func TestAccCloudIdsEndpoint_basic(t *testing.T) {
2929
ImportState: true,
3030
ImportStateVerify: true,
3131
},
32+
{
33+
Config: testCloudIds_basicUpdate(context),
34+
},
35+
{
36+
ResourceName: "google_cloud_ids_endpoint.endpoint",
37+
ImportState: true,
38+
ImportStateVerify: true,
39+
},
3240
},
3341
})
3442
}
@@ -52,11 +60,41 @@ resource "google_service_networking_connection" "private_service_connection" {
5260
}
5361
5462
resource "google_cloud_ids_endpoint" "endpoint" {
55-
name = "cloud-ids-test-%{random_suffix}"
56-
location = "us-central1-f"
57-
network = google_compute_network.default.id
58-
severity = "INFORMATIONAL"
59-
depends_on = [google_service_networking_connection.private_service_connection]
63+
name = "cloud-ids-test-%{random_suffix}"
64+
location = "us-central1-f"
65+
network = google_compute_network.default.id
66+
severity = "INFORMATIONAL"
67+
threat_exceptions = ["12", "67"]
68+
depends_on = [google_service_networking_connection.private_service_connection]
69+
}
70+
`, context)
71+
}
72+
73+
func testCloudIds_basicUpdate(context map[string]interface{}) string {
74+
return Nprintf(`
75+
resource "google_compute_network" "default" {
76+
name = "tf-test-my-network%{random_suffix}"
77+
}
78+
resource "google_compute_global_address" "service_range" {
79+
name = "address"
80+
purpose = "VPC_PEERING"
81+
address_type = "INTERNAL"
82+
prefix_length = 16
83+
network = google_compute_network.default.id
84+
}
85+
resource "google_service_networking_connection" "private_service_connection" {
86+
network = google_compute_network.default.id
87+
service = "servicenetworking.googleapis.com"
88+
reserved_peering_ranges = [google_compute_global_address.service_range.name]
89+
}
90+
91+
resource "google_cloud_ids_endpoint" "endpoint" {
92+
name = "cloud-ids-test-%{random_suffix}"
93+
location = "us-central1-f"
94+
network = google_compute_network.default.id
95+
severity = "INFORMATIONAL"
96+
threat_exceptions = ["33"]
97+
depends_on = [google_service_networking_connection.private_service_connection]
6098
}
6199
`, context)
62100
}

website/docs/r/cloud_ids_endpoint.html.markdown

+5
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ The following arguments are supported:
8686
(Optional)
8787
An optional description of the endpoint.
8888

89+
* `threat_exceptions` -
90+
(Optional)
91+
Configuration for threat IDs excluded from generating alerts. Limit: 99 IDs.
92+
8993
* `project` - (Optional) The ID of the project in which the resource belongs.
9094
If it is not provided, the provider project is used.
9195

@@ -115,6 +119,7 @@ This resource provides the following
115119
[Timeouts](/docs/configuration/resources.html#timeouts) configuration options:
116120

117121
- `create` - Default is 20 minutes.
122+
- `update` - Default is 20 minutes.
118123
- `delete` - Default is 20 minutes.
119124

120125
## Import

0 commit comments

Comments
 (0)