Package com.google.api.services.compute.model

Examples of com.google.api.services.compute.model.Instance


    refreshState();
  }

  public void refreshState() throws OpsException {
    Instance newState = computeClient.findInstanceByName(instance.getName());

    if (newState == null) {
      // Not found => deleted
      terminated = true;
    } else {
View Full Code Here


      // TODO: Fix this!
      String instanceName = "pl-" + UUID.randomUUID().toString();

      Operation createServerOperation;
      {
        Instance create = new Instance();

        create.setName(instanceName);

        create.setZone(buildZoneUrl(projectId, ZONE_US_CENTRAL1_A));

        {
          NetworkInterface networkInterface = new NetworkInterface();
          networkInterface.setNetwork(buildNetworkUrl(projectId, "default"));

          AccessConfig networkAccessConfig = new AccessConfig();
          networkAccessConfig.setType("ONE_TO_ONE_NAT");

          networkInterface.setAccessConfigs(Lists.newArrayList(networkAccessConfig));

          create.setNetworkInterfaces(Lists.newArrayList(networkInterface));
        }

        Metadata metadata = new Metadata();
        metadata.setItems(Lists.<Items> newArrayList());
        create.setMetadata(metadata);

        if (request.tags != null) {
          for (Tag tag : request.tags) {
            Metadata.Items meta = new Metadata.Items();
            meta.setKey(tag.getKey());
            meta.setValue(tag.getValue());
            metadata.getItems().add(meta);
          }
        }

        if (request.sshPublicKey != null) {
          Metadata.Items meta = new Metadata.Items();
          meta.setKey("sshKeys");
          meta.setValue(USER_NAME + ":" + OpenSshUtils.serialize(sshPublicKey));

          metadata.getItems().add(meta);
        }

        create.setImage(foundImage.getSelfLink());

        MachineType flavor = getClosestInstanceType(request);
        if (flavor == null) {
          throw new OpsException("Cannot determine machine type for request");
        }
        create.setMachineType(flavor.getSelfLink());

        if (request.securityGroups != null) {
          // TODO: Reimplement if needed
          throw new UnsupportedOperationException();
        }

        // if (createdSecurityGroup != null) {
        // ServerForCreate.SecurityGroup serverSecurityGroup = new ServerForCreate.SecurityGroup();
        // serverSecurityGroup.setName(createdSecurityGroup.getName());
        // create.getSecurityGroups().add(serverSecurityGroup);
        // }

        // create.setConfigDrive(cloudBehaviours.useConfigDrive());

        log.info("Launching new server: " + instanceName);
        try {
          createServerOperation = compute.instances().insert(projectId, create).execute();
        } catch (IOException e) {
          throw new OpsException("Error launching new instance", e);
        }
      }

      log.info("Waiting for server to be ready");
      createServerOperation = waitComplete(createServerOperation, 10, TimeUnit.MINUTES);

      Instance created;

      InstanceState state = null;
      while (true) {
        created = findInstanceByName(instanceName);
View Full Code Here

  }

  public Instance findInstanceByName(String name) throws OpsException {
    try {
      log.debug("Retrieving instance by name: " + name);
      Instance instance = compute.instances().get(projectId, name).execute();
      return instance;
    } catch (GoogleJsonResponseException e) {
      if (e.getStatusCode() == 404) {
        return null;
      }
View Full Code Here

        PlatformLayerKey instanceKey = instance.getKey();
        request.tags.add(Tag.buildParentTag(instanceKey));

        PublicKey servicePublicKey = service.getSshKey().getKeyPair().getPublic();
        Instance created = computeClient.createInstance(cloud, request, servicePublicKey);

        {
          Tag instanceTag = Tag.build(Tag.ASSIGNED, created.getName());
          platformLayer.addTag(instance.getKey(), instanceTag);
        }

        assignedInstanceIds.add(created.getName());
      }
    }

    if (assignedInstanceIds.isEmpty() && !OpsContext.isDelete()) {
      throw new OpsException("Instance not yet assigned");
    }

    GoogleComputeMachine machine = null;
    OpsTarget target = null;

    if (!assignedInstanceIds.isEmpty()) {
      if (assignedInstanceIds.size() != 1) {
        log.warn("Multiple instance ids found: " + assignedInstanceIds);
      }

      // We just take the first instance id
      String assignedInstanceId = Iterables.getFirst(assignedInstanceIds, null);

      Instance server = computeClient.findInstanceByName(assignedInstanceId);

      if (server == null) {
        if (OpsContext.isConfigure()) {
          throw new OpsException("Unable to find assigned server: " + assignedInstanceId);
        }
      } else {
        server = computeClient.ensureHasPublicIp(server);

        machine = new GoogleComputeMachine(computeClient, cloud, server);

        SshKey sshKey = service.getSshKey();
        target = machine.getTarget(GoogleComputeClient.USER_NAME, sshKey.getKeyPair());

        // We need to use sudo while we set up root access
        ((SshOpsTarget) target).setEnsureRunningAsRoot(true);
      }
    }

    if (!assignedInstanceIds.isEmpty() && OpsContext.isDelete()) {
      // CloudBehaviours cloudBehaviours = new CloudBehaviours(cloud);
      // boolean supportsSecurityGroups = cloudBehaviours.supportsSecurityGroups();

      for (String instanceId : assignedInstanceIds) {
        Instance server = computeClient.findInstanceByName(instanceId);
        if (server == null) {
          log.warn("Could not find assigned server: " + instanceId + ", ignoring");
          continue;
        }
View Full Code Here

        try {
            GccCredential credential = (GccCredential) stack.getCredential();
            GccTemplate template = (GccTemplate) stack.getTemplate();
            Compute.Instances.Get instanceGet = compute.instances().get(
                    credential.getProjectId(), template.getGccZone().getValue(), resource);
            Instance executeInstance = instanceGet.execute();
            CoreInstanceMetaData coreInstanceMetaData = new CoreInstanceMetaData(
                    resource,
                    executeInstance.getNetworkInterfaces().get(0).getNetworkIP(),
                    executeInstance.getNetworkInterfaces().get(0).getAccessConfigs().get(0).getNatIP(),
                    template.getVolumeCount(),
                    longName(resource, credential.getProjectId()),
                    template.getContainerCount()
            );
            return coreInstanceMetaData;
View Full Code Here

        List<AttachedDisk> listOfDisks = new ArrayList<>();
        listOfDisks.addAll(getBootDiskList(resources, gccCredential, gccTemplate));
        listOfDisks.addAll(getAttachedDisks(resources, gccCredential, gccTemplate));

        String name = String.format("%s-%s", stack.getName(), index);
        Instance instance = new Instance();
        instance.setMachineType(String.format("https://www.googleapis.com/compute/v1/projects/%s/zones/%s/machineTypes/%s",
                po.getProjectId(), gccTemplate.getGccZone().getValue(), gccTemplate.getGccInstanceType().getValue()));
        instance.setName(name);
        instance.setCanIpForward(Boolean.TRUE);
        instance.setNetworkInterfaces(getNetworkInterface(po.getProjectId(), stack.getName()));
        instance.setDisks(listOfDisks);
        Metadata metadata = new Metadata();
        metadata.setItems(Lists.<Metadata.Items>newArrayList());

        Metadata.Items sshMetaData = new Metadata.Items();
        sshMetaData.setKey("sshKeys");
        sshMetaData.setValue("ubuntu:" + gccCredential.getPublicKey());

        Metadata.Items startupScript = new Metadata.Items();
        startupScript.setKey("startup-script");
        startupScript.setValue(po.getUserData());

        metadata.getItems().add(sshMetaData);
        metadata.getItems().add(startupScript);
        instance.setMetadata(metadata);
        Compute.Instances.Insert ins =
                po.getCompute().instances().insert(gccCredential.getProjectId(), gccTemplate.getGccZone().getValue(), instance);

        ins.setPrettyPrint(Boolean.TRUE);
        ins.execute();
View Full Code Here

TOP

Related Classes of com.google.api.services.compute.model.Instance

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.