@Override
  public Iterable<Image> listImages() {
    List<Iterable<Image>> images = newArrayList();

    images.add(concat(api.images().list()));

    for (String project : imageProjects) {
      images.add(concat(api.images().listInProject(project)));
    }

    return Iterables.concat(images);
  }
 public void testListHardwareProfiles() throws Exception {
   GoogleComputeEngineApi api = client.getContext().unwrapApi(GoogleComputeEngineApi.class);
   ImmutableSet.Builder<String> deprecatedMachineTypes = ImmutableSet.builder();
   for (MachineType machine : api.machineTypesInZone(DEFAULT_ZONE_NAME).list().next()) {
     if (machine.deprecated() != null) {
       deprecatedMachineTypes.add(machine.id());
     }
   }
   ImmutableSet<String> deprecatedMachineTypeIds = deprecatedMachineTypes.build();
   for (Hardware hardwareProfile : client.listHardwareProfiles()) {
     assertFalse(contains(deprecatedMachineTypeIds, hardwareProfile.getId()));
   }
 }
 /** Unlike EC2, you cannot default GCE instances to a region. Hence, we constrain to zones. */
 @Override
 public Iterable<Location> listLocations() {
   Location provider = justProvider.get().iterator().next();
   ImmutableList.Builder<Location> zones = ImmutableList.builder();
   for (Region region : concat(api.regions().list())) {
     Location regionLocation =
         new LocationBuilder()
             .scope(LocationScope.REGION)
             .id(region.name())
             .description(region.selfLink().toString())
             .parent(provider)
             .build();
     for (URI zoneSelfLink : region.zones()) {
       String zoneName = toName(zoneSelfLink);
       zones.add(
           new LocationBuilder()
               .scope(LocationScope.ZONE)
               .id(zoneName)
               .description(zoneSelfLink.toString())
               .parent(regionLocation)
               .build());
     }
   }
   return zones.build();
 }
  protected void cleanUpNetworksAndFirewallsForGroup(String groupName) {
    String resourceName = namingConvention.create().sharedNameForGroup(groupName);
    AtomicReference<Operation> operation =
        new AtomicReference<Operation>(
            api.getFirewallApiForProject(project.get()).delete(resourceName));

    retry(
            operationDonePredicate,
            operationCompleteCheckTimeout,
            operationCompleteCheckInterval,
            MILLISECONDS)
        .apply(operation);

    if (operation.get().getHttpError().isPresent()) {
      HttpResponse response = operation.get().getHttpError().get();
      logger.warn(
          "delete orphaned firewall failed. Http Error Code: "
              + response.getStatusCode()
              + " HttpError: "
              + response.getMessage());
    }

    operation =
        new AtomicReference<Operation>(
            api.getNetworkApiForProject(project.get()).delete(resourceName));

    retry(
            operationDonePredicate,
            operationCompleteCheckTimeout,
            operationCompleteCheckInterval,
            MILLISECONDS)
        .apply(operation);

    if (operation.get().getHttpError().isPresent()) {
      HttpResponse response = operation.get().getHttpError().get();
      logger.warn(
          "delete orphaned network failed. Http Error Code: "
              + response.getStatusCode()
              + " HttpError: "
              + response.getMessage());
    }
  }
 @Override
 public Iterable<MachineType> listHardwareProfiles() {
   return filter(
       concat(api.aggregatedList().machineTypes()),
       new Predicate<MachineType>() {
         @Override
         public boolean apply(MachineType input) {
           return input.deprecated() == null;
         }
       });
 }
  public void testCreatePreemptibleNodeWithSsd() throws Exception {
    String group = this.group + "ssd";
    try {
      TemplateOptions options = client.templateOptions();

      options.as(GoogleComputeEngineTemplateOptions.class).bootDiskType("pd-ssd").preemptible(true);

      // create a node
      Set<? extends NodeMetadata> nodes = client.createNodesInGroup(group, 1, options);
      assertEquals(nodes.size(), 1, "One node should have been created");

      // Verify the disk on the instance is an ssd.
      NodeMetadata node = Iterables.get(nodes, 0);
      GoogleComputeEngineApi api = client.getContext().unwrapApi(GoogleComputeEngineApi.class);
      Instance instance = api.instancesInZone(node.getLocation().getId()).get(node.getName());
      Disk disk =
          api.disksInZone(node.getLocation().getId()).get(toName(instance.disks().get(0).source()));
      assertTrue(disk.type().toString().endsWith("pd-ssd"));
      assertTrue(instance.scheduling().preemptible());

    } finally {
      client.destroyNodesMatching(NodePredicates.inGroup(group));
    }
  }
 @Override
 public Iterable<Instance> listNodes() {
   return concat(api.aggregatedList().instances());
 }
 @Override
 public Image getImage(String selfLink) {
   return api.images().get(URI.create(checkNotNull(selfLink, "id")));
 }
  @Override
  public NodeAndInitialCredentials<Instance> createNodeWithGroupEncodedIntoName(
      String group, String name, Template template) {
    GoogleComputeEngineTemplateOptions options =
        GoogleComputeEngineTemplateOptions.class.cast(template.getOptions());

    checkNotNull(options.getNetworks(), "template options must specify a network");
    checkNotNull(template.getHardware().getUri(), "hardware must have a URI");
    checkNotNull(template.getImage().getUri(), "image URI is null");

    List<AttachDisk> disks = Lists.newArrayList();
    disks.add(AttachDisk.newBootDisk(template.getImage().getUri()));

    Iterator<String> networks = options.getNetworks().iterator();

    URI network = URI.create(networks.next());
    assert !networks.hasNext() : "Error: Options should specify only one network";

    // Add tags from template
    ArrayList<String> tags = new ArrayList<String>(options.getTags());

    // Add tags for firewalls
    FirewallTagNamingConvention naming = firewallTagNamingConvention.get(group);
    List<String> ports = simplifyPorts(options.getInboundPorts());
    if (ports != null) {
      tags.add(naming.name(ports));
    }

    NewInstance newInstance =
        new NewInstance.Builder(
                name,
                template.getHardware().getUri(), // machineType
                network,
                disks)
            .description(group)
            .tags(Tags.create(null, ImmutableList.copyOf(tags)))
            .serviceAccounts(options.serviceAccounts())
            .build();

    // Add metadata from template and for ssh key and image id
    newInstance.metadata().putAll(options.getUserMetadata());

    LoginCredentials credentials = resolveNodeCredentials(template);
    if (options.getPublicKey() != null) {
      newInstance
          .metadata()
          .put(
              "sshKeys",
              format(
                  "%s:%s %s@localhost",
                  credentials.getUser(), options.getPublicKey(), credentials.getUser()));
    }

    String zone = template.getLocation().getId();
    InstanceApi instanceApi = api.instancesInZone(zone);
    Operation create = instanceApi.create(newInstance);

    // We need to see the created instance so that we can access the newly created disk.
    AtomicReference<Instance> instance =
        Atomics.newReference(
            Instance.create( //
                "0000000000000000000", // id can't be null, but isn't available until provisioning
                                       // is done.
                null, // creationTimestamp
                create.targetLink(), // selfLink
                newInstance.name(), // name
                newInstance.description(), // description
                newInstance.tags(), // tags
                newInstance.machineType(), // machineType
                Instance.Status.PROVISIONING, // status
                null, // statusMessage
                create.zone(), // zone
                null, // canIpForward
                null, // networkInterfaces
                null, // disks
                newInstance.metadata(), // metadata
                newInstance.serviceAccounts(), // serviceAccounts
                Scheduling.create(OnHostMaintenance.MIGRATE, true) // scheduling
                ));
    checkState(instanceVisible.apply(instance), "instance %s is not api visible!", instance.get());

    // Add lookup for InstanceToNodeMetadata
    diskToSourceImage.put(instance.get().disks().get(0).source(), template.getImage().getUri());

    return new NodeAndInitialCredentials<Instance>(
        instance.get(), instance.get().selfLink().toString(), credentials);
  }