import type { FormikProps } from 'formik'; import React from 'react'; import type { Option } from 'react-select'; import type { IWizardPageComponent } from '@spinnaker/core'; import { HelpField, ReactInjector, TetheredSelect } from '@spinnaker/core'; import type { IAmazonServerGroupCommand } from '../../serverGroupConfiguration.service'; export interface IServerGroupLoadBalancersProps { hideLoadBalancers?: boolean; hideTargetGroups?: boolean; targetGroupTypeHelpText?: 'instance' | 'ip'; formik: FormikProps; } export interface IServerGroupLoadBalancersState { refreshed: boolean; refreshing: boolean; showVpcLoadBalancers: boolean; } const stringToOption = (value: string): Option => { return { value, label: value }; }; export class ServerGroupLoadBalancers extends React.Component implements IWizardPageComponent { public state = { refreshing: false, refreshed: false, showVpcLoadBalancers: false, }; public validate(values: IAmazonServerGroupCommand) { const errors = {} as any; if (values.viewState.dirty.targetGroups) { errors.targetGroups = 'You must confirm the removed target groups.'; } if (values.viewState.dirty.loadBalancers) { errors.loadBalancers = 'You must confirm the removed load balancers.'; } return errors; } public refreshLoadBalancers = () => { const { values } = this.props.formik; this.setState({ refreshing: true }); const configurationService: any = ReactInjector.providerServiceDelegate.getDelegate( values.cloudProvider || values.selectedProvider, 'serverGroup.configurationService', ); configurationService.refreshLoadBalancers(values).then(() => { this.setState({ refreshing: false, refreshed: true, }); }); }; public clearWarnings(key: 'loadBalancers' | 'targetGroups'): void { this.props.formik.values.viewState.dirty[key] = null; this.props.formik.validateForm(); } private targetGroupsChanged = (options: Array>) => { const targetGroups = options.map((o) => o.value); this.props.formik.setFieldValue('targetGroups', targetGroups); }; private loadBalancersChanged = (options: Array>) => { const loadBalancers = options.map((o) => o.value); this.props.formik.setFieldValue('loadBalancers', loadBalancers); }; private vpcLoadBalancersChanged = (options: Array>) => { const vpcLoadBalancers = options.map((o) => o.value); this.props.formik.setFieldValue('vpcLoadBalancers', vpcLoadBalancers); }; public render() { const { hideLoadBalancers, hideTargetGroups } = this.props; const { values } = this.props.formik; const { dirty } = values.viewState; const { refreshed, refreshing, showVpcLoadBalancers } = this.state; let targetGroupSection = null; if (!hideTargetGroups) { const targetGroupOptions = (values.backingData.filtered.targetGroups || []) .concat(values.viewState.spelTargetGroups || []) .map(stringToOption); targetGroupSection = ( <> {dirty.targetGroups && (

The following target groups could not be found in the selected account/region/VPC and were removed:

    {dirty.targetGroups.map((tg) => (
  • {tg}
  • ))}

this.clearWarnings('targetGroups')} > Okay

)}
Target Groups
{targetGroupOptions.length === 0 && (
No {this.props.targetGroupTypeHelpText ?? 'instance'} target groups found in the selected account/region/VPC
)} {targetGroupOptions.length > 0 && ( )}
); } let loadBalancersSection = null; if (!hideLoadBalancers) { const loadBalancerOptions = (values.backingData.filtered.loadBalancers || []) .concat(values.viewState.spelLoadBalancers || []) .map(stringToOption); const vpcLoadBalancerOptions = (values.backingData.filtered.vpcLoadBalancers || []).map(stringToOption); const hasVpcLoadBalancers = values.vpcLoadBalancers && values.vpcLoadBalancers.length > 0; loadBalancersSection = ( <> {dirty.loadBalancers && (

The following load balancers could not be found in the selected account/region/VPC and were removed:

    {dirty.loadBalancers.map((lb) => (
  • {lb}
  • ))}

this.clearWarnings('loadBalancers')} > Okay

)}
Classic Load Balancers
{loadBalancerOptions.length === 0 && (
No load balancers found in the selected account/region/VPC
)} {loadBalancerOptions.length > 0 && ( )}
{!values.vpcId && (
{!hasVpcLoadBalancers && !showVpcLoadBalancers && ( )} {hasVpcLoadBalancers && (
VPC Load Balancers
{vpcLoadBalancerOptions.length > 0 && ( )}
)}
)} {!refreshed && (
{refreshing && (

refreshing...

)} {!refreshing && (

If you are looking for a load balancer or target group from a different application,
click here {' '} to load all load balancers.

)}
)} ); } return (
{targetGroupSection} {loadBalancersSection}
); } }