Skip to content

Commit

Permalink
Handle None values from sync data (grafana#4775)
Browse files Browse the repository at this point in the history
This is required to support the install v2 endpoint (to be used by
backend plugin) which could be pushing null permissions, teams, or team
memberships.
  • Loading branch information
matiasb authored Aug 2, 2024
1 parent 4c1639c commit 7d6da1e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
8 changes: 5 additions & 3 deletions engine/apps/user_management/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def _sync_users_data(organization: Organization, sync_users: list[SyncUser], del
username=user.login,
role=getattr(LegacyAccessControlRole, user.role.upper(), LegacyAccessControlRole.NONE),
avatar_url=user.avatar_url,
permissions=[{"action": permission.action} for permission in user.permissions] or [],
permissions=[{"action": permission.action} for permission in user.permissions] if user.permissions else [],
)
for user in sync_users
)
Expand Down Expand Up @@ -361,7 +361,7 @@ def _sync_users_data(organization: Organization, sync_users: list[SyncUser], del
organization.users.filter(user_id__in=user_ids_to_delete).delete()


def _sync_teams_data(organization: Organization, sync_teams: list[SyncTeam]):
def _sync_teams_data(organization: Organization, sync_teams: list[SyncTeam] | None):
if sync_teams is None:
sync_teams = []
# keep existing team names mapping to check for possible metrics cache updates
Expand Down Expand Up @@ -413,7 +413,9 @@ def _sync_teams_data(organization: Organization, sync_teams: list[SyncTeam]):
metrics_bulk_update_team_label_cache(metrics_teams_to_update, organization.id)


def _sync_teams_members_data(organization: Organization, team_members: dict[int, list[int]]):
def _sync_teams_members_data(organization: Organization, team_members: dict[int, list[int]] | None):
if team_members is None:
return
# set team members
for team_id, members_ids in team_members.items():
team = organization.teams.get(team_id=team_id)
Expand Down
47 changes: 46 additions & 1 deletion engine/apps/user_management/tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

from apps.alerts.models import AlertReceiveChannel
from apps.api.permissions import LegacyAccessControlRole
from apps.grafana_plugin.sync_data import SyncUser
from apps.grafana_plugin.sync_data import SyncData, SyncSettings, SyncUser
from apps.user_management.models import User
from apps.user_management.sync import (
apply_sync_data,
cleanup_organization,
get_or_create_user,
sync_organization,
Expand Down Expand Up @@ -542,3 +543,47 @@ def test_get_or_create_user(make_organization, make_team, make_user_for_organiza
assert organization.users.count() == 2
assert team.users.count() == 2
assert team.users.filter(pk=user.pk).exists()


@pytest.mark.django_db
def test_apply_sync_data_none_values(make_organization):
organization = make_organization()
sync_data = SyncData(
users=[
SyncUser(
id=42,
email="[email protected]",
name="Test",
login="test",
avatar_url="https://test.com/test",
role="admin",
permissions=None,
teams=None,
),
],
teams=None,
team_members=None,
settings=SyncSettings(
stack_id=organization.stack_id,
rbac_enabled=True,
org_id=organization.org_id,
incident_enabled=True,
incident_backend_url="https://test.com",
labels_enabled=False,
license=settings.CLOUD_LICENSE_NAME,
oncall_api_url="https://test.com",
grafana_token=organization.api_token,
oncall_token=organization.gcom_token,
grafana_url=organization.grafana_url,
),
)

with patched_grafana_api_client(organization):
apply_sync_data(organization, sync_data)

# assert created/updated data
assert organization.users.count() == 1
user = organization.users.get()
assert user.user_id == 42
assert user.name == "Test"
assert user.permissions == []

0 comments on commit 7d6da1e

Please sign in to comment.