import React from 'react'; import type { Option } from 'react-select'; import Select from 'react-select'; import type { IServerGroupCommand } from '@spinnaker/core'; import { HelpField, SpelNumberInput } from '@spinnaker/core'; import type { IMinMaxDesiredProps } from './MinMaxDesired'; export interface ICapacitySelectorProps { command: IServerGroupCommand; setFieldValue: (field: keyof IServerGroupCommand, value: any, shouldValidate?: boolean) => void; MinMaxDesired: React.ComponentClass; } export class CapacitySelector extends React.Component { private preferSourceCapacityOptions = [ { label: 'fail the stage', value: false }, { label: 'use fallback values', value: true }, ]; private useSourceCapacityUpdated = (event: React.ChangeEvent): void => { const value = event.target.value === 'true'; const { command } = this.props; this.props.setFieldValue('useSourceCapacity', value); if (!value) { delete command.preferSourceCapacity; this.props.setFieldValue('preferSourceCapacity', undefined); } this.setState({}); }; private setSimpleCapacity(simpleCapacity: boolean) { const { command } = this.props; const newViewState = { ...command.viewState, useSimpleCapacity: simpleCapacity, }; this.props.setFieldValue('useSourceCapacity', false); this.props.setFieldValue('viewState', newViewState); this.setMinMax(command.capacity.desired); this.setState({}); } private simpleInstancesChanged = (value: number | string) => { this.setMinMax(value); }; private setMinMax(value: number | string) { const { command } = this.props; if (command.viewState.useSimpleCapacity) { command.capacity = { min: value, max: value, desired: value }; this.props.setFieldValue('useSourceCapacity', false); this.props.setFieldValue('capacity', command.capacity); } this.setState({}); } private preferSourceCapacityChanged = (option: Option) => { this.props.setFieldValue('preferSourceCapacity', option && option.value ? true : undefined); this.setState({}); }; private capacityFieldChanged = (fieldName: 'min' | 'max' | 'desired', value: number | string) => { const { command, setFieldValue } = this.props; command.capacity = { ...command.capacity }; command.capacity[fieldName] = value; setFieldValue('capacity', command.capacity); }; public render() { const { command, MinMaxDesired } = this.props; const readOnlyFields = command.viewState.readOnlyFields || {}; if (!command.viewState.useSimpleCapacity || command.useSourceCapacity) { return (

Sets up auto-scaling constraints for this server group.

To set min, max, and desired instance counts to the same value use the{' '} this.setSimpleCapacity(true)}> Simple Mode .

{/* // TODO: Test this in a clone server group dialog or an edit pipeline dialog */} {!readOnlyFields.useSourceCapacity && command.viewState.mode === 'editPipeline' && (
Capacity
{command.useSourceCapacity && (
If no current server group is found, Let me specify the capacity
)} {(!command.useSourceCapacity || command.viewState.mode !== 'editPipeline') && (
)}
); } return (

Sets the min, max, and desired instance counts to the same value.

{' '} To set capacity for auto-scaling, use the{' '} this.setSimpleCapacity(false)}> Advanced Mode .

Number of Instances
); } }