| 1 | from datetime import datetime |
| 2 | from typing import List |
| 3 | from typing import Optional |
| 4 | |
| 5 | from sqlmodel import Field |
| 6 | from sqlmodel import Relationship |
| 7 | from sqlmodel import SQLModel |
| 8 | |
| 9 | |
| 10 | class ConnectorHistory(SQLModel, table=True): |
| 11 | """ |
| 12 | Model representing the history logs of each connector. |
| 13 | |
| 14 | :ivar id: Unique integer ID of the history log. |
| 15 | :ivar connector_id: Foreign key linking to the Connectors table. |
| 16 | :ivar change_timestamp: Timestamp when the change was made. |
| 17 | :ivar change_description: Description of the change. |
| 18 | :ivar connector: Relationship to the Connectors model. |
| 19 | """ |
| 20 | |
| 21 | id: Optional[int] = Field(default=None, primary_key=True) |
| 22 | connector_id: int = Field(foreign_key="connectors.id") |
| 23 | change_timestamp: datetime = Field(default=datetime.utcnow()) |
| 24 | change_description: str |
| 25 | |
| 26 | # Relationship |
| 27 | connector: Optional["Connectors"] = Relationship(back_populates="history_logs") |
| 28 | |
| 29 | |
| 30 | class Connectors(SQLModel, table=True): |
| 31 | """ |
| 32 | Model representing each connector and its attributes. |
| 33 | |
| 34 | :ivar id: Unique integer ID of the connector. |
| 35 | :ivar connector_name: Name of the connector. |
| 36 | :ivar connector_type: Type or version of the connector. |
| 37 | :ivar connector_url: URL endpoint of the connector. |
| 38 | :ivar connector_last_updated: Timestamp when the connector was last updated. |
| 39 | :ivar connector_username: Optional username for the connector. |
| 40 | :ivar connector_password: Optional password for the connector. |
| 41 | :ivar connector_api_key: Optional API key for the connector. |
| 42 | :ivar connector_description: Description of what the connector does. |
| 43 | :ivar connector_supports: Information on what the connector supports. |
| 44 | :ivar connector_configured: Boolean indicating if the connector is configured. |
| 45 | :ivar connector_verified: Boolean indicating if the connector is verified. |
| 46 | :ivar connector_accepts_api_key: Boolean indicating if the connector accepts API keys. |
| 47 | :ivar connector_accepts_username_password: Boolean indicating if the connector accepts username and password. |
| 48 | :ivar connector_accepts_file: Boolean indicating if the connector accepts files. |
| 49 | :ivar history_logs: Relationship to the ConnectorHistory model. |
| 50 | """ |
| 51 | |
| 52 | id: Optional[int] = Field(default=None, primary_key=True) |
| 53 | connector_name: str = Field(max_length=256) |
| 54 | connector_type: str = Field(max_length=256) |
| 55 | connector_url: str = Field(max_length=750) |
| 56 | connector_last_updated: datetime = Field(default=datetime.utcnow()) |
| 57 | connector_username: Optional[str] = Field(default=None, max_length=256) |
| 58 | connector_password: Optional[str] = Field(default=None, max_length=256) |
| 59 | connector_api_key: Optional[str] = Field(default=None, max_length=750) |
| 60 | |
| 61 | # Fields moved from ConnectorsAvailable |
| 62 | connector_description: Optional[str] = Field(default=None, max_length=1000) |
| 63 | connector_supports: Optional[str] = Field(default=None, max_length=1000) |
| 64 | connector_configured: bool = Field(default=False) |
| 65 | connector_verified: bool = Field(default=False) |
| 66 | connector_accepts_host_only: bool = Field(default=False) |
| 67 | connector_accepts_api_key: bool = Field(default=False) |
| 68 | connector_accepts_username_password: bool = Field(default=False) |
| 69 | connector_accepts_file: bool = Field(default=False) |
| 70 | connector_accepts_extra_data: bool = Field(default=False) |
| 71 | connector_extra_data: Optional[str] = Field(default=None, max_length=1000) |
| 72 | connector_enabled: bool = Field(default=False) |
| 73 | |
| 74 | # Relationship |
| 75 | history_logs: List[ConnectorHistory] = Relationship( |
| 76 | back_populates="connector", |
| 77 | sa_relationship_kwargs={"lazy": "selectin"}, |
| 78 | ) |
| 79 | |
| 80 | |
| 81 | # Example usage |
| 82 | # new_connector = Connectors( |
| 83 | # connector_name="Wazuh-Indexer", |
| 84 | # connector_type="4.4.1", |
| 85 | # connector_url="https://ashwix01.socfortress.local:9200", |
| 86 | # connector_username="admin", |
| 87 | # connector_password="password_here", |
| 88 | # connector_api_key="api_key_here", |
| 89 | # # Fields from ConnectorsAvailable |
| 90 | # connector_description="Description here", |
| 91 | # connector_supports="Supports list here", |
| 92 | # connector_configured=True, |
| 93 | # connector_verified=True, |
| 94 | # connector_accepts_api_key=True, |
| 95 | # connector_accepts_username_password=True, |
| 96 | # connector_accepts_file=False |
| 97 | # ) |
| 98 | |
| 99 | # new_log = ConnectorHistory( |
| 100 | # connector_id=1, # This should be the ID of the corresponding connector |
| 101 | # change_description="Changed the API key." |
| 102 | # ) |