def getHostKeyEntry()

in src/main/scala/com/gu/ssm/Logic.scala [107:127]


  def getHostKeyEntry(ssmResult: Either[CommandStatus, CommandResult], preferredAlgs: List[String]): Either[FailedAttempt, String] = {
    ssmResult match {
      case Right(result) =>
        val resultLines = result.stdOut.linesIterator
        val preferredKeys = resultLines.filter(hostKey => preferredAlgs.exists(hostKey.startsWith))
        val preferenceOrderedKeys: Seq[String] = preferredKeys.toList.sortBy(
          hostKey => preferredAlgs.indexWhere(hostKey.startsWith)
        )

        preferenceOrderedKeys.headOption match {
          case Some(hostKey) => Right(hostKey)
          case None => Left(Failure(
            "host key with preferred algorithm not found",
            s"The remote instance did not return a host key with any preferred algorithm (preferred: $preferredAlgs)",
            NoHostKey,
            s"The result lines returned from the host:\n${resultLines.mkString("\n")}"
          ).attempt)
        }
      case Left(otherStatus) => Left(Failure("host keys not returned", s"The remote instance failed to return the host keys within the timeout window (status: $otherStatus)", AwsError).attempt)
    }
  }