From 6ddc37195321679c3fc7694e8b9c6bac347edb47 Mon Sep 17 00:00:00 2001 From: Raj Joshi Date: Mon, 1 Apr 2024 11:27:30 -0700 Subject: [PATCH] feat(hybridcloud): Created `get_org_by_id` method (#67980) Some of our `convert_args` functions for our endpoints use `get_org_by_slug(slug=organization_slug)`. We are working on supporting slugs and parameters for our apis, so we need a 1:1 function that we can use for these convert_args when id is passed instead of slug. Created the function & wrote a test. It will also have more upstream tests. --- .../hybrid_cloud/organization/impl.py | 17 ++++++++++++ .../hybrid_cloud/organization/service.py | 14 ++++++++++ tests/sentry/hybridcloud/test_organization.py | 26 +++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/src/sentry/services/hybrid_cloud/organization/impl.py b/src/sentry/services/hybrid_cloud/organization/impl.py index 949f7d9d7c36d8..18bc9b11722a37 100644 --- a/src/sentry/services/hybrid_cloud/organization/impl.py +++ b/src/sentry/services/hybrid_cloud/organization/impl.py @@ -138,6 +138,23 @@ def get_org_by_slug( except Organization.DoesNotExist: return None + def get_org_by_id( + self, + *, + id: int, + user_id: int | None = None, + ) -> RpcOrganizationSummary | None: + query = Organization.objects.filter(id=id) + if user_id is not None: + query = query.filter( + status=OrganizationStatus.ACTIVE, + member_set__user_id=user_id, + ) + try: + return serialize_organization_summary(query.get()) + except Organization.DoesNotExist: + return None + def get_organizations_by_user_and_scope( self, *, region_name: str, user: RpcUser, scope: str | None = None ) -> list[RpcOrganization]: diff --git a/src/sentry/services/hybrid_cloud/organization/service.py b/src/sentry/services/hybrid_cloud/organization/service.py index dc8cfa6ac88caf..6690d85636d53a 100644 --- a/src/sentry/services/hybrid_cloud/organization/service.py +++ b/src/sentry/services/hybrid_cloud/organization/service.py @@ -97,6 +97,20 @@ def get_org_by_slug( RpcOrganizationSummary instead of org contexts """ + @regional_rpc_method(resolve=ByOrganizationId("id"), return_none_if_mapping_not_found=True) + @abstractmethod + def get_org_by_id( + self, + *, + id: int, + user_id: int | None = None, + ) -> RpcOrganizationSummary | None: + """ + Fetches the organization, by an organization id. If user_id is passed, it will enforce visibility + rules. This method is differentiated from get_organization_by_id by not being cached and returning + RpcOrganizationSummary instead of org contexts + """ + @regional_rpc_method(resolve=ByRegionName()) @abstractmethod def get_organizations_by_user_and_scope( diff --git a/tests/sentry/hybridcloud/test_organization.py b/tests/sentry/hybridcloud/test_organization.py index 031104524efc1e..aa02e10d91635e 100644 --- a/tests/sentry/hybridcloud/test_organization.py +++ b/tests/sentry/hybridcloud/test_organization.py @@ -202,6 +202,32 @@ def test_get_organization_id(org_factory: Callable[[], tuple[Organization, list[ assert_get_organization_by_id_works(user_context, orm_org) +@assume_test_silo_mode(SiloMode.REGION) +def assert_get_org_by_id_works(user_context: User | None, orm_org: Organization): + assert ( + organization_service.get_org_by_id(id=-2, user_id=user_context.id if user_context else None) + is None + ) + org_context = organization_service.get_org_by_id( + id=orm_org.id, user_id=user_context.id if user_context else None + ) + assert org_context is not None + + assert orm_org.id == org_context.id + assert orm_org.name == org_context.name + assert orm_org.slug == org_context.slug + + +@django_db_all(transaction=True) +@all_silo_test +@parameterize_with_orgs +def test_get_org_id(org_factory: Callable[[], tuple[Organization, list[User]]]): + orm_org, orm_users = org_factory() + + for user_context in itertools.chain([None], orm_users): + assert_get_org_by_id_works(user_context, orm_org) + + @django_db_all(transaction=True) @all_silo_test @parameterize_with_orgs