// Path: provider/src/main/java/com/cloudera/director/google/util/Urls.java
// public final class Urls {
// 
//   @VisibleForTesting
//   static final String MALFORMED_RESOURCE_URL_MSG = "Malformed resource url '%s'.";
// 
//   private Urls() {}
// 
//   public static String getLocalName(String fullResourceUrl) {
//     if (fullResourceUrl == null || fullResourceUrl.isEmpty()) {
//       return null;
//     }
// 
//     GenericUrl url = new GenericUrl(fullResourceUrl);
//     return Iterables.getLast(url.getPathParts());
//   }
// 
//   public static String getProject(String fullResourceUrl) {
//     if (fullResourceUrl == null || fullResourceUrl.isEmpty()) {
//       return null;
//     }
// 
//     List<String> pathParts = new GenericUrl(fullResourceUrl).getPathParts();
// 
//     if (pathParts == null) {
//       throw new IllegalArgumentException(String.format(MALFORMED_RESOURCE_URL_MSG, fullResourceUrl));
//     }
// 
//     String[] urlParts = Iterables.toArray(pathParts, String.class);
// 
//     // Resource urls look like so: https://www.googleapis.com/compute/v1/projects/rhel-cloud/global/images/rhel-6-v20150526
//     // The path parts begin after the host and include a leading "" path part to force the leading slash.
//     if (urlParts.length < 8) {
//       throw new IllegalArgumentException(String.format(MALFORMED_RESOURCE_URL_MSG, fullResourceUrl));
//     } else {
//       return urlParts[urlParts.length - 4];
//     }
//   }
// 
//   public static String buildGenericApisUrl(String domainName, String version, String projectId, String... resourcePathParts) {
//     GenericUrl genericUrl = new GenericUrl();
//     genericUrl.setScheme("https");
//     genericUrl.setHost("www.googleapis.com");
// 
//     List<String> pathParts = Lists.newArrayList("", domainName, version);
//     pathParts.add("projects");
//     pathParts.add(projectId);
// 
//     if (resourcePathParts != null) {
//       pathParts.addAll(Lists.newArrayList(resourcePathParts));
//     }
// 
//     genericUrl.setPathParts(pathParts);
// 
//     return genericUrl.build();
//   }
// }


import com.cloudera.director.google.util.Dates;
import com.cloudera.director.google.util.Urls;
import com.cloudera.director.spi.v1.compute.util.AbstractComputeInstance;
import com.cloudera.director.spi.v1.model.DisplayProperty;
import com.cloudera.director.spi.v1.model.DisplayPropertyToken;
import com.cloudera.director.spi.v1.model.util.SimpleDisplayPropertyBuilder;
import com.cloudera.director.spi.v1.util.DisplayPropertiesUtil;
import com.google.api.services.compute.model.AccessConfig;
import com.google.api.services.compute.model.Disk;
import com.google.api.services.compute.model.Instance;
import com.google.api.services.compute.model.NetworkInterface;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;
import java.util.Map;
/*
 * 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.compute;



/**
 * Google Compute instance.
 */
public class GoogleComputeInstance
    extends AbstractComputeInstance<GoogleComputeInstanceTemplate, Instance> {

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

  /**
   * The list of display properties (including inherited properties).
   */
  private static final List<DisplayProperty> DISPLAY_PROPERTIES =
      DisplayPropertiesUtil.asDisplayPropertyList(GoogleComputeInstanceDisplayPropertyToken.values());

  /**
   * Returns the list of display properties for a Google instance, including inherited properties.
   *
   * @return the list of display properties for a Google instance, including inherited properties
   */
  public static List<DisplayProperty> getDisplayProperties() {
    return DISPLAY_PROPERTIES;
  }

  /**
   * Google compute instance display properties.
   */
  public enum GoogleComputeInstanceDisplayPropertyToken implements DisplayPropertyToken {

    IMAGE_ID(new SimpleDisplayPropertyBuilder()
        .displayKey("imageId")
        .name("Image ID")
        .defaultDescription("The ID of the image used to launch the instance.")
        .sensitive(false)
        .build()) {
      @Override
      protected String getPropertyValue(Instance instance, Disk bootDisk) {
// Path: provider/src/main/java/com/cloudera/director/google/util/Urls.java
// public final class Urls {
// 
//   @VisibleForTesting
//   static final String MALFORMED_RESOURCE_URL_MSG = "Malformed resource url '%s'.";
// 
//   private Urls() {}
// 
//   public static String getLocalName(String fullResourceUrl) {
//     if (fullResourceUrl == null || fullResourceUrl.isEmpty()) {
//       return null;
//     }
// 
//     GenericUrl url = new GenericUrl(fullResourceUrl);
//     return Iterables.getLast(url.getPathParts());
//   }
// 
//   public static String getProject(String fullResourceUrl) {
//     if (fullResourceUrl == null || fullResourceUrl.isEmpty()) {
//       return null;
//     }
// 
//     List<String> pathParts = new GenericUrl(fullResourceUrl).getPathParts();
// 
//     if (pathParts == null) {
//       throw new IllegalArgumentException(String.format(MALFORMED_RESOURCE_URL_MSG, fullResourceUrl));
//     }
// 
//     String[] urlParts = Iterables.toArray(pathParts, String.class);
// 
//     // Resource urls look like so: https://www.googleapis.com/compute/v1/projects/rhel-cloud/global/images/rhel-6-v20150526
//     // The path parts begin after the host and include a leading "" path part to force the leading slash.
//     if (urlParts.length < 8) {
//       throw new IllegalArgumentException(String.format(MALFORMED_RESOURCE_URL_MSG, fullResourceUrl));
//     } else {
//       return urlParts[urlParts.length - 4];
//     }
//   }
// 
//   public static String buildGenericApisUrl(String domainName, String version, String projectId, String... resourcePathParts) {
//     GenericUrl genericUrl = new GenericUrl();
//     genericUrl.setScheme("https");
//     genericUrl.setHost("www.googleapis.com");
// 
//     List<String> pathParts = Lists.newArrayList("", domainName, version);
//     pathParts.add("projects");
//     pathParts.add(projectId);
// 
//     if (resourcePathParts != null) {
//       pathParts.addAll(Lists.newArrayList(resourcePathParts));
//     }
// 
//     genericUrl.setPathParts(pathParts);
// 
//     return genericUrl.build();
//   }
// }


// Path: provider/src/main/java/com/cloudera/director/google/compute/GoogleComputeInstance.java
import com.cloudera.director.google.util.Dates;
import com.cloudera.director.google.util.Urls;
import com.cloudera.director.spi.v1.compute.util.AbstractComputeInstance;
import com.cloudera.director.spi.v1.model.DisplayProperty;
import com.cloudera.director.spi.v1.model.DisplayPropertyToken;
import com.cloudera.director.spi.v1.model.util.SimpleDisplayPropertyBuilder;
import com.cloudera.director.spi.v1.util.DisplayPropertiesUtil;
import com.google.api.services.compute.model.AccessConfig;
import com.google.api.services.compute.model.Disk;
import com.google.api.services.compute.model.Instance;
import com.google.api.services.compute.model.NetworkInterface;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/*
 * 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.compute;



/**
 * Google Compute instance.
 */
public class GoogleComputeInstance
    extends AbstractComputeInstance<GoogleComputeInstanceTemplate, Instance> {

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

  /**
   * The list of display properties (including inherited properties).
   */
  private static final List<DisplayProperty> DISPLAY_PROPERTIES =
      DisplayPropertiesUtil.asDisplayPropertyList(GoogleComputeInstanceDisplayPropertyToken.values());

  /**
   * Returns the list of display properties for a Google instance, including inherited properties.
   *
   * @return the list of display properties for a Google instance, including inherited properties
   */
  public static List<DisplayProperty> getDisplayProperties() {
    return DISPLAY_PROPERTIES;
  }

  /**
   * Google compute instance display properties.
   */
  public enum GoogleComputeInstanceDisplayPropertyToken implements DisplayPropertyToken {

    IMAGE_ID(new SimpleDisplayPropertyBuilder()
        .displayKey("imageId")
        .name("Image ID")
        .defaultDescription("The ID of the image used to launch the instance.")
        .sensitive(false)
        .build()) {
      @Override
      protected String getPropertyValue(Instance instance, Disk bootDisk) {
        return Urls.getLocalName(bootDisk.getSourceImage());