Skip to content

Commit 315716a

Browse files
committed
Fix Log Analytics Ingestion pep8 tests
1 parent 3515cc1 commit 315716a

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

plugins/callback/loganalytics_ingestion.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,10 @@
155155

156156
display = Display()
157157

158-
class AzureLogAnalyticsIngestionSource(object):
159158

160-
def __init__(self, dce_url, dcr_id, disable_attempts, disable_on_failure, client_id, client_secret, tenant_id, stream_name, include_task_args, include_content):
159+
class AzureLogAnalyticsIngestionSource(object):
160+
def __init__(self, dce_url, dcr_id, disable_attempts, disable_on_failure, client_id, client_secret, tenant_id, stream_name, include_task_args,
161+
include_content):
161162
self.dce_url = dce_url
162163
self.dcr_id = dcr_id
163164
self.disabled = False
@@ -185,21 +186,22 @@ def get_bearer_token(self):
185186
'grant_type': 'client_credentials',
186187
'client_id': self.client_id,
187188
'client_secret': self.client_secret,
188-
# The scope value comes from https://learn.microsoft.com/en-us/azure/azure-monitor/logs/logs-ingestion-api-overview#headers and https://learn.microsoft.com/en-us/entra/identity-platform/scopes-oidc#the-default-scope
189+
# The scope value comes from https://learn.microsoft.com/en-us/azure/azure-monitor/logs/logs-ingestion-api-overview#headers
190+
# and https://learn.microsoft.com/en-us/entra/identity-platform/scopes-oidc#the-default-scope
189191
'scope': 'https://monitor.azure.com/.default'
190192
}
191193
response = self.requests_session.post(url, data=payload)
192194
response.raise_for_status()
193-
self.token_expiration_time=datetime.now()+timedelta(seconds=response.json().get("expires_in"))
195+
self.token_expiration_time = datetime.now() + timedelta(seconds=response.json().get("expires_in"))
194196
return response.json().get('access_token')
195197

196198
def is_token_valid(self):
197-
return True if (datetime.now() + timedelta(seconds=10))< self.token_expiration_time else False
199+
return True if (datetime.now() + timedelta(seconds=10)) < self.token_expiration_time else False
198200

199201
# Method to send event data to the Azure Logs Ingestion API
200202
# This replaces the legacy API call and now uses the Logs Ingestion API endpoint
201203
def send_event(self, event_data):
202-
if not(self.is_token_valid()):
204+
if not self.is_token_valid():
203205
self.bearer_token = self.get_bearer_token()
204206
ingestion_url = f"{self.dce_url}/dataCollectionRules/{self.dcr_id}/streams/{self.stream_name}?api-version=2023-01-01"
205207
headers = {
@@ -241,12 +243,12 @@ def _send_to_loganalytics(self, playbook_name, result, state):
241243
if result._task._role:
242244
ansible_role = str(result._task._role)
243245

244-
#Include/Exclude task args
245-
if not(self.include_task_args):
246+
# Include/Exclude task args
247+
if not self.include_task_args:
246248
result._task_fields.pop('args', None)
247249

248-
#Include/Exclude content
249-
if not(self.include_content):
250+
# Include/Exclude content
251+
if not self.include_content:
250252
result._result.pop('content', None)
251253

252254
# Build the event data
@@ -263,7 +265,7 @@ def _send_to_loganalytics(self, playbook_name, result, state):
263265
"Result": result._result,
264266
"Session": self.session
265267
}]
266-
#Display event data
268+
# Display event data
267269
display.vvv(f"Event Data :{str(event_data)}")
268270

269271
# Send the event data using the new Logs Ingestion API method
@@ -300,7 +302,8 @@ def set_options(self, task_keys=None, var_options=None, direct=None):
300302

301303
# Initialize the AzureLogAnalyticsIngestionSource with the new settings
302304
self.azure_loganalytics = AzureLogAnalyticsIngestionSource(
303-
self.dce_url, self.dcr_id, self.disable_attempts, self.disable_on_failure, self.client_id, self.client_secret, self.tenant_id, self.stream_name, self.include_task_args, self.include_content
305+
self.dce_url, self.dcr_id, self.disable_attempts, self.disable_on_failure, self.client_id, self.client_secret, self.tenant_id, self.stream_name,
306+
self.include_task_args, self.include_content
304307
)
305308

306309
# Input checks

tests/unit/plugins/callback/test_loganalytics_ingestion.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from ansible.executor.task_result import TaskResult
1313
from ansible_collections.community.general.plugins.callback.loganalytics_ingestion import AzureLogAnalyticsIngestionSource
1414

15+
1516
class TestAzureLogAnalyticsIngestion(unittest.TestCase):
1617
dce_url = "https://fake.dce_url.ansible.com"
1718
dcr_id = "fake-dcr-id"

0 commit comments

Comments
 (0)