@hello.nrfcloud.com/nrfcloud-api-helpers 1.2.0 → 1.2.1
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.
package/dist/settings/index.d.ts
CHANGED
package/dist/settings/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { SSMClient } from '@aws-sdk/client-ssm';
|
|
2
|
-
/**
|
|
3
|
-
* Initializes the nRF Cloud Account
|
|
4
|
-
*/
|
|
5
|
-
export declare const initializeAccount: ({ ssm, stackName, account, iotEndpoint, }: {
|
|
6
|
-
ssm: SSMClient;
|
|
7
|
-
stackName: string;
|
|
8
|
-
account: string;
|
|
9
|
-
iotEndpoint: string;
|
|
10
|
-
}) => (reset?: boolean) => Promise<void>;
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { SSMClient } from '@aws-sdk/client-ssm';
|
|
2
|
-
import chalk from 'chalk';
|
|
3
|
-
import { NRFCLOUD_ACCOUNT_SCOPE, nrfCloudAccount } from './scope.js';
|
|
4
|
-
import { get } from '@bifravst/aws-ssm-settings-helpers';
|
|
5
|
-
import { createAccountDevice } from '../api/createAccountDevice.js';
|
|
6
|
-
import { deleteAccountDevice } from '../api/deleteAccountDevice.js';
|
|
7
|
-
import { getAccountInfo } from '../api/getAccountInfo.js';
|
|
8
|
-
import { defaultApiEndpoint, getSettings as getNRFCloudSettings, putSettings as putNRFCloudSettings, } from './settings.js';
|
|
9
|
-
/**
|
|
10
|
-
* Initializes the nRF Cloud Account
|
|
11
|
-
*/
|
|
12
|
-
export const initializeAccount = ({ ssm, stackName, account, iotEndpoint, }) => async (reset = false) => {
|
|
13
|
-
const settingsReader = get(ssm)({
|
|
14
|
-
stackName,
|
|
15
|
-
scope: NRFCLOUD_ACCOUNT_SCOPE,
|
|
16
|
-
context: nrfCloudAccount(account),
|
|
17
|
-
});
|
|
18
|
-
const { apiKey, apiEndpoint } = await settingsReader();
|
|
19
|
-
if (apiKey === undefined)
|
|
20
|
-
throw new Error(`nRF Cloud API key not configured.`);
|
|
21
|
-
let settingsWithAccountDevice = undefined;
|
|
22
|
-
try {
|
|
23
|
-
settingsWithAccountDevice = await getNRFCloudSettings({
|
|
24
|
-
ssm,
|
|
25
|
-
stackName,
|
|
26
|
-
account,
|
|
27
|
-
})();
|
|
28
|
-
console.log(chalk.white('Stack settings'));
|
|
29
|
-
Object.entries(settingsWithAccountDevice).forEach(([k, v]) => console.log(chalk.yellow(`${k}:`), chalk.blue(v)));
|
|
30
|
-
console.log();
|
|
31
|
-
}
|
|
32
|
-
catch (err) {
|
|
33
|
-
console.log(chalk.magenta(`Stack not configured.`));
|
|
34
|
-
}
|
|
35
|
-
const effectiveEndpoint = apiEndpoint === undefined ? defaultApiEndpoint : new URL(apiEndpoint);
|
|
36
|
-
const accountInfo = await getAccountInfo({
|
|
37
|
-
endpoint: effectiveEndpoint,
|
|
38
|
-
apiKey,
|
|
39
|
-
});
|
|
40
|
-
if ('error' in accountInfo) {
|
|
41
|
-
console.error(chalk.red('⚠️'), '', chalk.red(accountInfo.error.message));
|
|
42
|
-
process.exit(1);
|
|
43
|
-
}
|
|
44
|
-
console.log(chalk.yellow('AWS IoT endpoint:'), chalk.blue(iotEndpoint));
|
|
45
|
-
console.log();
|
|
46
|
-
console.log(chalk.white('nRF Cloud account info:'));
|
|
47
|
-
Object.entries(accountInfo).forEach(([k, v]) => console.log(chalk.yellow(`${k}:`), chalk.blue(v)));
|
|
48
|
-
console.log();
|
|
49
|
-
if (reset === true) {
|
|
50
|
-
console.debug(chalk.magenta(`Deleting account device ...`));
|
|
51
|
-
await deleteAccountDevice({ apiKey, endpoint: effectiveEndpoint });
|
|
52
|
-
console.log(chalk.green(`Account device deleted.`));
|
|
53
|
-
}
|
|
54
|
-
if (settingsWithAccountDevice === undefined || reset === true) {
|
|
55
|
-
console.debug(chalk.magenta(`Generating new account device credentials ...`));
|
|
56
|
-
const { clientCert, privateKey } = await createAccountDevice({
|
|
57
|
-
apiKey,
|
|
58
|
-
endpoint: effectiveEndpoint,
|
|
59
|
-
});
|
|
60
|
-
if (privateKey === undefined) {
|
|
61
|
-
console.error(chalk.red('⚠️'), chalk.red('Account device exists'));
|
|
62
|
-
throw new Error(`Account device exists in account ${accountInfo.team.tenantId}`);
|
|
63
|
-
}
|
|
64
|
-
console.log(chalk.green(`Account device created.`));
|
|
65
|
-
await putNRFCloudSettings({ ssm, stackName, account })({
|
|
66
|
-
accountDeviceClientCert: clientCert,
|
|
67
|
-
accountDevicePrivateKey: privateKey,
|
|
68
|
-
accountDeviceClientId: `account-${accountInfo.team.tenantId}`,
|
|
69
|
-
mqttEndpoint: accountInfo.mqttEndpoint,
|
|
70
|
-
mqttTopicPrefix: accountInfo.mqttTopicPrefix,
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
};
|