Skip to content

Commit

Permalink
add compliance partner heartbeat exclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
almenscorner committed Feb 16, 2024
1 parent c3f0045 commit 7f55185
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/IntuneCD/backup/Intune/backup_compliancePartner.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


# Get all Compliance Partners and save them in specified path
def savebackup(path, output, token, append_id):
def savebackup(path, output, exclude, token, append_id):
"""
Saves all Compliance Partners in Intune to a JSON or YAML file.
Expand All @@ -44,6 +44,12 @@ def savebackup(path, output, token, append_id):
fname = clean_filename(partner["displayName"])
if append_id:
fname = f"{fname}__{graph_id}"

if (
partner.get("lastHeartbeatDateTime")
and "CompliancePartnerHeartbeat" in exclude
):
partner.pop("lastHeartbeatDateTime", None)
# Save Compliance policy as JSON or YAML depending on configured
# value in "-o"
save_output(output, configpath, fname, partner)
Expand Down
2 changes: 1 addition & 1 deletion src/IntuneCD/backup_intune.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def backup_intune(results, path, output, exclude, token, prefix, append_id, args
if "CompliancePartner" not in exclude:
from .backup.Intune.backup_compliancePartner import savebackup

results.append(savebackup(path, output, token, append_id))
results.append(savebackup(path, output, exclude, token, append_id))

if "ManagementPartner" not in exclude:
from .backup.Intune.backup_managementPartner import savebackup
Expand Down
1 change: 1 addition & 0 deletions src/IntuneCD/run_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def start():
"ScopeTags",
"VPPusedLicenseCount",
"GPlaySyncTime",
"CompliancePartnerHeartbeat",
],
nargs="+",
)
Expand Down
33 changes: 29 additions & 4 deletions tests/Backup/Intune/test_backup_compliancePartner.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ def setUp(self):
self.directory = TempDirectory()
self.directory.create()
self.token = "token"
self.exclude = []
self.append_id = False
self.saved_path = f"{self.directory.path}/Partner Connections/Compliance/test."
self.expected_data = {
"@odata.type": "#microsoft.graph.complianceManagementPartner",
"partnerState": "unavailable",
"lastHeartbeatDateTime": "2022-01-28T12:28:48.975089Z",
"displayName": "test",
"macOsOnboarded": True,
"macOsEnrollmentAssignments": [
Expand All @@ -41,6 +43,7 @@ def setUp(self):
"@odata.type": "#microsoft.graph.complianceManagementPartner",
"id": "0",
"partnerState": "unavailable",
"lastHeartbeatDateTime": "2022-01-28T12:28:48.975089Z",
"displayName": "test",
"macOsOnboarded": True,
"macOsEnrollmentAssignments": [
Expand All @@ -66,7 +69,9 @@ def tearDown(self):
def test_backup_yml(self):
"""The folder should be created, the file should have the expected contents, and the count should be 1."""

self.count = savebackup(self.directory.path, "yaml", self.token, self.append_id)
self.count = savebackup(
self.directory.path, "yaml", self.exclude, self.token, self.append_id
)

with open(self.saved_path + "yaml", "r", encoding="utf-8") as f:
data = json.dumps(yaml.safe_load(f))
Expand All @@ -81,7 +86,9 @@ def test_backup_yml(self):
def test_backup_json(self):
"""The folder should be created, the file should have the expected contents, and the count should be 1."""

self.count = savebackup(self.directory.path, "json", self.token, self.append_id)
self.count = savebackup(
self.directory.path, "json", self.exclude, self.token, self.append_id
)

with open(self.saved_path + "json", "r", encoding="utf-8") as f:
self.saved_data = json.load(f)
Expand All @@ -96,20 +103,38 @@ def test_backup_with_no_return_data(self):
"""The count should be 0 if no data is returned."""

self.makeapirequest.return_value = {"value": [{"partnerState": "unknown"}]}
self.count = savebackup(self.directory.path, "json", self.token, self.append_id)
self.count = savebackup(
self.directory.path, "json", self.exclude, self.token, self.append_id
)
self.assertEqual(0, self.count["config_count"])

def test_backup_append_id(self):
"""The folder should be created, the file should have the expected contents, and the count should be 1."""

self.count = savebackup(self.directory.path, "json", self.token, True)
self.count = savebackup(
self.directory.path, "json", self.exclude, self.token, True
)

self.assertTrue(
Path(
f"{self.directory.path}/Partner Connections/Compliance/test__0.json"
).exists()
)

def test_backup_exclude_lastHeartbeatDateTime(self):
"""The lastHeartbeatDateTime should be excluded from the saved data."""

self.exclude.append("CompliancePartnerHeartbeat")

self.count = savebackup(
self.directory.path, "json", self.exclude, self.token, self.append_id
)

with open(self.saved_path + "json", "r", encoding="utf-8") as f:
saved_data = json.load(f)

self.assertNotIn("lastHeartbeatDateTime", saved_data)


if __name__ == "__main__":
unittest.main()

0 comments on commit 7f55185

Please sign in to comment.