@hiiretail/gcp-infra-cli 0.84.5 → 0.86.0

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.
@@ -0,0 +1,83 @@
1
+ const fs = require('fs');
2
+
3
+ const findScheduledJobs = (schedulerPath) => {
4
+ const fileContent = fs.readFileSync(schedulerPath, 'utf8');
5
+ const regex = /scheduled_jobs\s*=\s*\[\s*([\s\S]*?)\s*\]/;
6
+ const match = regex.exec(fileContent);
7
+ if (match) {
8
+ const scheduledJobsText = match[1];
9
+ return scheduledJobsText;
10
+ }
11
+ return '';
12
+ };
13
+
14
+ const writeScheduledJobs = (schedulerPath, scheduledJobs) => {
15
+ const fileContent = fs.readFileSync(schedulerPath, 'utf8');
16
+ const updatedContent = fileContent.replace(
17
+ /scheduled_jobs\s*=\s*\[([\s\S]*?)\]/,
18
+ `scheduled_jobs = [\n ${scheduledJobs}\n ]`,
19
+ );
20
+ fs.writeFileSync(schedulerPath, updatedContent);
21
+ };
22
+
23
+ const scheduledJobsToHCL = (scheduledJobs) => `${scheduledJobs.map((job) => {
24
+ const httpTargetBlock = job.http_target
25
+ ? `
26
+ http_target = {
27
+ uri = "${job.http_target.uri}"
28
+ http_method = "${job.http_target.http_method}"
29
+ }
30
+ }`
31
+ : '';
32
+ const pubsubTargetBlock = job.pubsub_target
33
+ ? `
34
+ pubsub_target = {
35
+ topic_name = "${job.pubsub_target.topic_name}"
36
+ data = "${job.pubsub_target.data}"
37
+ }
38
+ }`
39
+ : '';
40
+ return `
41
+ {
42
+ name = "${job.name}"
43
+ description = "${job.description}"
44
+ schedule = "${job.schedule}"
45
+ ${httpTargetBlock}${pubsubTargetBlock}`;
46
+ })
47
+ .join('\n')}`;
48
+
49
+ const handleJobs = (schedulerPath, answers) => {
50
+ const oldJobs = findScheduledJobs(schedulerPath);
51
+ const allJobs = [oldJobs];
52
+
53
+ if (answers.target === 'http') {
54
+ const newHttpJob = {
55
+ name: answers.name,
56
+ description: answers.description,
57
+ schedule: answers.schedule,
58
+ http_target: {
59
+ uri: answers.uri,
60
+ http_method: answers.httpMethod,
61
+ },
62
+ };
63
+
64
+ const newHttpJobHcl = scheduledJobsToHCL([newHttpJob]);
65
+ allJobs.push(newHttpJobHcl);
66
+ } else {
67
+ const newPubsubJob = {
68
+ name: answers.name,
69
+ description: answers.description,
70
+ schedule: answers.schedule,
71
+ pubsub_target: {
72
+ topic_name: answers.topicName,
73
+ data: answers.data,
74
+ },
75
+ };
76
+ const newPubsubJobHcl = scheduledJobsToHCL([newPubsubJob]);
77
+ allJobs.push(newPubsubJobHcl);
78
+ }
79
+
80
+ writeScheduledJobs(schedulerPath, allJobs);
81
+ };
82
+
83
+ module.exports = handleJobs;
@@ -1,7 +1,9 @@
1
1
  const path = require('path');
2
2
  const chalk = require('chalk');
3
+ const fs = require('fs');
3
4
  const BaseGenerator = require('../../../src/BaseGenerator');
4
5
  const { required } = require('../../../src/validators');
6
+ const handleJobs = require('./append');
5
7
 
6
8
  module.exports = class extends BaseGenerator {
7
9
  prompting() {
@@ -74,14 +76,22 @@ module.exports = class extends BaseGenerator {
74
76
 
75
77
  writing() {
76
78
  ['prod', 'staging'].forEach((env) => {
77
- this.copyDir(
78
- 'scheduler',
79
- path.join('infra', env, 'scheduler'),
80
- {
81
- ...this.answers,
82
- env,
83
- },
84
- );
79
+ const schedulerPath = path.join(process.cwd(), 'infra', env, 'scheduler', 'terragrunt.hcl');
80
+ if (!fs.existsSync(schedulerPath)) {
81
+ this.copyDir(
82
+ 'scheduler',
83
+ path.join('infra', env, 'scheduler'),
84
+ {
85
+ ...this.answers,
86
+ env,
87
+ },
88
+ );
89
+ } else {
90
+ handleJobs(
91
+ schedulerPath,
92
+ this.answers,
93
+ );
94
+ }
85
95
  });
86
96
  }
87
97
 
@@ -22,23 +22,23 @@ locals {
22
22
 
23
23
  # These are the variables we have to pass in to use the module specified in the terragrunt configuration above
24
24
  inputs = merge(
25
- local.project_vars.locals,
26
- {
27
- scheduled_jobs = [
28
- {
29
- name = "<%-name%>"
30
- description = "<%-description%>"
31
- schedule = "<%-schedule%>"
32
- <% if (target == 'http') { %>
33
- http_target = {
34
- uri = "<%-uri%>"
35
- http_method = "<%-httpMethod%>"
36
- } <% } else { %>
37
- pubsub_target = {
38
- topic_name = "projects/${local.project_vars.locals.project_id}/topics/${dependency.topic.outputs.topic}"
39
- data = "${base64encode("<%-data%>")}"
40
- }<% } %>
41
- },
42
- ]
43
- },
25
+ local.project_vars.locals,
26
+ {
27
+ scheduled_jobs = [
28
+ {
29
+ name = "<%-name%>"
30
+ description = "<%-description%>"
31
+ schedule = "<%-schedule%>"
32
+ <% if (target == 'http') { %>
33
+ http_target = {
34
+ uri = "<%-uri%>"
35
+ http_method = "<%-httpMethod%>"
36
+ } <% } else { %>
37
+ pubsub_target = {
38
+ topic_name = "projects/${local.project_vars.locals.project_id}/topics/${dependency.topic.outputs.topic}"
39
+ data = "${base64encode("<%-data%>")}"
40
+ }<% } %>
41
+ }
42
+ ],
43
+ },
44
44
  )
@@ -52,7 +52,7 @@ module.exports = class extends BaseGenerator {
52
52
  },
53
53
  {
54
54
  type: 'input',
55
- name: 'versionRetentionPeriod',
55
+ name: 'retentionPeriod',
56
56
  message: 'Please provide version retention period. Maximum value 7d, possible values include 84000s, 1h, 2d. Leave empty for default value 1h.',
57
57
  },
58
58
  ];
@@ -69,7 +69,7 @@ module.exports = class extends BaseGenerator {
69
69
  instanceAllocation,
70
70
  databaseName,
71
71
  ddl,
72
- versionRetentionPeriod,
72
+ retentionPeriod,
73
73
  } = this.answers;
74
74
 
75
75
  ['prod', 'staging'].forEach((env) => {
@@ -84,7 +84,7 @@ module.exports = class extends BaseGenerator {
84
84
  instanceAllocation,
85
85
  databaseName,
86
86
  ddl,
87
- versionRetentionPeriod,
87
+ retentionPeriod,
88
88
  },
89
89
  );
90
90
  });
@@ -1,4 +1,4 @@
1
1
  databases:
2
2
  - name: "<%-databaseName%>" <% if (ddl == '') { %><% } else { %>
3
- ddl:
4
- - "<%-ddl%>" <% } %>
3
+ ddl: "<%-ddl%>" <% } %> <% if (retentionPeriod == '') { %><% } else { %>
4
+ version_retention_period: "<%-retentionPeriod%>" <% } %>
@@ -17,8 +17,4 @@ locals {
17
17
  # The number of processing units allocated to this instance.
18
18
  # At most one of either num_nodes or processing_units can be present in terraform.
19
19
  processing_units = "<%-processingUnits%>" <% } %>
20
- # The time frame it will be possible to restore the database to a point in time.
21
- # Values from 1h up to max 7d
22
- <% if (versionRetentionPeriod !== '') { %>
23
- version_retention_period = "<%-versionRetentionPeriod%>" <% } %>
24
20
  }
@@ -1,7 +1,7 @@
1
1
  # Terragrunt will copy the Terraform configurations specified by the source parameter, along with any files in the
2
2
  # working directory, into a temporary folder, and execute your Terraform commands in that folder.
3
3
  terraform {
4
- source = "git::https://github.com/extenda/tf-module-gcp-spanner//?ref=v0.1.2"
4
+ source = "git::https://github.com/extenda/tf-module-gcp-spanner//?ref=v0.1.3"
5
5
  }
6
6
 
7
7
  # Include all settings from the root terragrunt.hcl file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hiiretail/gcp-infra-cli",
3
- "version": "0.84.5",
3
+ "version": "0.86.0",
4
4
  "description": "Infrastructure as code generator for GCP.",
5
5
  "main": "src/cli.js",
6
6
  "bin": {