Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customers db tables #66

Merged
merged 5 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@
from app.models import artifacts # noqa: F401
from app.models import cases # noqa: F401
from app.models import connectors # noqa: F401
from app.models import customers # noqa: F401
from app.models import graylog # noqa: F401
from app.models import influxdb_alerts # noqa: F401
from app.models import models # noqa: F401
from app.models import rules # noqa: F401
from app.models import smtp # noqa: F401
from app.models import sublime_alerts # noqa: F401
from app.models import users # noqa: F401
from app.models import wazuh_indexer # noqa: F401

migrate = Migrate(app, db)
Expand Down
222 changes: 222 additions & 0 deletions backend/app/models/customers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
from datetime import datetime

from sqlalchemy import Column
from sqlalchemy import DateTime
from sqlalchemy import Integer
from sqlalchemy import String

from app import db
from app import ma


class Customers(db.Model):
"""
Class for customers which stores various customer details.
This class inherits from SQLAlchemy's Model class.
"""

id: Column[Integer] = db.Column(db.Integer, primary_key=True)
customerCode: Column[String] = db.Column(db.String(11), nullable=False)
parentCustomerCode: Column[String] = db.Column(db.String(11))
customerName: Column[String] = db.Column(db.String(50), nullable=False)
contactLastName: Column[String] = db.Column(db.String(50))
contactFirstName: Column[String] = db.Column(db.String(50))
phone: Column[String] = db.Column(db.String(50))
addressLine1: Column[String] = db.Column(db.String(1024))
addressLine2: Column[String] = db.Column(db.String(1024))
city: Column[String] = db.Column(db.String(50))
state: Column[String] = db.Column(db.String(50))
postalCode: Column[String] = db.Column(db.String(15))
country: Column[String] = db.Column(db.String(50))
customerType: Column[String] = db.Column(db.String(50))
logoFile: Column[String] = db.Column(db.String(64))
createdAt: Column[DateTime] = db.Column(db.DateTime, default=datetime.utcnow)

def __init__(
self,
customerCode,
customerName,
parentCustomerCode=None,
contactLastName=None,
contactFirstName=None,
phone=None,
addressLine1=None,
addressLine2=None,
city=None,
state=None,
postalCode=None,
country=None,
customerType=None,
logoFile=None,
createdAt=None,
):
"""
Initialize a new instance of the Customer class.

:param customerCode: The code of the customer used as the Wazuh-Agent Label.
:param customerName: The name of the customer.
:param parentCustomerCode: The code of the parent customer.
:param contactLastName: The last name of the contact.
:param contactFirstName: The first name of the contact.
:param phone: The phone number of the contact.
:param addressLine1: The first line of the customer's address.
:param addressLine2: The second line of the customer's address.
:param city: The city of the customer's address.
:param state: The state of the customer's address.
:param postalCode: The postal code of the customer's address.
:param country: The country of the customer's address.
:param customerType: The type of the customer.
:param logoFile: The file name of the customer's logo.
:param createdAt: The date the customer was created.
"""
self.customerCode = customerCode
self.customerName = customerName
self.parentCustomerCode = parentCustomerCode
self.contactLastName = contactLastName
self.contactFirstName = contactFirstName
self.phone = phone
self.addressLine1 = addressLine1
self.addressLine2 = addressLine2
self.city = city
self.state = state
self.postalCode = postalCode
self.country = country
self.customerType = customerType
self.logoFile = logoFile
self.createdAt = createdAt

def __repr__(self) -> str:
"""
Returns a string representation of the Customer instance.

:return: A string representation of the customerCode.
"""
return f"<Customer {self.customerCode}>"


class CustomerSchema(ma.Schema):
"""
Schema for serializing and deserializing instances of the Customer class.
"""

class Meta:
"""
Meta class defines the fields to be serialized/deserialized.
"""

fields = (
"id",
"customerCode",
"parentCustomerCode",
"customerName",
"contactLastName",
"contactFirstName",
"phone",
"addressLine1",
"addressLine2",
"city",
"state",
"postalCode",
"country",
"customerType",
"logoFile",
"createdAt",
)


customer_schema: CustomerSchema = CustomerSchema()
customers_schema: CustomerSchema = CustomerSchema(many=True)


class CustomersMeta(db.Model):
"""
Class for customermeta which stores various customer metadata.
This class inherits from SQLAlchemy's Model class.
"""

id: Column[Integer] = db.Column(db.Integer, primary_key=True)
clientName: Column[String] = db.Column(db.String(255))
customerCode: Column[String] = db.Column(db.String(11), nullable=False)
customerMetaGraylogIndex: Column[String] = db.Column(db.String(1024))
customerMetaGraylogStream: Column[String] = db.Column(db.String(1024))
customerMetaInfluxOrg: Column[String] = db.Column(db.String(1024))
customerMetaGrafanaOrg: Column[String] = db.Column(db.String(1024))
customerMetaWazuhGroup: Column[String] = db.Column(db.String(1024))
indexRetention: Column[Integer] = db.Column(db.Integer)
wazuhRegistrationPort: Column[Integer] = db.Column(db.Integer)
wazuhLogIngestionPort: Column[Integer] = db.Column(db.Integer)

def __init__(
self,
customerCode,
clientName=None,
customerMetaGraylogIndex=None,
customerMetaGraylogStream=None,
customerMetaInfluxOrg=None,
customerMetaGrafanaOrg=None,
customerMetaWazuhGroup=None,
indexRetention=None,
wazuhRegistrationPort=None,
wazuhLogIngestionPort=None,
):
"""
Initialize a new instance of the CustomerMeta class.

:param customerCode: The code of the customer.
:param clientName: The name of the client.
:param customerMetaGraylogIndex: The Graylog index of the customer metadata.
:param customerMetaGraylogStream: The Graylog stream of the customer metadata.
:param customerMetaInfluxOrg: The InfluxOrg of the customer metadata.
:param customerMetaGrafanaOrg: The GrafanaOrg of the customer metadata.
:param customerMetaWazuhGroup: The WazuhGroup of the customer metadata.
:param indexRetention: The index retention of the customer metadata.
:param wazuhRegistrationPort: The Wazuh registration port of the customer's Wazuh Agents.
:param wazuhLogIngestionPort: The Wazuh log ingestion port of the customer's Wazuh Agents.
"""
self.customerCode = customerCode
self.clientName = clientName
self.customerMetaGraylogIndex = customerMetaGraylogIndex
self.customerMetaGraylogStream = customerMetaGraylogStream
self.customerMetaInfluxOrg = customerMetaInfluxOrg
self.customerMetaGrafanaOrg = customerMetaGrafanaOrg
self.customerMetaWazuhGroup = customerMetaWazuhGroup
self.indexRetention = indexRetention
self.wazuhRegistrationPort = wazuhRegistrationPort
self.wazuhLogIngestionPort = wazuhLogIngestionPort

def __repr__(self) -> str:
"""
Returns a string representation of the CustomerMeta instance.

:return: A string representation of the customerCode.
"""
return f"<CustomerMeta {self.customerCode}>"


class CustomerMetaSchema(ma.Schema):
"""
Schema for serializing and deserializing instances of the CustomerMeta class.
"""

class Meta:
"""
Meta class defines the fields to be serialized/deserialized.
"""

fields = (
"id",
"clientName",
"customerCode",
"customerMetaGraylogIndex",
"customerMetaGraylogStream",
"customerMetaInfluxOrg",
"customerMetaGrafanaOrg",
"customerMetaWazuhGroup",
"indexRetention",
"wazuhRegistrationPort",
"wazuhLogIngestionPort",
)


customer_meta_schema: CustomerMetaSchema = CustomerMetaSchema()
customers_meta_schema: CustomerMetaSchema = CustomerMetaSchema(many=True)
93 changes: 93 additions & 0 deletions backend/app/models/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from datetime import datetime

from sqlalchemy import Column
from sqlalchemy import DateTime
from sqlalchemy import Integer
from sqlalchemy import String

from app import db
from app import ma


class Users(db.Model):
"""
Class for users which stores various user details.
This class inherits from SQLAlchemy's Model class.
"""

id: Column[Integer] = db.Column(db.Integer, primary_key=True)
customerCode: Column[String] = db.Column(db.String(11), nullable=False)
usersFirstName: Column[String] = db.Column(db.String(50))
usersLastName: Column[String] = db.Column(db.String(50))
usersEmail: Column[String] = db.Column(db.String(50))
usersRole: Column[String] = db.Column(db.String(100))
imageFile: Column[String] = db.Column(db.String(64))
notifications: Column[Integer] = db.Column(db.SmallInteger, nullable=False)
createdAt: Column[DateTime] = db.Column(db.DateTime, default=datetime.utcnow)

def __init__(
self,
customerCode: str,
notifications: int,
usersFirstName: str = None,
usersLastName: str = None,
usersEmail: str = None,
usersRole: str = None,
imageFile: str = None,
createdAt: datetime = None,
):
"""
Initialize a new instance of the Users class.

:param customerCode: The code of the customer.
:param usersFirstName: The first name of the user.
:param usersLastName: The last name of the user.
:param usersEmail: The email of the user.
:param usersRole: The role of the user.
:param imageFile: The file name of the user's image.
:param notifications: Whether the user has notifications enabled (1) or not (0).
:param createdAt: The date the user was created.
"""
self.customerCode = customerCode
self.usersFirstName = usersFirstName
self.usersLastName = usersLastName
self.usersEmail = usersEmail
self.usersRole = usersRole
self.imageFile = imageFile
self.notifications = notifications
self.createdAt = createdAt

def __repr__(self) -> str:
"""
Returns a string representation of the Users instance.

:return: A string representation of the usersEmail.
"""
return f"<User {self.usersEmail}>"


class UsersSchema(ma.Schema):
"""
Schema for serializing and deserializing instances of the Users class.
"""

class Meta:
"""
Meta class defines the fields to be serialized/deserialized.
"""

fields = (
"id",
"customerCode",
"usersFirstName",
"usersLastName",
"usersEmail",
"usersRole",
"imageFile",
"notifications",
"createdAt",
)


users_schema: UsersSchema = UsersSchema()
users_schema: UsersSchema = UsersSchema(many=True)
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Add Users related models

Revision ID: 09189f03c3ee
Revises: 9353cd02b7fc
Create Date: 2023-07-27 00:14:11.238239

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "09189f03c3ee"
down_revision = "9353cd02b7fc"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"users",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("customerCode", sa.String(length=11), nullable=False),
sa.Column("usersFirstName", sa.String(length=50), nullable=True),
sa.Column("usersLastName", sa.String(length=50), nullable=True),
sa.Column("usersEmail", sa.String(length=50), nullable=True),
sa.Column("usersRole", sa.String(length=100), nullable=True),
sa.Column("imageFile", sa.String(length=64), nullable=True),
sa.Column("notifications", sa.SmallInteger(), nullable=False),
sa.Column("createdAt", sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("users")
# ### end Alembic commands ###
Loading