createProject static method

Future<void> createProject(
  1. Client cloudApiClient, {
  2. required CommandLogger logger,
  3. required String projectId,
  4. required PlanProfile? plan,
  5. required bool enableDb,
  6. bool skipConfirmation = false,
  7. bool suppressCommandMessages = false,
})

Subcommand to create a new tenant project.

Implementation

static Future<void> createProject(
  final Client cloudApiClient, {
  required final CommandLogger logger,
  required final String projectId,
  required final PlanProfile? plan,
  required final bool enableDb,
  final bool skipConfirmation = false,
  final bool suppressCommandMessages = false,
}) async {
  if (!skipConfirmation) {
    await UserConfirmations.confirmNewProjectCostAcceptance(logger);
  }

  UuidValue? subscriptionId;
  if (plan == null) {
    // If no plan is specified and user has a legacy plan, use that.
    final subscriptions = await cloudApiClient.plans.listSubscriptions();
    if (subscriptions.isNotEmpty) {
      final legacySubscription = subscriptions
          .where(
            (final s) =>
                _legacyPlanNames.contains(s.planProductId.split(':').first),
          )
          .firstOrNull;
      if (legacySubscription != null) {
        if (!suppressCommandMessages) {
          logger.init('Creating Serverpod Cloud project "$projectId".');
          logger.info('On plan: ${legacySubscription.planDisplayName}');
        }
        subscriptionId = legacySubscription.subscriptionId;
      }
    }
  }

  if (subscriptionId == null) {
    final planProductName = plan?.name ?? defaultPlan;
    subscriptionId = await cloudApiClient.plans.procurePlan(
      planProductName: planProductName,
    );
    if (!suppressCommandMessages) {
      logger.init('Creating Serverpod Cloud project "$projectId".');
      logger.info('On plan: $planProductName');
    }
  }

  try {
    await logger.progress(
      'Registering Serverpod Cloud project',
      successMessage: 'Project registration successful.',
      padRight: StatusCommands.progressMessagePadLength,
      newParagraph: true,
      () async {
        await cloudApiClient.projects.createProject(
          cloudProjectId: projectId,
          projectProductName: plan?.projectProductName,
          underSubscriptionId: subscriptionId,
        );
        return true;
      },
    );
  } on Exception catch (e, s) {
    throw FailureException.nested(
      e,
      s,
      'Request to create a new project failed',
    );
  }

  if (enableDb) {
    await logger.progress(
      'Requesting database creation',
      successMessage: 'Database creation request sent.',
      padRight: StatusCommands.progressMessagePadLength,
      () async {
        try {
          await cloudApiClient.database.enableDatabase(
            cloudCapsuleId: projectId,
          );
          return true;
        } on Exception catch (e, s) {
          throw FailureException.nested(
            e,
            s,
            'Request to create a database for the new project failed',
          );
        }
      },
    );
  }

  if (!suppressCommandMessages) {
    logger.success('Serverpod Cloud project created.', newParagraph: true);
  }
}