neonctl 1.29.1 → 1.29.4

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 (184) hide show
  1. package/analytics.js +78 -0
  2. package/api.js +35 -0
  3. package/auth.js +101 -0
  4. package/{src/cli.ts → cli.js} +0 -1
  5. package/commands/auth.js +102 -0
  6. package/commands/auth.test.js +42 -0
  7. package/commands/branches.js +303 -0
  8. package/commands/branches.test.js +321 -0
  9. package/commands/connection_string.js +158 -0
  10. package/commands/connection_string.test.js +253 -0
  11. package/commands/databases.js +79 -0
  12. package/commands/databases.test.js +51 -0
  13. package/commands/help.test.js +11 -0
  14. package/{src/commands/index.ts → commands/index.js} +10 -11
  15. package/commands/ip_allow.js +135 -0
  16. package/commands/ip_allow.test.js +78 -0
  17. package/commands/operations.js +28 -0
  18. package/commands/operations.test.js +11 -0
  19. package/commands/projects.js +186 -0
  20. package/commands/projects.test.js +132 -0
  21. package/commands/roles.js +57 -0
  22. package/commands/roles.test.js +42 -0
  23. package/commands/set_context.js +22 -0
  24. package/commands/set_context.test.js +53 -0
  25. package/commands/user.js +15 -0
  26. package/config.js +11 -0
  27. package/context.js +48 -0
  28. package/env.js +6 -0
  29. package/errors.js +16 -0
  30. package/help.js +146 -0
  31. package/index.js +168 -0
  32. package/log.js +15 -0
  33. package/package.json +1 -1
  34. package/parameters.gen.js +322 -0
  35. package/pkg.js +3 -45
  36. package/test_utils/mock_server.js +16 -0
  37. package/test_utils/oauth_server.js +9 -0
  38. package/test_utils/test_cli_command.js +80 -0
  39. package/types.js +1 -0
  40. package/utils/enrichers.js +49 -0
  41. package/utils/formats.js +5 -0
  42. package/utils/formats.test.js +32 -0
  43. package/utils/middlewares.js +20 -0
  44. package/utils/point_in_time.js +50 -0
  45. package/utils/psql.js +24 -0
  46. package/utils/string.js +5 -0
  47. package/utils/ui.js +59 -0
  48. package/writer.js +87 -0
  49. package/writer.test.js +86 -0
  50. package/.bump +0 -1
  51. package/.editorconfig +0 -7
  52. package/.eslintrc.cjs +0 -15
  53. package/.github/workflows/commitlint.yml +0 -46
  54. package/.github/workflows/pr.yml +0 -25
  55. package/.github/workflows/release.yml +0 -30
  56. package/.husky/commit-msg +0 -4
  57. package/.husky/pre-commit +0 -4
  58. package/.nvmrc +0 -1
  59. package/.prettierignore +0 -3
  60. package/.prettierrc.json +0 -3
  61. package/.releaserc.json +0 -47
  62. package/LICENSE +0 -202
  63. package/commitlint.config.cjs +0 -7
  64. package/generateOptionsFromSpec.ts +0 -68
  65. package/jest/setup.js +0 -5
  66. package/jest.config.ts +0 -199
  67. package/mocks/bin/psql.cjs +0 -9
  68. package/mocks/main/projects/GET.js +0 -27
  69. package/mocks/main/projects/POST.js +0 -22
  70. package/mocks/main/projects/shared/GET.js +0 -16
  71. package/mocks/main/projects/test/DELETE.json +0 -7
  72. package/mocks/main/projects/test/GET.json +0 -13
  73. package/mocks/main/projects/test/PATCH.js +0 -18
  74. package/mocks/main/projects/test/branches/GET.json +0 -25
  75. package/mocks/main/projects/test/branches/POST.js +0 -83
  76. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/DELETE.json +0 -7
  77. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/GET.json +0 -9
  78. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/PATCH.js +0 -14
  79. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/databases/GET.json +0 -6
  80. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/databases/POST.js +0 -13
  81. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/databases/test_db/DELETE.json +0 -6
  82. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/endpoints/GET.json +0 -26
  83. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/endpoints/POST.json +0 -6
  84. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/roles/GET.json +0 -3
  85. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/roles/POST.js +0 -14
  86. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/roles/test_role/DELETE.json +0 -6
  87. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/roles/test_role/reveal_password/GET.json +0 -3
  88. package/mocks/main/projects/test/branches/br-cloudy-branch-12345678/set_as_primary/POST.json +0 -9
  89. package/mocks/main/projects/test/branches/br-numbered-branch-123456/GET.json +0 -10
  90. package/mocks/main/projects/test/branches/br-sunny-branch-123456/DELETE.json +0 -7
  91. package/mocks/main/projects/test/branches/br-sunny-branch-123456/GET.json +0 -10
  92. package/mocks/main/projects/test/branches/br-sunny-branch-123456/PATCH.js +0 -14
  93. package/mocks/main/projects/test/branches/br-sunny-branch-123456/databases/GET.json +0 -6
  94. package/mocks/main/projects/test/branches/br-sunny-branch-123456/databases/POST.js +0 -13
  95. package/mocks/main/projects/test/branches/br-sunny-branch-123456/databases/test_db/DELETE.json +0 -6
  96. package/mocks/main/projects/test/branches/br-sunny-branch-123456/endpoints/GET.json +0 -26
  97. package/mocks/main/projects/test/branches/br-sunny-branch-123456/endpoints/POST.json +0 -6
  98. package/mocks/main/projects/test/branches/br-sunny-branch-123456/restore/POST.js +0 -16
  99. package/mocks/main/projects/test/branches/br-sunny-branch-123456/roles/GET.json +0 -3
  100. package/mocks/main/projects/test/branches/br-sunny-branch-123456/roles/POST.js +0 -14
  101. package/mocks/main/projects/test/branches/br-sunny-branch-123456/roles/test_role/DELETE.json +0 -6
  102. package/mocks/main/projects/test/branches/br-sunny-branch-123456/roles/test_role/reveal_password/GET.json +0 -3
  103. package/mocks/main/projects/test/branches/br-sunny-branch-123456/set_as_primary/POST.json +0 -9
  104. package/mocks/main/projects/test/endpoints/GET.json +0 -9
  105. package/mocks/main/projects/test/endpoints/POST.js +0 -32
  106. package/mocks/main/projects/test/endpoints/test_endpoint_id/DELETE.json +0 -7
  107. package/mocks/main/projects/test/endpoints/test_endpoint_id/GET.json +0 -9
  108. package/mocks/main/projects/test/endpoints/test_endpoint_id/PATCH.js +0 -17
  109. package/mocks/main/projects/test/operations/GET.json +0 -22
  110. package/mocks/main/users/me/GET.json +0 -5
  111. package/mocks/restore/projects/test/branches/GET.json +0 -21
  112. package/mocks/restore/projects/test/branches/br-another-branch-123456/GET.json +0 -6
  113. package/mocks/restore/projects/test/branches/br-another-branch-123456/restore/POST.js +0 -13
  114. package/mocks/restore/projects/test/branches/br-any-branch-123456/GET.json +0 -6
  115. package/mocks/restore/projects/test/branches/br-parent-tots-123456/GET.json +0 -7
  116. package/mocks/restore/projects/test/branches/br-parent-tots-123456/restore/POST.js +0 -14
  117. package/mocks/restore/projects/test/branches/br-self-tolsn-123456/GET.json +0 -6
  118. package/mocks/restore/projects/test/branches/br-self-tolsn-123456/restore/POST.js +0 -15
  119. package/mocks/single_project/projects/GET.json +0 -10
  120. package/mocks/single_project/projects/test-project-123456/GET.json +0 -14
  121. package/mocks/single_project/projects/test-project-123456/branches/GET.json +0 -11
  122. package/mocks/single_project/projects/test-project-123456/branches/br-main-branch-123456/databases/GET.json +0 -3
  123. package/mocks/single_project/projects/test-project-123456/branches/br-main-branch-123456/endpoints/GET.json +0 -10
  124. package/mocks/single_project/projects/test-project-123456/branches/br-main-branch-123456/roles/GET.json +0 -3
  125. package/mocks/single_project/projects/test-project-123456/branches/br-main-branch-123456/roles/test_role/reveal_password/GET.json +0 -3
  126. package/rollup.config.js +0 -20
  127. package/snapshots/commands/branches.test.snap +0 -221
  128. package/snapshots/commands/connection_string.test.snap +0 -70
  129. package/snapshots/commands/databases.test.snap +0 -20
  130. package/snapshots/commands/ip_allow.test.snap +0 -55
  131. package/snapshots/commands/operations.test.snap +0 -17
  132. package/snapshots/commands/projects.test.snap +0 -141
  133. package/snapshots/commands/roles.test.snap +0 -19
  134. package/snapshots/commands/set_context.test.snap +0 -30
  135. package/snapshots/writer.test.snap +0 -60
  136. package/snapshotsResolver.cjs +0 -32
  137. package/src/analytics.ts +0 -95
  138. package/src/api.ts +0 -44
  139. package/src/auth.ts +0 -137
  140. package/src/commands/auth.test.ts +0 -62
  141. package/src/commands/auth.ts +0 -148
  142. package/src/commands/branches.test.ts +0 -354
  143. package/src/commands/branches.ts +0 -451
  144. package/src/commands/connection_string.test.ts +0 -250
  145. package/src/commands/connection_string.ts +0 -210
  146. package/src/commands/databases.test.ts +0 -55
  147. package/src/commands/databases.ts +0 -129
  148. package/src/commands/help.test.ts +0 -13
  149. package/src/commands/ip_allow.test.ts +0 -86
  150. package/src/commands/ip_allow.ts +0 -202
  151. package/src/commands/operations.test.ts +0 -13
  152. package/src/commands/operations.ts +0 -41
  153. package/src/commands/projects.test.ts +0 -147
  154. package/src/commands/projects.ts +0 -275
  155. package/src/commands/roles.test.ts +0 -46
  156. package/src/commands/roles.ts +0 -100
  157. package/src/commands/set_context.test.ts +0 -64
  158. package/src/commands/set_context.ts +0 -27
  159. package/src/commands/user.ts +0 -21
  160. package/src/config.ts +0 -22
  161. package/src/context.ts +0 -61
  162. package/src/env.ts +0 -7
  163. package/src/errors.ts +0 -24
  164. package/src/help.ts +0 -185
  165. package/src/index.ts +0 -180
  166. package/src/log.ts +0 -16
  167. package/src/parameters.gen.ts +0 -332
  168. package/src/pkg.ts +0 -9
  169. package/src/test_utils/mock_server.ts +0 -27
  170. package/src/test_utils/oauth_server.ts +0 -10
  171. package/src/test_utils/test_cli_command.ts +0 -117
  172. package/src/types.ts +0 -25
  173. package/src/utils/enrichers.ts +0 -73
  174. package/src/utils/formats.test.ts +0 -41
  175. package/src/utils/formats.ts +0 -11
  176. package/src/utils/middlewares.ts +0 -23
  177. package/src/utils/point_in_time.ts +0 -86
  178. package/src/utils/psql.ts +0 -29
  179. package/src/utils/string.ts +0 -8
  180. package/src/utils/ui.ts +0 -64
  181. package/src/writer.test.ts +0 -98
  182. package/src/writer.ts +0 -131
  183. package/tsconfig.json +0 -17
  184. /package/{src/callback.html → callback.html} +0 -0
@@ -0,0 +1,253 @@
1
+ import { describe, expect } from '@jest/globals';
2
+ import { testCliCommand } from '../test_utils/test_cli_command';
3
+ describe('connection_string', () => {
4
+ testCliCommand({
5
+ name: 'connection_string',
6
+ args: [
7
+ 'connection-string',
8
+ 'test_branch',
9
+ '--project-id',
10
+ 'test',
11
+ '--database-name',
12
+ 'test_db',
13
+ '--role-name',
14
+ 'test_role',
15
+ ],
16
+ expected: {
17
+ snapshot: true,
18
+ },
19
+ });
20
+ testCliCommand({
21
+ name: 'connection_string branch id',
22
+ args: [
23
+ 'connection-string',
24
+ 'br-sunny-branch-123456',
25
+ '--project-id',
26
+ 'test',
27
+ '--database-name',
28
+ 'test_db',
29
+ '--role-name',
30
+ 'test_role',
31
+ ],
32
+ expected: {
33
+ snapshot: true,
34
+ },
35
+ });
36
+ testCliCommand({
37
+ name: 'connection_string branch id 8 digits',
38
+ args: [
39
+ 'connection-string',
40
+ 'br-cloudy-branch-12345678',
41
+ '--project-id',
42
+ 'test',
43
+ '--database-name',
44
+ 'test_db',
45
+ '--role-name',
46
+ 'test_role',
47
+ ],
48
+ expected: {
49
+ snapshot: true,
50
+ },
51
+ });
52
+ testCliCommand({
53
+ name: 'connection_string pooled',
54
+ args: [
55
+ 'connection-string',
56
+ 'test_branch',
57
+ '--project-id',
58
+ 'test',
59
+ '--database-name',
60
+ 'test_db',
61
+ '--role-name',
62
+ 'test_role',
63
+ '--pooled',
64
+ ],
65
+ expected: {
66
+ snapshot: true,
67
+ },
68
+ });
69
+ testCliCommand({
70
+ name: 'connection_string prisma',
71
+ args: [
72
+ 'connection-string',
73
+ 'test_branch',
74
+ '--project-id',
75
+ 'test',
76
+ '--database-name',
77
+ 'test_db',
78
+ '--role-name',
79
+ 'test_role',
80
+ '--prisma',
81
+ ],
82
+ expected: {
83
+ snapshot: true,
84
+ },
85
+ });
86
+ testCliCommand({
87
+ name: 'connection_string prisma pooled',
88
+ args: [
89
+ 'connection-string',
90
+ 'test_branch',
91
+ '--project-id',
92
+ 'test',
93
+ '--database-name',
94
+ 'test_db',
95
+ '--role-name',
96
+ 'test_role',
97
+ '--prisma',
98
+ '--pooled',
99
+ ],
100
+ expected: {
101
+ snapshot: true,
102
+ },
103
+ });
104
+ testCliCommand({
105
+ name: 'connection_string prisma pooled extended',
106
+ args: [
107
+ 'connection-string',
108
+ 'test_branch',
109
+ '--project-id',
110
+ 'test',
111
+ '--database-name',
112
+ 'test_db',
113
+ '--role-name',
114
+ 'test_role',
115
+ '--prisma',
116
+ '--pooled',
117
+ '--extended',
118
+ ],
119
+ expected: {
120
+ snapshot: true,
121
+ },
122
+ });
123
+ testCliCommand({
124
+ name: 'connection_string without any args should pass',
125
+ args: ['connection-string'],
126
+ mockDir: 'single_project',
127
+ expected: {
128
+ snapshot: true,
129
+ },
130
+ });
131
+ testCliCommand({
132
+ name: 'connection_string with psql',
133
+ args: [
134
+ 'connection-string',
135
+ 'test_branch',
136
+ '--project-id',
137
+ 'test',
138
+ '--database-name',
139
+ 'test_db',
140
+ '--role-name',
141
+ 'test_role',
142
+ '--psql',
143
+ ],
144
+ expected: {
145
+ snapshot: true,
146
+ },
147
+ });
148
+ testCliCommand({
149
+ name: 'connection_string with psql and psql args',
150
+ args: [
151
+ 'connection-string',
152
+ 'test_branch',
153
+ '--project-id',
154
+ 'test',
155
+ '--database-name',
156
+ 'test_db',
157
+ '--role-name',
158
+ 'test_role',
159
+ '--psql',
160
+ '--',
161
+ '-c',
162
+ 'SELECT 1',
163
+ ],
164
+ expected: {
165
+ snapshot: true,
166
+ },
167
+ });
168
+ testCliCommand({
169
+ name: 'connection_string without ssl',
170
+ args: [
171
+ 'connection-string',
172
+ 'test_branch',
173
+ '--project-id',
174
+ 'test',
175
+ '--database-name',
176
+ 'test_db',
177
+ '--role-name',
178
+ 'test_role',
179
+ '--ssl',
180
+ 'omit',
181
+ ],
182
+ expected: {
183
+ snapshot: true,
184
+ },
185
+ });
186
+ testCliCommand({
187
+ name: 'connection_string with ssl verify full',
188
+ args: [
189
+ 'connection-string',
190
+ 'test_branch',
191
+ '--project-id',
192
+ 'test',
193
+ '--database-name',
194
+ 'test_db',
195
+ '--role-name',
196
+ 'test_role',
197
+ '--ssl',
198
+ 'verify-full',
199
+ ],
200
+ expected: {
201
+ snapshot: true,
202
+ },
203
+ });
204
+ testCliCommand({
205
+ name: 'connection_string with lsn',
206
+ args: [
207
+ 'connection-string',
208
+ 'test_branch@0/123456',
209
+ '--project-id',
210
+ 'test',
211
+ '--database-name',
212
+ 'test_db',
213
+ '--role-name',
214
+ 'test_role',
215
+ ],
216
+ expected: {
217
+ snapshot: true,
218
+ },
219
+ });
220
+ testCliCommand({
221
+ name: 'connection_string with timestamp',
222
+ args: [
223
+ 'connection-string',
224
+ 'test_branch@2021-01-01T00:00:00Z',
225
+ '--project-id',
226
+ 'test',
227
+ '--database-name',
228
+ 'test_db',
229
+ '--role-name',
230
+ 'test_role',
231
+ ],
232
+ expected: {
233
+ snapshot: true,
234
+ },
235
+ });
236
+ testCliCommand({
237
+ name: 'connection_string fails for non-existing database',
238
+ args: [
239
+ 'connection-string',
240
+ 'test_branch',
241
+ '--project-id',
242
+ 'test',
243
+ '--database-name',
244
+ 'non_existing_db',
245
+ '--role-name',
246
+ 'test_role',
247
+ ],
248
+ expected: {
249
+ code: 1,
250
+ stderr: expect.stringMatching(/Database not found/),
251
+ },
252
+ });
253
+ });
@@ -0,0 +1,79 @@
1
+ import { retryOnLock } from '../api.js';
2
+ import { branchIdFromProps, fillSingleProject } from '../utils/enrichers.js';
3
+ import { writer } from '../writer.js';
4
+ const DATABASE_FIELDS = ['name', 'owner_name', 'created_at'];
5
+ export const command = 'databases';
6
+ export const describe = 'Manage databases';
7
+ export const aliases = ['database', 'db'];
8
+ export const builder = (argv) => argv
9
+ .usage('$0 databases <sub-command> [options]')
10
+ .options({
11
+ 'project-id': {
12
+ describe: 'Project ID',
13
+ type: 'string',
14
+ },
15
+ branch: {
16
+ describe: 'Branch ID or name',
17
+ type: 'string',
18
+ },
19
+ })
20
+ .middleware(fillSingleProject)
21
+ .command('list', 'List databases', (yargs) => yargs, async (args) => await list(args))
22
+ .command('create', 'Create a database', (yargs) => yargs.options({
23
+ name: {
24
+ describe: 'Database name',
25
+ type: 'string',
26
+ demandOption: true,
27
+ },
28
+ 'owner-name': {
29
+ describe: 'Owner name',
30
+ type: 'string',
31
+ },
32
+ }), async (args) => await create(args))
33
+ .command('delete <database>', 'Delete a database', (yargs) => yargs, async (args) => await deleteDb(args));
34
+ export const handler = (args) => {
35
+ return args;
36
+ };
37
+ export const list = async (props) => {
38
+ const branchId = await branchIdFromProps(props);
39
+ const { data } = await props.apiClient.listProjectBranchDatabases(props.projectId, branchId);
40
+ writer(props).end(data.databases, {
41
+ fields: DATABASE_FIELDS,
42
+ });
43
+ };
44
+ export const create = async (props) => {
45
+ const branchId = await branchIdFromProps(props);
46
+ const owner = props.ownerName ??
47
+ (await props.apiClient
48
+ .listProjectBranchRoles(props.projectId, branchId)
49
+ .then(({ data }) => {
50
+ if (data.roles.length === 0) {
51
+ throw new Error(`No roles found in branch ${branchId}`);
52
+ }
53
+ if (data.roles.length > 1) {
54
+ throw new Error(`More than one role found in branch ${branchId}. Please specify the owner name. Roles: ${data.roles
55
+ .map((r) => r.name)
56
+ .join(', ')}`);
57
+ }
58
+ return data.roles[0].name;
59
+ }));
60
+ if (!owner) {
61
+ throw new Error('No owner found');
62
+ }
63
+ const { data } = await retryOnLock(() => props.apiClient.createProjectBranchDatabase(props.projectId, branchId, {
64
+ database: {
65
+ name: props.name,
66
+ owner_name: owner,
67
+ },
68
+ }));
69
+ writer(props).end(data.database, {
70
+ fields: DATABASE_FIELDS,
71
+ });
72
+ };
73
+ export const deleteDb = async (props) => {
74
+ const branchId = await branchIdFromProps(props);
75
+ const { data } = await retryOnLock(() => props.apiClient.deleteProjectBranchDatabase(props.projectId, branchId, props.database));
76
+ writer(props).end(data.database, {
77
+ fields: DATABASE_FIELDS,
78
+ });
79
+ };
@@ -0,0 +1,51 @@
1
+ import { describe } from '@jest/globals';
2
+ import { testCliCommand } from '../test_utils/test_cli_command.js';
3
+ describe('databases', () => {
4
+ testCliCommand({
5
+ name: 'list',
6
+ args: [
7
+ 'databases',
8
+ 'list',
9
+ '--project-id',
10
+ 'test',
11
+ '--branch',
12
+ 'test_branch',
13
+ ],
14
+ expected: {
15
+ snapshot: true,
16
+ },
17
+ });
18
+ testCliCommand({
19
+ name: 'create',
20
+ args: [
21
+ 'databases',
22
+ 'create',
23
+ '--project-id',
24
+ 'test',
25
+ '--branch',
26
+ 'test_branch',
27
+ '--name',
28
+ 'test_db',
29
+ '--owner-name',
30
+ 'test_owner',
31
+ ],
32
+ expected: {
33
+ snapshot: true,
34
+ },
35
+ });
36
+ testCliCommand({
37
+ name: 'delete',
38
+ args: [
39
+ 'databases',
40
+ 'delete',
41
+ 'test_db',
42
+ '--project-id',
43
+ 'test',
44
+ '--branch',
45
+ 'test_branch',
46
+ ],
47
+ expected: {
48
+ snapshot: true,
49
+ },
50
+ });
51
+ });
@@ -0,0 +1,11 @@
1
+ import { describe, expect } from '@jest/globals';
2
+ import { testCliCommand } from '../test_utils/test_cli_command.js';
3
+ describe('help', () => {
4
+ testCliCommand({
5
+ name: 'without args',
6
+ args: [],
7
+ expected: {
8
+ stderr: expect.stringContaining(`neonctl <command> [options]`),
9
+ },
10
+ });
11
+ });
@@ -8,16 +8,15 @@ import * as roles from './roles.js';
8
8
  import * as operations from './operations.js';
9
9
  import * as cs from './connection_string.js';
10
10
  import * as setContext from './set_context.js';
11
-
12
11
  export default [
13
- auth,
14
- users,
15
- projects,
16
- ipAllow,
17
- branches,
18
- databases,
19
- roles,
20
- operations,
21
- cs,
22
- setContext,
12
+ auth,
13
+ users,
14
+ projects,
15
+ ipAllow,
16
+ branches,
17
+ databases,
18
+ roles,
19
+ operations,
20
+ cs,
21
+ setContext,
23
22
  ];
@@ -0,0 +1,135 @@
1
+ import { writer } from '../writer.js';
2
+ import { fillSingleProject } from '../utils/enrichers.js';
3
+ import { projectUpdateRequest } from '../parameters.gen.js';
4
+ import { log } from '../log.js';
5
+ const IP_ALLOW_FIELDS = [
6
+ 'id',
7
+ 'name',
8
+ 'IP_addresses',
9
+ 'primary_branch_only',
10
+ ];
11
+ export const command = 'ip-allow';
12
+ export const describe = 'Manage IP Allow';
13
+ export const builder = (argv) => {
14
+ return argv
15
+ .usage('$0 ip-allow <sub-command> [options]')
16
+ .options({
17
+ 'project-id': {
18
+ describe: 'Project ID',
19
+ type: 'string',
20
+ },
21
+ })
22
+ .middleware(fillSingleProject)
23
+ .command('list', 'List the IP allowlist', (yargs) => yargs, async (args) => {
24
+ await list(args);
25
+ })
26
+ .command('add [ips...]', 'Add IP addresses to the IP allowlist', (yargs) => yargs
27
+ .usage('$0 ip-allow add [ips...]')
28
+ .positional('ips', {
29
+ describe: 'The list of IP addresses to add',
30
+ type: 'string',
31
+ default: [],
32
+ array: true,
33
+ })
34
+ .options({
35
+ 'primary-only': {
36
+ describe: projectUpdateRequest['project.settings.allowed_ips.primary_branch_only'].description,
37
+ type: 'boolean',
38
+ },
39
+ }), async (args) => {
40
+ await add(args);
41
+ })
42
+ .command('remove [ips...]', 'Remove IP addresses from the IP allowlist', (yargs) => yargs.usage('$0 ip-allow remove [ips...]').positional('ips', {
43
+ describe: 'The list of IP addresses to remove',
44
+ type: 'string',
45
+ default: [],
46
+ array: true,
47
+ }), async (args) => {
48
+ await remove(args);
49
+ })
50
+ .command('reset [ips...]', 'Reset the IP allowlist', (yargs) => yargs.usage('$0 ip-allow reset [ips...]').positional('ips', {
51
+ describe: 'The list of IP addresses to reset',
52
+ type: 'string',
53
+ default: [],
54
+ array: true,
55
+ }), async (args) => {
56
+ await reset(args);
57
+ });
58
+ };
59
+ export const handler = (args) => {
60
+ return args;
61
+ };
62
+ const list = async (props) => {
63
+ const { data } = await props.apiClient.getProject(props.projectId);
64
+ writer(props).end(parse(data.project), {
65
+ fields: IP_ALLOW_FIELDS,
66
+ });
67
+ };
68
+ const add = async (props) => {
69
+ if (props.ips.length <= 0) {
70
+ throw new Error(`Enter individual IP addresses, define ranges with a dash, or use CIDR notation for more flexibility.
71
+ Example: neonctl ip-allow add 192.168.1.1, 192.168.1.20-192.168.1.50, 192.168.1.0/24 --project-id <id>`);
72
+ }
73
+ const project = {};
74
+ const { data } = await props.apiClient.getProject(props.projectId);
75
+ const existingAllowedIps = data.project.settings?.allowed_ips;
76
+ project.settings = {
77
+ allowed_ips: {
78
+ ips: [...new Set(props.ips.concat(existingAllowedIps?.ips ?? []))],
79
+ primary_branch_only: props.primaryOnly ?? existingAllowedIps?.primary_branch_only ?? false,
80
+ },
81
+ };
82
+ const { data: response } = await props.apiClient.updateProject(props.projectId, {
83
+ project,
84
+ });
85
+ writer(props).end(parse(response.project), {
86
+ fields: IP_ALLOW_FIELDS,
87
+ });
88
+ };
89
+ const remove = async (props) => {
90
+ if (props.ips.length <= 0) {
91
+ throw new Error(`Remove individual IP addresses and ranges. Example: neonctl ip-allow remove 192.168.1.1 --project-id <id>`);
92
+ }
93
+ const project = {};
94
+ const { data } = await props.apiClient.getProject(props.projectId);
95
+ const existingAllowedIps = data.project.settings?.allowed_ips;
96
+ project.settings = {
97
+ allowed_ips: {
98
+ ips: existingAllowedIps?.ips?.filter((ip) => !props.ips.includes(ip)) ?? [],
99
+ primary_branch_only: existingAllowedIps?.primary_branch_only ?? false,
100
+ },
101
+ };
102
+ const { data: response } = await props.apiClient.updateProject(props.projectId, {
103
+ project,
104
+ });
105
+ writer(props).end(parse(response.project), {
106
+ fields: IP_ALLOW_FIELDS,
107
+ });
108
+ };
109
+ const reset = async (props) => {
110
+ const project = {};
111
+ project.settings = {
112
+ allowed_ips: {
113
+ ips: props.ips,
114
+ primary_branch_only: false,
115
+ },
116
+ };
117
+ const { data } = await props.apiClient.updateProject(props.projectId, {
118
+ project,
119
+ });
120
+ writer(props).end(parse(data.project), {
121
+ fields: IP_ALLOW_FIELDS,
122
+ });
123
+ if (props.ips.length <= 0) {
124
+ log.info(`The IP allowlist has been reset. All databases on project "${data.project.name}" are now exposed to the internet`);
125
+ }
126
+ };
127
+ const parse = (project) => {
128
+ const ips = project.settings?.allowed_ips?.ips ?? [];
129
+ return {
130
+ id: project.id,
131
+ name: project.name,
132
+ IP_addresses: ips,
133
+ primary_branch_only: project.settings?.allowed_ips?.primary_branch_only ?? false,
134
+ };
135
+ };
@@ -0,0 +1,78 @@
1
+ import { describe } from '@jest/globals';
2
+ import { testCliCommand } from '../test_utils/test_cli_command.js';
3
+ describe('ip-allow', () => {
4
+ testCliCommand({
5
+ name: 'list IP allow',
6
+ args: ['ip-allow', 'list', '--project-id', 'test'],
7
+ expected: {
8
+ snapshot: true,
9
+ },
10
+ });
11
+ testCliCommand({
12
+ name: 'list IP Allow with single-project',
13
+ args: ['ip-allow', 'list'],
14
+ mockDir: 'single_project',
15
+ expected: {
16
+ snapshot: true,
17
+ },
18
+ });
19
+ testCliCommand({
20
+ name: 'Add IP allow - Error',
21
+ args: ['ip-allow', 'add', '--projectId', 'test'],
22
+ expected: {
23
+ code: 1,
24
+ stderr: `ERROR: Enter individual IP addresses, define ranges with a dash, or use CIDR notation for more flexibility.
25
+ Example: neonctl ip-allow add 192.168.1.1, 192.168.1.20-192.168.1.50, 192.168.1.0/24 --project-id <id>`,
26
+ },
27
+ });
28
+ testCliCommand({
29
+ name: 'Add IP allow',
30
+ args: [
31
+ 'ip-allow',
32
+ 'add',
33
+ '127.0.0.1',
34
+ '192.168.10.1-192.168.10.15',
35
+ '--primary-only',
36
+ '--project-id',
37
+ 'test',
38
+ ],
39
+ expected: {
40
+ snapshot: true,
41
+ },
42
+ });
43
+ testCliCommand({
44
+ name: 'Remove IP allow - Error',
45
+ args: ['ip-allow', 'remove', '--project-id', 'test'],
46
+ expected: {
47
+ code: 1,
48
+ stderr: `ERROR: Remove individual IP addresses and ranges. Example: neonctl ip-allow remove 192.168.1.1 --project-id <id>`,
49
+ },
50
+ });
51
+ testCliCommand({
52
+ name: 'Remove IP allow',
53
+ args: ['ip-allow', 'remove', '192.168.1.1', '--project-id', 'test'],
54
+ expected: {
55
+ snapshot: true,
56
+ },
57
+ });
58
+ testCliCommand({
59
+ name: 'Reset IP allow',
60
+ args: ['ip-allow', 'reset', '--project-id', 'test'],
61
+ expected: {
62
+ snapshot: true,
63
+ stdout: `id: test
64
+ name: test_project
65
+ IP_addresses: []
66
+ primary_branch_only: false
67
+ `,
68
+ stderr: `INFO: The IP allowlist has been reset. All databases on project "test_project" are now exposed to the internet`,
69
+ },
70
+ });
71
+ testCliCommand({
72
+ name: 'Reset IP allow to new list',
73
+ args: ['ip-allow', 'reset', '192.168.2.2', '--project-id', 'test'],
74
+ expected: {
75
+ snapshot: true,
76
+ },
77
+ });
78
+ });
@@ -0,0 +1,28 @@
1
+ import { fillSingleProject } from '../utils/enrichers.js';
2
+ import { writer } from '../writer.js';
3
+ const OPERATIONS_FIELDS = ['id', 'action', 'status', 'created_at'];
4
+ export const command = 'operations';
5
+ export const describe = 'Manage operations';
6
+ export const aliases = ['operation'];
7
+ export const builder = (argv) => argv
8
+ .usage('$0 operations <sub-command> [options]')
9
+ .options({
10
+ 'project-id': {
11
+ describe: 'Project ID',
12
+ type: 'string',
13
+ },
14
+ })
15
+ .middleware(fillSingleProject)
16
+ .command('list', 'List operations', (yargs) => yargs, async (args) => await list(args));
17
+ export const handler = (args) => {
18
+ return args;
19
+ };
20
+ export const list = async (props) => {
21
+ const { data } = await props.apiClient.listProjectOperations({
22
+ projectId: props.projectId,
23
+ limit: props.limit,
24
+ });
25
+ writer(props).end(data.operations, {
26
+ fields: OPERATIONS_FIELDS,
27
+ });
28
+ };
@@ -0,0 +1,11 @@
1
+ import { describe } from '@jest/globals';
2
+ import { testCliCommand } from '../test_utils/test_cli_command.js';
3
+ describe('operations', () => {
4
+ testCliCommand({
5
+ name: 'list',
6
+ args: ['operations', 'list', '--project-id', 'test'],
7
+ expected: {
8
+ snapshot: true,
9
+ },
10
+ });
11
+ });