String getRMWebAddress() throws IOException {
    Configuration conf = getConfig();
    String scheme = "http://";
    String path = "/app/v1/services/version";
    String rmAddress = conf
        .get("yarn.resourcemanager.webapp.address");
    if (YarnConfiguration.useHttps(conf)) {
      scheme = "https://";
      rmAddress = conf
          .get("yarn.resourcemanager.webapp.https.address");
    }

    if (HAUtil.isHAEnabled(conf)) {
      boolean useKerberos = UserGroupInformation.isSecurityEnabled();
      List<String> rmServers = getRMHAWebAddresses(conf);
      StringBuilder diagnosticsMsg = new StringBuilder();
      for (String host : rmServers) {
        try {
          Client client = Client.create();
          client.setFollowRedirects(false);
          StringBuilder sb = new StringBuilder();
          sb.append(scheme)
              .append(host)
              .append(path);
          if (!useKerberos) {
            try {
              String username = UserGroupInformation.getCurrentUser()
                  .getShortUserName();
              sb.append("?user.name=")
                  .append(username);
            } catch (IOException e) {
              LOG.debug("Fail to resolve username: {}", e);
            }
          }
          Builder builder = client
              .resource(sb.toString()).type(MediaType.APPLICATION_JSON);
          if (useKerberos) {
            String[] server = host.split(":");
            String challenge = YarnClientUtils.generateToken(server[0]);
            builder.header(HttpHeaders.AUTHORIZATION, "Negotiate " +
                challenge);
            LOG.debug("Authorization: Negotiate {}", challenge);
          }
          ClientResponse test = builder.get(ClientResponse.class);
          if (test.getStatus() == 200) {
            return scheme + host;
          }
        } catch (Exception e) {
          LOG.info("Fail to connect to: " + host);
          LOG.debug("Root cause: ", e);
          diagnosticsMsg.append("Error connecting to " + host
              + " due to " + e.getMessage() + "\n");
        }
      }
      throw new IOException(diagnosticsMsg.toString());
    }
    return scheme+rmAddress;
  }
@Test
  void testGetRMWebAddress() throws Exception {
    Configuration conf = new Configuration();
    conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, true);
    conf.set(YarnConfiguration.RM_HA_IDS, "rm1");
    conf.set(YarnConfiguration.RM_HA_ID, "rm1");
    conf.set("yarn.resourcemanager.webapp.address.rm1", "localhost:0");
    ApiServiceClient asc1 = new ApiServiceClient(conf);
    boolean exceptionCaught = false;
    String diagnosticsMsg = null;
    try {
      String rmWebAddress = asc1.getRMWebAddress();
    } catch (IOException e) {
      exceptionCaught = true;
      diagnosticsMsg = e.getMessage();
    }
    assertTrue(exceptionCaught, "ApiServiceClient failed to throw exception");
    assertTrue(diagnosticsMsg.contains("Error connecting to localhost:0"),
        "Exception Message does not match");
  }