import fetch from "cross-fetch" ; import * as debug from "debug" ; import { DiagnosticInformation , OnlineStatus , PagedDiagnosticActivation , PagedDiagnosticInformation } from ".." ; import { CredentialAuth } from "./credential-auth" ; import { OnboardingStatus } from "./mindconnect-models" ; import { checkAssetId } from "./utils" ; const log = debug ( "mindconnect-setup" ) ; deprecated export class export class MindConnectSetup extends CredentialAuth { param { string } agentId returns memberOf public async RegisterForDiagnostic ( agentId ) { checkAssetId ( agentId ) ; await this . RenewToken ( ) ; if ( ! this . _accessToken ) throw new Error ( "The agent doesn't have a valid access token." ) ; const headers = { ... this . _apiHeaders , Authorization : `Bearer ${ this . _accessToken . access_token }` } ; const url = `${ this . _gateway }/api/mindconnect/v3/diagnosticActivations` ; log ( `RegisterForDiagnostic Headers ${ JSON . stringify ( headers ) } Url ${ url }` ) ; const body = { agentId : agentId } ; try { const response = await fetch ( url , { method : "POST" , body : JSON . stringify ( body ) , headers : headers , agent : this . _proxyHttpAgent , } as RequestInit ) ; if ( ! response . ok ) { throw new Error ( `${ response . statusText } ${ await response . text ( ) }` ) ; } if ( response . status >= 200 && response . status <= 299 ) { const json = await response . json ( ) ; log ( `RegisterForDiagnostic Response ${ JSON . stringify ( json ) }` ) ; return json ; } else { throw new Error ( `invalid response ${ JSON . stringify ( response ) }` ) ; } } catch ( err ) { log ( err ) ; throw new Error ( `Network error occured ${ err . message }` ) ; } } deprecated param { string } agentId param { boolean } ignoreError returns memberOf public async DeleteDiagnostic ( agentId , ignoreError = false ) { if ( ! /[a-f0-9]{32}/gi . test ( agentId ) ) { throw new Error ( "You have to pass valid 32 char long agent id" ) ; } const activations = await this . GetDiagnosticActivations ( ) ; for ( const activation of activations . content ) { if ( activation . agentId === agentId ) { if ( ! activation . id ) { throw new Error ( "invalid activation id at diagnostic endpoint!" ) ; } await this . DeleteActivation ( activation . id ) ; return true ; } } if ( ! ignoreError ) { throw Error ( "there is no such agent id registered for diagnostic" ) ; } } private async DeleteActivation ( activationId ) { await this . RenewToken ( ) ; if ( ! this . _accessToken ) throw new Error ( "The agent doesn't have a valid access token." ) ; const headers = { ... this . _apiHeaders , Authorization : `Bearer ${ this . _accessToken . access_token }` } ; const url = `${ this . _gateway }/api/mindconnect/v3/diagnosticActivations/${ activationId }` ; log ( `DeleteDiagnostic Headers ${ JSON . stringify ( headers ) } Url ${ url }` ) ; try { const response = await fetch ( url , { method : "DELETE" , headers : headers , agent : this . _proxyHttpAgent , } as RequestInit ) ; if ( ! response . ok ) { throw new Error ( `${ response . statusText } ${ await response . text ( ) }` ) ; } if ( response . status >= 200 && response . status <= 299 ) { return true ; } else { throw new Error ( `invalid response ${ JSON . stringify ( response ) }` ) ; } } catch ( err ) { log ( err ) ; throw new Error ( `Network error occured ${ err . message }` ) ; } } deprecated memberOf public async DeleteAllDiagnosticActivations ( ) { const activations = await this . GetDiagnosticActivations ( ) ; for ( const activation of activations . content ) { if ( activation . id ) await this . DeleteActivation ( activation . id ) ; log ( `Deleting activation with ${ activation . id } for agent ${ activation . agentId }` ) ; } } deprecated returns { Promise < PagedDiagnosticActivation > } memberOf public async GetDiagnosticActivations ( ) { await this . RenewToken ( ) ; if ( ! this . _accessToken ) throw new Error ( "The agent doesn't have a valid access token." ) ; const headers = { ... this . _apiHeaders , Authorization : `Bearer ${ this . _accessToken . access_token }` } ; const url = `${ this . _gateway }/api/mindconnect/v3/diagnosticActivations` ; log ( `GetDiagnosticActivations Headers ${ JSON . stringify ( headers ) } Url ${ url }` ) ; try { const response = await fetch ( url , { method : "GET" , headers : headers , agent : this . _proxyHttpAgent , } as RequestInit ) ; if ( ! response . ok ) { throw new Error ( `${ response . statusText } ${ await response . text ( ) }` ) ; } if ( response . status >= 200 && response . status <= 299 ) { const json = await response . json ( ) ; log ( `GetDiagnosticActivations Response ${ JSON . stringify ( json ) }` ) ; return json ; } else { throw new Error ( `invalid response ${ JSON . stringify ( response ) }` ) ; } } catch ( err ) { log ( err ) ; throw new Error ( `Network error occured ${ err . message }` ) ; } } deprecated param { string } agentId param { ( x : DiagnosticInformation [ ] , ... args : any [ ] ) => any } callback param { * } callbackOptions returns memberOf public async GetDiagnosticInformation ( agentId ? , callback ? , callbackOptions ? , skipToLast = true ) { let pagedInformation ; let page = 0 ; do { pagedInformation = await this . GetPagedInforamtion ( page ++ , agentId ) ; if ( skipToLast ) { page = pagedInformation . totalPages - 2 ; if ( page < 0 ) page = 0 ; skipToLast = false ; if ( ! pagedInformation . last ) { continue ; } } if ( callback ) { callback ( pagedInformation . content , callbackOptions ) ; } } while ( ! pagedInformation . last ) ; return pagedInformation ; } private async GetPagedInforamtion ( page , agentId ? ) { await this . RenewToken ( ) ; if ( ! this . _accessToken ) { throw new Error ( "The agent doesn't have a valid access token." ) ; } const headers = { ... this . _apiHeaders , Authorization : `Bearer ${ this . _accessToken . access_token }` } ; let url = `${ this . _gateway }/api/mindconnect/v3/diagnosticInformation?size=50&page=${ page }` ; if ( agentId ) { url = `${ url }&filter={"agentId" : "${ agentId }"}` ; } log ( `GetDiagnosticInformation Headers ${ JSON . stringify ( headers ) } Url ${ url }` ) ; try { const response = await fetch ( url , { method : "GET" , headers : headers , agent : this . _proxyHttpAgent , } as RequestInit ) ; if ( ! response . ok ) { throw new Error ( `${ response . statusText } ${ await response . text ( ) }` ) ; } if ( response . status >= 200 && response . status <= 299 ) { const json = await response . json ( ) ; log ( `GetDiagnosticInformation Response ${ JSON . stringify ( json ) }` ) ; return json ; } else { throw new Error ( `invalid response ${ JSON . stringify ( response ) }` ) ; } } catch ( err ) { log ( err ) ; throw new Error ( `Network error occured ${ err . message }` ) ; } } public async GetAgentStatus ( agentId ) { await this . RenewToken ( ) ; if ( ! this . _accessToken ) { throw new Error ( "The agent doesn't have a valid access token." ) ; } const headers = { ... this . _apiHeaders , Authorization : `Bearer ${ this . _accessToken . access_token }` } ; const url = `${ this . _gateway }/api/agentmanagement/v3/agents/${ agentId }/status` ; log ( `GetAgentStatus Headers ${ JSON . stringify ( headers ) } Url ${ url }` ) ; try { const response = await fetch ( url , { method : "GET" , headers : headers , agent : this . _proxyHttpAgent , } as RequestInit ) ; if ( ! response . ok ) { throw new Error ( `${ response . statusText } ${ await response . text ( ) }` ) ; } if ( response . status >= 200 && response . status <= 299 ) { const json = await response . json ( ) ; log ( `GetAgentStatus Response ${ JSON . stringify ( json ) }` ) ; return json as OnlineStatus ; } else { throw new Error ( `invalid response ${ JSON . stringify ( response ) }` ) ; } } catch ( err ) { log ( err ) ; throw new Error ( `Network error occured ${ err . message }` ) ; } } public async GetBoardingStatus ( agentId ) { await this . RenewToken ( ) ; if ( ! this . _accessToken ) { throw new Error ( "The agent doesn't have a valid access token." ) ; } const headers = { ... this . _apiHeaders , Authorization : `Bearer ${ this . _accessToken . access_token }` } ; const url = `${ this . _gateway }/api/agentmanagement/v3/agents/${ agentId }/boarding/status` ; log ( `GetAgentStatus Headers ${ JSON . stringify ( headers ) } Url ${ url }` ) ; try { const response = await fetch ( url , { method : "GET" , headers : headers , agent : this . _proxyHttpAgent , } as RequestInit ) ; if ( ! response . ok ) { throw new Error ( `${ response . statusText } ${ await response . text ( ) }` ) ; } if ( response . status >= 200 && response . status <= 299 ) { const json = await response . json ( ) ; log ( `GetBoardingStatus Response ${ JSON . stringify ( json ) }` ) ; return json as OnboardingStatus ; } else { throw new Error ( `invalid response ${ JSON . stringify ( response ) }` ) ; } } catch ( err ) { log ( err ) ; throw new Error ( `Network error occured ${ err . message }` ) ; } } }