Skip to content

Commit 0039116

Browse files
add: all filters + cassette
1 parent 12845fc commit 0039116

File tree

5 files changed

+4949
-31
lines changed

5 files changed

+4949
-31
lines changed

docs/data-sources/audit_trail_event.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ data "scaleway_audit_trail_event" "find_by_product_name" {
4242
region = "nl-ams"
4343
product_name = "secret-manager"
4444
}
45+
46+
# Retrieve audit trail events with various filtering
47+
data "scaleway_audit_trail_event" "find_with_filters" {
48+
region = "fr-par"
49+
service_name = "instance"
50+
method_name = "CreateServer"
51+
principal_id = "11111111-1111-1111-1111-111111111111"
52+
source_ip = "192.0.2.1"
53+
status = 200
54+
recorded_after = "2025-10-01T00:00:00Z"
55+
recorded_before = "2025-12-31T23:59:59Z"
56+
order_by = "recorded_at_desc"
57+
}
4558
```
4659

4760
## Argument Reference
@@ -52,6 +65,14 @@ data "scaleway_audit_trail_event" "find_by_product_name" {
5265
- `resource_type` - (Optional) Type of the scaleway resources associated with the listed events. Possible values are: `secm_secret`, `secm_secret_version`, `kube_cluster`, `kube_pool`, `kube_node`, `kube_acl`, `keym_key`, `iam_user`, `iam_application`, `iam_group`, `iam_policy`, `iam_api_key`, `iam_ssh_key`, `iam_rule`, `iam_saml`, `iam_saml_certificate`, `secret_manager_secret`, `secret_manager_version`, `key_manager_key`, `account_user`, `account_organization`, `account_project`, `instance_server`, `instance_placement_group`, `instance_security_group`, `instance_volume`, `instance_snapshot`, `instance_image`, `apple_silicon_server`, `baremetal_server`, `baremetal_setting`, `ipam_ip`, `sbs_volume`, `sbs_snapshot`, `load_balancer_lb`, `load_balancer_ip`, `load_balancer_frontend`, `load_balancer_backend`, `load_balancer_route`, `load_balancer_acl`, `load_balancer_certificate`, `sfs_filesystem`, or `vpc_private_network`.
5366
- `resource_id` - (Optional) ID of the Scaleway resource associated with the listed events.
5467
- `product_name` - (Optional) Name of the Scaleway product in a hyphenated format.
68+
- `service_name` - (Optional) Name of the service of the API call performed.
69+
- `method_name` - (Optional) Name of the method of the API call performed.
70+
- `principal_id` - (Optional) ID of the User or IAM application at the origin of the event.
71+
- `source_ip` - (Optional) IP address at the origin of the event.
72+
- `status` - (Optional) HTTP status code of the request.
73+
- `recorded_after` - (Optional) The `recorded_after` parameter defines the earliest timestamp from which Audit Trail events are retrieved. Returns `one hour ago` by default (Format ISO 8601).
74+
- `recorded_before` - (Optional) The `recorded_before` parameter defines the latest timestamp up to which Audit Trail events are retrieved. Must be later than recorded_after. Returns `now` by default (Format ISO 8601).
75+
- `order_by` - (Optional) Defines the order in which events are returned. Possible values are `recorded_at_asc` and `recorded_at_desc`. Default value: `recorded_at_desc`.
5576

5677

5778
## Attributes Reference
@@ -77,4 +98,3 @@ In addition to all arguments above, the following attributes are exported:
7798
- `request_id` - Unique identifier of the request at the origin of the event. (UUID format)
7899
- `request_body` - Request at the origin of the event.
79100
- `status_code` - HTTP status code resulting of the API call.
80-

internal/services/audittrail/event_data_source.go

Lines changed: 107 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package audittrail
22

33
import (
44
"context"
5-
"strconv"
5+
"fmt"
66

77
"github.com/google/uuid"
88
"github.com/hashicorp/go-cty/cty"
99
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1112
audittrailSDK "github.com/scaleway/scaleway-sdk-go/api/audit_trail/v1alpha1"
1213
"github.com/scaleway/scaleway-sdk-go/scw"
1314
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
@@ -58,6 +59,50 @@ func DataSourceEvent() *schema.Resource {
5859
Description: "Scaleway product associated with the listed events in a hyphenated format",
5960
Optional: true,
6061
},
62+
"service_name": {
63+
Type: schema.TypeString,
64+
Description: "Name of the service of the API call performed",
65+
Optional: true,
66+
},
67+
"method_name": {
68+
Type: schema.TypeString,
69+
Description: "Name of the method of the API call performed",
70+
Optional: true,
71+
},
72+
"principal_id": {
73+
Type: schema.TypeString,
74+
Description: "ID of the User or IAM application at the origin of the event",
75+
Optional: true,
76+
},
77+
"source_ip": {
78+
Type: schema.TypeString,
79+
Description: "IP address at the origin of the event",
80+
Optional: true,
81+
},
82+
"status": {
83+
Type: schema.TypeInt,
84+
Description: "HTTP status code of the request",
85+
Optional: true,
86+
},
87+
"recorded_after": {
88+
Type: schema.TypeString,
89+
Description: "The `recorded_after` parameter defines the earliest timestamp from which Audit Trail events are retrieved. Returns `one hour ago` by default (Format ISO 8601)",
90+
Optional: true,
91+
},
92+
"recorded_before": {
93+
Type: schema.TypeString,
94+
Description: "The `recorded_before` parameter defines the latest timestamp up to which Audit Trail events are retrieved. Must be later than recorded_after. Returns `now` by default (Format ISO 8601)",
95+
Optional: true,
96+
},
97+
"order_by": {
98+
Type: schema.TypeString,
99+
Description: "Defines the order in which events are returned. Default value: recorded_at_desc",
100+
Optional: true,
101+
ValidateFunc: validation.StringInSlice([]string{
102+
string(audittrailSDK.ListAuthenticationEventsRequestOrderByRecordedAtAsc),
103+
string(audittrailSDK.ListAuthenticationEventsRequestOrderByRecordedAtDesc),
104+
}, true),
105+
},
61106
"events": {
62107
Type: schema.TypeList,
63108
Computed: true,
@@ -154,7 +199,7 @@ func DataSourceEvent() *schema.Resource {
154199
Computed: true,
155200
},
156201
"status_code": {
157-
Type: schema.TypeString,
202+
Type: schema.TypeInt,
158203
Description: "HTTP status code resulting of the API call",
159204
Computed: true,
160205
},
@@ -176,6 +221,32 @@ func DataSourceEventsRead(ctx context.Context, d *schema.ResourceData, m any) di
176221
Region: region,
177222
}
178223

224+
err = readOptionalData(d, &req)
225+
226+
if err != nil {
227+
return diag.FromErr(err)
228+
}
229+
230+
res, err := auditTrailAPI.ListEvents(&req, scw.WithContext(ctx))
231+
if err != nil {
232+
return diag.FromErr(err)
233+
}
234+
235+
d.SetId(uuid.New().String())
236+
_ = d.Set("organization_id", orgID)
237+
_ = d.Set("region", region)
238+
239+
flattenedEvents, err := flattenEvents(res.Events)
240+
if err != nil {
241+
return diag.FromErr(err)
242+
}
243+
244+
_ = d.Set("events", flattenedEvents)
245+
246+
return nil
247+
}
248+
249+
func readOptionalData(d *schema.ResourceData, req *audittrailSDK.ListEventsRequest) error {
179250
if projectID, ok := d.GetOk("project_id"); ok {
180251
req.ProjectID = types.ExpandStringPtr(projectID)
181252
}
@@ -192,21 +263,44 @@ func DataSourceEventsRead(ctx context.Context, d *schema.ResourceData, m any) di
192263
req.ResourceID = types.ExpandStringPtr(resourceID)
193264
}
194265

195-
res, err := auditTrailAPI.ListEvents(&req, scw.WithContext(ctx))
196-
if err != nil {
197-
return diag.FromErr(err)
266+
if serviceName, ok := d.GetOk("service_name"); ok {
267+
req.ServiceName = types.ExpandStringPtr(serviceName)
198268
}
199269

200-
d.SetId(uuid.New().String())
201-
_ = d.Set("organization_id", orgID)
202-
_ = d.Set("region", region)
270+
if methodName, ok := d.GetOk("method_name"); ok {
271+
req.MethodName = types.ExpandStringPtr(methodName)
272+
}
203273

204-
flattenedEvents, err := flattenEvents(res.Events)
205-
if err != nil {
206-
return diag.FromErr(err)
274+
if principalID, ok := d.GetOk("principal_id"); ok {
275+
req.PrincipalID = types.ExpandStringPtr(principalID)
207276
}
208277

209-
_ = d.Set("events", flattenedEvents)
278+
if sourceIP, ok := d.GetOk("source_ip"); ok {
279+
req.SourceIP = types.ExpandStringPtr(sourceIP)
280+
}
281+
282+
if status, ok := d.GetOk("status"); ok {
283+
req.Status = types.ExpandUint32Ptr(status)
284+
}
285+
286+
if recordedBefore, ok := d.GetOk("recorded_before"); ok {
287+
req.RecordedBefore = types.ExpandTimePtr(recordedBefore)
288+
}
289+
290+
if recordedAfter, ok := d.GetOk("recorded_after"); ok {
291+
req.RecordedAfter = types.ExpandTimePtr(recordedAfter)
292+
}
293+
294+
if orderBy, ok := d.GetOk("order_by"); ok {
295+
switch orderBy.(string) {
296+
case "recorded_at_asc":
297+
req.OrderBy = audittrailSDK.ListEventsRequestOrderByRecordedAtAsc
298+
case "recorded_at_desc":
299+
req.OrderBy = audittrailSDK.ListEventsRequestOrderByRecordedAtDesc
300+
default:
301+
return fmt.Errorf("invalid order_by value: %s, must be 'recorded_at_asc' or 'recorded_at_desc'", orderBy)
302+
}
303+
}
210304

211305
return nil
212306
}
@@ -240,7 +334,7 @@ func flattenEvents(events []*audittrailSDK.Event) ([]map[string]any, error) {
240334
"resources": flattenResources(event.Resources),
241335
"request_id": event.RequestID,
242336
"request_body": requestBody,
243-
"status_code": strconv.FormatUint(uint64(event.StatusCode), 10),
337+
"status_code": event.StatusCode,
244338
}
245339
}
246340

0 commit comments

Comments
 (0)