createPlan static method

Future<CreatedPlan> createPlan(
  1. Client cloudApiClient, {
  2. required PlanProfile? plan,
})

Resolves or procures the subscription the new project will be created under.

Implementation

static Future<CreatedPlan> createPlan(
  Client cloudApiClient, {
  required PlanProfile? plan,
}) async {
  if (plan == null) {
    late final List<SubscriptionInfo> subscriptions;
    try {
      subscriptions = await cloudApiClient.plans.listSubscriptions();
    } on Exception catch (e, s) {
      throw FailureException.nested(e, s, 'Request to list plans failed');
    }
    if (subscriptions.isNotEmpty) {
      final legacySubscription = subscriptions
          .where(
            (s) =>
                _legacyPlanNames.contains(s.planProductId.split(':').first),
          )
          .firstOrNull;
      if (legacySubscription != null) {
        return CreatedPlan(
          subscriptionId: legacySubscription.subscriptionId,
          planDisplayName: legacySubscription.planDisplayName,
        );
      }
    }
  }

  final planProductName = plan?.name ?? defaultPlan;
  try {
    final subscriptionId = await cloudApiClient.plans.procurePlan(
      planProductName: planProductName,
    );
    return CreatedPlan(
      subscriptionId: subscriptionId,
      planDisplayName: planProductName,
    );
  } on Exception catch (e, s) {
    throw FailureException.nested(e, s, 'Request to procure a plan failed');
  }
}