import { get , trim } from 'lodash' ; import React from 'react' ; import type { IManifest } from '@spinnaker/core' ; import { AccountService , ReactInjector } from '@spinnaker/core' ; const UNMAPPED_K8S_RESOURCE_STATE_KEY = 'kubernetesResource' ; export interface IManifestDetailsProps { manifest : IManifest ; linkName : string ; accountId : string ; } export interface IManifestDetailsState { url : string ; } export class ManifestDetailsLink extends React . Component < IManifestDetailsProps , IManifestDetailsState > { private spinnakerKindStateMap : { [ k ] : string ; } = { serverGroupManagers : 'serverGroupManager' , serverGroups : 'serverGroup' , } ; constructor ( props ) { super ( props ) ; this . state = { url : '' , } ; this . loadUrl ( ) ; } private canOpen ( ) { return ! ! this . props . manifest . manifest && ! ! this . state . url ; } private spinnakerKindFromKubernetesKind ( kind , kindMap ) { const foundKind = Object . keys ( kindMap ) . find ( ( k ) => k . toLowerCase ( ) === kind . toLowerCase ( ) ) ; return kindMap [ foundKind ] ; } private resourceRegion ( ) { return trim ( get ( this . props , [ 'manifest' , 'manifest' , 'metadata' , 'annotations' , 'artifact.spinnaker.io/location' ] , '' ) ) ; } private getStateParams ( stateKey ) { const kind = this . props . manifest . manifest . kind . toLowerCase ( ) ; const name = this . props . manifest . manifest . metadata . name ; const region = this . resourceRegion ( ) ; const params = { accountId : this . props . accountId , provider : 'kubernetes' , region , reg : region , [ stateKey ] : `${ kind } ${ name }` , } ; if ( ! params . region && kind === 'namespace' && stateKey === UNMAPPED_K8S_RESOURCE_STATE_KEY ) { params . region = name ; } if ( ! params . region || params . region === '' ) { params . region = '_' ; } return params ; } private loadUrl ( ) { const kind = get ( this . props , [ 'manifest' , 'manifest' , 'kind' ] , '' ) ; const { accountId } = this . props ; AccountService . getAccountDetails ( accountId ) . then ( ( account ) => { const spinnakerKind = this . spinnakerKindFromKubernetesKind ( kind , account . spinnakerKindMap ) ; const stateKey = this . spinnakerKindStateMap [ spinnakerKind ] || UNMAPPED_K8S_RESOURCE_STATE_KEY ; const params = this . getStateParams ( stateKey ) ; const url = ReactInjector . $state . href ( `home.applications.application.insight.clusters.${ stateKey }` , params ) ; this . setState ( { url } ) ; } ) ; } public render ( ) { if ( this . canOpen ( ) ) { return ( < a href = { this . state . url } className = "clickable" > { this . props . linkName } < /a>); } else { return null ; } } }