-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreate.ts
More file actions
102 lines (88 loc) · 2.85 KB
/
create.ts
File metadata and controls
102 lines (88 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { getOption } from '../../utils/options.js';
import { getLoginMethod } from '../../auth/s3-client.js';
import { getAuthClient } from '../../auth/client.js';
import { getSelectedOrganization } from '../../auth/storage.js';
import { getTigrisConfig } from '../../auth/config.js';
import { createAccessKey } from '@tigrisdata/iam';
import {
printStart,
printSuccess,
printFailure,
msg,
} from '../../utils/messages.js';
import {
exitWithError,
getSuccessNextActions,
printNextActions,
} from '../../utils/exit.js';
const context = msg('access-keys', 'create');
export default async function create(options: Record<string, unknown>) {
printStart(context);
const json = getOption<boolean>(options, ['json']);
const format = json
? 'json'
: getOption<string>(options, ['format', 'f', 'F'], 'table');
const name = getOption<string>(options, ['name']);
if (!name) {
printFailure(context, 'Access key name is required');
exitWithError('Access key name is required', context);
}
const loginMethod = await getLoginMethod();
if (loginMethod !== 'oauth') {
printFailure(
context,
'Access keys can only be created when logged in via OAuth.\nRun "tigris login oauth" first.'
);
exitWithError(
'Access keys can only be created when logged in via OAuth.\nRun "tigris login oauth" first.',
context
);
}
const authClient = getAuthClient();
const isAuthenticated = await authClient.isAuthenticated();
if (!isAuthenticated) {
printFailure(context, 'Not authenticated. Run "tigris login oauth" first.');
exitWithError(
'Not authenticated. Run "tigris login oauth" first.',
context
);
}
const accessToken = await authClient.getAccessToken();
const selectedOrg = getSelectedOrganization();
const tigrisConfig = getTigrisConfig();
const { data, error } = await createAccessKey(name, {
config: {
sessionToken: accessToken,
organizationId: selectedOrg ?? undefined,
iamEndpoint: tigrisConfig.iamEndpoint,
},
});
if (error) {
printFailure(context, error.message);
exitWithError(error, context);
}
if (format === 'json') {
const nextActions = getSuccessNextActions(context, {
name: data.name,
id: data.id,
});
const output: Record<string, unknown> = {
action: 'created',
name: data.name,
id: data.id,
secret: data.secret,
};
if (nextActions.length > 0) output.nextActions = nextActions;
console.log(JSON.stringify(output));
} else {
console.log(` Name: ${data.name}`);
console.log(` Access Key ID: ${data.id}`);
console.log(` Secret Access Key: ${data.secret}`);
console.log('');
console.log(
' Save these credentials securely. The secret will not be shown again.'
);
}
printSuccess(context);
printNextActions(context, { name: data.name, id: data.id });
}