Skip to content

Commit

Permalink
Use product tier account limits when adding/enabling accounts (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
meln1k authored Mar 21, 2024
1 parent 1c01df7 commit 816da05
Show file tree
Hide file tree
Showing 24 changed files with 675 additions and 295 deletions.
1 change: 1 addition & 0 deletions fixbackend/cloud_accounts/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class Discovered(CloudAccountState):

state_name: ClassVar[str] = "discovered"
access: CloudAccess
enabled: bool # when it moves into configured, use this value for enabled and scan fields

def cloud_access(self) -> Optional[CloudAccess]:
return self.access
Expand Down
4 changes: 2 additions & 2 deletions fixbackend/cloud_accounts/models/orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ def state() -> models.CloudAccountState:
return models.CloudAccountStates.Configured(
access=access(), enabled=self.enabled, scan=self.scan
)
return models.CloudAccountStates.Discovered(access=access())
return models.CloudAccountStates.Discovered(access=access(), enabled=self.enabled)

case models.CloudAccountStates.Detected.state_name:
return models.CloudAccountStates.Detected()
case models.CloudAccountStates.Discovered.state_name:
return models.CloudAccountStates.Discovered(access=access())
return models.CloudAccountStates.Discovered(access=access(), enabled=self.enabled)
case models.CloudAccountStates.Configured.state_name:
return models.CloudAccountStates.Configured(access=access(), enabled=self.enabled, scan=self.scan)
case models.CloudAccountStates.Degraded.state_name:
Expand Down
27 changes: 25 additions & 2 deletions fixbackend/cloud_accounts/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from typing import Annotated, Callable, List, Optional

from fastapi import Depends
from sqlalchemy import select
from sqlalchemy import func, select
from sqlalchemy.orm.exc import StaleDataError
from fixcloudutils.util import utc

Expand Down Expand Up @@ -51,6 +51,12 @@ async def list_by_workspace_id(
) -> List[CloudAccount]:
raise NotImplementedError

@abstractmethod
async def count_by_workspace_id(
self, workspace_id: WorkspaceId, ready_for_collection: bool = False, non_deleted: bool = False
) -> int:
raise NotImplementedError

@abstractmethod
async def list_all_discovered_accounts(self) -> List[CloudAccount]:
raise NotImplementedError
Expand Down Expand Up @@ -81,10 +87,11 @@ def _update_state_dependent_fields(
case CloudAccountStates.Detected():
state = CloudAccountStates.Detected.state_name

case CloudAccountStates.Discovered(AwsCloudAccess(external_id, role_name)):
case CloudAccountStates.Discovered(AwsCloudAccess(external_id, role_name), ex_enabled):
role_name = role_name
external_id = external_id
state = CloudAccountStates.Discovered.state_name
enabled = ex_enabled

case CloudAccountStates.Configured(AwsCloudAccess(external_id, role_name), ex_enabled, ex_scan):
external_id = external_id
Expand Down Expand Up @@ -212,6 +219,22 @@ async def list_by_workspace_id(
accounts = results.scalars().all()
return [acc.to_model() for acc in accounts]

async def count_by_workspace_id(
self, workspace_id: WorkspaceId, ready_for_collection: bool = False, non_deleted: bool = False
) -> int:
"""Get a list of cloud accounts by tenant id."""
async with self.session_maker() as session:
statement = select(func.count(orm.CloudAccount.id)).where(orm.CloudAccount.tenant_id == workspace_id)
if ready_for_collection:
statement = statement.where(orm.CloudAccount.state == CloudAccountStates.Configured.state_name).where(
orm.CloudAccount.enabled.is_(True)
)
if non_deleted:
statement = statement.where(orm.CloudAccount.state != CloudAccountStates.Deleted.state_name)
results = await session.execute(statement)
accounts_count = results.scalar_one()
return accounts_count

async def list_all_discovered_accounts(self) -> List[CloudAccount]:
"""Get a list of all discovered cloud accounts."""
async with self.session_maker() as session:
Expand Down
2 changes: 1 addition & 1 deletion fixbackend/cloud_accounts/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async def delete_cloud_account(
service: CloudAccountServiceDependency,
_: Annotated[bool, Depends(WorkspacePermissionChecker(WorkspacePermissions.update_cloud_accounts))],
) -> None:
await service.delete_cloud_account(user, cloud_account_id, workspace.id)
await service.delete_cloud_account(user.id, cloud_account_id, workspace.id)

@router.patch("/{workspace_id}/cloud_account/{cloud_account_id}/enable")
async def enable_cloud_account(
Expand Down
4 changes: 2 additions & 2 deletions fixbackend/cloud_accounts/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from abc import ABC, abstractmethod
from typing import List, Optional

from fixbackend.auth.models import User
from fixbackend.cloud_accounts.models import CloudAccount
from fixbackend.ids import (
AwsRoleName,
Expand All @@ -25,6 +24,7 @@
ExternalId,
FixCloudAccountId,
UserCloudAccountName,
UserId,
WorkspaceId,
)

Expand All @@ -49,7 +49,7 @@ async def create_aws_account(

@abstractmethod
async def delete_cloud_account(
self, user: User, cloud_account_id: FixCloudAccountId, workspace_id: WorkspaceId
self, user_id: UserId, cloud_account_id: FixCloudAccountId, workspace_id: WorkspaceId
) -> None:
"""Delete a cloud account."""
raise NotImplementedError
Expand Down
Loading

0 comments on commit 816da05

Please sign in to comment.