Skip to content

Commit f87e765

Browse files
authored
feat(audit_trail): add event export (#1281)
1 parent c11d844 commit f87e765

File tree

8 files changed

+566
-0
lines changed

8 files changed

+566
-0
lines changed

scaleway-async/scaleway_async/audit_trail/v1alpha1/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@
3838
from .types import Event
3939
from .types import SystemEvent
4040
from .types import ProductService
41+
from .types import ExportJobS3
4142
from .types import ListCombinedEventsResponseCombinedEvent
4243
from .types import Product
44+
from .types import CreateExportJobRequest
45+
from .types import ExportJob
4346
from .types import ListAuthenticationEventsRequest
4447
from .types import ListAuthenticationEventsResponse
4548
from .types import ListCombinedEventsRequest
@@ -89,8 +92,11 @@
8992
"Event",
9093
"SystemEvent",
9194
"ProductService",
95+
"ExportJobS3",
9296
"ListCombinedEventsResponseCombinedEvent",
9397
"Product",
98+
"CreateExportJobRequest",
99+
"ExportJob",
94100
"ListAuthenticationEventsRequest",
95101
"ListAuthenticationEventsResponse",
96102
"ListCombinedEventsRequest",

scaleway-async/scaleway_async/audit_trail/v1alpha1/api.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,21 @@
1616
ListCombinedEventsRequestOrderBy,
1717
ListEventsRequestOrderBy,
1818
ResourceType,
19+
CreateExportJobRequest,
20+
ExportJob,
21+
ExportJobS3,
1922
ListAuthenticationEventsResponse,
2023
ListCombinedEventsResponse,
2124
ListEventsResponse,
2225
ListProductsResponse,
2326
)
2427
from .marshalling import (
28+
unmarshal_ExportJob,
2529
unmarshal_ListAuthenticationEventsResponse,
2630
unmarshal_ListCombinedEventsResponse,
2731
unmarshal_ListEventsResponse,
2832
unmarshal_ListProductsResponse,
33+
marshal_CreateExportJobRequest,
2934
)
3035

3136

@@ -248,3 +253,53 @@ async def list_products(
248253

249254
self._throw_on_error(res)
250255
return unmarshal_ListProductsResponse(res.json())
256+
257+
async def create_export_job(
258+
self,
259+
*,
260+
name: str,
261+
region: Optional[ScwRegion] = None,
262+
organization_id: Optional[str] = None,
263+
s3: Optional[ExportJobS3] = None,
264+
tags: Optional[dict[str, str]] = None,
265+
) -> ExportJob:
266+
"""
267+
Create an export job.
268+
Create an export job for a specified organization. This allows you to export audit trail events to a destination, such as an S3 bucket. The request requires the organization ID, a name for the export, and a destination configuration.
269+
:param name: Name of the export.
270+
:param region: Region to target. If none is passed will use default region from the config.
271+
:param organization_id: ID of the Organization to target.
272+
:param s3: The configuration specifying the bucket where the audit trail events will be exported.
273+
One-Of ('destination'): at most one of 's3' could be set.
274+
:param tags: Tags of the export.
275+
:return: :class:`ExportJob <ExportJob>`
276+
277+
Usage:
278+
::
279+
280+
result = await api.create_export_job(
281+
name="example",
282+
)
283+
"""
284+
285+
param_region = validate_path_param(
286+
"region", region or self.client.default_region
287+
)
288+
289+
res = self._request(
290+
"POST",
291+
f"/audit-trail/v1alpha1/regions/{param_region}/export-jobs",
292+
body=marshal_CreateExportJobRequest(
293+
CreateExportJobRequest(
294+
name=name,
295+
region=region,
296+
organization_id=organization_id,
297+
tags=tags,
298+
s3=s3,
299+
),
300+
self.client,
301+
),
302+
)
303+
304+
self._throw_on_error(res)
305+
return unmarshal_ExportJob(res.json())

scaleway-async/scaleway_async/audit_trail/v1alpha1/marshalling.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44
from typing import Any
55
from dateutil import parser
66

7+
from scaleway_core.profile import ProfileDefaults
8+
from scaleway_core.utils import (
9+
OneOfPossibility,
10+
resolve_one_of,
11+
)
712
from .types import (
813
AuthenticationEventFailureReason,
914
AuthenticationEventMFAType,
1015
AuthenticationEventMethod,
1116
AuthenticationEventOrigin,
1217
AuthenticationEventResult,
18+
ExportJobS3,
19+
ExportJob,
1320
AccountOrganizationInfo,
1421
AccountProjectInfo,
1522
AccountUserInfo,
@@ -44,12 +51,103 @@
4451
ProductService,
4552
Product,
4653
ListProductsResponse,
54+
CreateExportJobRequest,
4755
)
4856
from ...std.types import (
4957
CountryCode as StdCountryCode,
5058
)
5159

5260

61+
def unmarshal_ExportJobS3(data: Any) -> ExportJobS3:
62+
if not isinstance(data, dict):
63+
raise TypeError(
64+
"Unmarshalling the type 'ExportJobS3' failed as data isn't a dictionary."
65+
)
66+
67+
args: dict[str, Any] = {}
68+
69+
field = data.get("bucket", None)
70+
if field is not None:
71+
args["bucket"] = field
72+
else:
73+
args["bucket"] = None
74+
75+
field = data.get("region", None)
76+
if field is not None:
77+
args["region"] = field
78+
else:
79+
args["region"] = None
80+
81+
field = data.get("prefix", None)
82+
if field is not None:
83+
args["prefix"] = field
84+
else:
85+
args["prefix"] = None
86+
87+
field = data.get("project_id", None)
88+
if field is not None:
89+
args["project_id"] = field
90+
else:
91+
args["project_id"] = None
92+
93+
return ExportJobS3(**args)
94+
95+
96+
def unmarshal_ExportJob(data: Any) -> ExportJob:
97+
if not isinstance(data, dict):
98+
raise TypeError(
99+
"Unmarshalling the type 'ExportJob' failed as data isn't a dictionary."
100+
)
101+
102+
args: dict[str, Any] = {}
103+
104+
field = data.get("id", None)
105+
if field is not None:
106+
args["id"] = field
107+
else:
108+
args["id"] = None
109+
110+
field = data.get("organization_id", None)
111+
if field is not None:
112+
args["organization_id"] = field
113+
else:
114+
args["organization_id"] = None
115+
116+
field = data.get("name", None)
117+
if field is not None:
118+
args["name"] = field
119+
else:
120+
args["name"] = None
121+
122+
field = data.get("tags", None)
123+
if field is not None:
124+
args["tags"] = field
125+
else:
126+
args["tags"] = {}
127+
128+
field = data.get("s3", None)
129+
if field is not None:
130+
args["s3"] = unmarshal_ExportJobS3(field)
131+
else:
132+
args["s3"] = None
133+
134+
field = data.get("created_at", None)
135+
if field is not None:
136+
args["created_at"] = parser.isoparse(field) if isinstance(field, str) else field
137+
else:
138+
args["created_at"] = None
139+
140+
field = data.get("last_run_at", None)
141+
if field is not None:
142+
args["last_run_at"] = (
143+
parser.isoparse(field) if isinstance(field, str) else field
144+
)
145+
else:
146+
args["last_run_at"] = None
147+
148+
return ExportJob(**args)
149+
150+
53151
def unmarshal_AccountOrganizationInfo(data: Any) -> AccountOrganizationInfo:
54152
if not isinstance(data, dict):
55153
raise TypeError(
@@ -1144,3 +1242,55 @@ def unmarshal_ListProductsResponse(data: Any) -> ListProductsResponse:
11441242
args["total_count"] = 0
11451243

11461244
return ListProductsResponse(**args)
1245+
1246+
1247+
def marshal_ExportJobS3(
1248+
request: ExportJobS3,
1249+
defaults: ProfileDefaults,
1250+
) -> dict[str, Any]:
1251+
output: dict[str, Any] = {}
1252+
1253+
if request.bucket is not None:
1254+
output["bucket"] = request.bucket
1255+
1256+
if request.region is not None:
1257+
output["region"] = request.region
1258+
else:
1259+
output["region"] = defaults.default_region
1260+
1261+
if request.prefix is not None:
1262+
output["prefix"] = request.prefix
1263+
1264+
if request.project_id is not None:
1265+
output["project_id"] = request.project_id
1266+
1267+
return output
1268+
1269+
1270+
def marshal_CreateExportJobRequest(
1271+
request: CreateExportJobRequest,
1272+
defaults: ProfileDefaults,
1273+
) -> dict[str, Any]:
1274+
output: dict[str, Any] = {}
1275+
output.update(
1276+
resolve_one_of(
1277+
[
1278+
OneOfPossibility(
1279+
param="s3", value=request.s3, marshal_func=marshal_ExportJobS3
1280+
),
1281+
]
1282+
),
1283+
)
1284+
1285+
if request.name is not None:
1286+
output["name"] = request.name
1287+
1288+
if request.organization_id is not None:
1289+
output["organization_id"] = request.organization_id
1290+
else:
1291+
output["organization_id"] = defaults.default_organization_id
1292+
1293+
if request.tags is not None:
1294+
output["tags"] = {key: value for key, value in request.tags.items()}
1295+
1296+
return output

scaleway-async/scaleway_async/audit_trail/v1alpha1/types.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,18 @@ class ProductService:
495495
methods: list[str]
496496

497497

498+
@dataclass
499+
class ExportJobS3:
500+
bucket: str
501+
region: ScwRegion
502+
"""
503+
Region to target. If none is passed will use default region from the config.
504+
"""
505+
506+
prefix: Optional[str] = None
507+
project_id: Optional[str] = None
508+
509+
498510
@dataclass
499511
class ListCombinedEventsResponseCombinedEvent:
500512
api: Optional[Event] = None
@@ -522,6 +534,66 @@ class Product:
522534
"""
523535

524536

537+
@dataclass
538+
class CreateExportJobRequest:
539+
name: str
540+
"""
541+
Name of the export.
542+
"""
543+
544+
region: Optional[ScwRegion] = None
545+
"""
546+
Region to target. If none is passed will use default region from the config.
547+
"""
548+
549+
organization_id: Optional[str] = None
550+
"""
551+
ID of the Organization to target.
552+
"""
553+
554+
tags: Optional[dict[str, str]] = field(default_factory=dict)
555+
"""
556+
Tags of the export.
557+
"""
558+
559+
s3: Optional[ExportJobS3] = None
560+
561+
562+
@dataclass
563+
class ExportJob:
564+
id: str
565+
"""
566+
ID of the export job.
567+
"""
568+
569+
organization_id: str
570+
"""
571+
ID of the targeted Organization.
572+
"""
573+
574+
name: str
575+
"""
576+
Name of the export.
577+
"""
578+
579+
tags: dict[str, str]
580+
"""
581+
Tags of the export.
582+
"""
583+
584+
created_at: Optional[datetime] = None
585+
"""
586+
Export job creation date.
587+
"""
588+
589+
last_run_at: Optional[datetime] = None
590+
"""
591+
Last export date.
592+
"""
593+
594+
s3: Optional[ExportJobS3] = None
595+
596+
525597
@dataclass
526598
class ListAuthenticationEventsRequest:
527599
region: Optional[ScwRegion] = None

scaleway/scaleway/audit_trail/v1alpha1/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@
3838
from .types import Event
3939
from .types import SystemEvent
4040
from .types import ProductService
41+
from .types import ExportJobS3
4142
from .types import ListCombinedEventsResponseCombinedEvent
4243
from .types import Product
44+
from .types import CreateExportJobRequest
45+
from .types import ExportJob
4346
from .types import ListAuthenticationEventsRequest
4447
from .types import ListAuthenticationEventsResponse
4548
from .types import ListCombinedEventsRequest
@@ -89,8 +92,11 @@
8992
"Event",
9093
"SystemEvent",
9194
"ProductService",
95+
"ExportJobS3",
9296
"ListCombinedEventsResponseCombinedEvent",
9397
"Product",
98+
"CreateExportJobRequest",
99+
"ExportJob",
94100
"ListAuthenticationEventsRequest",
95101
"ListAuthenticationEventsResponse",
96102
"ListCombinedEventsRequest",

0 commit comments

Comments
 (0)