// Path: provider/src/main/java/com/cloudera/director/google/util/Names.java
// public class Names {
// 
//   public static String buildApplicationNameVersionTag(Config applicationProperties) {
//     return applicationProperties.getString("application.name") + "/" +
//         applicationProperties.getString("application.version");
//   }
// 
//   public static String convertCloudSQLRegionToComputeRegion(String cloudSQLRegion, Config googleConfig) {
//     return googleConfig.getString(Configurations.CLOUD_SQL_REGIONS_ALIASES_SECTION + cloudSQLRegion);
//   }
// }


import java.util.Collections;
import com.cloudera.director.google.util.Names;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.compute.Compute;
import com.google.api.services.compute.ComputeScopes;
import com.google.api.services.sqladmin.SQLAdmin;
import com.google.api.services.sqladmin.SQLAdminScopes;
import com.typesafe.config.Config;
import java.io.ByteArrayInputStream;
import java.util.Collection;

  public GoogleCredentials(Config applicationProperties, String projectId, String jsonKey) {
    this.applicationProperties = applicationProperties;
    this.projectId = projectId;
    this.jsonKey = jsonKey;
    this.compute = buildCompute();
    this.sqlAdmin = buildSQLAdmin();
  }

  private Compute buildCompute() {
    try {
      JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
      HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
      GoogleCredential credential;

      if (jsonKey != null) {
        credential = GoogleCredential.fromStream(
            new ByteArrayInputStream(jsonKey.getBytes()), httpTransport, JSON_FACTORY)
            .createScoped(Collections.singleton(ComputeScopes.COMPUTE));
      } else {
        Collection COMPUTE_SCOPES = Collections.singletonList(ComputeScopes.COMPUTE);

        credential = GoogleCredential
            .getApplicationDefault(httpTransport, JSON_FACTORY)
            .createScoped(COMPUTE_SCOPES);
      }

      return new Compute.Builder(httpTransport,
          JSON_FACTORY,
          null)
// Path: provider/src/main/java/com/cloudera/director/google/util/Names.java
// public class Names {
// 
//   public static String buildApplicationNameVersionTag(Config applicationProperties) {
//     return applicationProperties.getString("application.name") + "/" +
//         applicationProperties.getString("application.version");
//   }
// 
//   public static String convertCloudSQLRegionToComputeRegion(String cloudSQLRegion, Config googleConfig) {
//     return googleConfig.getString(Configurations.CLOUD_SQL_REGIONS_ALIASES_SECTION + cloudSQLRegion);
//   }
// }


// Path: provider/src/main/java/com/cloudera/director/google/internal/GoogleCredentials.java
import java.util.Collections;
import com.cloudera.director.google.util.Names;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.compute.Compute;
import com.google.api.services.compute.ComputeScopes;
import com.google.api.services.sqladmin.SQLAdmin;
import com.google.api.services.sqladmin.SQLAdminScopes;
import com.typesafe.config.Config;
import java.io.ByteArrayInputStream;
import java.util.Collection;


  public GoogleCredentials(Config applicationProperties, String projectId, String jsonKey) {
    this.applicationProperties = applicationProperties;
    this.projectId = projectId;
    this.jsonKey = jsonKey;
    this.compute = buildCompute();
    this.sqlAdmin = buildSQLAdmin();
  }

  private Compute buildCompute() {
    try {
      JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
      HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
      GoogleCredential credential;

      if (jsonKey != null) {
        credential = GoogleCredential.fromStream(
            new ByteArrayInputStream(jsonKey.getBytes()), httpTransport, JSON_FACTORY)
            .createScoped(Collections.singleton(ComputeScopes.COMPUTE));
      } else {
        Collection COMPUTE_SCOPES = Collections.singletonList(ComputeScopes.COMPUTE);

        credential = GoogleCredential
            .getApplicationDefault(httpTransport, JSON_FACTORY)
            .createScoped(COMPUTE_SCOPES);
      }

      return new Compute.Builder(httpTransport,
          JSON_FACTORY,
          null)
          .setApplicationName(Names.buildApplicationNameVersionTag(applicationProperties))