<commit_before>/*
 * 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;

import com.cloudera.director.spi.v1.compute.ComputeInstance;
import com.cloudera.director.spi.v1.compute.ComputeInstanceTemplate;
import com.cloudera.director.spi.v1.compute.ComputeProvider;
import com.cloudera.director.spi.v1.model.ConfigurationProperty;
import com.cloudera.director.spi.v1.model.InstanceState;
import com.cloudera.director.spi.v1.model.InstanceStatus;
import com.cloudera.director.spi.v1.model.util.SimpleConfiguration;
import com.cloudera.director.spi.v1.provider.CloudProvider;
import com.cloudera.director.spi.v1.provider.CloudProviderMetadata;
import com.cloudera.director.spi.v1.provider.Launcher;
import com.cloudera.director.spi.v1.provider.ResourceProviderMetadata;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

import static com.cloudera.director.google.GoogleCredentialsProviderConfigurationProperty.JSONKEY;
import static com.cloudera.director.google.GoogleCredentialsProviderConfigurationProperty.PROJECTID;
import static com.cloudera.director.spi.v1.compute.ComputeInstanceTemplate.ComputeInstanceTemplateConfigurationProperty.IMAGE;
import static com.cloudera.director.spi.v1.compute.ComputeInstanceTemplate.ComputeInstanceTemplateConfigurationProperty.TYPE;
import static com.cloudera.director.google.compute.GoogleComputeInstanceTemplateConfigurationProperty.NETWORK;
import static com.cloudera.director.google.compute.GoogleComputeProviderConfigurationProperty.ZONE;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;

public class GoogleTest {

  // These two values need to be customized prior to running the test.
  private static final String JSON_KEY_PATH = "";
  private static final String GCP_PROJECT_ID = "scratch-project";

  private static final int POLLING_INTERVAL_SECONDS = 5;

  private static String PROJECT_ID;
  private static String JSON_KEY;

  @BeforeClass
  public static void beforeClass() throws IOException {
    PROJECT_ID = GCP_PROJECT_ID;
    JSON_KEY = readFile(JSON_KEY_PATH, Charset.defaultCharset());
  }

  @Test
  public void testFullCycle() throws InterruptedException {

    // After a plugin is discovered and validated we get an instance of the Launcher.

    Launcher launcher = new GoogleLauncher();

    // We register all the available providers based on metadata.

    assertEquals(1, launcher.getCloudProviderMetadata().size());
    CloudProviderMetadata metadata = launcher.getCloudProviderMetadata().get(0);

    assertEquals(GoogleCloudProvider.ID, metadata.getId());

    // During environment configuration we ask the user for the following properties.

    System.out.println("Configurations required for credentials:");
    for (ConfigurationProperty property :
        metadata.getCredentialsProviderMetadata().getCredentialsConfigurationProperties()) {
      System.out.println(property);
    }

    System.out.println("Other provider level configurations:");
    for (ConfigurationProperty property :
        metadata.getProviderConfigurationProperties()) {
      System.out.println(property);
    }

    // In order to create a cloud provider we need to configure credentials
    // (we expect them to be eagerly validated on cloud provider creation).

    Map<String, String> environmentConfig = new HashMap<String, String>();
    environmentConfig.put(PROJECTID.getConfigKey(), PROJECT_ID);
    environmentConfig.put(JSONKEY.getConfigKey(), JSON_KEY);

    CloudProvider provider = launcher.createCloudProvider(
        GoogleCloudProvider.ID, new SimpleConfiguration(environmentConfig));

    assertNotNull(provider);

    // Get the provider for compute instances.

    ResourceProviderMetadata computeMetadata = metadata.getResourceProviderMetadata("compute");

    System.out.println("Configurations required for 'compute' resource provider:");
    for (ConfigurationProperty property :
        computeMetadata.getProviderConfigurationProperties()) {
      System.out.println(property);
    }

    // TODO(duftler): The zone should really be selected by the user from a list of valid choices.
    // Do we want to enhance ConfigurationProperty to support querying provider for a set of values to choose from?
    Map<String, String> computeConfig = new HashMap<String, String>();
    computeConfig.put(ZONE.getConfigKey(), "us-central1-a");

    ComputeProvider<ComputeInstance<ComputeInstanceTemplate>, ComputeInstanceTemplate> compute =
        (ComputeProvider<ComputeInstance<ComputeInstanceTemplate>, ComputeInstanceTemplate>)
            provider.createResourceProvider("compute", new SimpleConfiguration(computeConfig));

    // Prepare a resource template.

    System.out.println("Configurations required for template:");
    for (ConfigurationProperty property :
        computeMetadata.getResourceTemplateConfigurationProperties()) {
      System.out.println(property);
    }

    Map<String, String> templateConfig = new HashMap<String, String>();
    templateConfig.put(IMAGE.getConfigKey(), "ubuntu");
    templateConfig.put(TYPE.getConfigKey(), "f1-micro");
    templateConfig.put(NETWORK.getConfigKey(), "default");

    ComputeInstanceTemplate template = (ComputeInstanceTemplate)
        compute.createResourceTemplate("template-1", new SimpleConfiguration(templateConfig),
            new HashMap<String, String>());

    assertNotNull(template);

    // Use the template to create one resource.

    System.out.println("About to provision an instance...");

    List<String> instanceIds = Arrays.asList(UUID.randomUUID().toString());
    Collection<ComputeInstance<ComputeInstanceTemplate>> instances =
        compute.allocate(template, instanceIds, 1);
    assertEquals(1, instances.size());

    ComputeInstance instance = instances.iterator().next();
    assertEquals(instanceIds.get(0), instance.getId());

    // Run a find by ID.

    System.out.println("About to lookup an instance...");

    Collection<ComputeInstance<ComputeInstanceTemplate>> found = compute.find(template, instanceIds);

    // Loop until found.

    while (found.size() == 0) {
      Thread.sleep(POLLING_INTERVAL_SECONDS * 1000);

      System.out.println("Polling...");<commit_msg>Rename GoogleComputeInstanceTemplateConfigurationProperty configKey network to networkName in GoogleTest reference. Fix import order.<commit_after>/*
 * 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;

import com.cloudera.director.spi.v1.compute.ComputeInstance;
import com.cloudera.director.spi.v1.compute.ComputeInstanceTemplate;
import com.cloudera.director.spi.v1.compute.ComputeProvider;
import com.cloudera.director.spi.v1.model.ConfigurationProperty;
import com.cloudera.director.spi.v1.model.InstanceState;
import com.cloudera.director.spi.v1.model.InstanceStatus;
import com.cloudera.director.spi.v1.model.util.SimpleConfiguration;
import com.cloudera.director.spi.v1.provider.CloudProvider;
import com.cloudera.director.spi.v1.provider.CloudProviderMetadata;
import com.cloudera.director.spi.v1.provider.Launcher;
import com.cloudera.director.spi.v1.provider.ResourceProviderMetadata;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

import static com.cloudera.director.google.GoogleCredentialsProviderConfigurationProperty.JSONKEY;
import static com.cloudera.director.google.GoogleCredentialsProviderConfigurationProperty.PROJECTID;
import static com.cloudera.director.google.compute.GoogleComputeInstanceTemplateConfigurationProperty.NETWORKNAME;
import static com.cloudera.director.google.compute.GoogleComputeProviderConfigurationProperty.ZONE;
import static com.cloudera.director.spi.v1.compute.ComputeInstanceTemplate.ComputeInstanceTemplateConfigurationProperty.IMAGE;
import static com.cloudera.director.spi.v1.compute.ComputeInstanceTemplate.ComputeInstanceTemplateConfigurationProperty.TYPE;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;

public class GoogleTest {

  // These two values need to be customized prior to running the test.
  private static final String JSON_KEY_PATH = "";
  private static final String GCP_PROJECT_ID = "scratch-project";

  private static final int POLLING_INTERVAL_SECONDS = 5;

  private static String PROJECT_ID;
  private static String JSON_KEY;

  @BeforeClass
  public static void beforeClass() throws IOException {
    PROJECT_ID = GCP_PROJECT_ID;
    JSON_KEY = readFile(JSON_KEY_PATH, Charset.defaultCharset());
  }

  @Test
  public void testFullCycle() throws InterruptedException {

    // After a plugin is discovered and validated we get an instance of the Launcher.

    Launcher launcher = new GoogleLauncher();

    // We register all the available providers based on metadata.

    assertEquals(1, launcher.getCloudProviderMetadata().size());
    CloudProviderMetadata metadata = launcher.getCloudProviderMetadata().get(0);

    assertEquals(GoogleCloudProvider.ID, metadata.getId());

    // During environment configuration we ask the user for the following properties.

    System.out.println("Configurations required for credentials:");
    for (ConfigurationProperty property :
        metadata.getCredentialsProviderMetadata().getCredentialsConfigurationProperties()) {
      System.out.println(property);
    }

    System.out.println("Other provider level configurations:");
    for (ConfigurationProperty property :
        metadata.getProviderConfigurationProperties()) {
      System.out.println(property);
    }

    // In order to create a cloud provider we need to configure credentials
    // (we expect them to be eagerly validated on cloud provider creation).

    Map<String, String> environmentConfig = new HashMap<String, String>();
    environmentConfig.put(PROJECTID.getConfigKey(), PROJECT_ID);
    environmentConfig.put(JSONKEY.getConfigKey(), JSON_KEY);

    CloudProvider provider = launcher.createCloudProvider(
        GoogleCloudProvider.ID, new SimpleConfiguration(environmentConfig));

    assertNotNull(provider);

    // Get the provider for compute instances.

    ResourceProviderMetadata computeMetadata = metadata.getResourceProviderMetadata("compute");

    System.out.println("Configurations required for 'compute' resource provider:");
    for (ConfigurationProperty property :
        computeMetadata.getProviderConfigurationProperties()) {
      System.out.println(property);
    }

    // TODO(duftler): The zone should really be selected by the user from a list of valid choices.
    // Do we want to enhance ConfigurationProperty to support querying provider for a set of values to choose from?
    Map<String, String> computeConfig = new HashMap<String, String>();
    computeConfig.put(ZONE.getConfigKey(), "us-central1-a");

    ComputeProvider<ComputeInstance<ComputeInstanceTemplate>, ComputeInstanceTemplate> compute =
        (ComputeProvider<ComputeInstance<ComputeInstanceTemplate>, ComputeInstanceTemplate>)
            provider.createResourceProvider("compute", new SimpleConfiguration(computeConfig));

    // Prepare a resource template.

    System.out.println("Configurations required for template:");
    for (ConfigurationProperty property :
        computeMetadata.getResourceTemplateConfigurationProperties()) {
      System.out.println(property);
    }

    Map<String, String> templateConfig = new HashMap<String, String>();
    templateConfig.put(IMAGE.getConfigKey(), "ubuntu");
    templateConfig.put(TYPE.getConfigKey(), "f1-micro");
    templateConfig.put(NETWORKNAME.getConfigKey(), "default");

    ComputeInstanceTemplate template = (ComputeInstanceTemplate)
        compute.createResourceTemplate("template-1", new SimpleConfiguration(templateConfig),
            new HashMap<String, String>());

    assertNotNull(template);

    // Use the template to create one resource.

    System.out.println("About to provision an instance...");

    List<String> instanceIds = Arrays.asList(UUID.randomUUID().toString());
    Collection<ComputeInstance<ComputeInstanceTemplate>> instances =
        compute.allocate(template, instanceIds, 1);
    assertEquals(1, instances.size());

    ComputeInstance instance = instances.iterator().next();
    assertEquals(instanceIds.get(0), instance.getId());

    // Run a find by ID.

    System.out.println("About to lookup an instance...");

    Collection<ComputeInstance<ComputeInstanceTemplate>> found = compute.find(template, instanceIds);

    // Loop until found.

    while (found.size() == 0) {
      Thread.sleep(POLLING_INTERVAL_SECONDS * 1000);

      System.out.println("Polling...");
{'fraction_non_alphanumeric': 0.09395109395109395, 'fraction_numerical': 0.005005005005005005, 'mean_word_length': 4.774979355904211, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3312858', 'n_tokens_mistral': 3692, 'n_tokens_neox': 3578, 'n_words': 934}