Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions linode_api4/groups/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
)
from linode_api4.objects.monitor import (
AkamaiObjectStorageLogsDestinationDetails,
AuditLogsStreamDetails,
CustomHTTPSLogsDestinationDetails,
LogsStreamDetails,
LogsStreamQuota,
Expand Down Expand Up @@ -557,7 +558,9 @@ def stream_create(
label: str,
type: Union[LogsStreamType, str],
status: Optional[Union[LogsStreamStatus, str]] = None,
details: Optional[LogsStreamDetails] = None,
details: Optional[
Union[LogsStreamDetails, AuditLogsStreamDetails]
] = None,
) -> LogsStream:
"""
Creates a new :any:`LogsStream` for logs on this account. For example::
Expand All @@ -572,6 +575,16 @@ def stream_create(
type="audit_logs"
)

# audit_logs stream with data plane logs
data_plane_stream = client.monitor.stream_create(
destinations=[1234],
label="Data_plane_logs",
type="audit_logs",
details=AuditLogsStreamDetails(
categories=["data_plane"],
)
)

# lke_audit_logs stream with specific clusters
lke_stream = client.monitor.stream_create(
destinations=[1234],
Expand All @@ -597,9 +610,12 @@ def stream_create(
:param status: (Optional) The availability status of the stream. Possible values are: ``active``, ``inactive``.
Defaults to ``active``.
:type status: str
:param details: (Optional) Additional stream details. Only applicable for
``lke_audit_logs`` streams. Omit for ``audit_logs`` streams.
:type details: LogsStreamDetails
:param details: (Optional) Use AuditLogsStreamDetails to select categories
for ``audit_logs``, or LogsStreamDetails for
``lke_audit_logs``. For audit logs, the API defaults to
``config`` and ``login`` when categories are omitted
or empty.
:type details: LogsStreamDetails or AuditLogsStreamDetails

:returns: The newly created logs stream.
:rtype: LogsStream
Expand Down
30 changes: 29 additions & 1 deletion linode_api4/objects/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"RuleCriteria",
"TriggerConditions",
"AkamaiObjectStorageLogsDestinationDetails",
"AuditLogsStreamDetails",
"AuthenticationType",
"BasicAuthenticationDetails",
"ClientCertificateDetails",
Expand Down Expand Up @@ -774,7 +775,7 @@ class LogsStreamDetails(JSONObject):
Additional details for a logs stream.

This object only applies to streams with a ``type`` of ``lke_audit_logs``.
Leave it out of requests that use a ``type`` of ``audit_logs``.
For ``audit_logs`` categories, use :any:`AuditLogsStreamDetails` instead.

.. note::
When updating a stream, any existing settings need to be included to
Expand All @@ -794,6 +795,33 @@ class LogsStreamDetails(JSONObject):
cluster_ids: Optional[List[int]] = None
is_auto_add_all_clusters_enabled: bool = False

@classmethod
def from_json(
cls, json: Dict[str, Any]
) -> Optional[Union["LogsStreamDetails", "AuditLogsStreamDetails"]]:
if json is not None and "categories" in json:
return AuditLogsStreamDetails.from_json(json)

return super().from_json(json)


@dataclass
class AuditLogsStreamDetails(JSONObject):
"""
Details for streams with a ``type`` of ``audit_logs``.

:param categories: Optional selection of ``config``, ``login``, and
``data_plane`` categories. When creating a stream, omitting this field
or providing an empty list lets the API select ``config`` and ``login``.
When updating a stream, a non-empty list replaces the selection;
omitting this field or providing an empty list preserves it.
The API returns categories in alphabetical order. Selecting a disabled
category returns HTTP 400, unless the stream already uses that category.
:type categories: Optional[List[str]]
"""

categories: Optional[List[str]] = None


@dataclass
class LogsStreamDestination(JSONObject):
Expand Down
110 changes: 110 additions & 0 deletions test/unit/objects/monitor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

from linode_api4.objects import (
AlertChannel,
AuditLogsStreamDetails,
LogsDestination,
LogsDestinationHistory,
LogsStream,
LogsStreamDestination,
LogsStreamHistory,
MonitorDashboard,
MonitorService,
)
Expand Down Expand Up @@ -795,3 +797,111 @@ def test_create_audit_logs_stream_omits_details(self):
)

self.assertNotIn("details", m.call_data)


class AuditLogsStreamTest(ClientBaseCase):
def test_create_audit_logs_stream_categories(self):
for categories in (
["config"],
["login"],
["data_plane"],
["config", "data_plane", "login"],
[],
None,
):
with self.subTest(categories=categories):
requested_details = (
{} if categories is None else {"categories": categories}
)
response_categories = categories or ["config", "login"]
response = {
"id": 5,
"type": "audit_logs",
"details": {"categories": response_categories},
}

with self.mock_post(response) as mock:
stream = self.client.monitor.stream_create(
destinations=[1],
label="Audit stream",
type=LogsStreamType.audit_logs,
details=AuditLogsStreamDetails(categories=categories),
)

self.assertEqual(mock.call_url, "/monitor/streams")
self.assertEqual(
mock.call_data,
{
"destinations": [1],
"label": "Audit stream",
"type": "audit_logs",
"details": requested_details,
},
)
self.assertIsInstance(stream.details, AuditLogsStreamDetails)
self.assertEqual(stream.details.categories, response_categories)

def test_create_audit_logs_stream_defaults_from_api(self):
response = {
"id": 5,
"type": "audit_logs",
"details": {"categories": ["config", "login"]},
}
with self.mock_post(response) as mock:
stream = self.client.monitor.stream_create(
destinations=[1],
label="Audit stream",
type="audit_logs",
)

self.assertNotIn("details", mock.call_data)
self.assertIsInstance(stream.details, AuditLogsStreamDetails)
self.assertEqual(stream.details.categories, ["config", "login"])

def test_load_audit_logs_stream_and_history_categories(self):
for model in (LogsStream, LogsStreamHistory):
with self.subTest(model=model.__name__):
stream = model(
self.client,
5,
{
"id": 5,
"type": "audit_logs",
"details": {
"categories": ["config", "data_plane", "login"],
},
},
)

self.assertIsInstance(stream.details, AuditLogsStreamDetails)
self.assertEqual(
stream.details.categories,
["config", "data_plane", "login"],
)

def test_update_audit_logs_stream_categories(self):
stream = LogsStream(
self.client,
5,
{
"id": 5,
"label": "Audit stream",
"status": "active",
"type": "audit_logs",
"details": {"categories": ["config", "login"]},
},
)
stream.details = AuditLogsStreamDetails(categories=["data_plane"])

with self.mock_put({}) as mock:
self.assertTrue(stream.save())

self.assertEqual(mock.call_url, "/monitor/streams/5")
self.assertEqual(
mock.call_data,
{
"label": "Audit stream",
"status": "active",
"details": {"categories": ["data_plane"]},
},
)
Loading