void setCredentialFactoryClass()

in sdk/src/main/java/com/google/cloud/dataflow/sdk/options/GcpOptions.java [165:244]


  void setCredentialFactoryClass(
      Class<? extends CredentialFactory> credentialFactoryClass);

  /**
   * The credential instance that should be used to authenticate against GCP services.
   * If no credential has been set explicitly, the default is to use the instance factory
   * that constructs a credential based upon the currently set credentialFactoryClass.
   */
  @JsonIgnore
  @Description("The credential instance that should be used to authenticate against GCP services. "
      + "If no credential has been set explicitly, the default is to use the instance factory "
      + "that constructs a credential based upon the currently set credentialFactoryClass.")
  @Default.InstanceFactory(GcpUserCredentialsFactory.class)
  @Hidden
  Credential getGcpCredential();
  void setGcpCredential(Credential value);

  /**
   * Attempts to get infer the default project based upon the environment this application
   * is executing within. Currently this only supports getting the default project from gCloud.
   */
  public static class DefaultProjectFactory implements DefaultValueFactory<String> {
    private static final Logger LOG = LoggerFactory.getLogger(DefaultProjectFactory.class);

    @Override
    public String create(PipelineOptions options) {
      try {
        File configDir;
        if (getEnvironment().containsKey("CLOUDSDK_CONFIG")) {
          configDir = new File(getEnvironment().get("CLOUDSDK_CONFIG"));
        } else if (isWindows() && getEnvironment().containsKey("APPDATA")) {
          configDir = new File(getEnvironment().get("APPDATA"), "gcloud");
        } else {
          configDir = new File(System.getProperty("user.home"), ".config/gcloud");
        }
        String section = null;
        Pattern projectPattern = Pattern.compile("^project\\s*=\\s*(.*)$");
        Pattern sectionPattern = Pattern.compile("^\\[(.*)\\]$");
        for (String line : Files.readLines(
            new File(configDir, "properties"), StandardCharsets.UTF_8)) {
          line = line.trim();
          if (line.isEmpty() || line.startsWith(";")) {
            continue;
          }
          Matcher matcher = sectionPattern.matcher(line);
          if (matcher.matches()) {
            section = matcher.group(1);
          } else if (section == null || section.equals("core")) {
            matcher = projectPattern.matcher(line);
            if (matcher.matches()) {
              String project = matcher.group(1).trim();
              LOG.info("Inferred default GCP project '{}' from gCloud. If this is the incorrect "
                  + "project, please cancel this Pipeline and specify the command-line "
                  + "argument --project.", project);
              return project;
            }
          }
        }
      } catch (IOException expected) {
        LOG.debug("Failed to find default project.", expected);
      }
      // return null if can't determine
      return null;
    }

    /**
     * Returns true if running on the Windows OS.
     */
    private static boolean isWindows() {
      return System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows");
    }

    /**
     * Used to mock out getting environment variables.
     */
    @VisibleForTesting
    Map<String, String> getEnvironment() {
        return System.getenv();
    }
  }