@adobe/aio-cli-plugin-api-mesh 1.0.3-beta → 1.1.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.
@@ -14,24 +14,38 @@ const { readFile } = require('fs/promises');
14
14
 
15
15
  const logger = require('../../classes/logger');
16
16
  const { initSdk, initRequestId, promptConfirm } = require('../../helpers');
17
+ const { ignoreCacheFlag, autoConfirmActionFlag } = require('../../utils');
18
+ const { getMeshId, updateMesh } = require('../../lib/devConsole');
19
+
20
+ require('dotenv').config();
17
21
 
18
22
  class UpdateCommand extends Command {
19
- static args = [{ name: 'meshId' }, { name: 'file' }];
23
+ static args = [{ name: 'file' }];
24
+ static flags = {
25
+ ignoreCache: ignoreCacheFlag,
26
+ autoConfirmAction: autoConfirmActionFlag,
27
+ };
20
28
 
21
29
  async run() {
22
30
  await initRequestId();
23
31
 
24
32
  logger.info(`RequestId: ${global.requestId}`);
25
33
 
26
- const { args } = this.parse(UpdateCommand);
34
+ const { args, flags } = await this.parse(UpdateCommand);
27
35
 
28
- if (!args.meshId || !args.file) {
36
+ if (!args.file) {
29
37
  this.error('Missing required args. Run aio api-mesh update --help for more info.');
30
38
 
31
39
  return;
32
40
  }
33
41
 
34
- const { schemaServiceClient, imsOrgId, projectId, workspaceId } = await initSdk();
42
+ const ignoreCache = await flags.ignoreCache;
43
+ const autoConfirmAction = await flags.autoConfirmAction;
44
+
45
+ const { imsOrgId, projectId, workspaceId } = await initSdk({
46
+ ignoreCache,
47
+ });
48
+
35
49
  let data;
36
50
 
37
51
  try {
@@ -45,34 +59,48 @@ class UpdateCommand extends Command {
45
59
  );
46
60
  }
47
61
 
48
- const shouldContinue = await promptConfirm(
49
- `Are you sure you want to update the mesh: ${args.meshId}?`,
50
- );
51
-
52
- if (shouldContinue) {
53
- try {
54
- const response = await schemaServiceClient.updateMesh(
55
- imsOrgId,
56
- projectId,
57
- workspaceId,
58
- args.meshId,
59
- data,
60
- );
62
+ let meshId = null;
61
63
 
62
- this.log('Successfully updated the mesh with the id: %s', args.meshId);
64
+ try {
65
+ meshId = await getMeshId(imsOrgId, projectId, workspaceId);
66
+ } catch (err) {
67
+ this.error(
68
+ `Unable to get mesh ID. Please check the details and try again. RequestId: ${global.requestId}`,
69
+ );
70
+ }
63
71
 
64
- return response;
65
- } catch (error) {
66
- this.log(error.message);
72
+ if (meshId) {
73
+ let shouldContinue = true;
67
74
 
68
- this.error(
69
- `Unable to update the mesh. Please check the mesh configuration file and try again. If the error persists please contact support. RequestId: ${global.requestId}`,
75
+ if (!autoConfirmAction) {
76
+ shouldContinue = await promptConfirm(
77
+ `Are you sure you want to update the mesh: ${meshId}?`,
70
78
  );
71
79
  }
72
- } else {
73
- this.log('Update cancelled');
74
80
 
75
- return 'Update cancelled';
81
+ if (shouldContinue) {
82
+ try {
83
+ const response = await updateMesh(imsOrgId, projectId, workspaceId, meshId, data);
84
+
85
+ this.log('Successfully updated the mesh with the id: %s', meshId);
86
+
87
+ return response;
88
+ } catch (error) {
89
+ this.log(error.message);
90
+
91
+ this.error(
92
+ `Unable to update the mesh. Please check the mesh configuration file and try again. If the error persists please contact support. RequestId: ${global.requestId}`,
93
+ );
94
+ }
95
+ } else {
96
+ this.log('Update cancelled');
97
+
98
+ return 'Update cancelled';
99
+ }
100
+ } else {
101
+ this.error(
102
+ `Unable to update. No mesh found for Org(${imsOrgId}) -> Project(${projectId}) -> Workspace(${workspaceId}). Please check the details and try again.`,
103
+ );
76
104
  }
77
105
  }
78
106
  }
@@ -0,0 +1,21 @@
1
+ const { getCliEnv } = require('@adobe/aio-lib-env');
2
+
3
+ const clientEnv = getCliEnv();
4
+
5
+ const StageConstants = {
6
+ MULTITENANT_GRAPHQL_SERVER_BASE_URL: 'https://graph-stage.adobe.io/api',
7
+ DEV_CONSOLE_BASE_URL: 'https://developers-stage.adobe.io/console',
8
+ DEV_CONSOLE_API_KEY: 'adobe-api-manager-sms-stage',
9
+ DEV_CONSOLE_TRANSPORTER_API_KEY: 'UDPWeb1',
10
+ AIO_CLI_API_KEY: 'aio-cli-console-auth-stage',
11
+ };
12
+
13
+ const ProdConstants = {
14
+ MULTITENANT_GRAPHQL_SERVER_BASE_URL: 'https://graph.adobe.io/api',
15
+ DEV_CONSOLE_BASE_URL: 'https://developers.adobe.io/console',
16
+ DEV_CONSOLE_API_KEY: 'adobe-graph-prod',
17
+ DEV_CONSOLE_TRANSPORTER_API_KEY: 'UDPWeb1',
18
+ AIO_CLI_API_KEY: 'aio-cli-console-auth',
19
+ };
20
+
21
+ module.exports = clientEnv === 'stage' ? StageConstants : ProdConstants;
package/src/helpers.js CHANGED
@@ -1,3 +1,4 @@
1
+ /* eslint-disable no-console */
1
2
  /*
2
3
  Copyright 2021 Adobe. All rights reserved.
3
4
  This file is licensed to you under the Apache License, Version 2.0 (the "License");
@@ -17,18 +18,13 @@ const { getToken, context } = require('@adobe/aio-lib-ims');
17
18
  const { CLI } = require('@adobe/aio-lib-ims/src/context');
18
19
  const libConsoleCLI = require('@adobe/aio-cli-lib-console');
19
20
  const { getCliEnv } = require('@adobe/aio-lib-env');
20
- const aioConsoleLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-api-mesh', {
21
- provider: 'debug',
22
- });
23
21
 
24
- const { SchemaServiceClient } = require('./classes/SchemaServiceClient');
25
22
  const logger = require('../src/classes/logger');
26
23
  const { UUID } = require('./classes/UUID');
24
+ const CONSTANTS = require('./constants');
25
+ const { objToString } = require('./utils');
27
26
 
28
- const CONSOLE_API_KEYS = {
29
- prod: 'aio-cli-console-auth',
30
- stage: 'aio-cli-console-auth-stage',
31
- };
27
+ const { DEV_CONSOLE_BASE_URL, DEV_CONSOLE_API_KEY, AIO_CLI_API_KEY } = CONSTANTS;
32
28
 
33
29
  /**
34
30
  * @returns {any} Returns a config object or null
@@ -38,9 +34,9 @@ async function getDevConsoleConfig() {
38
34
 
39
35
  if (!configFile) {
40
36
  return {
41
- baseUrl: 'https://developers.adobe.io/console',
37
+ baseUrl: DEV_CONSOLE_BASE_URL,
42
38
  accessToken: (await getLibConsoleCLI()).accessToken,
43
- apiKey: 'adobe-graph-prod',
39
+ apiKey: DEV_CONSOLE_API_KEY,
44
40
  };
45
41
  } else {
46
42
  try {
@@ -81,66 +77,191 @@ async function getDevConsoleConfig() {
81
77
  * @returns {string} Returns organizations the user belongs to
82
78
  */
83
79
  async function getAuthorizedOrganization() {
80
+ logger.info(`Initializing organization selection for`);
81
+
84
82
  const { consoleCLI } = await getLibConsoleCLI();
85
83
 
86
- aioConsoleLogger.debug('Get the selected organization');
84
+ logger.debug('Get the selected organization');
87
85
 
88
86
  const consoleConfigOrg = Config.get('console.org');
89
87
 
90
88
  if (!consoleConfigOrg) {
91
89
  const organizations = await consoleCLI.getOrganizations();
92
- const selectedOrg = await consoleCLI.promptForSelectOrganization(organizations);
93
90
 
94
- aioConsoleLogger.debug('Set the console config');
91
+ logger.info(`Retrieved organizations : ${objToString(organizations)}`);
95
92
 
96
- Config.set('console.org', {
97
- id: selectedOrg.id,
98
- code: selectedOrg.code,
99
- name: selectedOrg.name,
100
- });
93
+ if (organizations.length !== 0) {
94
+ const selectedOrg = await consoleCLI.promptForSelectOrganization(organizations);
101
95
 
102
- return Object.assign({}, selectedOrg);
96
+ logger.debug('Set the console org config');
97
+
98
+ Config.set('console.org', selectedOrg);
99
+
100
+ // remove selected project and workspace from config and let the user select a new one
101
+ Config.delete('console.project');
102
+ Config.delete('console.workspace');
103
+ return Object.assign({}, selectedOrg);
104
+ } else {
105
+ logger.error(`No organizations found`);
106
+ }
103
107
  } else {
104
- logger.info(`Selecting your organization as: ${consoleConfigOrg.name}`);
108
+ logger.debug(`Selected organization config ${objToString(consoleConfigOrg)}`);
109
+ console.log(`Selected organization: ${consoleConfigOrg.name}`);
105
110
 
106
111
  return Object.assign({}, consoleConfigOrg);
107
112
  }
108
113
  }
109
114
 
115
+ /**
116
+ * @param imsOrgId
117
+ * @param imsOrgTitle
118
+ */
110
119
  async function getProject(imsOrgId, imsOrgTitle) {
111
120
  logger.info(`Initializing project selection for ${imsOrgId}`);
112
121
 
113
122
  const { consoleCLI } = await getLibConsoleCLI();
114
123
 
115
- const projects = await consoleCLI.getProjects(imsOrgId);
116
- if (projects.length !== 0) {
117
- const selectedProject = await consoleCLI.promptForSelectProject(projects);
124
+ logger.debug('Get the selected project');
125
+
126
+ const consoleConfigProject = Config.get('console.project');
127
+
128
+ if (!consoleConfigProject) {
129
+ const projects = await consoleCLI.getProjects(imsOrgId);
118
130
 
119
- return selectedProject;
131
+ logger.debug(`Retrieved projects for ${imsOrgId} : ${objToString(projects)}`);
132
+
133
+ if (projects.length !== 0) {
134
+ const selectedProject = await consoleCLI.promptForSelectProject(projects);
135
+
136
+ const shouldCacheProject = await promptConfirm(
137
+ `Do you want to use ${selectedProject.title} as selected project for future operations?`,
138
+ );
139
+
140
+ if (shouldCacheProject) {
141
+ Config.set('console.project', selectedProject);
142
+ }
143
+
144
+ // remove selected workspace from config and let the user select a new one
145
+ Config.delete('console.workspace');
146
+
147
+ return Object.assign({}, selectedProject);
148
+ } else {
149
+ logger.error(`No projects found for the selected organization: ${imsOrgTitle}`);
150
+ }
120
151
  } else {
121
- aioConsoleLogger.error(`No projects found for the selected organization: ${imsOrgTitle}`);
152
+ logger.debug(`Selected project config ${objToString(consoleConfigProject)}`);
153
+ console.log(`Selected project: ${consoleConfigProject.title}`);
154
+
155
+ return consoleConfigProject;
122
156
  }
123
157
  }
124
158
 
159
+ /**
160
+ * @param orgId
161
+ * @param projectId
162
+ * @param imsOrgTitle
163
+ * @param projectTitle
164
+ */
125
165
  async function getWorkspace(orgId, projectId, imsOrgTitle, projectTitle) {
126
- logger.info(`Initializing workspace selection for ${orgId} / ${projectId}`);
166
+ logger.info(`Initializing workspace selection for ${orgId} -> ${projectId}`);
167
+
168
+ const { consoleCLI } = await getLibConsoleCLI();
169
+
170
+ logger.debug('Get the selected workspace');
171
+
172
+ const consoleConfigWorkspace = Config.get('console.workspace');
173
+
174
+ if (!consoleConfigWorkspace) {
175
+ const workspaces = await consoleCLI.getWorkspaces(orgId, projectId);
176
+
177
+ logger.debug(`Retrieved workspaces for ${orgId} -> ${projectId} : ${objToString(workspaces)}`);
178
+
179
+ if (workspaces.length !== 0) {
180
+ const selectedWorkspace = await consoleCLI.promptForSelectWorkspace(workspaces);
181
+
182
+ const shouldCacheWorkspace = await promptConfirm(
183
+ `Do you want to use ${selectedWorkspace.name} as selected workspace for future operations?`,
184
+ );
185
+
186
+ if (shouldCacheWorkspace) {
187
+ Config.set('console.workspace', selectedWorkspace);
188
+ }
189
+
190
+ return Object.assign({}, selectedWorkspace);
191
+ } else {
192
+ logger.error(
193
+ `No workspaces found for the selected organization: ${imsOrgTitle} and project: ${projectTitle}`,
194
+ );
195
+ }
196
+ } else {
197
+ logger.debug(`Selected workspace config ${objToString(consoleConfigWorkspace)}`);
198
+ console.log(`Select workspace: ${consoleConfigWorkspace.name}`);
199
+
200
+ return {
201
+ id: consoleConfigWorkspace.id,
202
+ title: consoleConfigWorkspace.name,
203
+ };
204
+ }
205
+ }
206
+
207
+ const selectAuthorizedOrganization = async () => {
208
+ const { consoleCLI } = await getLibConsoleCLI();
209
+ const organizations = await consoleCLI.getOrganizations();
210
+
211
+ if (organizations.length > 0) {
212
+ const selectedOrg = await consoleCLI.promptForSelectOrganization(organizations);
213
+
214
+ if (selectedOrg) {
215
+ return selectedOrg;
216
+ } else {
217
+ throw new Error('No org selected');
218
+ }
219
+ } else {
220
+ this.error('No organizations found');
221
+ }
222
+ };
127
223
 
224
+ const selectProject = async (imsOrgId, imsOrgTitle) => {
128
225
  const { consoleCLI } = await getLibConsoleCLI();
226
+ const projects = await consoleCLI.getProjects(imsOrgId);
129
227
 
228
+ if (projects.length > 0) {
229
+ const selectedProject = await consoleCLI.promptForSelectProject(projects);
230
+
231
+ if (selectedProject) {
232
+ return selectedProject;
233
+ } else {
234
+ throw new Error('No project selected');
235
+ }
236
+ } else {
237
+ this.error('No projects found for the selected organization: ' + imsOrgTitle);
238
+ }
239
+ };
240
+
241
+ const selectWorkspace = async (orgId, projectId, imsOrgTitle, projectTitle) => {
242
+ const { consoleCLI } = await getLibConsoleCLI();
130
243
  const workspaces = await consoleCLI.getWorkspaces(orgId, projectId);
131
- if (workspaces.length !== 0) {
244
+
245
+ if (workspaces.length > 0) {
132
246
  const selectedWorkspace = await consoleCLI.promptForSelectWorkspace(workspaces);
133
247
 
134
- return selectedWorkspace;
248
+ if (selectedWorkspace) {
249
+ return selectedWorkspace;
250
+ } else {
251
+ throw new Error('No workspace selected');
252
+ }
135
253
  } else {
136
- aioConsoleLogger.error(
137
- `No workspaces found for the selected organization: ${imsOrgTitle} and project: ${projectTitle}`,
254
+ this.error(
255
+ 'No workspaces found for the selected organization: ' +
256
+ imsOrgTitle +
257
+ ' and project: ' +
258
+ projectTitle,
138
259
  );
139
260
  }
140
- }
261
+ };
141
262
 
142
263
  /**
143
- * @private
264
+ * @returns {consoleCLI, accessToken}
144
265
  */
145
266
  async function getLibConsoleCLI() {
146
267
  await context.setCli({ 'cli.bare-output': true }, false);
@@ -151,7 +272,7 @@ async function getLibConsoleCLI() {
151
272
 
152
273
  const consoleCLI = await libConsoleCLI.init({
153
274
  accessToken: accessToken,
154
- apiKey: CONSOLE_API_KEYS[clientEnv],
275
+ apiKey: AIO_CLI_API_KEY,
155
276
  env: clientEnv,
156
277
  });
157
278
 
@@ -161,24 +282,30 @@ async function getLibConsoleCLI() {
161
282
  /**
162
283
  * @returns {any} Returns an object with properties ready for consumption
163
284
  */
164
- async function initSdk() {
165
- const org = await getAuthorizedOrganization();
166
- const project = await getProject(org.id, org.name);
167
- const workspace = await getWorkspace(org.id, project.id, org.name, project.title);
285
+ async function initSdk(options) {
286
+ const { ignoreCache = false } = options;
168
287
 
169
- aioConsoleLogger.log(
288
+ let org;
289
+ let project;
290
+ let workspace;
291
+
292
+ if (!ignoreCache) {
293
+ org = await getAuthorizedOrganization();
294
+ project = await getProject(org.id, org.name);
295
+ workspace = await getWorkspace(org.id, project.id, org.name, project.title);
296
+ } else {
297
+ org = await selectAuthorizedOrganization();
298
+ project = await selectProject(org.id, org.name);
299
+ workspace = await selectWorkspace(org.id, project.id, org.name, project.title);
300
+ }
301
+
302
+ logger.info(
170
303
  `Initializing SDK for org: ${org.name}, project: ${project.title} and workspace: ${workspace.title}`,
171
304
  );
172
305
 
173
306
  logger.info('Initialized user login and the selected organization');
174
307
 
175
- const { baseUrl, accessToken, apiKey } = await getDevConsoleConfig();
176
-
177
- const schemaServiceClient = new SchemaServiceClient();
178
- schemaServiceClient.init(baseUrl, accessToken, apiKey);
179
-
180
308
  return {
181
- schemaServiceClient: schemaServiceClient,
182
309
  imsOrgId: org.id,
183
310
  projectId: project.id,
184
311
  workspaceId: workspace.id,
@@ -213,10 +340,52 @@ async function promptConfirm(message) {
213
340
  return confirm.res;
214
341
  }
215
342
 
343
+ /**
344
+ * Function to run the CLI selectable list
345
+ *
346
+ * @param {string} message - prompt message
347
+ * @param {object[]} choices - list of options
348
+ * @returns {object[]} - selected options
349
+ */
350
+ async function promptMultiselect(message, choices) {
351
+ const selected = await inquirer.prompt([
352
+ {
353
+ name: 'items',
354
+ message: message,
355
+ type: 'checkbox',
356
+ choices: choices,
357
+ },
358
+ ]);
359
+
360
+ return selected.items;
361
+ }
362
+
363
+ /**
364
+ * Function to run the CLI selectable list
365
+ *
366
+ * @param {string} message - prompt message
367
+ * @param {object[]} choices - list of options
368
+ * @returns {object[]} - selected options
369
+ */
370
+ async function promptSelect(message, choices) {
371
+ const selected = await inquirer.prompt([
372
+ {
373
+ name: 'item',
374
+ message: message,
375
+ type: 'list',
376
+ choices: choices,
377
+ },
378
+ ]);
379
+
380
+ return selected.item;
381
+ }
382
+
216
383
  module.exports = {
217
384
  promptConfirm,
218
385
  getLibConsoleCLI,
219
386
  getDevConsoleConfig,
220
387
  initSdk,
221
388
  initRequestId,
389
+ promptSelect,
390
+ promptMultiselect,
222
391
  };