import { isEmpty } from 'lodash' ; import { Observable } from 'rxjs' ; import type { IServerGroupDetailsProps , ISubnet } from '@spinnaker/core' ; import { AccountService , ServerGroupReader , SubnetReader } from '@spinnaker/core' ; import type { IAmazonLoadBalancer , IAmazonServerGroup , IAmazonServerGroupView } from '../../domain' ; import { AwsReactInjector } from '../../reactShims' ; import { AutoScalingProcessService } from './scalingProcesses/AutoScalingProcessService' ; function extractServerGroupSummary ( props ) { const { app , serverGroup } = props ; return app . ready ( ) . then ( ( ) => { let summary = app . serverGroups . data . find ( ( toCheck ) => { return ( toCheck . name === serverGroup . name && toCheck . account === serverGroup . accountId && toCheck . region === serverGroup . region ) ; } ) ; if ( ! summary ) { app . loadBalancers . data . some ( ( loadBalancer ) => { if ( loadBalancer . account === serverGroup . accountId && loadBalancer . region === serverGroup . region ) { return loadBalancer . serverGroups . some ( ( possibleServerGroup ) => { if ( possibleServerGroup . name === serverGroup . name ) { summary = possibleServerGroup ; return true ; } return false ; } ) ; } return false ; } ) ; } return summary ; } ) ; } export function amazonServerGroupDetailsGetter ( props , autoClose ) { const { app , serverGroup : serverGroupInfo } = props ; return new Observable < IAmazonServerGroupView > ( ( observer ) => { extractServerGroupSummary ( props ) . then ( ( summary ) => { ServerGroupReader . getServerGroup ( app . name , serverGroupInfo . accountId , serverGroupInfo . region , serverGroupInfo . name ) . then ( ( details ) => { Object . assign ( details , summary , { account : serverGroupInfo . accountId } ) ; const serverGroup = AwsReactInjector . awsServerGroupTransformer . normalizeServerGroupDetails ( details ) ; AccountService . getAccountDetails ( serverGroup . account ) . then ( ( accountDetails ) => { serverGroup . accountDetails = accountDetails ; observer . next ( serverGroup ) ; } ) ; if ( ! isEmpty ( serverGroup ) ) { const vpc = serverGroup . asg ? serverGroup . asg . vpczoneIdentifier : '' ; if ( vpc !== '' ) { const subnetId = vpc . split ( ',' ) [ 0 ] ; SubnetReader . listSubnets ( ) . then ( ( subnets ) => { const subnet = subnets . find ( ( s ) => s . id === subnetId ) ; serverGroup . subnetType = subnet . purpose ; observer . next ( serverGroup ) ; } ) ; } serverGroup . disabledDate = AutoScalingProcessService . getDisabledDate ( serverGroup ) ; observer . next ( serverGroup ) ; } else { autoClose ( ) ; } } , autoClose ) ; } , autoClose ) ; } ) ; }