Skip to content

Commit

Permalink
update provisioning
Browse files Browse the repository at this point in the history
  • Loading branch information
pablonyx committed Nov 5, 2024
1 parent a23004a commit a5217ac
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 237 deletions.
7 changes: 3 additions & 4 deletions backend/danswer/auth/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@
from shared_configs.configs import POSTGRES_DEFAULT_SCHEMA
from shared_configs.contextvars import CURRENT_TENANT_ID_CONTEXTVAR


logger = setup_logger()


Expand Down Expand Up @@ -239,7 +238,7 @@ async def create(
safe: bool = False,
request: Optional[Request] = None,
) -> User:
tenant_id = get_or_create_tenant_id(user_create.email)
tenant_id = await get_or_create_tenant_id(user_create.email)

async with get_async_session_with_tenant(tenant_id) as db_session:
token = CURRENT_TENANT_ID_CONTEXTVAR.set(tenant_id)
Expand All @@ -262,7 +261,7 @@ async def create(
user_create.role = UserRole.BASIC

try:
user = await super().create(user_create, safe=safe, request=request)
user = await super().create(user_create, safe=safe, request=request) # type: ignore
except exceptions.UserAlreadyExists:
user = await self.get_by_email(user_create.email)
# Handle case where user has used product outside of web and is now creating an account through web
Expand Down Expand Up @@ -299,7 +298,7 @@ async def oauth_callback(
associate_by_email: bool = False,
is_verified_by_default: bool = False,
) -> models.UOAP:
tenant_id = get_or_create_tenant_id(account_email)
tenant_id = await get_or_create_tenant_id(account_email)

if not tenant_id:
raise HTTPException(status_code=401, detail="User not found")
Expand Down
2 changes: 1 addition & 1 deletion backend/danswer/server/manage/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
from ee.danswer.db.user_group import remove_curator_status__no_commit
from ee.danswer.server.tenants.billing import register_tenant_users
from ee.danswer.server.tenants.provisioning import add_users_to_tenant
from ee.danswer.server.tenants.provisioning import remove_users_from_tenant
from ee.danswer.server.tenants.user_mapping import remove_users_from_tenant
from shared_configs.configs import MULTI_TENANT

logger = setup_logger()
Expand Down
54 changes: 0 additions & 54 deletions backend/ee/danswer/server/tenants/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,15 @@
from danswer.db.users import get_user_by_email
from danswer.server.settings.store import load_settings
from danswer.server.settings.store import store_settings
from danswer.setup import setup_danswer
from danswer.utils.logger import setup_logger
from ee.danswer.auth.users import current_cloud_superuser
from ee.danswer.configs.app_configs import STRIPE_SECRET_KEY
from ee.danswer.server.tenants.access import control_plane_dep
from ee.danswer.server.tenants.billing import fetch_billing_information
from ee.danswer.server.tenants.billing import fetch_tenant_stripe_information
from ee.danswer.server.tenants.models import BillingInformation
from ee.danswer.server.tenants.models import CreateTenantRequest
from ee.danswer.server.tenants.models import ImpersonateRequest
from ee.danswer.server.tenants.models import ProductGatingRequest
from ee.danswer.server.tenants.provisioning import add_users_to_tenant
from ee.danswer.server.tenants.provisioning import configure_default_api_keys
from ee.danswer.server.tenants.provisioning import ensure_schema_exists
from ee.danswer.server.tenants.provisioning import run_alembic_migrations
from ee.danswer.server.tenants.provisioning import user_owns_a_tenant
from shared_configs.configs import MULTI_TENANT
from shared_configs.contextvars import CURRENT_TENANT_ID_CONTEXTVAR

stripe.api_key = STRIPE_SECRET_KEY
Expand All @@ -40,52 +32,6 @@
router = APIRouter(prefix="/tenants")


@router.post("/create")
def create_tenant(
create_tenant_request: CreateTenantRequest, _: None = Depends(control_plane_dep)
) -> dict[str, str]:
if not MULTI_TENANT:
raise HTTPException(status_code=403, detail="Multi-tenancy is not enabled")

tenant_id = create_tenant_request.tenant_id
email = create_tenant_request.initial_admin_email
token = None

if user_owns_a_tenant(email):
raise HTTPException(
status_code=409, detail="User already belongs to an organization"
)

try:
if not ensure_schema_exists(tenant_id):
logger.info(f"Created schema for tenant {tenant_id}")
else:
logger.info(f"Schema already exists for tenant {tenant_id}")

token = CURRENT_TENANT_ID_CONTEXTVAR.set(tenant_id)
run_alembic_migrations(tenant_id)

with get_session_with_tenant(tenant_id) as db_session:
setup_danswer(db_session, tenant_id)

configure_default_api_keys(db_session)

add_users_to_tenant([email], tenant_id)

return {
"status": "success",
"message": f"Tenant {tenant_id} created successfully",
}
except Exception as e:
logger.exception(f"Failed to create tenant {tenant_id}: {str(e)}")
raise HTTPException(
status_code=500, detail=f"Failed to create tenant: {str(e)}"
)
finally:
if token is not None:
CURRENT_TENANT_ID_CONTEXTVAR.reset(token)


@router.post("/product-gating")
def gate_product(
product_gating_request: ProductGatingRequest, _: None = Depends(control_plane_dep)
Expand Down
Loading

0 comments on commit a5217ac

Please sign in to comment.