Skip to content

Commit

Permalink
fix: Handle case where no ECS tasks are returned (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
niallthomson authored May 31, 2024
1 parent 2e54c29 commit b441b78
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 9 deletions.
59 changes: 59 additions & 0 deletions plugins/ecs/backend/src/service/DefaultAmazonEcsService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,65 @@ describe('DefaultAmazonEcsService', () => {

expect(mockResourceLocator.getResourceArns).toHaveBeenCalledTimes(0);
});

it('handles empty tasks', async () => {
const cluster1 = mockEcsCluster('cluster1');
const service1 = mockEcsService('service1', 'cluster1', 1, 1, 0);

ecsMock
.on(DescribeClustersCommand, {
clusters: ['cluster1'],
})
.resolves({
clusters: [cluster1],
});

ecsMock
.on(DescribeServicesCommand, {
cluster: 'cluster1',
// services: ['service1'],
})
.resolves({
services: [service1],
});

ecsMock
.on(ListTasksCommand, {
cluster: 'cluster1',
serviceName: 'service1',
})
.resolves({
taskArns: [],
});

const service = await configureProvider(
{},
{
metadata: {
annotations: {
[AWS_ECS_SERVICE_ARN_ANNOTATION]:
'arn:aws:ecs:us-west-2:1234567890:service/cluster1/service1',
},
},
},
);

const response = await service.getServicesByEntity({ entityRef });

await expect(response).toMatchObject({
clusters: [
{
cluster: cluster1,
services: [
{
service: service1,
tasks: [],
},
],
},
],
});
});
});

it('throws on missing annotation', async () => {
Expand Down
21 changes: 14 additions & 7 deletions plugins/ecs/backend/src/service/DefaultAmazonEcsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
DescribeClustersCommand,
ListTasksCommand,
DescribeTasksCommand,
Task,
} from '@aws-sdk/client-ecs';
import { parse } from '@aws-sdk/util-arn-parser';
import { CatalogApi } from '@backstage/catalog-client';
Expand Down Expand Up @@ -205,16 +206,22 @@ export class DefaultAmazonEcsService implements AmazonECSService {
}),
);

const describeTasksResp = await client.send(
new DescribeTasksCommand({
cluster,
tasks: listTasksResp.taskArns,
}),
);
let tasks: Task[] = [];

if (listTasksResp.taskArns?.length) {
const describeTasksResp = await client.send(
new DescribeTasksCommand({
cluster,
tasks: listTasksResp.taskArns,
}),
);

tasks = describeTasksResp.tasks || [];
}

serviceResponseObjects.push({
service: serviceResp,
tasks: describeTasksResp.tasks!,
tasks,
});
}
}
Expand Down
12 changes: 10 additions & 2 deletions plugins/ecs/frontend/src/components/EcsServices/EcsServices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,11 @@ const ClusterSummary = ({ cluster }: { cluster: ClusterResponse }) => {
spacing={0}
>
<Grid item>
<StatusOK>{runningTasks} running tasks</StatusOK>
{runningTasks > 0 ? (
<StatusOK>{runningTasks} running tasks</StatusOK>
) : (
<StatusOK>No running tasks</StatusOK>
)}
</Grid>
<Grid item>
{pendingTasks > 0 ? (
Expand Down Expand Up @@ -279,7 +283,11 @@ const ServiceSummary = ({ service }: { service: Service }) => {
spacing={0}
>
<Grid item>
<StatusOK>{service.runningCount} running tasks</StatusOK>
{service.runningCount! > 0 ? (
<StatusOK>{service.runningCount} running tasks</StatusOK>
) : (
<StatusOK>No running tasks</StatusOK>
)}
</Grid>
<Grid item>
{service.pendingCount! > 0 ? (
Expand Down

0 comments on commit b441b78

Please sign in to comment.