Skip to content

Commit eba6a45

Browse files
Added validation for management token before importing a stack data (#1481)
* Added validation for management token before importing a stack data * Updated display error color to red * Added optional chaining for managementTokenResult variable
1 parent e9c40b8 commit eba6a45

File tree

4 files changed

+103
-6
lines changed

4 files changed

+103
-6
lines changed

packages/contentstack-bootstrap/src/bootstrap/utils.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ export const setupEnvironments = async (
6767
.stack({ api_key: api_key })
6868
.managementToken()
6969
.create(managementBody);
70+
if(!managementTokenResult.uid){
71+
cliux.print(
72+
`Info: Failed to generate a management token.\nNote: Management token is not available in your plan. Please contact the admin for support.`,
73+
{
74+
color: 'yellow',
75+
},
76+
);
77+
if ((await continueBootstrapCommand()) === 'no') {
78+
return;
79+
}
80+
}
7081
}
7182
if (Array.isArray(environmentResult.items) && environmentResult.items.length > 0) {
7283
for (const environment of environmentResult.items) {
@@ -229,15 +240,13 @@ const envFileHandler = async (
229240
filePath = pathValidator(path.join(sanitizePath(clonedDirectory), sanitizePath(fileName)));
230241
content = `CONTENTSTACK_API_KEY=${environmentVariables.api_key}\nCONTENTSTACK_DELIVERY_TOKEN=${
231242
environmentVariables.deliveryToken
232-
}\n${
243+
}\nCONTENTSTACK_BRANCH=main${
233244
livePreviewEnabled
234245
? `\nCONTENTSTACK_PREVIEW_TOKEN=${
235246
environmentVariables.preview_token || `''`
236247
}\nCONTENTSTACK_PREVIEW_HOST=${previewHost}\nCONTENTSTACK_APP_HOST=${appHost}\n`
237248
: '\n'
238-
}CONTENTSTACK_ENVIRONMENT=${environmentVariables.environment}\nCONTENTSTACK_API_HOST=${
239-
customHost ? customHost : managementAPIHost
240-
}${
249+
}CONTENTSTACK_ENVIRONMENT=${environmentVariables.environment}${
241250
!isUSRegion && !customHost ? '\nCONTENTSTACK_REGION=' + region.name : ''
242251
}\nCONTENTSTACK_LIVE_PREVIEW=${livePreviewEnabled}\nCONTENTSTACK_LIVE_EDIT_TAGS=false\nCONTENTSTACK_API_HOST=${
243252
customHost ? customHost : managementAPIHost

packages/contentstack-bootstrap/src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const config: Configuration = {
5050
stack: 'contentstack/stack-contentstack-angular-modularblock-example',
5151
},
5252
'compass-app': {
53-
source: 'SunilLsagar/universal-demo',
53+
source: 'SunilLsagar/universal-demo-latest',
5454
stack: 'SunilLsagar/stack-universal-demo-latest',
5555
master_locale: 'en',
5656
},

packages/contentstack-seed/src/seed/contentstack/client.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,33 @@ export interface Stack {
1616
org_uid: string;
1717
}
1818

19+
export interface ManagementToken {
20+
response_code: string;
21+
response_message: string;
22+
}
23+
24+
1925
export interface CreateStackOptions {
2026
name: string;
2127
description: string;
2228
master_locale: string;
2329
org_uid: string;
2430
}
2531

32+
export interface createManagementTokenOptions{
33+
name: string;
34+
description: string;
35+
expires_on: string;
36+
scope: {
37+
module: string;
38+
acl: {
39+
read: boolean;
40+
write?: boolean;
41+
};
42+
branches?: string[];
43+
}[];
44+
}
45+
2646
export default class ContentstackClient {
2747
instance: Promise<ContentstackManagementSDK.ContentstackClient>;
2848

@@ -167,6 +187,37 @@ export default class ContentstackClient {
167187
}
168188
}
169189

190+
async createManagementToken(api_key: string, managementToken: any, options: createManagementTokenOptions): Promise<ManagementToken> {
191+
try {
192+
const client = await this.instance;
193+
const body = {
194+
token: {
195+
name: options.name,
196+
description: options.description,
197+
scope: options.scope,
198+
expires_on: options.expires_on,
199+
},
200+
};
201+
202+
const response = await client.stack({ api_key: api_key, management_token: managementToken }).managementToken().create(body);
203+
return {
204+
response_code: response.errorCode,
205+
response_message: response.errorMessage
206+
};
207+
} catch (error: unknown) {
208+
const typedError = error as { errorCode: string };
209+
210+
if (typedError.errorCode === '401') {
211+
return {
212+
response_code: '401',
213+
response_message: 'You do not have access to create management tokens. Please try again or ask an Administrator for assistance.'
214+
}
215+
}
216+
throw this.buildError(typedError);
217+
}
218+
219+
}
220+
170221
private buildError(error: any) {
171222
const message = error.errorMessage || error.response.data?.errorMessage || error.response.statusText;
172223
const status = error.status;

packages/contentstack-seed/src/seed/index.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,46 @@ export default class ContentModelSeeder {
184184
let count;
185185
const stack_details = await this.csClient.getStack(api_key);
186186
if(this.options.master_locale != stack_details.master_locale){
187-
cliux.print(`Compass app requires the master locale to be set to English (en).`);
187+
cliux.print(`Compass app requires the master locale to be set to English (en).`,{
188+
color: "yellow",
189+
bold: true,
190+
});
188191
return false;
189192
}
193+
const managementBody = {
194+
"name":"Checking roles for creating management token",
195+
"description":"This is a compass app management token.",
196+
"scope":[
197+
{
198+
"module":"content_type",
199+
"acl":{
200+
"read":true,
201+
"write":true
202+
}
203+
},
204+
{
205+
"module":"branch",
206+
"branches":[
207+
"main"
208+
],
209+
"acl":{
210+
"read":true
211+
}
212+
}
213+
],
214+
"expires_on": "3000-01-01",
215+
"is_email_notification_enabled":false
216+
}
217+
let managementTokenResult = await this.csClient.createManagementToken(api_key, this.managementToken, managementBody);
218+
if(managementTokenResult?.response_code == "161" || managementTokenResult?.response_code == "401"){
219+
cliux.print(
220+
`Info: Failed to generate a management token.\nNote: Management token is not available in your plan. Please contact the admin for support.`,
221+
{
222+
color: 'red',
223+
},
224+
);
225+
return false;
226+
}
190227
count = await this.csClient.getContentTypeCount(api_key, this.managementToken);
191228

192229
if (count > 0 && this._options.skipStackConfirmation !== 'yes') {

0 commit comments

Comments
 (0)