import { EmbeddedFunctionsEnvironment } from './embedded-functions.environment' ; import { PortalService } from './../portal.service' ; import { LogCategories } from './../../models/constants' ; import { AzureTryEnvironment } from './azure-try.environment' ; import { TranslateService } from '@ngx-translate/core' ; import { LogService } from './../log.service' ; import { NationalCloudEnvironment } from './national-cloud.environment' ; import { DynamicSiteEnvironment } from './dynamic-site.environment' ; import { SiteSlotEnvironment } from './site-slot.environment' ; import { Observable } from 'rxjs/Observable' ; import { AzureEnvironment } from './azure.environment' ; import { ScenarioCheckResult , ScenarioResult } from './scenario.models' ; import { ScenarioCheckInput } from './scenario.models' ; import { StandaloneEnvironment } from './stand-alone.environment' ; import { OnPremEnvironment } from './onprem.environment' ; import { Environment } from './scenario.models' ; import { Injectable , Injector } from '@angular/core' ; import { LinuxSiteEnvironment } from 'app/shared/services/scenario/linux-site.environment' ; import { XenonSiteEnvironment } from 'app/shared/services/scenario/xenon-site.environment' ; import { DynamicLinuxEnvironment } from './dynamic-linux.environment' ; import { FunctionAppEnvironment } from './function-app.environment' ; import { WindowsCodeEnvironment } from './windows-code.environment' ; import { ContainerEnvironment } from './container.environment' ; import { KubeAppEnvironment } from './kube-app.environment' ; import { AppOnAse } from './app-on-ase.environment' ; export interface IScenarioService { checkScenario ( id , input ? ) : ScenarioCheckResult ; checkScenarioAsync ( id , input ? ) : Observable < ScenarioCheckResult > ; } @ Injectable ( ) export class ScenarioService implements IScenarioService { private _environments : Environment [ ] = [ new StandaloneEnvironment ( ) , new OnPremEnvironment ( this . _injector ) , new SiteSlotEnvironment ( this . _translateService ) , new DynamicSiteEnvironment ( this . _translateService ) , new LinuxSiteEnvironment ( this . _translateService ) , new XenonSiteEnvironment ( this . _injector ) , new AzureTryEnvironment ( ) , new EmbeddedFunctionsEnvironment ( this . _portalService ) , new DynamicLinuxEnvironment ( this . _translateService ) , new FunctionAppEnvironment ( this . _injector ) , new WindowsCodeEnvironment ( this . _injector ) , new ContainerEnvironment ( ) , new KubeAppEnvironment ( ) , new AppOnAse ( this . _injector ) , ] ; constructor ( private _logService , private _translateService , private _portalService , private _injector ) { if ( NationalCloudEnvironment . isNationalCloud ( ) ) { this . _environments . splice ( 0 , 0 , new NationalCloudEnvironment ( _injector ) ) ; } else { this . _environments . splice ( 0 , 0 , new AzureEnvironment ( _injector ) ) ; } } public checkScenario ( id , input ? ) { const results = this . _environments . filter ( env => env . isCurrentEnvironment ( input ) && env . scenarioChecks [ id ] ) . map ( env => { const check = env . scenarioChecks [ id ] ; if ( check . runCheckAsync ) { throw Error ( `An async check was defined for Environment: "${ env . name }", Scenario: "${ check . id }".  You must run checkScenarioAsync instead.` ) ; } if ( check . runCheck ) { const result = check . runCheck ( input ) ; return < ScenarioCheckResult > { status : result && result . status , data : result && result . data , environmentName : env . name , id : id , } ; } else { throw Error ( 'No runCheck method implemented for Environment: "${env.name}", Scenario: "${check.id}"' ) ; } } ) ; return this . _getFinalResult ( id , results ) ; } public checkScenarioAsync ( id , input ? ) { const checks = this . _environments . filter ( env => env . isCurrentEnvironment ( input ) && env . scenarioChecks [ id ] ) . map ( env => { const check = env . scenarioChecks [ id ] ; let runCheckObs ; if ( check . runCheckAsync ) { runCheckObs = check . runCheckAsync ( input ) ; } else if ( check . runCheck ) { runCheckObs = Observable . of ( check . runCheck ( input ) ) ; } else { throw Error ( 'No runCheckAsync or runCheck method implemented for Environment: "${env.name}", Scenario: "${check.id}"' ) ; } return runCheckObs . map ( r => { return < ScenarioCheckResult > { status : r . status , data : r . data , environmentName : env . name , id : id , } ; } ) ; } ) ; if ( checks . length === 0 ) { return Observable . of ( < ScenarioCheckResult > { id : id , status : null , environmentName : null , } ) ; } return Observable . zip . apply ( this , checks ) . map ( results => { return this . _getFinalResult ( id , results ) ; } ) ; } private _getFinalResult ( id , results ) { let enabledResult ; let result ; const disabledResult = results . find ( r => { if ( r && r . status === 'disabled' ) { return true ; } else if ( r && r . status === 'enabled' && ! enabledResult ) { enabledResult = r ; } return false ; } ) ; if ( disabledResult ) { result = disabledResult ; } else if ( enabledResult ) { result = enabledResult ; } else { result = < ScenarioCheckResult > { id : id , status : null , environmentName : null , } ; } this . _logService . debug ( LogCategories . scenarioService , `Final result: id = ${ result . id }, environment = ${ result . environmentName }, status = ${ result . status }` ) ; return result ; } }