Package org.platformlayer.service.cloud.google.ops.compute

Examples of org.platformlayer.service.cloud.google.ops.compute.GoogleComputeClient


    GoogleCloud cloud = findCloud();
    if (cloud == null) {
      throw new OpsException("Could not find cloud");
    }
    GoogleComputeClient computeClient = googleComputeClientFactory.getComputeClient(cloud);

    getRecursionState().pushChildScope(cloud);

    List<String> assignedInstanceIds = instanceTags.findAll(Tag.ASSIGNED);
    if (assignedInstanceIds.isEmpty()) {
      if (createInstance && !OpsContext.isDelete()) {
        MachineCreationRequest request = buildMachineCreationRequest();

        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;
        }

        // TODO: Remove associated firewall rules
        log.warn("Deleting firewall rules not yet implemented");

        // SecurityGroup securityGroup = null;
        // if (supportsSecurityGroups) {
        // securityGroup = openstackHelpers.getMachineSecurityGroup(computeClient, server);
        // }

        Operation terminateOperation = computeClient.terminateInstance(instanceId);
        try {
          computeClient.waitComplete(terminateOperation, 5, TimeUnit.MINUTES);
        } catch (TimeoutException e) {
          throw new OpsException("Timeout while waiting for instance termination", e);
        }

        // if (securityGroup != null) {
View Full Code Here


  @Inject
  GoogleComputeClientFactory googleComputeClientFactory;

  @Handler
  public void handler(GoogleCloud cloud, GoogleComputeMachine machine) throws OpsException {
    GoogleComputeClient client = googleComputeClientFactory.getComputeClient(cloud);

    // Find the public address, although the Google Cloud firewall may be blocking it
    publicAddress = machine.getNetworkPoint().getBestAddress(NetworkPoint.forPublicInternet());

    String serverLink = machine.getServerSelfLink();
    List<Firewall> rules = client.getInstanceFirewallRules(serverLink);

    Firewall matchingRule = findMatchingRule(rules);

    if (OpsContext.isConfigure()) {
      if (matchingRule == null) {
        Firewall rule = new Firewall();
        rule.setSourceRanges(Arrays.asList("0.0.0.0/0"));
        rule.setName("pl-" + UUID.randomUUID().toString());

        Allowed allowed = new Allowed();
        allowed.setIPProtocol("tcp");
        allowed.setPorts(Arrays.asList("" + model.publicPort));
        rule.setAllowed(Arrays.asList(allowed));

        rule.setNetwork(client.buildNetworkUrl("default"));

        client.createFirewallRule(rule);
      }
    }

    if (OpsContext.isDelete()) {
      if (matchingRule != null) {
        client.deleteFirewallRule(matchingRule);
      }
    }
  }
View Full Code Here

TOP

Related Classes of org.platformlayer.service.cloud.google.ops.compute.GoogleComputeClient

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.