import { groupBy , sortBy } from 'lodash' ; import { AccountService , CertificateReader } from '@spinnaker/core' ; import type { IAmazonCertificate } from '../domain' ; export class AmazonCertificateReader { public static listCertificates ( ) { return CertificateReader . listCertificatesByProvider ( 'aws' ) . then ( ( certificates ) => { return AccountService . listAllAccounts ( 'aws' ) . then ( ( allAccountDetails ) => { const accountIdToName = allAccountDetails . reduce ( ( acc , accountDetails ) => { acc [ accountDetails . accountId ] = accountDetails . name ; return acc ; } , { } as { [ id ] : string ; } ) ; const sortedCertificates = sortBy ( certificates , 'serverCertificateName' ) ; return groupBy ( sortedCertificates , ( cert ) => { const [ , , , , accountId ] = cert . arn . split ( ':' ) ; return accountIdToName [ accountId ] || 'unknown' ; } ) ; } ) ; } ) ; } }