22
33const { fetch, fetchApi, CorelliumError } = require ( './util/fetch' )
44const Project = require ( './project' )
5+ const Instance = require ( './instance' )
56const Team = require ( './team' )
67const User = require ( './user' )
78const Role = require ( './role' )
@@ -487,7 +488,6 @@ class Corellium {
487488 * await corellium.getInstance({ id: 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa' })
488489 */
489490 async getInstance ( opts ) {
490- let lastError
491491 let id , throwIfNotOn
492492 if ( typeof opts === 'string' ) {
493493 id = opts
@@ -496,24 +496,19 @@ class Corellium {
496496 ( { id, throwIfNotOn = true } = opts )
497497 }
498498
499- const projects = await this . projects ( )
500- for ( const project of projects ) {
501- try {
502- const instance = await project . getInstance ( id )
503- if ( throwIfNotOn && ( instance . info . state !== 'on' ) ) {
504- throw new Error ( 'The instance is not turned on' )
505- }
506- return instance
507- } catch ( err ) {
508- if ( ! ( err instanceof CorelliumError ) ) {
509- throw err
510- } else {
511- lastError = err
512- }
499+ try {
500+ const info = await fetchApi ( this , `/instances/${ id } ` )
501+ const project = await this . getProject ( info . project ) ;
502+ const instance = new Instance ( project , info ) ;
503+
504+ if ( throwIfNotOn && ( instance . info . state !== 'on' ) ) {
505+ throw new Error ( 'The instance is not turned on' )
513506 }
514- }
515507
516- throw lastError || new Error ( `Could not retrieve instance! instanceId=${ id } ` )
508+ return instance
509+ } catch ( err ) {
510+ throw err ;
511+ }
517512 }
518513}
519514
0 commit comments