@adobe/aio-cli-plugin-api-mesh 1.0.3-beta → 1.0.4-beta

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.
@@ -11,60 +11,80 @@ governing permissions and limitations under the License.
11
11
 
12
12
  const { Command } = require('@oclif/command');
13
13
  const { writeFile } = require('fs/promises');
14
+
14
15
  const logger = require('../../classes/logger');
15
16
  const { initSdk, initRequestId } = require('../../helpers');
17
+ const { ignoreCacheFlag } = require('../../utils');
18
+ const { getMeshId, getMesh } = require('../../lib/devConsole');
16
19
 
17
20
  require('dotenv').config();
18
21
 
19
22
  class GetCommand extends Command {
20
- static args = [{ name: 'meshId' }, { name: 'file' }];
23
+ static args = [{ name: 'file' }];
24
+ static flags = {
25
+ ignoreCache: ignoreCacheFlag,
26
+ };
21
27
 
22
28
  async run() {
23
29
  await initRequestId();
24
30
 
25
31
  logger.info(`RequestId: ${global.requestId}`);
26
32
 
27
- const { args } = this.parse(GetCommand);
33
+ const { args, flags } = await this.parse(GetCommand);
28
34
 
29
- if (!args.meshId) {
30
- this.error('Missing Mesh ID. Run aio api-mesh get --help for more info.');
35
+ const ignoreCache = await flags.ignoreCache;
31
36
 
32
- return;
33
- }
37
+ const { imsOrgId, projectId, workspaceId } = await initSdk({
38
+ ignoreCache,
39
+ });
34
40
 
35
- const { schemaServiceClient, imsOrgId, projectId, workspaceId } = await initSdk();
41
+ let meshId = null;
36
42
 
37
43
  try {
38
- const mesh = await schemaServiceClient.getMesh(imsOrgId, projectId, workspaceId, args.meshId);
44
+ meshId = await getMeshId(imsOrgId, projectId, workspaceId);
45
+ } catch (err) {
46
+ this.error(
47
+ `Unable to get mesh ID. Please check the details and try again. RequestId: ${global.requestId}`,
48
+ );
49
+ }
50
+
51
+ if (meshId) {
52
+ try {
53
+ const mesh = await getMesh(imsOrgId, projectId, workspaceId, meshId);
39
54
 
40
- if (mesh) {
41
- this.log('Successfully retrieved mesh %s', JSON.stringify(mesh, null, 2));
55
+ if (mesh) {
56
+ this.log('Successfully retrieved mesh %s', JSON.stringify(mesh, null, 2));
42
57
 
43
- if (args.file) {
44
- try {
45
- const { meshConfig } = mesh;
46
- await writeFile(args.file, JSON.stringify({ meshConfig }, null, 2));
58
+ if (args.file) {
59
+ try {
60
+ const { meshConfig } = mesh;
61
+ await writeFile(args.file, JSON.stringify({ meshConfig }, null, 2));
47
62
 
48
- this.log('Successfully wrote mesh to file %s', args.file);
49
- } catch (error) {
50
- this.log('Unable to write mesh to file %s', args.file);
63
+ this.log('Successfully wrote mesh to file %s', args.file);
64
+ } catch (error) {
65
+ this.log('Unable to write mesh to file %s', args.file);
51
66
 
52
- logger.error(error);
67
+ logger.error(error);
68
+ }
53
69
  }
70
+
71
+ return mesh;
72
+ } else {
73
+ this.error(
74
+ `Unable to get mesh with the ID ${meshId}. Please check the mesh ID and try again. RequestId: ${global.requestId}`,
75
+ { exit: false },
76
+ );
54
77
  }
78
+ } catch (error) {
79
+ this.log(error.message);
55
80
 
56
- return mesh;
57
- } else {
58
81
  this.error(
59
- `Unable to get mesh with the ID ${args.meshId}. Please check the mesh ID and try again. RequestId: ${global.requestId}`,
60
- { exit: false },
82
+ `Unable to get mesh. Please check the details and try again. If the error persists please contact support. RequestId: ${global.requestId}`,
61
83
  );
62
84
  }
63
- } catch (error) {
64
- this.log(error.message);
65
-
85
+ } else {
66
86
  this.error(
67
- `Unable to get mesh. Please check the details and try again. If the error persists please contact support. RequestId: ${global.requestId}`,
87
+ `Unable to get mesh config. No mesh found for Org(${imsOrgId}) -> Project(${projectId}) -> Workspace(${workspaceId}). Please check the details and try again.`,
68
88
  );
69
89
  }
70
90
  }
@@ -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,27 +77,37 @@ 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)}`);
92
+
93
+ if (organizations.length !== 0) {
94
+ const selectedOrg = await consoleCLI.promptForSelectOrganization(organizations);
95
+
96
+ logger.debug('Set the console org config');
95
97
 
96
- Config.set('console.org', {
97
- id: selectedOrg.id,
98
- code: selectedOrg.code,
99
- name: selectedOrg.name,
100
- });
98
+ Config.set('console.org', selectedOrg);
101
99
 
102
- return Object.assign({}, selectedOrg);
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
+
104
+ return Object.assign({}, selectedOrg);
105
+ } else {
106
+ logger.error(`No organizations found`);
107
+ }
103
108
  } else {
104
- logger.info(`Selecting your organization as: ${consoleConfigOrg.name}`);
109
+ logger.debug(`Selected organization config ${objToString(consoleConfigOrg)}`);
110
+ console.log(`Selected organization: ${consoleConfigOrg.name}`);
105
111
 
106
112
  return Object.assign({}, consoleConfigOrg);
107
113
  }
@@ -112,35 +118,141 @@ async function getProject(imsOrgId, imsOrgTitle) {
112
118
 
113
119
  const { consoleCLI } = await getLibConsoleCLI();
114
120
 
115
- const projects = await consoleCLI.getProjects(imsOrgId);
116
- if (projects.length !== 0) {
117
- const selectedProject = await consoleCLI.promptForSelectProject(projects);
121
+ logger.debug('Get the selected project');
122
+
123
+ const consoleConfigProject = Config.get('console.project');
124
+
125
+ if (!consoleConfigProject) {
126
+ const projects = await consoleCLI.getProjects(imsOrgId);
127
+
128
+ logger.debug(`Retrieved projects for ${imsOrgId} : ${objToString(projects)}`);
129
+
130
+ if (projects.length !== 0) {
131
+ const selectedProject = await consoleCLI.promptForSelectProject(projects);
132
+
133
+ const shouldCacheProject = await promptConfirm(
134
+ `Do you want to use ${selectedProject.title} as selected project for future operations?`,
135
+ );
136
+
137
+ if (shouldCacheProject) {
138
+ Config.set('console.project', selectedProject);
139
+ }
140
+
141
+ // remove selected workspace from config and let the user select a new one
142
+ Config.delete('console.workspace');
118
143
 
119
- return selectedProject;
144
+ return Object.assign({}, selectedProject);
145
+ } else {
146
+ logger.error(`No projects found for the selected organization: ${imsOrgTitle}`);
147
+ }
120
148
  } else {
121
- aioConsoleLogger.error(`No projects found for the selected organization: ${imsOrgTitle}`);
149
+ logger.debug(`Selected project config ${objToString(consoleConfigProject)}`);
150
+ console.log(`Selected project: ${consoleConfigProject.title}`);
151
+
152
+ return consoleConfigProject;
122
153
  }
123
154
  }
124
155
 
125
156
  async function getWorkspace(orgId, projectId, imsOrgTitle, projectTitle) {
126
- logger.info(`Initializing workspace selection for ${orgId} / ${projectId}`);
157
+ logger.info(`Initializing workspace selection for ${orgId} -> ${projectId}`);
127
158
 
128
159
  const { consoleCLI } = await getLibConsoleCLI();
129
160
 
161
+ logger.debug('Get the selected workspace');
162
+
163
+ const consoleConfigWorkspace = Config.get('console.workspace');
164
+
165
+ if (!consoleConfigWorkspace) {
166
+ const workspaces = await consoleCLI.getWorkspaces(orgId, projectId);
167
+
168
+ logger.debug(`Retrieved workspaces for ${orgId} -> ${projectId} : ${objToString(workspaces)}`);
169
+
170
+ if (workspaces.length !== 0) {
171
+ const selectedWorkspace = await consoleCLI.promptForSelectWorkspace(workspaces);
172
+
173
+ const shouldCacheWorkspace = await promptConfirm(
174
+ `Do you want to use ${selectedWorkspace.name} as selected workspace for future operations?`,
175
+ );
176
+
177
+ if (shouldCacheWorkspace) {
178
+ Config.set('console.workspace', selectedWorkspace);
179
+ }
180
+
181
+ return Object.assign({}, selectedWorkspace);
182
+ } else {
183
+ logger.error(
184
+ `No workspaces found for the selected organization: ${imsOrgTitle} and project: ${projectTitle}`,
185
+ );
186
+ }
187
+ } else {
188
+ logger.debug(`Selected workspace config ${objToString(consoleConfigWorkspace)}`);
189
+ console.log(`Select workspace: ${consoleConfigWorkspace.name}`);
190
+
191
+ return {
192
+ id: consoleConfigWorkspace.id,
193
+ title: consoleConfigWorkspace.name,
194
+ };
195
+ }
196
+ }
197
+
198
+ const selectAuthorizedOrganization = async () => {
199
+ const { consoleCLI } = await getLibConsoleCLI();
200
+ const organizations = await consoleCLI.getOrganizations();
201
+
202
+ if (organizations.length > 0) {
203
+ const selectedOrg = await consoleCLI.promptForSelectOrganization(organizations);
204
+
205
+ if (selectedOrg) {
206
+ return selectedOrg;
207
+ } else {
208
+ throw new Error('No org selected');
209
+ }
210
+ } else {
211
+ this.error('No organizations found');
212
+ }
213
+ };
214
+
215
+ const selectProject = async (imsOrgId, imsOrgTitle) => {
216
+ const { consoleCLI } = await getLibConsoleCLI();
217
+ const projects = await consoleCLI.getProjects(imsOrgId);
218
+
219
+ if (projects.length > 0) {
220
+ const selectedProject = await consoleCLI.promptForSelectProject(projects);
221
+
222
+ if (selectedProject) {
223
+ return selectedProject;
224
+ } else {
225
+ throw new Error('No project selected');
226
+ }
227
+ } else {
228
+ this.error('No projects found for the selected organization: ' + imsOrgTitle);
229
+ }
230
+ };
231
+
232
+ const selectWorkspace = async (orgId, projectId, imsOrgTitle, projectTitle) => {
233
+ const { consoleCLI } = await getLibConsoleCLI();
130
234
  const workspaces = await consoleCLI.getWorkspaces(orgId, projectId);
131
- if (workspaces.length !== 0) {
235
+
236
+ if (workspaces.length > 0) {
132
237
  const selectedWorkspace = await consoleCLI.promptForSelectWorkspace(workspaces);
133
238
 
134
- return selectedWorkspace;
239
+ if (selectedWorkspace) {
240
+ return selectedWorkspace;
241
+ } else {
242
+ throw new Error('No workspace selected');
243
+ }
135
244
  } else {
136
- aioConsoleLogger.error(
137
- `No workspaces found for the selected organization: ${imsOrgTitle} and project: ${projectTitle}`,
245
+ this.error(
246
+ 'No workspaces found for the selected organization: ' +
247
+ imsOrgTitle +
248
+ ' and project: ' +
249
+ projectTitle,
138
250
  );
139
251
  }
140
- }
252
+ };
141
253
 
142
254
  /**
143
- * @private
255
+ * @returns {consoleCLI, accessToken}
144
256
  */
145
257
  async function getLibConsoleCLI() {
146
258
  await context.setCli({ 'cli.bare-output': true }, false);
@@ -151,7 +263,7 @@ async function getLibConsoleCLI() {
151
263
 
152
264
  const consoleCLI = await libConsoleCLI.init({
153
265
  accessToken: accessToken,
154
- apiKey: CONSOLE_API_KEYS[clientEnv],
266
+ apiKey: AIO_CLI_API_KEY,
155
267
  env: clientEnv,
156
268
  });
157
269
 
@@ -161,24 +273,30 @@ async function getLibConsoleCLI() {
161
273
  /**
162
274
  * @returns {any} Returns an object with properties ready for consumption
163
275
  */
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);
276
+ async function initSdk(options) {
277
+ const { ignoreCache = false } = options;
278
+
279
+ let org;
280
+ let project;
281
+ let workspace;
282
+
283
+ if (!ignoreCache) {
284
+ org = await getAuthorizedOrganization();
285
+ project = await getProject(org.id, org.name);
286
+ workspace = await getWorkspace(org.id, project.id, org.name, project.title);
287
+ } else {
288
+ org = await selectAuthorizedOrganization();
289
+ project = await selectProject(org.id, org.name);
290
+ workspace = await selectWorkspace(org.id, project.id, org.name, project.title);
291
+ }
168
292
 
169
- aioConsoleLogger.log(
293
+ logger.info(
170
294
  `Initializing SDK for org: ${org.name}, project: ${project.title} and workspace: ${workspace.title}`,
171
295
  );
172
296
 
173
297
  logger.info('Initialized user login and the selected organization');
174
298
 
175
- const { baseUrl, accessToken, apiKey } = await getDevConsoleConfig();
176
-
177
- const schemaServiceClient = new SchemaServiceClient();
178
- schemaServiceClient.init(baseUrl, accessToken, apiKey);
179
-
180
299
  return {
181
- schemaServiceClient: schemaServiceClient,
182
300
  imsOrgId: org.id,
183
301
  projectId: project.id,
184
302
  workspaceId: workspace.id,