@hiiretail/gcp-infra-generators 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/dist/generators/clan-resources/clan-project/index.js +89 -189
  2. package/dist/generators/common-resources/bigquery/index.js +172 -267
  3. package/dist/generators/common-resources/budget/index.js +67 -153
  4. package/dist/generators/common-resources/cloud-armor/index.js +17 -167
  5. package/dist/generators/common-resources/cloud-storage/index.js +96 -205
  6. package/dist/generators/common-resources/cloudsql/index.js +71 -177
  7. package/dist/generators/common-resources/cloudsql-database/index.js +40 -287
  8. package/dist/generators/common-resources/confluent-cluster/index.js +23 -132
  9. package/dist/generators/common-resources/datastore/index.js +48 -194
  10. package/dist/generators/common-resources/elastic-cloud/index.js +22 -132
  11. package/dist/generators/common-resources/elastic-index-policy/handle-yaml.js +76 -0
  12. package/dist/generators/common-resources/elastic-index-policy/index.js +131 -286
  13. package/dist/generators/common-resources/elastic-template/index.js +52 -162
  14. package/dist/generators/common-resources/firestore/index.js +93 -233
  15. package/dist/generators/common-resources/iam/index.js +35 -157
  16. package/dist/generators/common-resources/iam/valid-prefix.js +8 -0
  17. package/dist/generators/common-resources/kafka-connect/index.js +35 -144
  18. package/dist/generators/common-resources/kafka-topics/index.js +20 -129
  19. package/dist/generators/common-resources/kms/index.js +31 -141
  20. package/dist/generators/common-resources/memorystore/index.js +42 -328
  21. package/dist/generators/common-resources/monitoring/handle-yaml.js +49 -0
  22. package/dist/generators/common-resources/monitoring/index.js +144 -322
  23. package/dist/generators/common-resources/monitoring/templates/alerts/generic-infra.yaml +37 -2
  24. package/dist/generators/common-resources/monitoring/validate.js +58 -0
  25. package/dist/generators/common-resources/pubsub/append.js +130 -0
  26. package/dist/generators/common-resources/pubsub/get-gcp-projects.js +34 -0
  27. package/dist/generators/common-resources/pubsub/handle-subscribers.js +68 -0
  28. package/dist/generators/common-resources/pubsub/index.js +194 -536
  29. package/dist/generators/common-resources/pubsub/validate.js +53 -0
  30. package/dist/generators/common-resources/scheduler/append.js +85 -0
  31. package/dist/generators/common-resources/scheduler/index.js +62 -249
  32. package/dist/generators/common-resources/spanner/append.js +31 -0
  33. package/dist/generators/common-resources/spanner/index.js +102 -269
  34. package/dist/generators/common-resources/spanner/validate.js +38 -0
  35. package/dist/generators/docs/rca/index.js +25 -135
  36. package/dist/generators/docs/runbook/index.js +16 -126
  37. package/dist/generators/docs/srb/index.js +33 -147
  38. package/dist/generators/docs/srb/run-docker.js +2 -0
  39. package/dist/generators/init/clan-infra/gcp-projects.js +47 -0
  40. package/dist/generators/init/clan-infra/index.js +95 -290
  41. package/dist/generators/init/clan-infra/tribe-clan-repo.js +38 -0
  42. package/dist/generators/init/clan-infra/validate.js +8 -0
  43. package/dist/generators/maintenance/manage-states/index.js +142 -219
  44. package/dist/generators/maintenance/update-modules/index.js +56 -155
  45. package/dist/generators/organization/clan-project/googlecloud.js +124 -0
  46. package/dist/generators/organization/clan-project/index.js +81 -303
  47. package/dist/node_modules/.package-lock.json +88 -31
  48. package/dist/node_modules/@google-cloud/storage/build/cjs/src/bucket.js +5 -5
  49. package/dist/node_modules/@google-cloud/storage/build/cjs/src/file.d.ts +1 -0
  50. package/dist/node_modules/@google-cloud/storage/build/cjs/src/file.js +10 -1
  51. package/dist/node_modules/@google-cloud/storage/build/cjs/src/storage.js +1 -1
  52. package/dist/node_modules/@google-cloud/storage/build/cjs/src/transfer-manager.d.ts +4 -4
  53. package/dist/node_modules/@google-cloud/storage/build/cjs/src/transfer-manager.js +4 -4
  54. package/dist/node_modules/@google-cloud/storage/build/cjs/src/util.d.ts +1 -1
  55. package/dist/node_modules/@google-cloud/storage/build/cjs/src/util.js +2 -2
  56. package/dist/node_modules/@google-cloud/storage/build/esm/src/bucket.js +5 -5
  57. package/dist/node_modules/@google-cloud/storage/build/esm/src/file.d.ts +1 -0
  58. package/dist/node_modules/@google-cloud/storage/build/esm/src/file.js +10 -1
  59. package/dist/node_modules/@google-cloud/storage/build/esm/src/storage.js +1 -1
  60. package/dist/node_modules/@google-cloud/storage/build/esm/src/transfer-manager.d.ts +4 -4
  61. package/dist/node_modules/@google-cloud/storage/build/esm/src/transfer-manager.js +4 -4
  62. package/dist/node_modules/@google-cloud/storage/build/esm/src/util.d.ts +1 -1
  63. package/dist/node_modules/@google-cloud/storage/build/esm/src/util.js +2 -2
  64. package/dist/node_modules/@google-cloud/storage/package.json +5 -5
  65. package/dist/package.json +45 -0
  66. package/dist/src/BaseGenerator.js +84 -0
  67. package/dist/src/SecretsGenerator.js +137 -0
  68. package/dist/src/cli.js +54 -255
  69. package/dist/src/dependency-check.js +48 -0
  70. package/dist/src/update-check.js +38 -0
  71. package/dist/src/validators.js +33 -0
  72. package/dist/src/yeoman.js +80 -0
  73. package/package.json +2 -3
  74. package/dist/node_modules/@google-cloud/storage/CHANGELOG.md +0 -1769
@@ -19,6 +19,7 @@ cloud_function:
19
19
  - resource.label.function_name
20
20
  documentation:
21
21
  subject: 'Function: `$${resource.label.function_name}`'
22
+ content: 'Review the function in Cloud Functions: https://console.cloud.google.com/run?deploymentType=function&project=<%-projectId%>'
22
23
  cloud_run:
23
24
  error_count:
24
25
  display_name: "[P3] <%-clan%> - Cloud Run | Error rate"
@@ -40,6 +41,7 @@ cloud_run:
40
41
  - resource.label.service_name
41
42
  documentation:
42
43
  subject: 'Service: `$${resource.label.service_name}`'
44
+ content: 'Review the service in Cloud Run: https://console.cloud.google.com/run?project=<%-projectId%>'
43
45
  request_latency:
44
46
  display_name: "[P3] <%-clan%> - Cloud Run | Request Latency"
45
47
  conditions:
@@ -59,6 +61,7 @@ cloud_run:
59
61
  - resource.label.service_name
60
62
  documentation:
61
63
  subject: 'Service: `$${resource.label.service_name}`'
64
+ content: 'Review the service in Cloud Run: https://console.cloud.google.com/run?project=<%-projectId%>'
62
65
  cpu_utilization:
63
66
  display_name: "[P3] <%-clan%> - Cloud Run | CPU Utilization"
64
67
  conditions:
@@ -72,12 +75,13 @@ cloud_run:
72
75
  duration: 300s
73
76
  aggregations:
74
77
  - alignment_period: 60s
75
- per_series_aligner: ALIGN_MEAN
78
+ per_series_aligner: ALIGN_PERCENTILE_99
76
79
  cross_series_reducer: REDUCE_PERCENTILE_99
77
80
  group_by_fields:
78
81
  - resource.label.service_name
79
82
  documentation:
80
83
  subject: 'Service: `$${resource.label.service_name}`'
84
+ content: 'Review the service in Cloud Run: https://console.cloud.google.com/run?project=<%-projectId%>'
81
85
  memory_utilization:
82
86
  display_name: "[P3] <%-clan%> - Cloud Run | Memory Utilization"
83
87
  conditions:
@@ -91,12 +95,13 @@ cloud_run:
91
95
  duration: 300s
92
96
  aggregations:
93
97
  - alignment_period: 60s
94
- per_series_aligner: ALIGN_MEAN
98
+ per_series_aligner: ALIGN_PERCENTILE_99
95
99
  cross_series_reducer: REDUCE_PERCENTILE_99
96
100
  group_by_fields:
97
101
  - resource.label.service_name
98
102
  documentation:
99
103
  subject: 'Service: `$${resource.label.service_name}`'
104
+ content: 'Review the service in Cloud Run: https://console.cloud.google.com/run?project=<%-projectId%>'
100
105
  startup_latency:
101
106
  display_name: "[P4] <%-clan%> - Cloud Run | Startup Latency"
102
107
  conditions:
@@ -116,6 +121,7 @@ cloud_run:
116
121
  - resource.label.service_name
117
122
  documentation:
118
123
  subject: 'Service: `$${resource.label.service_name}`'
124
+ content: 'Review the service in Cloud Run: https://console.cloud.google.com/run?project=<%-projectId%>'
119
125
  cloud_scheduler:
120
126
  failed_job:
121
127
  display_name: "[P4] <%-clan%> - Cloud Scheduler | Job Failed"
@@ -135,6 +141,7 @@ cloud_scheduler:
135
141
  - resource.label.job_id
136
142
  documentation:
137
143
  subject: 'Job: `$${resource.label.job_id}`'
144
+ content: 'Review the job in Cloud Scheduler: https://console.cloud.google.com/cloudscheduler?project=<%-projectId%>'
138
145
  failed_executions:
139
146
  display_name: "[P3] <%-clan%> - Cloud Scheduler | Failed Executions"
140
147
  conditions:
@@ -155,6 +162,7 @@ cloud_scheduler:
155
162
  - resource.label.job_id
156
163
  documentation:
157
164
  subject: 'Job: `$${resource.label.job_id}`'
165
+ content: 'Review the job in Cloud Scheduler: https://console.cloud.google.com/cloudscheduler?project=<%-projectId%>'
158
166
  cloud_sql:
159
167
  cpu_over_65:
160
168
  display_name: "[P3] <%-clan%> - CloudSQL | CPU over 65%"
@@ -175,6 +183,7 @@ cloud_sql:
175
183
  - resource.label.database_id
176
184
  documentation:
177
185
  subject: 'Database: `$${resource.label.database_id}`'
186
+ content: 'Review the database in Cloud SQL: https://console.cloud.google.com/sql/instances?project=<%-projectId%>'
178
187
  cpu_over_85:
179
188
  display_name: "[P3] <%-clan%> - CloudSQL | CPU over 85%"
180
189
  conditions:
@@ -194,6 +203,7 @@ cloud_sql:
194
203
  - resource.label.database_id
195
204
  documentation:
196
205
  subject: 'Database: `$${resource.label.database_id}`'
206
+ content: 'Review the database in Cloud SQL: https://console.cloud.google.com/sql/instances?project=<%-projectId%>'
197
207
  cpu_over_90:
198
208
  display_name: "[P3] <%-clan%> - CloudSQL | CPU over 90%"
199
209
  conditions:
@@ -213,6 +223,7 @@ cloud_sql:
213
223
  - resource.label.database_id
214
224
  documentation:
215
225
  subject: 'Database: `$${resource.label.database_id}`'
226
+ content: 'Review the database in Cloud SQL: https://console.cloud.google.com/sql/instances?project=<%-projectId%>'
216
227
  memory_over_90:
217
228
  display_name: "[P2] <%-clan%> - CloudSQL | Memory utilization above 90%"
218
229
  conditions:
@@ -232,6 +243,7 @@ cloud_sql:
232
243
  - resource.label.database_id
233
244
  documentation:
234
245
  subject: 'Database: `$${resource.label.database_id}`'
246
+ content: 'Review the database in Cloud SQL: https://console.cloud.google.com/sql/instances?project=<%-projectId%>'
235
247
  query_over_1s:
236
248
  display_name: "[P4] <%-clan%> - CloudSQL | Slow query"
237
249
  conditions:
@@ -251,6 +263,7 @@ cloud_sql:
251
263
  - resource.label.resource_id
252
264
  documentation:
253
265
  subject: 'Database: `$${resource.label.resource_id}`'
266
+ content: 'Review the database in Cloud SQL: https://console.cloud.google.com/sql/instances?project=<%-projectId%>'
254
267
  postgresql_connections:
255
268
  display_name: "[P3] <%-clan%> - CloudSQL | PostgreSQL Connections"
256
269
  conditions:
@@ -269,6 +282,7 @@ cloud_sql:
269
282
  - resource.label.database_id
270
283
  documentation:
271
284
  subject: 'Database: `$${resource.label.database_id}`'
285
+ content: 'Review the database in Cloud SQL: https://console.cloud.google.com/sql/instances?project=<%-projectId%>'
272
286
  aggregated_lock_time:
273
287
  display_name: "[P4] <%-clan%> - CloudSQL | Aggregated Lock Time"
274
288
  conditions:
@@ -287,6 +301,7 @@ cloud_sql:
287
301
  - resource.label.database_id
288
302
  documentation:
289
303
  subject: 'Database: `$${resource.label.database_id}`'
304
+ content: 'Review the database in Cloud SQL: https://console.cloud.google.com/sql/instances?project=<%-projectId%>'
290
305
  firestore:
291
306
  request_latencies:
292
307
  display_name: "[P3] <%-clan%> - Firestore | High request latencies"
@@ -306,6 +321,7 @@ firestore:
306
321
  - resource.label.database_id
307
322
  documentation:
308
323
  subject: 'Database: `$${resource.label.database_id}`'
324
+ content: 'Review the database in Firestore: https://console.cloud.google.com/firestore/databases?project=<%-projectId%>'
309
325
  failed_commits:
310
326
  display_name: "[P3] <%-clan%> - Firestore | High failed commits errors"
311
327
  conditions:
@@ -324,6 +340,9 @@ firestore:
324
340
  cross_series_reducer: REDUCE_SUM
325
341
  group_by_fields:
326
342
  - metric.label."response_code"
343
+ documentation:
344
+ subject: 'Database: `$${resource.label.database_id}`'
345
+ content: 'Review the database in Firestore: https://console.cloud.google.com/firestore/databases?project=<%-projectId%>'
327
346
  memorystore:
328
347
  memory_over_50:
329
348
  display_name: "[P3] <%-clan%> - Memorystore | Memory over 50%"
@@ -344,6 +363,7 @@ memorystore:
344
363
  - resource.label.instance_id
345
364
  documentation:
346
365
  subject: 'Instance: `$${resource.label.instance_id}`'
366
+ content: 'Review the instance in Memorystore: https://console.cloud.google.com/memorystore/redis/instances?project=<%-projectId%>'
347
367
  memory_over_75:
348
368
  display_name: "[P3] <%-clan%> - Memorystore | Memory over 75%"
349
369
  conditions:
@@ -363,6 +383,7 @@ memorystore:
363
383
  - resource.label.instance_id
364
384
  documentation:
365
385
  subject: 'Instance: `$${resource.label.instance_id}`'
386
+ content: 'Review the instance in Memorystore: https://console.cloud.google.com/memorystore/redis/instances?project=<%-projectId%>'
366
387
  memory_over_90:
367
388
  display_name: "[P2] <%-clan%> - Memorystore | Memory over 90%"
368
389
  conditions:
@@ -382,6 +403,7 @@ memorystore:
382
403
  - resource.label.instance_id
383
404
  documentation:
384
405
  subject: 'Instance: `$${resource.label.instance_id}`'
406
+ content: 'Review the instance in Memorystore: https://console.cloud.google.com/memorystore/redis/instances?project=<%-projectId%>'
385
407
  cpu_utilization:
386
408
  display_name: "[P2] <%-clan%> - Memorystore | CPU Utilization"
387
409
  conditions:
@@ -401,6 +423,7 @@ memorystore:
401
423
  - resource.label.instance_id
402
424
  documentation:
403
425
  subject: 'Instance: `$${resource.label.instance_id}`'
426
+ content: 'Review the instance in Memorystore: https://console.cloud.google.com/memorystore/redis/instances?project=<%-projectId%>'
404
427
  system_memory_overload_duration:
405
428
  display_name: "[P1] <%-clan%> - Memorystore | System Memory Overload Duration"
406
429
  conditions:
@@ -419,6 +442,7 @@ memorystore:
419
442
  - resource.label.instance_id
420
443
  documentation:
421
444
  subject: 'Instance: `$${resource.label.instance_id}`'
445
+ content: 'Review the instance in Memorystore: https://console.cloud.google.com/memorystore/redis/instances?project=<%-projectId%>'
422
446
  calls:
423
447
  display_name: "[P3] <%-clan%> - Memorystore | Calls"
424
448
  conditions:
@@ -438,6 +462,7 @@ memorystore:
438
462
  - resource.label.instance_id
439
463
  documentation:
440
464
  subject: 'Instance: `$${resource.label.instance_id}`'
465
+ content: 'Review the instance in Memorystore: https://console.cloud.google.com/memorystore/redis/instances?project=<%-projectId%>'
441
466
  pub_sub:
442
467
  unacknowledged_messages:
443
468
  display_name: "[P3] <%-clan%> - Pub/Sub | Undelivered message(s)"
@@ -457,6 +482,7 @@ pub_sub:
457
482
  - resource.label.subscription_id
458
483
  documentation:
459
484
  subject: 'Subscription: `$${resource.label.subscription_id}`'
485
+ content: 'Review the subscription in Pub/Sub: https://console.cloud.google.com/cloudpubsub/subscriptions?project=<%-projectId%>'
460
486
  messages_in_dlq:
461
487
  display_name: "[P3] <%-clan%> - Pub/Sub | Message(s) in DLQ"
462
488
  conditions:
@@ -477,6 +503,7 @@ pub_sub:
477
503
  - metric.label.response_code
478
504
  documentation:
479
505
  subject: 'Subscription: `$${resource.label.subscription_id}`, Response code: `$${metric.label.response_code}`'
506
+ content: 'Review the subscription in Pub/Sub: https://console.cloud.google.com/cloudpubsub/subscriptions?project=<%-projectId%>'
480
507
  latency:
481
508
  display_name: "[P3] <%-clan%> - Pub/Sub | Response latency distribution"
482
509
  conditions:
@@ -496,6 +523,7 @@ pub_sub:
496
523
  - resource.label.subscription_id
497
524
  documentation:
498
525
  subject: 'Subscription: `$${resource.label.subscription_id}`'
526
+ content: 'Review the subscription in Pub/Sub: https://console.cloud.google.com/cloudpubsub/subscriptions?project=<%-projectId%>'
499
527
  oldest_unacked_message_age:
500
528
  display_name: "[P3] <%-clan%> - Pub/Sub | Oldest unacked message age"
501
529
  conditions:
@@ -514,6 +542,7 @@ pub_sub:
514
542
  - resource.label.subscription_id
515
543
  documentation:
516
544
  subject: 'Subscription: `$${resource.label.subscription_id}`'
545
+ content: 'Review the subscription in Pub/Sub: https://console.cloud.google.com/cloudpubsub/subscriptions?project=<%-projectId%>'
517
546
  push_requests_ack:
518
547
  display_name: "[P3] <%-clan%> - Pub/Sub | Push requests ack"
519
548
  conditions:
@@ -533,6 +562,7 @@ pub_sub:
533
562
  - resource.label.subscription_id
534
563
  documentation:
535
564
  subject: 'Subscription: `$${resource.label.subscription_id}`'
565
+ content: 'Review the subscription in Pub/Sub: https://console.cloud.google.com/cloudpubsub/subscriptions?project=<%-projectId%>'
536
566
  push_requests_success:
537
567
  display_name: "[P3] <%-clan%> - Pub/Sub | Push requests success"
538
568
  conditions:
@@ -552,6 +582,7 @@ pub_sub:
552
582
  - resource.label.subscription_id
553
583
  documentation:
554
584
  subject: 'Subscription: `$${resource.label.subscription_id}`'
585
+ content: 'Review the subscription in Pub/Sub: https://console.cloud.google.com/cloudpubsub/subscriptions?project=<%-projectId%>'
555
586
  delivery_latency_health_score:
556
587
  display_name: "[P3] <%-clan%> - Pub/Sub | Delivery latency health score"
557
588
  conditions:
@@ -571,6 +602,7 @@ pub_sub:
571
602
  - resource.label.subscription_id
572
603
  documentation:
573
604
  subject: 'Subscription: `$${resource.label.subscription_id}`'
605
+ content: 'Review the subscription in Pub/Sub: https://console.cloud.google.com/cloudpubsub/subscriptions?project=<%-projectId%>'
574
606
  spanner:
575
607
  cpu_utilization_by_priority:
576
608
  display_name: "[P2] <%-clan%> - Spanner | CPU Utilization"
@@ -591,6 +623,7 @@ spanner:
591
623
  - metric.label.database
592
624
  documentation:
593
625
  subject: 'Instance: `$${resource.label.instance_id}`, Database: `$${resource.label.database}`'
626
+ content: 'Review the instance in Spanner: https://console.cloud.google.com/spanner/instances?project=<%-projectId%>'
594
627
  api_request_error_rate:
595
628
  display_name: "[P2] <%-clan%> - Spanner | API request error rate"
596
629
  conditions:
@@ -611,6 +644,7 @@ spanner:
611
644
  - resource.label.database
612
645
  documentation:
613
646
  subject: 'Instance: `$${resource.label.instance_id}`, Database: `$${resource.label.database}`'
647
+ content: 'Review the instance in Spanner: https://console.cloud.google.com/spanner/instances?project=<%-projectId%>'
614
648
  request_latencies:
615
649
  display_name: '[P3] <%-clan%> - Spanner | API transaction latency'
616
650
  conditions:
@@ -631,3 +665,4 @@ spanner:
631
665
  per_series_aligner: ALIGN_PERCENTILE_95
632
666
  documentation:
633
667
  subject: 'Instance: `$${resource.label.instance_id}`, Database: `$${resource.label.database}}`'
668
+ content: 'Review the instance in Spanner: https://console.cloud.google.com/spanner/instances?project=<%-projectId%>'
@@ -0,0 +1,58 @@
1
+ const validate = {};
2
+
3
+ const hasWhitespace = (str) => /\s/.test(str);
4
+
5
+ const isUrl = (str) => {
6
+ try {
7
+ return Boolean(new URL(str));
8
+ } catch {
9
+ return false;
10
+ }
11
+ };
12
+
13
+ validate.clan = (input) => {
14
+ if (!hasWhitespace(input)) return true;
15
+ return 'Clan name must not include spaces';
16
+ };
17
+
18
+ validate.projectId = (input) => {
19
+ if (input.trim() !== '' && !hasWhitespace(input)) return true;
20
+ return 'Project ID must not be empty or contain spaces';
21
+ };
22
+
23
+ validate.hostname = (input) => {
24
+ if (input.split('.').at(-1) === 'com' && !hasWhitespace(input)) return true;
25
+ return 'Hostname must not include path to the page to run the check against or spaces';
26
+ };
27
+
28
+ validate.systemName = (input) => {
29
+ if (input.length === 3 && !hasWhitespace(input)) return true;
30
+ return 'System name must be 3 characters';
31
+ };
32
+
33
+ validate.confluenceUrl = (input) => {
34
+ if (input === '' || (isUrl(input) && !hasWhitespace(input))) return true;
35
+ return 'You must enter a valid URL';
36
+ };
37
+
38
+ validate.instanceID = (input) => {
39
+ if (input.split('/').length === 6 && !hasWhitespace(input)) return true;
40
+ return 'You must enter the full instance path (example: projects/example/locations/europe-west1/instances/instanceID)';
41
+ };
42
+
43
+ validate.databaseId = (input) => {
44
+ if (input.split(':').length === 2 && !hasWhitespace(input)) return true;
45
+ return 'You must enter the full database path (example: my-project:databaseID)';
46
+ };
47
+
48
+ validate.pubSubSubscription = (input) => {
49
+ if (input.split('/').length === 4 && !hasWhitespace(input)) return true;
50
+ return 'You must enter the full subscription path (example: projects/example/subscriptions/subscriptionId)';
51
+ };
52
+
53
+ validate.path = (input) => {
54
+ if (input.includes('/') && !hasWhitespace(input)) return true;
55
+ return 'Path must contain a slash and not include spaces';
56
+ };
57
+
58
+ module.exports = validate;
@@ -0,0 +1,130 @@
1
+ const yaml = require('js-yaml');
2
+ const fs = require('fs');
3
+
4
+ const addDLQ = async (yamlArray, env, dlqTopic) => {
5
+ if (env === 'prod') {
6
+ yamlArray[0].dead_letter_topic = dlqTopic;
7
+ }
8
+ };
9
+
10
+ const appendNotIncludePull = async (inputs, subscriptionFilePath, dlqTopic) => {
11
+ const pullArray = [];
12
+
13
+ pullArray.push({
14
+ name: `${inputs.existingTopic}+${inputs.subscriberName}`,
15
+ ack_deadline_seconds: '60',
16
+ expiration_policy: '',
17
+ });
18
+
19
+ await addDLQ(pullArray, inputs.env, dlqTopic);
20
+ const yamlPullArray = yaml.dump(pullArray);
21
+ fs.appendFileSync(
22
+ subscriptionFilePath,
23
+ `pull_subscriptions:\n${yamlPullArray}`,
24
+ );
25
+ };
26
+
27
+ const appendIncludePull = async (
28
+ fileContent,
29
+ originalContentYaml,
30
+ subscriptionFilePath,
31
+ input,
32
+ dlqTopic,
33
+ ) => {
34
+ if (fileContent.includes('pull_subscriptions')) {
35
+ const pullArray = Object.values(originalContentYaml.pull_subscriptions);
36
+ const yamlPullArray = yaml.dump(pullArray);
37
+ fs.writeFileSync(
38
+ subscriptionFilePath,
39
+ `pull_subscriptions:\n${yamlPullArray}`,
40
+ );
41
+ }
42
+
43
+ const newPullArray = [];
44
+
45
+ newPullArray.push({
46
+ name: `${input.existingTopic}+${input.subscriberName}`,
47
+ ack_deadline_seconds: '60',
48
+ expiration_policy: '',
49
+ });
50
+
51
+ await addDLQ(newPullArray, input.env, dlqTopic);
52
+ const finalYamlPullArray = yaml.dump(newPullArray);
53
+ fs.appendFileSync(subscriptionFilePath, finalYamlPullArray);
54
+
55
+ if (fileContent.includes('push_subscriptions')) {
56
+ const pushArray = Object.values(originalContentYaml.push_subscriptions);
57
+ const yamlPushArray = yaml.dump(pushArray);
58
+ fs.appendFileSync(
59
+ subscriptionFilePath,
60
+ `push_subscriptions:\n${yamlPushArray}`,
61
+ );
62
+ }
63
+ };
64
+
65
+ const appendNotIncludePush = async (inputs, subscriptionFilePath, dlqTopic) => {
66
+ const pushArray = [];
67
+
68
+ pushArray.push({
69
+ name: `${inputs.existingTopic}+${inputs.subscriberName}`,
70
+ push_endpoint: inputs.pushEndpoint,
71
+ oidc_service_account_email: inputs.oidcEmail,
72
+ audience: inputs.audience,
73
+ expiration_policy: '',
74
+ filter: `attributes.Consumer = "${inputs.subscriberName}" OR NOT attributes:Consumer`,
75
+ });
76
+
77
+ await addDLQ(pushArray, inputs.env, dlqTopic);
78
+ const yamlPushArray = yaml.dump(pushArray);
79
+ fs.appendFileSync(
80
+ subscriptionFilePath,
81
+ `push_subscriptions:\n${yamlPushArray}`,
82
+ );
83
+ };
84
+
85
+ const appendIncludePush = async (
86
+ fileContent,
87
+ originalContentYaml,
88
+ subscriptionFilePath,
89
+ input,
90
+ dlqTopic,
91
+ ) => {
92
+ if (fileContent.includes('push_subscriptions')) {
93
+ const pushArray = Object.values(originalContentYaml.push_subscriptions);
94
+ const yamlPushArray = yaml.dump(pushArray);
95
+ fs.writeFileSync(
96
+ subscriptionFilePath,
97
+ `push_subscriptions:\n${yamlPushArray}`,
98
+ );
99
+ }
100
+
101
+ const newPushArray = [];
102
+
103
+ newPushArray.push({
104
+ name: `${input.existingTopic}+${input.subscriberName}`,
105
+ push_endpoint: input.pushEndpoint,
106
+ oidc_service_account_email: input.oidcEmail,
107
+ audience: input.audience,
108
+ expiration_policy: '',
109
+ filter: `attributes.Consumer = "${input.subscriberName}" OR NOT attributes:Consumer`,
110
+ });
111
+ await addDLQ(newPushArray, input.env, dlqTopic);
112
+ const yamlPushArray = yaml.dump(newPushArray);
113
+ fs.appendFileSync(subscriptionFilePath, yamlPushArray);
114
+
115
+ if (fileContent.includes('pull_subscriptions')) {
116
+ const pullArray = Object.values(originalContentYaml.pull_subscriptions);
117
+ const yamlPullArray = yaml.dump(pullArray);
118
+ fs.appendFileSync(
119
+ subscriptionFilePath,
120
+ `pull_subscriptions:\n${yamlPullArray}`,
121
+ );
122
+ }
123
+ };
124
+
125
+ module.exports = {
126
+ appendNotIncludePull,
127
+ appendIncludePull,
128
+ appendNotIncludePush,
129
+ appendIncludePush,
130
+ };
@@ -0,0 +1,34 @@
1
+ const path = require('path');
2
+ const fs = require('fs');
3
+
4
+ const getKeyValue = (content, key) => {
5
+ let value = '';
6
+ content.split(/\r?\n/).forEach((line) => {
7
+ if (line.startsWith(key)) {
8
+ value = line.split('=')[1].replace('"', '').replace('"', '').trim();
9
+ }
10
+ });
11
+ return value;
12
+ };
13
+
14
+ const getProjectId = (env) => {
15
+ let projectId = '';
16
+ const projectHCL = path.join('infra', env, 'project.hcl');
17
+ if (fs.existsSync(projectHCL)) {
18
+ const projectHCLContent = fs.readFileSync(projectHCL, 'utf8');
19
+ projectId = getKeyValue(projectHCLContent, ' project_id');
20
+ }
21
+
22
+ return projectId;
23
+ };
24
+
25
+ const getProdPushEndopint = (pushEndpoint) => {
26
+ let prodPushEndopint = '';
27
+ prodPushEndopint = pushEndpoint.replace('.dev', '.com');
28
+ return prodPushEndopint;
29
+ };
30
+
31
+ module.exports = {
32
+ getProjectId,
33
+ getProdPushEndopint,
34
+ };
@@ -0,0 +1,68 @@
1
+ const fs = require('fs');
2
+ const yaml = require('js-yaml');
3
+ const {
4
+ appendNotIncludePull,
5
+ appendIncludePull,
6
+ appendNotIncludePush,
7
+ appendIncludePush,
8
+ } = require('./append');
9
+
10
+ const handleSubscribers = async (
11
+ env,
12
+ answers,
13
+ oidcEmail,
14
+ pushEndpoint,
15
+ subscriptionFilePath,
16
+ dlqTopic,
17
+ ) => {
18
+ const { subscriberName, existingTopic, pushOrPull, audience } = answers;
19
+
20
+ const subscriptionFileContent = fs.readFileSync(subscriptionFilePath, 'utf8');
21
+
22
+ const inputs = {
23
+ ...this.answers,
24
+ env,
25
+ existingTopic,
26
+ subscriberName,
27
+ audience,
28
+ oidcEmail,
29
+ pushEndpoint,
30
+ dlqTopic,
31
+ };
32
+
33
+ if (pushOrPull === 'pull') {
34
+ if (
35
+ subscriptionFileContent.length === 0 ||
36
+ !subscriptionFileContent.includes('pull_subscriptions')
37
+ ) {
38
+ await appendNotIncludePull(inputs, subscriptionFilePath, dlqTopic);
39
+ } else {
40
+ const originalContentYaml = yaml.load(subscriptionFileContent);
41
+ const fileContent = subscriptionFileContent;
42
+ await appendIncludePull(
43
+ fileContent,
44
+ originalContentYaml,
45
+ subscriptionFilePath,
46
+ inputs,
47
+ dlqTopic,
48
+ );
49
+ }
50
+ } else if (
51
+ subscriptionFileContent.length === 0 ||
52
+ !subscriptionFileContent.includes('push_subscriptions')
53
+ ) {
54
+ await appendNotIncludePush(inputs, subscriptionFilePath, dlqTopic);
55
+ } else {
56
+ const originalContentYaml = yaml.load(subscriptionFileContent);
57
+ const fileContent = subscriptionFileContent;
58
+ await appendIncludePush(
59
+ fileContent,
60
+ originalContentYaml,
61
+ subscriptionFilePath,
62
+ inputs,
63
+ dlqTopic,
64
+ );
65
+ }
66
+ };
67
+
68
+ module.exports = handleSubscribers;