public static resolvePeerToPort()

in packages/constructs/L2/ec2-constructs/lib/securitygroup.ts [286:317]


  public static resolvePeerToPort(peer: MdaaPeer): Port {
    const protocol: Protocol = Protocol[peer.protocol.toUpperCase() as keyof typeof Protocol];
    if (typeof protocol === undefined || protocol == undefined) {
      throw new Error(`Unknown protocol defined: ${peer.protocol}`);
    }
    const fromPort = peer.port;
    const toPort = peer.toPort || fromPort;

    let stringRepresentation = `${protocol.toString()}`;
    if (protocol == Protocol.ALL) {
      stringRepresentation = `${stringRepresentation} ALL TRAFFIC`;
    } else {
      if (fromPort && toPort) {
        if (toPort == fromPort) {
          stringRepresentation = `${stringRepresentation} PORT ${this.renderPort(fromPort)}`;
        } else {
          stringRepresentation = `${stringRepresentation} RANGE ${this.renderPort(fromPort)}-${this.renderPort(
            toPort,
          )}`;
        }
      } else {
        throw new Error("Port must be specified if protocol is not 'ALL'");
      }
    }
    const portProps: PortProps = {
      protocol: protocol,
      fromPort: fromPort,
      toPort: toPort,
      stringRepresentation: stringRepresentation,
    };
    return new Port(portProps);
  }