-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathbackend.ts
More file actions
741 lines (660 loc) · 25.1 KB
/
backend.ts
File metadata and controls
741 lines (660 loc) · 25.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
import * as gcf from "../../gcp/cloudfunctions";
import * as gcfV2 from "../../gcp/cloudfunctionsv2";
import * as utils from "../../utils";
import { Runtime } from "./runtimes/supported";
import { FirebaseError } from "../../error";
import { Context } from "./args";
import { assertExhaustive, flattenArray } from "../../functional";
/** Retry settings for a ScheduleSpec. */
export interface ScheduleRetryConfig {
retryCount?: number | null;
maxRetrySeconds?: number | null;
minBackoffSeconds?: number | null;
maxBackoffSeconds?: number | null;
maxDoublings?: number | null;
}
export interface ScheduleTrigger {
// Note: schedule is missing in the existingBackend because we
// don't actually spend the API call looking up the schedule;
// we just infer identifiers from function labels. OTOH, schedule cannot
// be null, because there is no default to set it to.
schedule?: string;
timeZone?: string | null;
retryConfig?: ScheduleRetryConfig | null;
attemptDeadlineSeconds?: number | null;
}
/** Something that has a ScheduleTrigger */
export interface ScheduleTriggered {
scheduleTrigger: ScheduleTrigger;
}
/** API agnostic version of a Cloud Function's HTTPs trigger. */
export interface HttpsTrigger {
invoker?: string[] | null;
}
/** Something that has an HTTPS trigger */
export interface HttpsTriggered {
httpsTrigger: HttpsTrigger;
}
/** API agnostic version of a Firebase callable function. */
export type CallableTrigger = {
genkitAction?: string;
};
/** Something that has a callable trigger */
export interface CallableTriggered {
callableTrigger: CallableTrigger;
}
type EventFilterKey = "resource" | "topic" | "bucket" | "alerttype" | "appid" | string;
/** API agnostic version of a Cloud Function's event trigger. */
export interface EventTrigger {
/**
* Primary filter for events. Must be specified for all triggers.
* Event sources introduced during the GCFv1 alpha will have a
* eventType that looks like providers/firebase.database/eventTypes/ref.create
* Event sources from GCF beta+ have event types that look like
* google.firebase.database.ref.create.
* Event sources from EventArc are versioned and have names that
* look like google.cloud.pubsub.topic.v1.messagePublished
*/
eventType: string;
/**
* Additional exact-match filters for narrowing down which events to receive.
*
* While not required by the GCF API, this is always provided in
* the Cloud Console, and we are likely to always require it as well.
* V1 functions will always (and only) have the "resource" filter.
* V2 will have arbitrary filters and some EventArc filters will be
* top-level keys in the GCF API (e.g. "pubsubTopic").
*/
eventFilters?: Record<EventFilterKey, string>;
/**
* Additional path-pattern filters for narrowing down which events to receive.
*/
eventFilterPathPatterns?: Record<string, string>;
/** Should failures in a function execution cause an event to be retried. */
retry: boolean;
/**
* The region of a trigger, which may not be the same region as the function.
* Cross-regional triggers are not permitted, i.e. triggers that are in a
* single-region location that is different from the function's region.
* When omitted, the region defaults to the function's region.
*/
region?: string;
/**
* Which service account EventArc should use to emit a function.
* This field is ignored for v1 and defaults to the
*/
serviceAccount?: string | null;
/**
* The name of the channel where the function receive events.
* Must be provided to receive custom events.
*/
channel?: string;
}
/** Something that has an EventTrigger */
export interface EventTriggered {
eventTrigger: EventTrigger;
}
export interface TaskQueueRateLimits {
maxConcurrentDispatches?: number | null;
maxDispatchesPerSecond?: number | null;
}
export interface TaskQueueRetryConfig {
maxAttempts?: number | null;
maxRetrySeconds?: number | null;
maxBackoffSeconds?: number | null;
maxDoublings?: number | null;
minBackoffSeconds?: number | null;
}
export interface TaskQueueTrigger {
rateLimits?: TaskQueueRateLimits | null;
retryConfig?: TaskQueueRetryConfig | null;
invoker?: string[] | null;
}
export interface TaskQueueTriggered {
taskQueueTrigger: TaskQueueTrigger;
}
export interface BlockingTrigger {
eventType: string;
options?: Record<string, unknown>;
}
export interface BlockingTriggered {
blockingTrigger: BlockingTrigger;
}
/** A user-friendly string for the kind of trigger of an endpoint. */
export function endpointTriggerType(endpoint: Endpoint): string {
if (isScheduleTriggered(endpoint)) {
return "scheduled";
} else if (isHttpsTriggered(endpoint)) {
return "https";
} else if (isCallableTriggered(endpoint)) {
return "callable";
} else if (isEventTriggered(endpoint)) {
return endpoint.eventTrigger.eventType;
} else if (isTaskQueueTriggered(endpoint)) {
return "taskQueue";
} else if (isBlockingTriggered(endpoint)) {
return endpoint.blockingTrigger.eventType;
}
assertExhaustive(endpoint);
}
// TODO(inlined): Enum types should be singularly named
export type VpcEgressSettings = "PRIVATE_RANGES_ONLY" | "ALL_TRAFFIC";
export const AllVpcEgressSettings: VpcEgressSettings[] = ["PRIVATE_RANGES_ONLY", "ALL_TRAFFIC"];
export type IngressSettings = "ALLOW_ALL" | "ALLOW_INTERNAL_ONLY" | "ALLOW_INTERNAL_AND_GCLB";
export const AllIngressSettings: IngressSettings[] = [
"ALLOW_ALL",
"ALLOW_INTERNAL_ONLY",
"ALLOW_INTERNAL_AND_GCLB",
];
export type MemoryOptions = 128 | 256 | 512 | 1024 | 2048 | 4096 | 8192 | 16384 | 32768;
const allMemoryOptions: MemoryOptions[] = [128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768];
/**
* Is a given number a valid MemoryOption?
*/
export function isValidMemoryOption(mem: unknown): mem is MemoryOptions {
return allMemoryOptions.includes(mem as MemoryOptions);
}
export function isValidEgressSetting(egress: unknown): egress is VpcEgressSettings {
return egress === "PRIVATE_RANGES_ONLY" || egress === "ALL_TRAFFIC";
}
export const MIN_ATTEMPT_DEADLINE_SECONDS = 15;
export const MAX_ATTEMPT_DEADLINE_SECONDS = 1800; // 30 mins
/**
* Is a given number a valid attempt deadline?
*/
export function isValidAttemptDeadline(seconds: number): boolean {
return seconds >= MIN_ATTEMPT_DEADLINE_SECONDS && seconds <= MAX_ATTEMPT_DEADLINE_SECONDS;
}
/** Returns a human-readable name with MB or GB suffix for a MemoryOption (MB). */
export function memoryOptionDisplayName(option: MemoryOptions): string {
return {
128: "128MB",
256: "256MB",
512: "512MB",
1024: "1GB",
2048: "2GB",
4096: "4GB",
8192: "8GB",
16384: "16GB",
32768: "32GB",
}[option];
}
/**
* Returns the gen 1 mapping of CPU for RAM. Used whenever a customer sets cpu to "gcf_gen1".
* Note that these values must be the right number of decimal places and include
* rounding errors (e.g. 0.1666 instead of 0.1667) so that we match GCF's
* behavior and don't unnecessarily create a new Run revision because our target
* CPU doesn't exactly match their CPU.
*/
export function memoryToGen1Cpu(memory: MemoryOptions): number {
return {
128: 0.0833, // ~1/12
256: 0.1666, // ~1/6
512: 0.3333, // ~1/3
1024: 0.5833, // ~5/7
2048: 1,
4096: 2,
8192: 2,
16384: 4,
32768: 8,
}[memory];
}
/**
* The amount of CPU we allocate in V2.
* Where these don't match with memoryToGen1Cpu we must manually configure these
* at the run service.
*/
export function memoryToGen2Cpu(memory: MemoryOptions): number {
return {
128: 1,
256: 1,
512: 1,
1024: 1,
2048: 1,
4096: 2,
8192: 2,
16384: 4,
32768: 8,
}[memory];
}
export const DEFAULT_CONCURRENCY = 80;
export const DEFAULT_MEMORY: MemoryOptions = 256;
export const MIN_CPU_FOR_CONCURRENCY = 1;
export const SCHEDULED_FUNCTION_LABEL = Object.freeze({ deployment: "firebase-schedule" });
/**
* IDs used to identify a regional resource.
* This type exists so we can have lightweight references from a Pub/Sub topic
* or Cloud Scheduler job to a function it invokes. Methods that operate on
* a function name should take a TargetIds instead of a FunctionSpec
* (e.g. functionName or functionLabel)
*
* It's possible that this type will need to become more complex when we support
* a Cloud Run revision. We'll cross that bridge when we get to it.
*/
export interface TargetIds {
id: string;
region: string;
project: string;
}
/**
* Represents a Secret or Secret Version resource.
* Based on https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations.functions#secretenvvar
*/
export interface SecretEnvVar {
key: string; // The environment variable this secret is accessible at
secret: string; // The id of the SecretVersion - ie for projects/myproject/secrets/mysecret, this is 'mysecret'
projectId: string; // The project containing the Secret
// Internal use only. Users cannot pin secret to a specific version.
version?: string;
}
/**
* Returns full resource name of a secret version.
*/
export function secretVersionName(s: SecretEnvVar): string {
return `projects/${s.projectId}/secrets/${s.secret}/versions/${s.version ?? "latest"}`;
}
export interface ServiceConfiguration {
concurrency?: number | null;
labels?: Record<string, string> | null;
environmentVariables?: Record<string, string> | null;
secretEnvironmentVariables?: SecretEnvVar[] | null;
availableMemoryMb?: MemoryOptions | null;
cpu?: number | "gcf_gen1" | null;
timeoutSeconds?: number | null;
maxInstances?: number | null;
minInstances?: number | null;
vpc?: {
connector: string;
egressSettings?: VpcEgressSettings | null;
} | null;
ingressSettings?: IngressSettings | null;
serviceAccount?: string | null;
}
export const AllFunctionsPlatforms = ["gcfv1", "gcfv2", "run"] as const;
export type FunctionsPlatform = (typeof AllFunctionsPlatforms)[number];
export type Triggered =
| HttpsTriggered
| CallableTriggered
| EventTriggered
| ScheduleTriggered
| TaskQueueTriggered
| BlockingTriggered;
/** Whether something has an HttpsTrigger */
export function isHttpsTriggered(triggered: Triggered): triggered is HttpsTriggered {
return {}.hasOwnProperty.call(triggered, "httpsTrigger");
}
/** Whether something has a CallableTrigger */
export function isCallableTriggered(triggered: Triggered): triggered is CallableTriggered {
return {}.hasOwnProperty.call(triggered, "callableTrigger");
}
/** Whether something has an EventTrigger */
export function isEventTriggered(triggered: Triggered): triggered is EventTriggered {
return {}.hasOwnProperty.call(triggered, "eventTrigger");
}
/** Whether something has a ScheduleTrigger */
export function isScheduleTriggered(triggered: Triggered): triggered is ScheduleTriggered {
return {}.hasOwnProperty.call(triggered, "scheduleTrigger");
}
/** Whether something has a TaskQueueTrigger */
export function isTaskQueueTriggered(triggered: Triggered): triggered is TaskQueueTriggered {
return {}.hasOwnProperty.call(triggered, "taskQueueTrigger");
}
/** Whether something has a BlockingTrigger */
export function isBlockingTriggered(triggered: Triggered): triggered is BlockingTriggered {
return {}.hasOwnProperty.call(triggered, "blockingTrigger");
}
export type EndpointState = "ACTIVE" | "FAILED" | "DEPLOYING" | "DELETING" | "UNKONWN";
/**
* An endpoint that serves traffic to a stack of services.
* For now, this is always a Cloud Function. Future iterations may use complex
* type unions to enforce that _either_ the Stack is all Functions or the
* stack is all Services.
*/
export type Endpoint = TargetIds &
ServiceConfiguration &
Triggered & {
entryPoint: string;
platform: FunctionsPlatform;
runtime?: Runtime;
// Output only
// "Codebase" is not part of the container contract. Instead, it's value is provided by firebase.json or derived
// from function labels.
codebase?: string;
// URI is available on GCFv1 for HTTPS triggers and on GCFv2 always
uri?: string;
// sourceUploadUrl is available on GCFv1 only
sourceUploadUrl?: string;
// source is available on GCFv2 only
source?: gcfV2.Source;
// TODO(colerogers): yank this field and set securityLevel to SECURE_ALWAYS
// in functionFromEndpoint during a breaking change release.
// This is a temporary fix to address https://github.com/firebase/firebase-tools/issues/4171
// GCFv1 can be http or https and GCFv2 is always https
securityLevel?: gcf.SecurityLevel;
// "Hash" is a value derived from function labels that is used to check if there are any changes
// between the serverside and local copies of the function.
hash?: string;
// Marked as true if a user specifically called this function or codebase with the --only flag.
targetedByOnly?: boolean;
// Output only. For v2 functions, this is the run service ID.
// This may eventually be different than id because GCF is going to start
// doing name translations
runServiceId?: string;
// State of the endpoint.
state?: EndpointState;
};
export interface RequiredAPI {
reason?: string;
api: string;
}
/** An API agnostic definition of an entire deployment a customer has or wants. */
export interface Backend {
/**
* requiredAPIs will be enabled when a Backend is deployed.
*/
requiredAPIs: RequiredAPI[];
environmentVariables: EnvironmentVariables;
// region -> id -> Endpoint
endpoints: Record<string, Record<string, Endpoint>>;
}
/**
* A helper utility to create an empty backend.
* Tests that verify the behavior of one possible resource in a Backend can use
* this method to avoid compiler errors when new fields are added to Backend.
*/
export function empty(): Backend {
return {
requiredAPIs: [],
endpoints: {},
environmentVariables: {},
};
}
/**
* A helper utility to create a backend from a list of endpoints.
* Useful in unit tests.
*/
export function of(...endpoints: Endpoint[]): Backend {
const bkend = { ...empty() };
for (const endpoint of endpoints) {
bkend.endpoints[endpoint.region] = bkend.endpoints[endpoint.region] || {};
if (bkend.endpoints[endpoint.region][endpoint.id]) {
throw new Error("Trying to create a backend with the same endpoint twice");
}
bkend.endpoints[endpoint.region][endpoint.id] = endpoint;
}
return bkend;
}
/**
* A helper utility to merge backends.
*/
export function merge(...backends: Backend[]): Backend {
// Merge all endpoints
const merged = of(...flattenArray(backends.map((b) => allEndpoints(b))));
// Merge all APIs
const apiToReasons: Record<string, Set<string>> = {};
for (const b of backends) {
for (const { api, reason } of b.requiredAPIs) {
const reasons = apiToReasons[api] || new Set();
if (reason) {
reasons.add(reason);
}
apiToReasons[api] = reasons;
}
// Mere all environment variables.
merged.environmentVariables = { ...merged.environmentVariables, ...b.environmentVariables };
}
for (const [api, reasons] of Object.entries(apiToReasons)) {
merged.requiredAPIs.push({ api, reason: Array.from(reasons).join(" ") });
}
return merged;
}
/**
* A helper utility to test whether a backend is empty.
* Consumers should use this before assuming a backend is empty (e.g. nooping
* deploy processes) because it's possible that fields have been added.
*/
export function isEmptyBackend(backend: Backend): boolean {
return (
Object.keys(backend.requiredAPIs).length === 0 && Object.keys(backend.endpoints).length === 0
);
}
/**
* Deprecated fields for Runtime Config.
* RuntimeConfig will not be available in production for GCFv2 functions.
* Future refactors of this code should move this type deeper into the codebase.
*/
export type RuntimeConfigValues = Record<string, unknown>;
/**
* Environment variables to be applied to backend instances.
* Applies to both GCFv1 and GCFv2 backends.
*/
export type EnvironmentVariables = Record<string, string>;
/**
* Gets the formal resource name for a Cloud Function.
*/
export function functionName(cloudFunction: TargetIds): string {
return `projects/${cloudFunction.project}/locations/${cloudFunction.region}/functions/${cloudFunction.id}`;
}
/**
* The naming pattern used to create a Pub/Sub Topic or Scheduler Job ID for a given scheduled function.
* This pattern is hard-coded and assumed throughout tooling, both in the Firebase Console and in the CLI.
* For e.g., we automatically assume a schedule and topic with this name exists when we list functions and
* see a label that it has an attached schedule. This saves us from making extra API calls.
* DANGER: We use the pattern defined here to deploy and delete schedules,
* and to display scheduled functions in the Firebase console
* If you change this pattern, Firebase console will stop displaying schedule descriptions
* and schedules created under the old pattern will no longer be cleaned up correctly
*/
export function scheduleIdForFunction(cloudFunction: TargetIds): string {
return `firebase-schedule-${cloudFunction.id}-${cloudFunction.region}`;
}
/**
* A caching accessor of the existing backend.
* The method explicitly loads Cloud Functions from their API but implicitly deduces
* functions' schedules and topics based on function labels. Functions that are not
* deployed with the Firebase CLI are included so that we can support customers moving
* a function that was managed with GCloud to managed by Firebase as an update operation.
* To determine whether a function was already managed by firebase-tools use
* deploymentTool.isFirebaseManaged(function.labels)
* @param context A context object, passed from the Command library and used for caching.
* @param forceRefresh If true, ignores and overwrites the cache. These cases should eventually go away.
* @return The backend
*/
export function existingBackend(context: Context, forceRefresh?: boolean): Promise<Backend> {
if (!context.existingBackendPromise || forceRefresh) {
context.existingBackendPromise = loadExistingBackend(context);
}
return context.existingBackendPromise;
}
async function loadExistingBackend(ctx: Context): Promise<Backend> {
// Note: is it worth deducing the APIs that must have been enabled for this backend to work?
// it could reduce redundant API calls for enabling the APIs.
const existingBackend = {
...empty(),
};
const unreachableRegions = {
gcfV1: [] as string[],
gcfV2: [] as string[],
run: [] as string[],
};
const gcfV1Results = await gcf.listAllFunctions(ctx.projectId);
for (const apiFunction of gcfV1Results.functions) {
const endpoint = gcf.endpointFromFunction(apiFunction);
existingBackend.endpoints[endpoint.region] = existingBackend.endpoints[endpoint.region] || {};
existingBackend.endpoints[endpoint.region][endpoint.id] = endpoint;
}
unreachableRegions.gcfV1 = gcfV1Results.unreachable;
let gcfV2Results;
try {
gcfV2Results = await gcfV2.listAllFunctions(ctx.projectId);
for (const apiFunction of gcfV2Results.functions) {
const endpoint = gcfV2.endpointFromFunction(apiFunction);
existingBackend.endpoints[endpoint.region] = existingBackend.endpoints[endpoint.region] || {};
existingBackend.endpoints[endpoint.region][endpoint.id] = endpoint;
}
unreachableRegions.gcfV2 = gcfV2Results.unreachable;
} catch (err: any) {
if (err.status === 404 && err.message?.toLowerCase().includes("method not found")) {
// customer has preview enabled without allowlist set
} else {
throw err;
}
}
ctx.existingBackend = existingBackend;
ctx.unreachableRegions = unreachableRegions;
return ctx.existingBackend;
}
/**
* A helper function that guards against unavailable regions affecting a backend deployment.
* If the desired backend uses a region that is unavailable, a FirebaseError is thrown.
* If a region is unavailable but the desired backend does not use it, a warning is logged
* that the standard cleanup process won't happen in that region.
* @param context A context object from the Command library. Used for caching.
* @param want The desired backend. Can be backend.empty() to only warn about unavailability.
*/
export async function checkAvailability(context: Context, want: Backend): Promise<void> {
await existingBackend(context);
const gcfV1Regions = new Set();
const gcfV2Regions = new Set();
for (const ep of allEndpoints(want)) {
if (ep.platform === "gcfv1") {
gcfV1Regions.add(ep.region);
} else {
gcfV2Regions.add(ep.region);
}
}
const neededUnreachableV1 = context.unreachableRegions?.gcfV1.filter((region) =>
gcfV1Regions.has(region),
);
const neededUnreachableV2 = context.unreachableRegions?.gcfV2.filter((region) =>
gcfV2Regions.has(region),
);
if (neededUnreachableV1?.length) {
throw new FirebaseError(
"The following Cloud Functions regions are currently unreachable:\n\t" +
neededUnreachableV1.join("\n\t") +
"\nThis deployment contains functions in those regions. Please try again in a few minutes, or exclude these regions from your deployment.",
);
}
if (neededUnreachableV2?.length) {
throw new FirebaseError(
"The following Cloud Functions V2 regions are currently unreachable:\n\t" +
neededUnreachableV2.join("\n\t") +
"\nThis deployment contains functions in those regions. Please try again in a few minutes, or exclude these regions from your deployment.",
);
}
if (context.unreachableRegions?.gcfV1.length) {
utils.logLabeledWarning(
"functions",
"The following Cloud Functions regions are currently unreachable:\n" +
context.unreachableRegions.gcfV1.join("\n") +
"\nCloud Functions in these regions won't be deleted.",
);
}
if (context.unreachableRegions?.gcfV2.length) {
utils.logLabeledWarning(
"functions",
"The following Cloud Functions V2 regions are currently unreachable:\n" +
context.unreachableRegions.gcfV2.join("\n") +
"\nCloud Functions in these regions won't be deleted.",
);
}
if (context.unreachableRegions?.run.length) {
utils.logLabeledWarning(
"functions",
"The following Cloud Run regions are currently unreachable:\n" +
context.unreachableRegions.run.join("\n") +
"\nCloud Run services in these regions won't be deleted.",
);
}
}
/** A helper utility for flattening all endpoints in a backend since typing is a bit wonky. */
export function allEndpoints(backend: Backend): Endpoint[] {
return Object.values(backend.endpoints).reduce((accum, perRegion) => {
return [...accum, ...Object.values(perRegion)];
}, [] as Endpoint[]);
}
/** A helper utility for checking whether an endpoint matches a predicate. */
export function someEndpoint(
backend: Backend,
predicate: (endpoint: Endpoint) => boolean,
): boolean {
for (const endpoints of Object.values(backend.endpoints)) {
if (Object.values<Endpoint>(endpoints).some(predicate)) {
return true;
}
}
return false;
}
/** A helper utility for finding an endpoint that matches the predicate. */
export function findEndpoint(
backend: Backend,
predicate: (endpoint: Endpoint) => boolean,
): Endpoint | undefined {
for (const endpoints of Object.values(backend.endpoints)) {
const endpoint = Object.values<Endpoint>(endpoints).find(predicate);
if (endpoint) return endpoint;
}
}
/** A helper utility function that returns a subset of the backend that includes only matching endpoints */
export function matchingBackend(
backend: Backend,
predicate: (endpoint: Endpoint) => boolean,
): Backend {
const filtered: Backend = {
...backend,
endpoints: {},
};
for (const endpoint of allEndpoints(backend)) {
if (!predicate(endpoint)) {
continue;
}
filtered.endpoints[endpoint.region] = filtered.endpoints[endpoint.region] || {};
filtered.endpoints[endpoint.region][endpoint.id] = endpoint;
}
return filtered;
}
/** A helper utility for flattening all endpoints in a region since typing is a bit wonky. */
export function regionalEndpoints(backend: Backend, region: string): Endpoint[] {
return backend.endpoints[region] ? Object.values<Endpoint>(backend.endpoints[region]) : [];
}
/** A curried function used for filters, returns a matcher for functions in a backend. */
export const hasEndpoint =
(backend: Backend) =>
(endpoint: Endpoint): boolean => {
return (
!!backend.endpoints[endpoint.region] && !!backend.endpoints[endpoint.region][endpoint.id]
);
};
/** A curried function that is the opposite of hasEndpoint */
export const missingEndpoint =
(backend: Backend) =>
(endpoint: Endpoint): boolean => {
return !hasEndpoint(backend)(endpoint);
};
/**
* A standard method for sorting endpoints for display.
* Future versions might consider sorting region by pricing tier before
* alphabetically
*/
export function compareFunctions(
left: TargetIds & { platform: FunctionsPlatform },
right: TargetIds & { platform: FunctionsPlatform },
): number {
if (left.platform !== right.platform) {
return right.platform < left.platform ? -1 : 1;
}
if (left.region < right.region) {
return -1;
}
if (left.region > right.region) {
return 1;
}
if (left.id < right.id) {
return -1;
}
if (left.id > right.id) {
return 1;
}
return 0;
}