@hiiretail/gcp-infra-cli 0.85.0 → 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
  )
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hiiretail/gcp-infra-cli",
3
- "version": "0.85.0",
3
+ "version": "0.86.0",
4
4
  "description": "Infrastructure as code generator for GCP.",
5
5
  "main": "src/cli.js",
6
6
  "bin": {