// Path: provider/src/main/java/com/cloudera/director/google/internal/GoogleCredentials.java
// public class GoogleCredentials {
// 
//   private final Config applicationProperties;
//   private final String projectId;
//   private final String jsonKey;
//   private final Compute compute;
//   private final SQLAdmin sqlAdmin;
// 
//   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))
//           .setHttpRequestInitializer(credential)
//           .build();
// 
//     } catch (Exception e) {
//       throw new RuntimeException(e);
//     }
//   }
// 
//   private SQLAdmin buildSQLAdmin() {
//     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(SQLAdminScopes.SQLSERVICE_ADMIN));
//       } else {
//         Collection SQLSERVICE_ADMIN_SCOPES = Collections.singletonList(SQLAdminScopes.SQLSERVICE_ADMIN);
// 
//         credential = GoogleCredential
//             .getApplicationDefault(httpTransport, JSON_FACTORY)
//             .createScoped(SQLSERVICE_ADMIN_SCOPES);
//       }
// 
//       return new SQLAdmin.Builder(httpTransport,
//           JSON_FACTORY,
//           null)
//           .setApplicationName(Names.buildApplicationNameVersionTag(applicationProperties))
//           .setHttpRequestInitializer(credential)
//           .build();
// 
//     } catch (Exception e) {
//       throw new RuntimeException(e);
//     }
//   }
// 
//   public String getProjectId() {
//     return projectId;
//   }
// 
//   public String getJsonKey() {
//     return jsonKey;
//   }
// 
//   public Compute getCompute() {
//     return compute;
//   }
// 
//   public SQLAdmin getSQLAdmin() {
//     return sqlAdmin;
//   }
// 
//   public boolean match(String projectId, String jsonKey) {
//     return projectId.equals(this.projectId) &&
//         jsonKey.equals(this.jsonKey);
//   }
// }


import java.io.IOException;
import java.util.List;
import static com.cloudera.director.google.sql.GoogleCloudSQLProviderConfigurationProperty.REGION;
import static com.cloudera.director.spi.v1.model.util.Validations.addError;
import com.cloudera.director.google.internal.GoogleCredentials;
import com.cloudera.director.spi.v1.model.ConfigurationValidator;
import com.cloudera.director.spi.v1.model.Configured;
import com.cloudera.director.spi.v1.model.LocalizationContext;
import com.cloudera.director.spi.v1.model.exception.PluginExceptionConditionAccumulator;
import com.cloudera.director.spi.v1.model.exception.TransientProviderException;
import com.google.api.services.sqladmin.SQLAdmin;
import com.google.api.services.sqladmin.model.Tier;
import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
 * Copyright (c) 2015 Google, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cloudera.director.google.sql;




/**
 * Validates Google Cloud SQL provider configuration.
 */
public class GoogleCloudSQLProviderConfigurationValidator implements ConfigurationValidator {

  private static final Logger LOG =
      LoggerFactory.getLogger(GoogleCloudSQLProviderConfigurationValidator.class);

  @VisibleForTesting
  static final String REGION_NOT_FOUND_MSG = "Region '%s' not found for project '%s'.";

  private GoogleCredentials credentials;

  /**
   * Creates a Google Cloud SQL provider configuration validator with the specified parameters.
   */
  public GoogleCloudSQLProviderConfigurationValidator(GoogleCredentials credentials) {
    this.credentials = credentials;
  }

  @Override
  public void validate(String name, Configured configuration,
      PluginExceptionConditionAccumulator accumulator, LocalizationContext localizationContext) {

    checkRegion(configuration, accumulator, localizationContext);
  }

  /**
   * Validates the configured region.
   *
   * @param configuration       the configuration to be validated
   * @param accumulator         the exception condition accumulator
   * @param localizationContext the localization context
   */
  void checkRegion(Configured configuration,
      PluginExceptionConditionAccumulator accumulator,
      LocalizationContext localizationContext) {
    String regionName = configuration.getConfigurationValue(REGION, localizationContext);

    LOG.info(">> Querying region '{}'", regionName);

    SQLAdmin sqlAdmin = credentials.getSQLAdmin();
    String projectId = credentials.getProjectId();

// Path: provider/src/main/java/com/cloudera/director/google/internal/GoogleCredentials.java
// public class GoogleCredentials {
// 
//   private final Config applicationProperties;
//   private final String projectId;
//   private final String jsonKey;
//   private final Compute compute;
//   private final SQLAdmin sqlAdmin;
// 
//   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))
//           .setHttpRequestInitializer(credential)
//           .build();
// 
//     } catch (Exception e) {
//       throw new RuntimeException(e);
//     }
//   }
// 
//   private SQLAdmin buildSQLAdmin() {
//     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(SQLAdminScopes.SQLSERVICE_ADMIN));
//       } else {
//         Collection SQLSERVICE_ADMIN_SCOPES = Collections.singletonList(SQLAdminScopes.SQLSERVICE_ADMIN);
// 
//         credential = GoogleCredential
//             .getApplicationDefault(httpTransport, JSON_FACTORY)
//             .createScoped(SQLSERVICE_ADMIN_SCOPES);
//       }
// 
//       return new SQLAdmin.Builder(httpTransport,
//           JSON_FACTORY,
//           null)
//           .setApplicationName(Names.buildApplicationNameVersionTag(applicationProperties))
//           .setHttpRequestInitializer(credential)
//           .build();
// 
//     } catch (Exception e) {
//       throw new RuntimeException(e);
//     }
//   }
// 
//   public String getProjectId() {
//     return projectId;
//   }
// 
//   public String getJsonKey() {
//     return jsonKey;
//   }
// 
//   public Compute getCompute() {
//     return compute;
//   }
// 
//   public SQLAdmin getSQLAdmin() {
//     return sqlAdmin;
//   }
// 
//   public boolean match(String projectId, String jsonKey) {
//     return projectId.equals(this.projectId) &&
//         jsonKey.equals(this.jsonKey);
//   }
// }


// Path: provider/src/main/java/com/cloudera/director/google/sql/GoogleCloudSQLProviderConfigurationValidator.java
import java.io.IOException;
import java.util.List;
import static com.cloudera.director.google.sql.GoogleCloudSQLProviderConfigurationProperty.REGION;
import static com.cloudera.director.spi.v1.model.util.Validations.addError;
import com.cloudera.director.google.internal.GoogleCredentials;
import com.cloudera.director.spi.v1.model.ConfigurationValidator;
import com.cloudera.director.spi.v1.model.Configured;
import com.cloudera.director.spi.v1.model.LocalizationContext;
import com.cloudera.director.spi.v1.model.exception.PluginExceptionConditionAccumulator;
import com.cloudera.director.spi.v1.model.exception.TransientProviderException;
import com.google.api.services.sqladmin.SQLAdmin;
import com.google.api.services.sqladmin.model.Tier;
import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * Copyright (c) 2015 Google, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cloudera.director.google.sql;




/**
 * Validates Google Cloud SQL provider configuration.
 */
public class GoogleCloudSQLProviderConfigurationValidator implements ConfigurationValidator {

  private static final Logger LOG =
      LoggerFactory.getLogger(GoogleCloudSQLProviderConfigurationValidator.class);

  @VisibleForTesting
  static final String REGION_NOT_FOUND_MSG = "Region '%s' not found for project '%s'.";

  private GoogleCredentials credentials;

  /**
   * Creates a Google Cloud SQL provider configuration validator with the specified parameters.
   */
  public GoogleCloudSQLProviderConfigurationValidator(GoogleCredentials credentials) {
    this.credentials = credentials;
  }

  @Override
  public void validate(String name, Configured configuration,
      PluginExceptionConditionAccumulator accumulator, LocalizationContext localizationContext) {

    checkRegion(configuration, accumulator, localizationContext);
  }

  /**
   * Validates the configured region.
   *
   * @param configuration       the configuration to be validated
   * @param accumulator         the exception condition accumulator
   * @param localizationContext the localization context
   */
  void checkRegion(Configured configuration,
      PluginExceptionConditionAccumulator accumulator,
      LocalizationContext localizationContext) {
    String regionName = configuration.getConfigurationValue(REGION, localizationContext);

    LOG.info(">> Querying region '{}'", regionName);

    SQLAdmin sqlAdmin = credentials.getSQLAdmin();
    String projectId = credentials.getProjectId();

    try {