mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-03-21 20:59:10 +00:00
New Callback plugin: loganalytics_ingestion adding Azure Log Analytics Ingestion (#10306)
* Add Azure Log Analytics Ingestion API plugin The Ingestion API allows sending data to a Log Analytics workspace in Azure Monitor. * Fix LogAnalytics Ingestion shebang * Fix Log Analytics Ingestion pep8 tests * Fix Log Analytics Ingestion pylint tests * Fix Log Analytics Ingestion import tests * Fix Log Analytics Ingestion pylint test * Add Log Analytics Ingestion auth timeout Previous behavior was to use the 'request' module's default timeout; this makes auth timeout value consistent with the task submission timeout value. * Display Log Analytics Ingestion event data as JSON Previous behavior was to display the data as a Python dictionary. The new behavior makes it easier to generate a sample JSON file in order to import into Azure when creating the table. * Add Azure Log Analytics Ingestion timeout param This parameter controls how long the plugin will wait for an HTTP response from the Azure Log Analytics API before considering the request a failure. Previous behavior was hardcoded to 2 seconds. * Fix Azure Log Ingestion unit test The class instantiation was missing an additional argument that was added in a previous patch; add it. Converting to JSON also caused the Mock TaskResult object to throw a serialization error; override the function for JSON conversion to just return bogus data instead. * Fix loganalytics_ingestion linter errors * Fix LogAnalytics Ingestion env vars Prefix the LogAnalytics Ingestion plugin's environment variable names with 'ANSIBLE_' in order to align with plugin best practices. * Remove LogAnalytics 'requests' dep from docs The LogAnalytics callback plugin does not actually require 'requests', so remove it from the documented dependencies. * Refactor LogAnalytics Ingestion to use URL utils This replaces the previous behavior of depending on the external 'requests' library. * Simplify LogAnalytics Ingestion token valid check Co-authored-by: Felix Fontein <felix@fontein.de> * Remove LogAnalytics Ingestion extra arg validation Argument validation should be handled by ansible-core, so remove the extra argument validation in the plugin itself. * Update LogAnalytics Ingestion version added * Remove LogAnalytics Ingestion coding marker The marker is no longer needed as Python2 is no longer supported. * Fix some LogAnalytics Ingestion grammar errors * Refactor LogAnalytics Ingestion plugin messages Consistently use "plugin" instead of module, and refer to the module by its FQCN instead of its prose name. * Remove LogAnalytics Ingestion extra logic A few unused vars were being set; stop setting them. * Fix LogAnalytics Ingestion nox sanity tests * Fix LogAnalytics Ingestion unit tests The refactor to move away from the 'requests' dependency to use module_utils broke the plugin's unit tests; re-write the plugin's unit tests for module_utils. * Add nox formatting to LogAnalytics Ingestion * Fix Log Analytics Ingestion urllib import Remove the compatibility import via 'six' for 'urllib' since Python 2 support is no longer supported. * Bump LogAnalytics Ingestion plugin version added * Remove LogAnalytics Ingestion required: false docs Required being false is the default, so no need to explicitly add it. * Simplify LogAnalytics Ingestion role name logic * Clean LogAnalytics Ingestion redundant comments * Clean LogAnalytics Ingestion unit test code Rename all Mock objects to use snake_case and consistently use '_mock' as a suffix instead of sometimes using it as a prefix and sometimes using it as a suffix. * Refactor LogAnalytics Ingestion unit tests Move all of the tests outside of the 'setUp' method. * Refactor LogAnalytics Ingestion test Add a test to validate that part of the contents sent match what was supposed to be sent. * Refactor LogAnalytics Ingestion test Make the names consistent again. * Add LogAnalytics Ingestion sample data docs * Apply suggestions from code review Co-authored-by: Felix Fontein <felix@fontein.de> --------- Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
34938ca1ef
commit
38f93c80f1
3 changed files with 425 additions and 0 deletions
82
tests/unit/plugins/callback/test_loganalytics_ingestion.py
Normal file
82
tests/unit/plugins/callback/test_loganalytics_ingestion.py
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
# Copyright (c) Ansible project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
import json
|
||||
import time
|
||||
import unittest
|
||||
import unittest.mock
|
||||
import urllib
|
||||
|
||||
from ansible.executor.task_result import TaskResult
|
||||
|
||||
from ansible_collections.community.general.plugins.callback.loganalytics_ingestion import (
|
||||
AzureLogAnalyticsIngestionSource,
|
||||
)
|
||||
|
||||
|
||||
class TestAzureLogAnalyticsIngestion(unittest.TestCase):
|
||||
dce_url = "https://fake.dce_url.ansible.com"
|
||||
dcr_id = "fake-dcr-id"
|
||||
client_id = "fake-client_id"
|
||||
client_secret = "fake-client-secret"
|
||||
tenant_id = "fake-tenant-id"
|
||||
stream_name = "fake-stream-name"
|
||||
fake_access_token = None
|
||||
|
||||
def setUp(self):
|
||||
self.fake_access_token = json.dumps(
|
||||
{"expires_in": time.time() + 3600, "access_token": "fake_access_token"}
|
||||
).encode("utf-8")
|
||||
|
||||
@unittest.mock.patch(
|
||||
"ansible_collections.community.general.plugins.callback.loganalytics_ingestion.open_url", autospec=True
|
||||
)
|
||||
@unittest.mock.patch("ansible.executor.task_result.TaskResult")
|
||||
def test_sending_data(self, task_result_mock, open_url_mock):
|
||||
"""
|
||||
Tests sending data by verifying that the expected POST requests are submitted to the expected hosts.
|
||||
"""
|
||||
# The data returned from 'open_url' is only ever read during authentication.
|
||||
open_url_mock.return_value.read.return_value = self.fake_access_token
|
||||
|
||||
# TODO: How to set plugin default arguments?
|
||||
# I tried instantiating the 'CallbackModule' but all it ever did was complain that 'client_id' wasn't defined.
|
||||
self.loganalytics = AzureLogAnalyticsIngestionSource(
|
||||
self.dce_url,
|
||||
self.dcr_id,
|
||||
3,
|
||||
True,
|
||||
self.client_id,
|
||||
self.client_secret,
|
||||
self.tenant_id,
|
||||
self.stream_name,
|
||||
False,
|
||||
False,
|
||||
2,
|
||||
"community.general.loganalytics_ingestion",
|
||||
)
|
||||
|
||||
assert open_url_mock.call_count == 1
|
||||
url = urllib.parse.urlparse(open_url_mock.call_args_list[0][0][0])
|
||||
assert url.netloc == "login.microsoftonline.com"
|
||||
|
||||
results = ["foo", "bar", "biz"]
|
||||
for i, result in enumerate(results, start=1):
|
||||
host_mock = unittest.mock.Mock("host_mock")
|
||||
host_mock.name = "fake-name"
|
||||
task_mock = unittest.mock.Mock("task_mock")
|
||||
task_mock._role = "fake-role"
|
||||
task_mock.get_name = lambda r=result: r
|
||||
|
||||
task_result = TaskResult(
|
||||
host=host_mock, task=task_mock, return_data={}, task_fields={"action": "fake-action", "args": {}}
|
||||
)
|
||||
self.loganalytics.send_to_loganalytics("fake-playbook", task_result, "OK")
|
||||
|
||||
assert open_url_mock.call_count == 1 + i
|
||||
|
||||
url = urllib.parse.urlparse(open_url_mock.call_args_list[i][0][0])
|
||||
assert url.scheme + "://" + url.netloc == self.dce_url
|
||||
|
||||
assert json.loads(open_url_mock.call_args_list[i].kwargs.get("data"))[0].get("TaskName") == result
|
||||
Loading…
Add table
Add a link
Reference in a new issue