Skip to content
This repository was archived by the owner on Dec 16, 2024. It is now read-only.

Commit 92bf37c

Browse files
author
Luke Hoban
committed
Expose defaultEndpoint on Service.
In addition to exposing the full map of endpoints, also add a `defaultEndpoint` property which gets the first endpoint exposed by the service. Currently, services can have at most one exposed endpoint, so this will always return the only one. Fixes #408.
1 parent dbb960c commit 92bf37c

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

api/service.ts

+6
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ export interface Service {
238238
*/
239239
endpoints: pulumi.Output<{ [containerName: string]: { [port: number]: Endpoint } }>;
240240

241+
/**
242+
* The first endpoint exposed by the service. Use [endpoints] to get other
243+
* endpoints exposed on the service by container name and port.
244+
*/
245+
defaultEndpoint: pulumi.Output<Endpoint | undefined>;
246+
241247
/**
242248
* The exposed hostname and port for connecting to the given containerName
243249
* on the given containerPort. If containerName is not provided, the first

aws/service.ts

+26-15
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,7 @@ export class Service extends pulumi.ComponentResource implements cloud.Service {
577577
public readonly ecsService: aws.ecs.Service;
578578

579579
public readonly endpoints: pulumi.Output<Endpoints>;
580+
public readonly defaultEndpoint: pulumi.Output<Endpoint | undefined>;
580581

581582
public readonly getEndpoint: (containerName?: string, containerPort?: number) => Promise<cloud.Endpoint>;
582583

@@ -661,27 +662,17 @@ export class Service extends pulumi.ComponentResource implements cloud.Service {
661662

662663
const localEndpoints = getEndpoints(ports);
663664
this.endpoints = localEndpoints;
665+
this.defaultEndpoint = localEndpoints.apply(getDefaultEndpoint);
664666

665667
this.getEndpoint = async (containerName, containerPort) => {
666668
const endpoints = localEndpoints.get();
667669

668-
containerName = containerName || Object.keys(endpoints)[0];
669-
if (!containerName) {
670-
throw new Error(`No containers available in this service`);
670+
const defaultEndpoint = getDefaultEndpoint(endpoints, containerName, containerPort);
671+
if (!defaultEndpoint) {
672+
throw new Error(`No endpoint found matching provided container name and port.`);
671673
}
672674

673-
const containerPorts = endpoints[containerName] || {};
674-
containerPort = containerPort || +Object.keys(containerPorts)[0];
675-
if (!containerPort) {
676-
throw new Error(`No ports available in service container ${containerName}`);
677-
}
678-
679-
const endpoint = containerPorts[containerPort];
680-
if (!endpoint) {
681-
throw new Error(`No exposed port for ${containerName} port ${containerPort}`);
682-
}
683-
684-
return endpoint;
675+
return defaultEndpoint;
685676
};
686677
}
687678
}
@@ -698,6 +689,26 @@ function getEndpoints(ports: ExposedPorts): pulumi.Output<Endpoints> {
698689
}));
699690
}
700691

692+
function getDefaultEndpoint(endpoints: Endpoints, containerName?: string, containerPort?: number): Endpoint | undefined {
693+
containerName = containerName || Object.keys(endpoints)[0];
694+
if (!containerName) {
695+
return;
696+
}
697+
698+
const containerPorts = endpoints[containerName] || {};
699+
containerPort = containerPort || +Object.keys(containerPorts)[0];
700+
if (!containerPort) {
701+
return;
702+
}
703+
704+
const endpoint = containerPorts[containerPort];
705+
if (!endpoint) {
706+
return;
707+
}
708+
709+
return endpoint;
710+
}
711+
701712
const volumeNames = new Set<string>();
702713

703714
export interface Volume extends cloud.Volume {

0 commit comments

Comments
 (0)