import { get, set } from 'lodash'; import React, { useEffect, useState } from 'react'; import type { IAccount, IStageConfigProps } from '@spinnaker/core'; import { AccountRegionClusterSelector, AccountService, AppListExtractor, DeploymentStrategySelector, HelpField, NameUtils, PlatformHealthOverride, Registry, StageConfigField, StageConstants, TargetSelect, } from '@spinnaker/core'; const capacityFields = ['min', 'max', 'desired'] as const; export function AwsCloneServerGroupStageConfig(props: IStageConfigProps) { const { application, pipeline, stage, stageFieldUpdated, updateStageField } = props; const [accounts, setAccounts] = useState([]); useEffect(() => { const changes: Record = {}; if (!stage.application) { changes.application = application.name; } if (stage.cloudProvider !== 'aws') { changes.cloudProvider = 'aws'; } if (stage.cloudProviderType !== 'aws') { changes.cloudProviderType = 'aws'; } if (!stage.credentials && application.defaultCredentials?.aws) { changes.credentials = application.defaultCredentials.aws; } if (stage.target === undefined) { changes.target = StageConstants.TARGET_LIST[0].val; } if (stage.useSourceCapacity === undefined) { changes.useSourceCapacity = true; } if ( stage.isNew && application.attributes.platformHealthOnlyShowOverride && application.attributes.platformHealthOnly && stage.interestingHealthProviderNames === undefined ) { changes.interestingHealthProviderNames = ['Amazon']; } if (stage.isNew) { changes.useAmiBlockDeviceMappings = get( application, 'attributes.providerSettings.aws.useAmiBlockDeviceMappings', false, ); changes.copySourceCustomBlockDeviceMappings = false; } if (Object.keys(changes).length) { updateStageField(changes); } }, []); useEffect(() => { let active = true; AccountService.listAccounts('aws').then((loadedAccounts) => active && setAccounts(loadedAccounts)); return () => { active = false; }; }, []); useEffect(() => { if (stage.targetCluster) { const filterByCluster = AppListExtractor.monikerClusterNameFilter(stage.targetCluster); const moniker = AppListExtractor.getMonikers([application], filterByCluster)[0]; if (moniker) { updateStageField({ stack: moniker.stack, freeFormDetails: moniker.detail }); } else { const nameParts = NameUtils.parseClusterName(stage.targetCluster); updateStageField({ stack: nameParts.stack, freeFormDetails: nameParts.freeFormDetails }); } } else if (stage.stack || stage.freeFormDetails) { updateStageField({ stack: '', freeFormDetails: '' }); } }, [stage.targetCluster]); const toggleSuspendedProcess = (process: string) => { const suspendedProcesses: string[] = stage.suspendedProcesses ? [...stage.suspendedProcesses] : []; const index = suspendedProcesses.indexOf(process); if (index === -1) { suspendedProcesses.push(process); } else { suspendedProcesses.splice(index, 1); } updateStageField({ suspendedProcesses }); }; const blockDeviceMappingsSource = stage.copySourceCustomBlockDeviceMappings ? 'source' : stage.useAmiBlockDeviceMappings ? 'ami' : 'default'; const selectBlockDeviceMappingsSource = (selection: string) => { if (selection === 'source') { updateStageField({ copySourceCustomBlockDeviceMappings: true, useAmiBlockDeviceMappings: false }); } else if (selection === 'ami') { updateStageField({ copySourceCustomBlockDeviceMappings: false, useAmiBlockDeviceMappings: true }); } else { updateStageField({ copySourceCustomBlockDeviceMappings: false, useAmiBlockDeviceMappings: false }); } }; return (
{!pipeline.strategy && ( )} updateStageField({ target })} options={StageConstants.TARGET_LIST} />
{stage.useSourceCapacity === false && ( <>
Min
Max
Desired
{capacityFields.map((field) => (
updateStageField({ capacity: { ...stage.capacity, [field]: event.target.value === '' ? undefined : Number(event.target.value), }, }) } required type="number" value={stage.capacity?.[field] ?? ''} />
))}
)}
{application.attributes.platformHealthOnlyShowOverride && ( updateStageField({ interestingHealthProviderNames })} platformHealthType="Amazon" /> )} { set(stage, key, value); stageFieldUpdated(); }} onStrategyChange={() => stageFieldUpdated()} />
); } export const awsCloneServerGroupStage = { key: 'cloneServerGroup', provides: 'cloneServerGroup', cloudProvider: 'aws', component: AwsCloneServerGroupStageConfig, accountExtractor: (stage: any) => [stage.context.credentials], validators: [ { type: 'requiredField', fieldName: 'targetCluster', fieldLabel: 'cluster' }, { type: 'requiredField', fieldName: 'target' }, { type: 'requiredField', fieldName: 'region' }, { type: 'requiredField', fieldName: 'credentials', fieldLabel: 'account' }, ], }; export function registerAwsCloneServerGroupStage() { Registry.pipeline.registerStage(awsCloneServerGroupStage); } registerAwsCloneServerGroupStage();