Java源码示例:org.jclouds.googlecomputeengine.features.InstanceApi

示例1
@Override
public void detachVolume(String instanceId, String volumeId) {
    InstanceApi instApi = getGCEInstanceApi();
    String zone = getZone();
    Instance inst = instApi.get(instanceId);
    log.info("Trying to detach volume: " + volumeId + " from instance: " + instanceId + " in zone: " + zone);
    if (inst == null) {
        log.error("Failed to find instance: " + instanceId + " in zone: " + zone);
        return;
    }
    for (Instance.AttachedDisk disk : inst.disks()) {
        if (disk.deviceName().equals(volumeId)) {
            log.info("Found disk to be detached. Source: " + disk.source() + " devicename: " + disk.deviceName());
            Operation oper = instApi.detachDisk(instanceId, disk.deviceName());
            oper = waitGCEOperationDone(oper);
            if (!oper.status().equals(Operation.Status.DONE)) {
                log.error("Failed to detach volume: " + volumeId + " to instance: " + instanceId +
                        " in zone: " + zone + " at device: " + disk.deviceName() + " result operation: " + oper);
            }
            return;
        }
    }
    log.error("Cannot find volume: " + volumeId + " in instance: " + instanceId);
}
 
示例2
@Override
public String attachVolume(String instanceId, String volumeId, String deviceName) {
    DiskApi diskApi = getGCEDiskApi();
    InstanceApi instApi = getGCEInstanceApi();
    String zone = getZone();

    log.info("Trying to attach volume: " + volumeId + " to instance: " + instanceId +
            " in zone: " + zone + " at devicename: " + deviceName);

    Disk disk = diskApi.get(volumeId);
    if (disk == null) {
        log.error("Failed to get volume: " + volumeId + " in zone: " + zone);
        return null;
    }
    log.debug("Found volumeId: " + volumeId + " volume: " + disk);
    try {
        Operation oper =
                instApi.attachDisk(instanceId, AttachDisk.create(AttachDisk.Type.PERSISTENT, AttachDisk.Mode
                        .READ_WRITE, disk.selfLink(), deviceName, true, null, false, null, null));
        oper = waitGCEOperationDone(oper);
        if (!oper.status().equals(Operation.Status.DONE)) {
            log.error("Failed to attach volume: " + volumeId + " to instance: " + instanceId +
                    " in zone: " + zone + " at device: " + deviceName + " operation: " + oper);
            return null;
        }
        return volumeId;
    }
    catch (Exception e) {
        log.error("Error attaching volume", e);
    }
    return null;
}
 
示例3
/**
 *
 * @param vmZone
 * @param clusterName
 * @param groupNames
 * @throws KaramelException
 */
public void cleanup(Map<String, List<String>> vmZone, String clusterName, Set<String> groupNames)
    throws KaramelException {
  Iterator<Map.Entry<String, List<String>>> iterator = vmZone.entrySet().iterator();
  LinkedList<Operation> operations = new LinkedList<>();
  while (iterator.hasNext()) {
    Map.Entry<String, List<String>> entry = iterator.next();
    String zone = entry.getKey();
    List<String> vms = entry.getValue();
    logger.info(String.format("Killing following machines with names: \n %s.", vms.toString()));
    InstanceApi instanceApi = context.getGceApi().instancesInZone(zone);
    for (String vm : vms) {
      Operation op = instanceApi.delete(vm);
      if (op != null) {
        operations.add(op);
      }
    }
  }

  for (Operation operation : operations) {
    if (waitForOperation(context.getGceApi().operations(), operation) == 1) {
      logger.warn(String.format("%s operation has timedout: %s\n",
          operation.operationType(), operation.httpErrorMessage()));
    } else {
      logger.info(String.format("Operation %s  was successfully done on %s\n.",
          operation.operationType(), operation.targetLink()));
    }
  }

  // TODO: Handle the operations failures situation.
  operations.clear();
  /*
   * NetworkApi netApi = context.getNetworkApi();
   * FirewallApi fwApi = context.getFireWallApi();
   * RouteApi routeApi = context.getRouteApi();
   * //Delete network firewalls and routes first and then delete network, Otherwise network deletion will not work.
   * for (String group : groupNames) {
   * String networkName = Settings.UNIQUE_GROUP_NAME(GCE_PROVIDER, clusterName, group);
   * Network network = netApi.get(networkName);
   * if (network != null) {
   * URI networkUri = network.selfLink();
   * Iterator<ListPage<Firewall>> fwIterator = fwApi.list();
   * while (fwIterator.hasNext()) {
   * ListPage<Firewall> page = fwIterator.next();
   * for (Firewall fw : page) {
   * if (fw.network().equals(networkUri)) {
   * operations.add(fwApi.delete(fw.name()));
   * }
   * }
   * }
   * }
   * }
   *
   * for (Operation operation : operations) {
   * if (waitForOperation(context.getGceApi().operations(), operation) == 1) {
   * logger.warn(String.format("%s operation has timedout: %s\n",
   * operation.operationType(), operation.httpErrorMessage()));
   * } else {
   * logger.info(String.format("Operation %s was successfully done on %s\n.",
   * operation.operationType(), operation.targetLink()));
   * }
   * }
   *
   * operations.clear();
   *
   * for (String group : groupNames) {
   * String networkName = Settings.UNIQUE_GROUP_NAME(GCE_PROVIDER, clusterName, group);
   * Operation op = netApi.delete(networkName);
   * if (op != null) {
   * operations.add(op);
   * } else {
   * logger.info(String.format("Network %s does not exist.", networkName));
   * }
   * }
   *
   * for (Operation operation : operations) {
   * if (waitForOperation(context.getGceApi().operations(), operation) == 1) {
   * logger.warn(String.format("%s operation has timedout: %s\n",
   * operation.operationType(), operation.httpErrorMessage()));
   * } else {
   * logger.info(String.format("Operation %s was successfully done on %s\n.",
   * operation.operationType(), operation.targetLink()));
   * }
   * }
   */
}
 
示例4
private InstanceApi getGCEInstanceApi() {
    return getGCEApi().instancesInZone(getZone());
}