Skip to content

Commit

Permalink
feat(ecs): migrate capacity provider wizard to react (#8824)
Browse files Browse the repository at this point in the history
* feat(ecs): migrate capacity provider wizard to react

Co-authored-by: Parag Bhingre <pbhingre@gmail.com>
  • Loading branch information
paragbhingre and Parag Bhingre committed Jan 11, 2021
1 parent 22e0621 commit a14ae08
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 63 deletions.
2 changes: 2 additions & 0 deletions app/scripts/modules/ecs/src/ecs.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ECS_NETWORKING_REACT } from './serverGroup/configure/wizard/networking/
import { SERVICE_DISCOVERY_REACT } from './serverGroup/configure/wizard/serviceDiscovery/ServiceDiscovery';
import { TASK_DEFINITION_REACT } from './serverGroup/configure/wizard/taskDefinition/TaskDefinition';
import { ECS_SECURITY_GROUP_MODULE } from './securityGroup/securityGroup.module';
import { ECS_CAPACITY_PROVIDER_REACT } from "./serverGroup/configure/wizard/capacityProvider/CapacityProvider";

import ecsLogo from './logo/ecs.logo.svg';
import './logo/ecs.logo.less';
Expand Down Expand Up @@ -66,6 +67,7 @@ module(ECS_MODULE, [
CONTAINER_REACT,
ECS_NETWORKING_REACT,
SERVICE_DISCOVERY_REACT,
ECS_CAPACITY_PROVIDER_REACT,
ECS_SERVER_GROUP_LOGGING,
ECS_CLUSTER_READ_SERVICE,
ECS_SECRET_READ_SERVICE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ export interface IEcsServiceDiscoveryRegistryAssociation {
containerName: string;
}

export interface IEcsCapacityProviderStrategyItem {
capacityProvider: string;
base: number;
weight: number
}

export interface IEcsServerGroupCommand extends IServerGroupCommand {
associatePublicIpAddress: boolean;
backingData: IEcsServerGroupCommandBackingData;
Expand All @@ -137,6 +143,7 @@ export interface IEcsServerGroupCommand extends IServerGroupCommand {
serviceDiscoveryAssociations: IEcsServiceDiscoveryRegistryAssociation[];
useTaskDefinitionArtifact: boolean;

capacityProviderStrategy: IEcsCapacityProviderStrategyItem[];
subnetTypeChanged: (command: IEcsServerGroupCommand) => IServerGroupCommandResult;
placementStrategyNameChanged: (command: IEcsServerGroupCommand) => IServerGroupCommandResult;
regionIsDeprecated: (command: IEcsServerGroupCommand) => boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import React from 'react';
import { module } from 'angular';
import { react2angular } from 'react2angular';
import {IEcsCapacityProviderStrategyItem, IEcsServerGroupCommand} from '../../serverGroupConfiguration.service';
import { HelpField, withErrorBoundary } from '@spinnaker/core';

export interface IEcsCapacityProviderProps {
command: IEcsServerGroupCommand;
notifyAngular: (key: string, value: any) => void;
}

interface IEcsCapacityProviderState {
capacityProviderStrategy: IEcsCapacityProviderStrategyItem[],
}

class EcsCapacityProvider extends React.Component<IEcsCapacityProviderProps, IEcsCapacityProviderState>{
constructor(props: IEcsCapacityProviderProps) {
super(props);
const cmd = this.props.command;

this.state = {
capacityProviderStrategy: cmd.capacityProviderStrategy,
};
}

private addCapacityProviderStrategy = () => {
const capacityProviderStrategy = this.state.capacityProviderStrategy;
capacityProviderStrategy.push({ capacityProvider: '', base: null, weight: null});
this.props.notifyAngular('capacityProviderStrategy', capacityProviderStrategy);
this.setState({ capacityProviderStrategy : capacityProviderStrategy });
};

private removeCapacityProviderStrategy = (index: number) => {
const capacityProviderStrategy = this.state.capacityProviderStrategy;
capacityProviderStrategy.splice(index, 1);
this.props.notifyAngular('capacityProviderStrategy', capacityProviderStrategy);
this.setState({capacityProviderStrategy : capacityProviderStrategy });
}

private updateCapacityProviderName = (index: number, targetCapacityProviderName: string) => {
const capacityProviderStrategy = this.state.capacityProviderStrategy;
const targetCapacityProviderStrategy = capacityProviderStrategy[index];
targetCapacityProviderStrategy.capacityProvider = targetCapacityProviderName;
this.props.notifyAngular('capacityProviderStrategy', capacityProviderStrategy);
this.setState({ capacityProviderStrategy: capacityProviderStrategy });
};

private updateCapacityProviderBase = (index: number, targetCapacityProviderBase: number) => {
const capacityProviderStrategy = this.state.capacityProviderStrategy;
const targetCapacityProviderStrategy = capacityProviderStrategy[index];
targetCapacityProviderStrategy.base = targetCapacityProviderBase;
this.props.notifyAngular('capacityProviderStrategy', capacityProviderStrategy);
this.setState({ capacityProviderStrategy: capacityProviderStrategy });
};

private updateCapacityProviderWeight = (index: number, targetCapacityProviderWeight: number) => {
const capacityProviderStrategy = this.state.capacityProviderStrategy;
const targetCapacityProviderStrategy = capacityProviderStrategy[index];
targetCapacityProviderStrategy.weight= targetCapacityProviderWeight;
this.props.notifyAngular('capacityProviderStrategy', capacityProviderStrategy);
this.setState({ capacityProviderStrategy: capacityProviderStrategy });
};


render(): React.ReactElement<EcsCapacityProvider> {

const updateCapacityProviderName = this.updateCapacityProviderName;
const updateCapacityProviderBase = this.updateCapacityProviderBase;
const updateCapacityProviderWeight = this.updateCapacityProviderWeight;
const addCapacityProviderStrategy = this.addCapacityProviderStrategy;
const removeCapacityProviderStrategy = this.removeCapacityProviderStrategy;

const capacityProviderInputs = this.state.capacityProviderStrategy.map(function (mapping, index) {
return (
<tr key={index}>
<td>
<input
data-test-id={"capacityProvider.name." + index }
type="string"
className="form-control input-sm no-spel"
required={true}
value={mapping.capacityProvider}
onChange={(e) => updateCapacityProviderName(index, e.target.value)}
/>
</td>
<td>
<input
data-test-id={"capacityProvider.base." + index }
type="number"
className="form-control input-sm no-spel"
required={true}
value={mapping.base}
onChange={(e) => updateCapacityProviderBase(index, e.target.valueAsNumber)}
/>
</td>
<td>
<input
data-test-id={"capacityProvider.weight." + index }
type="number"
className="form-control input-sm no-spel"
required={true}
value={mapping.weight}
onChange={(e) => updateCapacityProviderWeight(index, e.target.valueAsNumber)}
/>
</td>
<td>
<div className="form-control-static">
<a className="btn-link sm-label" onClick={() => removeCapacityProviderStrategy(index)}>
<span className="glyphicon glyphicon-trash" />
<span className="sr-only">Remove</span>
</a>
</div>
</td>
</tr>
);
});

const newCapacityProviderStrategy = (
<button className="btn btn-block btn-sm add-new" onClick={addCapacityProviderStrategy} data-test-id="ServerGroup.addCapacityProvider">
<span className="glyphicon glyphicon-plus-sign" />
Add New Capacity Provider
</button>
)


return (
<div>
<div className="sm-label-left">
<b>Capacity Provider Strategy</b>
<HelpField id="ecs.capacityProviderStrategy" />
</div>
<table className="table table-condensed packed tags">
<thead>
<th style={{ width: '50%' }}> Provider name <HelpField id="ecs.capacityProviderName" /></th>
<th style={{ width: '25%' }}> Base <HelpField id="ecs.capacityProviderBase" /></th>
<th style={{ width: '25%' }}>Weight <HelpField id="ecs.capacityProviderWeight" /></th>
</thead>
<tbody>
{capacityProviderInputs}
</tbody>
<tfoot>
<tr>
<td colSpan={4}>{newCapacityProviderStrategy}</td>
</tr>
</tfoot>
</table>

</div>

);
}
}

export const ECS_CAPACITY_PROVIDER_REACT = 'spinnaker.ecs.serverGroup.configure.wizard.capacityProvider.react';
module(ECS_CAPACITY_PROVIDER_REACT, []).component(
'ecsCapacityProviderReact',
react2angular(withErrorBoundary(EcsCapacityProvider, 'ecsCapacityProviderReact'), ['command', 'notifyAngular']),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class="clearfix">
<div class="row">
<div class="col-md-12">
<ecs-capacity-provider-react
command="command"
notify-angular="notifyAngular"
></ecs-capacity-provider-react>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -33,69 +33,8 @@
</div>

<div class="form-group" ng-if="$ctrl.capacityProviderState.useCapacityProviders">
<div class="sm-label-left">
<b>Capacity Provider Strategy</b>
<help-field key="ecs.capacityProviderStrategy"></help-field>
</div>
<div>
<table class="table table-condensed packed tags">
<thead>
<th style="width: 50%">Provider name <help-field key="ecs.capacityProviderName"></help-field></th>
<th style="width: 25%">Base <help-field key="ecs.capacityProviderBase"></help-field></th>
<th style="width: 25%">Weight <help-field key="ecs.capacityProviderWeight"></help-field></th>
</thead>
<tbody>
<tr ng-repeat="cp in $ctrl.command.capacityProviderStrategy">
<td>
<input
type="text"
data-test-id="capacityProvider.name.{{ $index }}"
class="form-control input-sm no-spel"
ng-model="cp.capacityProvider"
/>
</td>
<td>
<input
type="text"
data-test-id="capacityProvider.base.{{ $index }}"
class="form-control input-sm no-spel"
ng-model="cp.base"
/>
</td>
<td>
<input
type="text"
data-test-id="capacityProvider.weight.{{ $index }}"
class="form-control input-sm no-spel"
ng-model="cp.weight"
/>
</td>
<td>
<div class="form-control-static">
<a class="btn-link sm-label" ng-click="$ctrl.command.capacityProviderStrategy.splice($index, 1)">
<span class="glyphicon glyphicon-trash"></span>
<span class="sr-only">Remove</span>
</a>
</div>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3">
<button
class="btn btn-block btn-sm add-new"
ng-click="$ctrl.command.capacityProviderStrategy.push({})"
data-test-id="ServerGroup.addCapacityProvider"
>
<span class="glyphicon glyphicon-plus-sign"></span>
Add New Capacity Provider
</button>
</td>
</tr>
</tfoot>
</table>
</div>
<ecs-capacity-provider-react command="$ctrl.command"
notify-angular="$ctrl.notifyAngular"/>
</div>

<div class="form-group" ng-if="!$ctrl.capacityProviderState.useCapacityProviders" style="padding-top: 10px">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module(ECS_SERVERGROUP_CONFIGURE_WIZARD_HORIZONTALSCALING_HORIZONTALSCALING_COMP
command: '=',
application: '=',
capacityProviderState: '=',
notifyAngular: '='
},
templateUrl: require('./horizontalScaling.component.html'),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
command="command"
application="application"
capacity-provider-state="capacityProviderState"
notify-angular="notifyAngular"
></ecs-server-group-horizontal-scaling>
</div>
</div>
Expand Down

0 comments on commit a14ae08

Please sign in to comment.