Skip to content

Commit

Permalink
Merge pull request #2 from someengineering/lloesche/cloudaccounts
Browse files Browse the repository at this point in the history
Implement Workspace Status
  • Loading branch information
lloesche authored Sep 23, 2024
2 parents f6ce766 + c0ad3a5 commit 8d700bc
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ on:
pull_request:
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}
cancel-in-progress: true

jobs:
split-build:
name: "Build Docker images"
Expand Down
2 changes: 1 addition & 1 deletion fixattiosync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
__author__ = "Some Engineering Inc."
__license__ = "AGPL-3.0"
__copyright__ = "Copyright © 2024 Some Engineering Inc."
__version__ = "0.0.9"
__version__ = "0.0.10"
10 changes: 8 additions & 2 deletions fixattiosync/attioresources.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from datetime import datetime
from uuid import UUID
from typing import Optional, Self, Type, ClassVar, Any
from enum import Enum
from .logger import log


Expand Down Expand Up @@ -51,9 +52,14 @@ class AttioWorkspace(AttioResource):
users: list[AttioUser] = field(default_factory=list)

def __eq__(self: Self, other: Any) -> bool:
if not hasattr(other, "id") or not hasattr(other, "tier"):
if (
not hasattr(other, "id")
or not hasattr(other, "tier")
or not hasattr(other, "status")
or not isinstance(other.status, Enum)
):
return False
return bool(self.id == other.id and self.tier == other.tier)
return bool(self.id == other.id and self.tier == other.tier and self.status == other.status.value)

@classmethod
def make(cls: Type[Self], data: dict[str, Any]) -> Self:
Expand Down
15 changes: 14 additions & 1 deletion fixattiosync/fixdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from uuid import UUID
from argparse import ArgumentParser
from .logger import log
from .fixresources import FixUser, FixWorkspace
from .fixresources import FixUser, FixWorkspace, FixCloudAccount
from typing import Optional


Expand All @@ -20,6 +20,7 @@ def __init__(self, db: str, user: str, password: str, host: str = "localhost", p
self.hydrated = False
self.__workspaces: dict[UUID, FixWorkspace] = {}
self.__users: dict[UUID, FixUser] = {}
self.__cloud_accounts: dict[UUID, FixCloudAccount] = {}

@property
def users(self) -> list[FixUser]:
Expand Down Expand Up @@ -75,13 +76,25 @@ def hydrate(self) -> None:
for row in rows:
self.__workspaces[row["organization_id"]].users.append(self.__users[row["user_id"]])
self.__users[row["user_id"]].workspaces.append(self.__workspaces[row["organization_id"]])
with self.conn.cursor(row_factory=dict_row) as cursor:
cursor.execute('SELECT * FROM public."cloud_account";')
rows = cursor.fetchall()
for row in rows:
cloud_account = FixCloudAccount(**row)
self.__cloud_accounts[cloud_account.id] = cloud_account
if cloud_account.tenant_id in self.__workspaces:
self.__workspaces[cloud_account.tenant_id].cloud_accounts.append(cloud_account)
self.__workspaces[cloud_account.tenant_id].update_status()
else:
log.error(f"Data error: cloud account {cloud_account.id} does not have a workspace")
except psycopg.Error as e:
log.error(f"Error fetching data: {e}")
sys.exit(2)
finally:
self.close()
log.debug(f"Found {len(self.__workspaces)} workspaces in database")
log.debug(f"Found {len(self.__users)} users in database")
log.debug(f"Found {len(self.__cloud_accounts)} cloud accounts in database")
if len(self.__users) == 0 or len(self.__workspaces) == 0:
log.fatal("No data found in Fix database")
sys.exit(2)
Expand Down
68 changes: 66 additions & 2 deletions fixattiosync/fixresources.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime
from uuid import UUID
from typing import Optional, Self, Any
from enum import Enum
from .attioresources import AttioPerson, AttioWorkspace


Expand Down Expand Up @@ -71,6 +72,14 @@ def attio_person(self) -> dict[str, Any]:
}


class FixWorkspaceStatus(Enum):
Created = "Created"
Configured = "Configured"
Collected = "Collected"
Subscribed = "Subscribed"
Unsubscribed = "Unsubscribed"


@dataclass
class FixWorkspace:
id: UUID
Expand All @@ -88,11 +97,32 @@ class FixWorkspace:
tier_updated_at: Optional[datetime]
owner: Optional[FixUser] = None
users: list[FixUser] = field(default_factory=list)
cloud_accounts: list[FixCloudAccount] = field(default_factory=list)
status: FixWorkspaceStatus = FixWorkspaceStatus.Created

def __eq__(self: Self, other: Any) -> bool:
if not hasattr(other, "id") or not hasattr(other, "name") or not hasattr(other, "tier"):
if (
not hasattr(other, "id")
or not hasattr(other, "name")
or not hasattr(other, "tier")
or not hasattr(other, "status")
):
return False
return bool(self.id == other.id and self.name == other.name and self.tier == other.tier)
return bool(
self.id == other.id
and self.name == other.name
and self.tier == other.tier
and self.status.value == other.status
)

def update_status(self) -> None:
if self.subscription_id is not None:
self.status = FixWorkspaceStatus.Subscribed
return
if any(cloud_account.is_configured for cloud_account in self.cloud_accounts):
self.status = FixWorkspaceStatus.Configured
if any(cloud_account.last_scan_resources_scanned > 0 for cloud_account in self.cloud_accounts):
self.status = FixWorkspaceStatus.Collected

def attio_data(self) -> dict[str, Any]:
object_id = "workspaces"
Expand All @@ -103,6 +133,7 @@ def attio_data(self) -> dict[str, Any]:
"workspace_id": str(self.id),
"name": self.name,
"product_tier": self.tier,
"status": self.status.value,
}
}
}
Expand All @@ -111,3 +142,36 @@ def attio_data(self) -> dict[str, Any]:
"matching_attribute": matching_attribute,
"data": data,
}


@dataclass
class FixCloudAccount:
id: UUID
tenant_id: UUID
cloud: str
account_id: str
aws_role_name: Optional[str]
aws_external_id: Optional[UUID]
is_configured: bool
enabled: bool
privileged: bool
user_account_name: Optional[str]
api_account_name: Optional[str]
api_account_alias: Optional[str]
state: Optional[str]
error: Optional[str]
last_scan_duration_seconds: int
last_scan_started_at: Optional[datetime]
last_scan_resources_scanned: int
created_at: datetime
updated_at: datetime
state_updated_at: datetime
version_id: int
cf_stack_version: Optional[int]
scan: bool
failed_scan_count: int
gcp_service_account_key_id: Optional[UUID]
last_task_id: Optional[str]
azure_credential_id: Optional[UUID]
last_scan_resources_errors: int
last_degraded_scan_started_at: Optional[datetime]
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "fixattiosync"
version = "0.0.9"
version = "0.0.10"
authors = [{name="Some Engineering Inc."}]
description = "Fix Attio Sync"
license = {file="LICENSE"}
Expand Down

0 comments on commit 8d700bc

Please sign in to comment.