@@ -6,6 +6,10 @@ import checkNewVersions from './check-new-version';
66import { AWSSecretsManagerUtils } from "./aws-sm-utils" ;
77import { AWSIoTCoreConnector } from "./aws-iotcore-connector" ;
88import searchList from 'inquirer-search-list' ;
9+ import { EdgeImpulseApi } from '../sdk/studio' ;
10+ import os from 'node:os' ;
11+
12+ const PREFIX = '\x1b[33m[INT]\x1b[0m' ;
913
1014inquirer . registerPrompt ( 'search-list' , searchList ) ;
1115
@@ -164,6 +168,7 @@ export async function setupCliApp(configFactory: Config, config: EdgeImpulseConf
164168 apiKeyArgv : string | undefined ,
165169 devArgv : boolean ,
166170 hmacKeyArgv : string | undefined ,
171+ verboseArgv : boolean ,
167172 connectProjectMsg : string ,
168173 getProjectFromConfig ?: ( deviceId : string | undefined ) => Promise < { projectId : number } | undefined >
169174} , deviceId : string | undefined ) {
@@ -228,24 +233,63 @@ export async function setupCliApp(configFactory: Config, config: EdgeImpulseConf
228233 apiKey : opts . apiKeyArgv || '' ,
229234 hmacKey : opts . hmacKeyArgv || '0'
230235 } ;
231- if ( ! opts . apiKeyArgv ) {
236+
237+ // get hmac dev key (if set)
238+ if ( ! opts . hmacKeyArgv ) {
232239 try {
233- let dk = ( await config . api . projects . listDevkeys ( projectId ) ) ;
240+ let dk = ( await config . api . projects . getHmacDevkey ( projectId ) ) ;
241+ if ( dk . hmacKey ) {
242+ devKeys . hmacKey = dk . hmacKey ;
243+ }
244+ }
245+ catch ( ex2 ) {
246+ // noop, e.g. key with not enough permissions
247+ if ( opts . verboseArgv ) {
248+ const ex = < Error > ex2 ;
249+ console . log ( PREFIX , `Could not fetch development Hmac key: ` + ( ex . message || ex . toString ( ) ) ) ;
250+ }
251+ }
252+ }
234253
235- if ( ! dk . apiKey ) {
236- throw new Error ( 'No API key set (via --api-key), and no development API keys configured for ' +
237- 'this project. Add a development API key from the Edge Impulse dashboard to continue.' ) ;
254+ if ( ! opts . apiKeyArgv ) {
255+ // create API key
256+ let apiKey = await configFactory . getApiKeyForProject ( projectId ) ;
257+ if ( apiKey ) {
258+ // validate API key...
259+ const api = new EdgeImpulseApi ( {
260+ endpoint : config . endpoints . internal . api ,
261+ extraHeaders : { 'User-Agent' : 'EDGE_IMPULSE_CLI' } ,
262+ } ) ;
263+ api . authenticate ( {
264+ method : 'apiKey' ,
265+ apiKey : apiKey ,
266+ } ) ;
267+ try {
268+ await api . projects . getProjectInfo ( projectId ) ;
238269 }
270+ catch ( ex2 ) {
271+ // noop, e.g. key with not enough permissions
272+ if ( opts . verboseArgv ) {
273+ const ex = < Error > ex2 ;
274+ console . log ( PREFIX , `Failed to do request with stored API key for project ${ projectId } (ERR: ${ ex . message || ex . toString ( ) } ). ` +
275+ `Creating new API key...` ) ;
276+ }
239277
240- devKeys . apiKey = dk . apiKey ;
241- if ( ! opts . hmacKeyArgv && dk . hmacKey ) {
242- devKeys . hmacKey = dk . hmacKey ;
278+ apiKey = undefined ;
243279 }
244280 }
245- catch ( ex2 ) {
246- let ex = < Error > ex2 ;
247- throw new Error ( 'Failed to load development keys: ' + ( ex . message || ex . toString ( ) ) ) ;
281+
282+ if ( ! apiKey ) {
283+ const res = await config . api . projects . addProjectApiKey ( projectId , {
284+ name : `Edge Impulse CLI (${ os . hostname ( ) } )` ,
285+ role : 'ingestion_deployment' ,
286+ isDevelopmentKey : false ,
287+ } ) ;
288+ apiKey = res . apiKey ;
289+ await configFactory . setApiKeyForProject ( projectId , apiKey ) ;
248290 }
291+
292+ devKeys . apiKey = apiKey ;
249293 }
250294
251295 return {
0 commit comments