com.google.api.services.compute.Compute Java Examples

The following examples show how to use com.google.api.services.compute.Compute. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GcpInstanceResourceBuilder.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
private CloudVmInstanceStatus stopStart(GcpContext context, AuthenticatedContext auth, CloudInstance instance, boolean stopRequest) {
    String projectId = GcpStackUtil.getProjectId(auth.getCloudCredential());
    String availabilityZone = context.getLocation().getAvailabilityZone().value();
    Compute compute = context.getCompute();
    String instanceId = instance.getInstanceId();
    try {
        Get get = compute.instances().get(projectId, availabilityZone, instanceId);
        String state = stopRequest ? "RUNNING" : "TERMINATED";
        Instance instanceResponse = get.execute();
        if (state.equals(instanceResponse.getStatus())) {
            Operation operation = stopRequest ? compute.instances().stop(projectId, availabilityZone, instanceId).setPrettyPrint(true).execute()
                    : executeStartOperation(projectId, availabilityZone, compute, instanceId, instance.getTemplate(), instanceResponse.getDisks());
            CloudInstance operationAwareCloudInstance = createOperationAwareCloudInstance(instance, operation);
            return new CloudVmInstanceStatus(operationAwareCloudInstance, InstanceStatus.IN_PROGRESS);
        } else {
            LOGGER.debug("Instance {} is not in {} state - won't start it.", state, instanceId);
            return null;
        }
    } catch (IOException e) {
        throw new GcpResourceException(String.format("An error occurred while stopping the vm '%s'", instanceId), e);
    }
}
 
Example #2
Source File: GcpInstanceResourceBuilder.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
private Operation executeStartOperation(String projectId, String availabilityZone, Compute compute, String instanceId, InstanceTemplate template,
        List<AttachedDisk> disks) throws IOException {

    if (gcpDiskEncryptionService.hasCustomEncryptionRequested(template)) {
        CustomerEncryptionKey customerEncryptionKey = gcpDiskEncryptionService.createCustomerEncryptionKey(template);
        List<CustomerEncryptionKeyProtectedDisk> protectedDisks = disks
                .stream()
                .map(AttachedDisk::getSource)
                .map(toCustomerEncryptionKeyProtectedDisk(customerEncryptionKey))
                .collect(Collectors.toList());
        InstancesStartWithEncryptionKeyRequest request = new InstancesStartWithEncryptionKeyRequest();
        request.setDisks(protectedDisks);
        return compute.instances().startWithEncryptionKey(projectId, availabilityZone, instanceId, request).setPrettyPrint(true).execute();
    } else {
        return compute.instances().start(projectId, availabilityZone, instanceId).setPrettyPrint(true).execute();
    }
}
 
Example #3
Source File: GcpNetworkInterfaceProvider.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
public List<Instance> getInstances(AuthenticatedContext authenticatedContext, String instanceNamePrefix) {
    List<Instance> instances = new ArrayList<>();
    CloudContext cloudContext = authenticatedContext.getCloudContext();
    String stackName = cloudContext.getName();
    LOGGER.debug(String.format("Collecting instances for stack: %s", stackName));
    long startTime = new Date().getTime();

    try {
        Compute.Instances.List request = getRequest(authenticatedContext, instanceNamePrefix);
        InstanceList response;
        do {
            response = request.execute();
            if (response.getItems() == null) {
                continue;
            }
            instances.addAll(response.getItems());
            request.setPageToken(response.getNextPageToken());
        } while (response.getNextPageToken() != null);
    } catch (IOException e) {
        LOGGER.debug("Error during instance collection", e);
    }
    logResponse(instances, startTime, stackName);
    return instances;
}
 
Example #4
Source File: GcpProvisionSetup.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Override
public ImageStatusResult checkImageStatus(AuthenticatedContext authenticatedContext, CloudStack stack, com.sequenceiq.cloudbreak.cloud.model.Image image) {
    CloudCredential credential = authenticatedContext.getCloudCredential();
    String projectId = getProjectId(credential);
    String imageName = image.getImageName();
    try {
        Image gcpApiImage = new Image();
        gcpApiImage.setName(getImageName(imageName));
        Compute compute = buildCompute(credential);
        Get getImages = compute.images().get(projectId, gcpApiImage.getName());
        String status = getImages.execute().getStatus();
        LOGGER.debug("Status of image {} copy: {}", gcpApiImage.getName(), status);
        if (READY.equals(status)) {
            return new ImageStatusResult(ImageStatus.CREATE_FINISHED, ImageStatusResult.COMPLETED);
        }
    } catch (IOException e) {
        LOGGER.info("Failed to retrieve image copy status", e);
        return new ImageStatusResult(ImageStatus.CREATE_FAILED, 0);
    }
    return new ImageStatusResult(ImageStatus.IN_PROGRESS, ImageStatusResult.HALF);
}
 
Example #5
Source File: GoogleProviderUtils.java    From halyard with Apache License 2.0 6 votes vote down vote up
public static void resize(
    AccountDeploymentDetails<GoogleAccount> details,
    String zone,
    String managedInstaceGroupName,
    int targetSize) {
  Compute compute = getCompute(details);
  try {
    compute
        .instanceGroupManagers()
        .resize(details.getAccount().getProject(), zone, managedInstaceGroupName, targetSize);
  } catch (IOException e) {
    throw new HalException(
        FATAL,
        "Unable to resize instance group manager "
            + managedInstaceGroupName
            + ": "
            + e.getMessage(),
        e);
  }
}
 
Example #6
Source File: GcpFirewallInResourceBuilder.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Override
public CloudResourceStatus update(GcpContext context, AuthenticatedContext auth, Group group, Network network, Security security, CloudResource resource) {
    String projectId = context.getProjectId();
    Compute compute = context.getCompute();
    String resourceName = resource.getName();
    try {
        Firewall fireWall = compute.firewalls().get(projectId, resourceName).execute();
        List<String> sourceRanges = getSourceRanges(security);
        fireWall.setSourceRanges(sourceRanges);
        Operation operation = compute.firewalls().update(projectId, resourceName, fireWall).execute();
        CloudResource cloudResource = createOperationAwareCloudResource(resource, operation);
        return checkResources(context, auth, Collections.singletonList(cloudResource)).get(0);
    } catch (IOException e) {
        throw new GcpResourceException("Failed to update resource!", GCP_FIREWALL_IN, resourceName, e);
    }
}
 
Example #7
Source File: GoogleProviderUtils.java    From halyard with Apache License 2.0 6 votes vote down vote up
static String getInstanceIp(
    AccountDeploymentDetails<GoogleAccount> details, String instanceName) {
  Compute compute = getCompute(details);
  Instance instance = null;
  try {
    instance =
        compute
            .instances()
            .get(details.getAccount().getProject(), "us-central1-f", instanceName)
            .execute();
  } catch (IOException e) {
    throw new HalException(FATAL, "Unable to get instance " + instanceName);
  }

  return instance.getNetworkInterfaces().stream()
      .map(
          i ->
              i.getAccessConfigs().stream()
                  .map(AccessConfig::getNatIP)
                  .filter(ip -> !StringUtils.isEmpty(ip))
                  .findFirst())
      .filter(Optional::isPresent)
      .map(Optional::get)
      .findFirst()
      .orElseThrow(() -> new HalException(FATAL, "No public IP associated with" + instanceName));
}
 
Example #8
Source File: GcpNetworkResourceBuilder.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Override
public CloudResource build(GcpContext context, AuthenticatedContext auth, Network network, Security security, CloudResource resource) throws Exception {
    if (!isExistingNetwork(network)) {
        Compute compute = context.getCompute();
        String projectId = context.getProjectId();

        com.google.api.services.compute.model.Network gcpNetwork = new com.google.api.services.compute.model.Network();
        gcpNetwork.setName(resource.getName());
        gcpNetwork.setAutoCreateSubnetworks(false);
        Insert networkInsert = compute.networks().insert(projectId, gcpNetwork);
        try {
            Operation operation = networkInsert.execute();
            if (operation.getHttpErrorStatusCode() != null) {
                throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), resource.getName());
            }
            context.putParameter(NETWORK_NAME, resource.getName());
            return createOperationAwareCloudResource(resource, operation);
        } catch (GoogleJsonResponseException e) {
            throw new GcpResourceException(checkException(e), resourceType(), resource.getName());
        }
    }
    context.putParameter(NETWORK_NAME, resource.getName());
    return new Builder().cloudResource(resource).persistent(false).build();
}
 
Example #9
Source File: GcpPlatformResources.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Override
public CloudSecurityGroups securityGroups(CloudCredential cloudCredential, Region region, Map<String, String> filters) throws IOException {
    Compute compute = GcpStackUtil.buildCompute(cloudCredential);
    String projectId = GcpStackUtil.getProjectId(cloudCredential);

    Map<String, Set<CloudSecurityGroup>> result = new HashMap<>();
    FirewallList firewallList = compute.firewalls().list(projectId).execute();
    for (Firewall firewall : firewallList.getItems()) {
        Map<String, Object> properties = new HashMap<>();
        properties.put("network", getNetworkName(firewall));
        CloudSecurityGroup cloudSecurityGroup = new CloudSecurityGroup(firewall.getName(), firewall.getName(), properties);
        result.computeIfAbsent(region.value(), k -> new HashSet<>()).add(cloudSecurityGroup);
    }

    return new CloudSecurityGroups(result);
}
 
Example #10
Source File: GceApi.java    From simpleci with MIT License 6 votes vote down vote up
public void createInstance(GoogleComputeProvider provider, String workerId) {
    try {
        final String instanceName = instanceName(workerId);
        Compute compute = createApi(provider);
        Operation diskOperation = createDisk(provider, compute, instanceName, provider.snapshotName);
        logger.info(String.format("Wait for disk creation: %s", instanceName));
        waitForOperation(provider, compute, diskOperation);
        Operation instanceCreateOperation = makeInstance(provider, compute, workerId, instanceName);
        logger.info(String.format("Wait for instance creation: %s", instanceName));
        waitForOperation(provider, compute, instanceCreateOperation);
        logger.info(String.format("Instance %s created successfully", instanceName));
    } catch (Exception e) {
        logger.error("Failed to created google compute engine instance", e);
    }

}
 
Example #11
Source File: GceApi.java    From simpleci with MIT License 6 votes vote down vote up
public void stopAndRemoveInstance(GoogleComputeProvider provider, String workerId) {
    try {
        final String instanceName = instanceName(workerId);

        Compute compute = createApi(provider);
        logger.info(String.format("Stopping and removing instance: %s", instanceName));
        Operation operation = compute
                .instances()
                .stop(provider.project, provider.zone, instanceName)
                .execute();
        waitForOperation(provider, compute, operation);
        operation = compute.instances().delete(
                provider.project, provider.zone, instanceName).execute();
        waitForOperation(provider, compute, operation);
        logger.info(String.format("Instance %s stopped", instanceName));
    } catch (IOException | GeneralSecurityException e) {
        logger.error("", e);
    }
}
 
Example #12
Source File: GcpPlatformResources.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Override
@Cacheable(cacheNames = "cloudResourceRegionCache", key = "#cloudCredential?.id")
public CloudRegions regions(CloudCredential cloudCredential, Region region, Map<String, String> filters, boolean availabilityZonesNeeded) throws Exception {
    Compute compute = GcpStackUtil.buildCompute(cloudCredential);
    String projectId = GcpStackUtil.getProjectId(cloudCredential);

    Map<Region, List<AvailabilityZone>> regionListMap = new HashMap<>();
    Map<Region, String> displayNames = new HashMap<>();
    Map<Region, Coordinate> coordinates = new HashMap<>();

    String defaultRegion = gcpZoneParameterDefault;
    RegionList regionList = compute.regions().list(projectId).execute();
    for (com.google.api.services.compute.model.Region gcpRegion : regionList.getItems()) {
        if (region == null || Strings.isNullOrEmpty(region.value()) || gcpRegion.getName().equals(region.value())) {
            List<AvailabilityZone> availabilityZones = new ArrayList<>();
            for (String s : gcpRegion.getZones()) {
                String[] split = s.split("/");
                if (split.length > 0) {
                    availabilityZones.add(AvailabilityZone.availabilityZone(split[split.length - 1]));
                }
            }
            regionListMap.put(region(gcpRegion.getName()), availabilityZones);
            displayNames.put(region(gcpRegion.getName()), displayName(gcpRegion.getName()));
            addCoordinate(coordinates, gcpRegion);
        }
    }
    if (region != null && !Strings.isNullOrEmpty(region.value())) {
        defaultRegion = region.value();
    }
    return new CloudRegions(regionListMap, displayNames, coordinates, defaultRegion, true);
}
 
Example #13
Source File: GcpReservedIpResourceBuilder.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Override
public CloudResource delete(GcpContext context, AuthenticatedContext auth, CloudResource resource) throws Exception {
    Compute compute = context.getCompute();
    String projectId = context.getProjectId();
    String region = context.getLocation().getRegion().value();
    try {
        Operation operation = compute.addresses().delete(projectId, region, resource.getName()).execute();
        return createOperationAwareCloudResource(resource, operation);
    } catch (GoogleJsonResponseException e) {
        exceptionHandler(e, resource.getName(), resourceType());
        return null;
    }
}
 
Example #14
Source File: GcpSubnetResourceBuilder.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Override
public CloudResource build(GcpContext context, AuthenticatedContext auth, Network network, Security security, CloudResource resource) throws Exception {
    if (isNewNetworkAndSubnet(network) || isNewSubnetInExistingNetwork(network)) {
        Compute compute = context.getCompute();
        String projectId = context.getProjectId();

        Subnetwork gcpSubnet = new Subnetwork();
        gcpSubnet.setName(resource.getName());
        gcpSubnet.setIpCidrRange(network.getSubnet().getCidr());

        String networkName = context.getStringParameter(GcpNetworkResourceBuilder.NETWORK_NAME);
        if (isNotEmpty(getSharedProjectId(network))) {
            gcpSubnet.setNetwork(String.format("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s",
                    getSharedProjectId(network), networkName));
        } else {
            gcpSubnet.setNetwork(String.format("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s", projectId, networkName));
        }

        Insert snInsert = compute.subnetworks().insert(projectId, auth.getCloudContext().getLocation().getRegion().value(), gcpSubnet);
        try {
            Operation operation = snInsert.execute();
            if (operation.getHttpErrorStatusCode() != null) {
                throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), resource.getName());
            }
            context.putParameter(SUBNET_NAME, resource.getName());
            return createOperationAwareCloudResource(resource, operation);
        } catch (GoogleJsonResponseException e) {
            throw new GcpResourceException(checkException(e), resourceType(), resource.getName());
        }
    }
    context.putParameter(SUBNET_NAME, resource.getName());
    return new Builder().cloudResource(resource).persistent(false).build();
}
 
Example #15
Source File: GcpSubnetResourceBuilder.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Override
public CloudResource delete(GcpContext context, AuthenticatedContext auth, CloudResource resource, Network network) throws Exception {
    if (isNewNetworkAndSubnet(network) || isNewSubnetInExistingNetwork(network)) {
        Compute compute = context.getCompute();
        String projectId = context.getProjectId();
        try {
            Operation operation = compute.subnetworks().delete(projectId, context.getLocation().getRegion().value(), resource.getName()).execute();
            return createOperationAwareCloudResource(resource, operation);
        } catch (GoogleJsonResponseException e) {
            exceptionHandler(e, resource.getName(), resourceType());
            return null;
        }
    }
    return null;
}
 
Example #16
Source File: GcpNetworkResourceBuilder.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Override
public CloudResource delete(GcpContext context, AuthenticatedContext auth, CloudResource resource, Network network) throws Exception {
    if (!isExistingNetwork(network)) {
        Compute compute = context.getCompute();
        String projectId = context.getProjectId();
        try {
            Operation operation = compute.networks().delete(projectId, resource.getName()).execute();
            return createOperationAwareCloudResource(resource, operation);
        } catch (GoogleJsonResponseException e) {
            exceptionHandler(e, resource.getName(), resourceType());
            return null;
        }
    }
    return null;
}
 
Example #17
Source File: GcpPlatformResources.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Override
public CloudNetworks networks(CloudCredential cloudCredential, Region region, Map<String, String> filters) throws Exception {
    Compute compute = GcpStackUtil.buildCompute(cloudCredential);
    String projectId = GcpStackUtil.getProjectId(cloudCredential);
    Map<String, Set<CloudNetwork>> result = new HashMap<>();

    Set<CloudNetwork> cloudNetworks = new HashSet<>();
    NetworkList networkList = compute.networks().list(projectId).execute();
    List<Subnetwork> subnetworkList = compute.subnetworks().list(projectId, region.value()).execute().getItems();
    for (Network network : networkList.getItems()) {
        Map<String, Object> properties = new HashMap<>();
        properties.put("gatewayIPv4", Strings.nullToEmpty(network.getGatewayIPv4()));
        properties.put("description", Strings.nullToEmpty(network.getDescription()));
        properties.put("IPv4Range", Strings.nullToEmpty(network.getIPv4Range()));
        properties.put("creationTimestamp", Strings.nullToEmpty(network.getCreationTimestamp()));

        Set<CloudSubnet> subnets = new HashSet<>();
        if (subnetworkList != null && network.getSubnetworks() != null) {
            for (Subnetwork subnetwork : subnetworkList) {
                if (network.getSubnetworks().contains(subnetwork.getSelfLink())) {
                    subnets.add(new CloudSubnet(subnetwork.getId().toString(), subnetwork.getName(), null, subnetwork.getIpCidrRange()));
                }
            }
        }

        CloudNetwork cloudNetwork = new CloudNetwork(network.getName(), network.getId().toString(), subnets, properties);
        cloudNetworks.add(cloudNetwork);
    }
    result.put(region.value(), cloudNetworks);

    return new CloudNetworks(result);
}
 
Example #18
Source File: GCEComputeResourceController.java    From pubsub with Apache License 2.0 5 votes vote down vote up
public GCEComputeResourceController(
    String project,
    ClientParams params,
    Integer count,
    ScheduledExecutorService executor,
    Compute compute) {
  super(executor);
  this.project = project;
  this.params = params;
  this.count = count;
  this.executor = executor;
  this.compute = compute;
}
 
Example #19
Source File: GcpContext.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public GcpContext(String name, Location location, String projectId, String serviceAccountId, Compute compute, boolean noPublicIp,
        int parallelResourceRequest, boolean build) {
    super(name, location, parallelResourceRequest, build);
    putParameter(PROJECT_ID, projectId);
    putParameter(SERVICE_ACCOUNT_ID, serviceAccountId);
    putParameter(COMPUTE, compute);
    putParameter(NO_PUBLIC_IP, noPublicIp);
}
 
Example #20
Source File: GcpContextBuilder.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Override
public GcpContext contextInit(CloudContext context, AuthenticatedContext auth, Network network, List<CloudResource> resources, boolean build) {
    CloudCredential credential = auth.getCloudCredential();
    String projectId = GcpStackUtil.getProjectId(credential);
    String serviceAccountId = GcpStackUtil.getServiceAccountId(credential);
    Compute compute = GcpStackUtil.buildCompute(credential);
    Location location = context.getLocation();
    boolean noPublicIp = network != null ? GcpStackUtil.noPublicIp(network) : false;
    return new GcpContext(context.getName(), location, projectId, serviceAccountId, compute, noPublicIp, PARALLEL_RESOURCE_REQUEST, build);
}
 
Example #21
Source File: GcpNetworkInterfaceProvider.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private Compute.Instances.List getRequest(AuthenticatedContext authenticatedContext, String instanceNamePrefix) throws IOException {
    CloudCredential credential = authenticatedContext.getCloudCredential();
    Compute compute = gcpApiFactory.getComputeApi(credential);
    return compute.instances()
            .list(GcpStackUtil.getProjectId(credential), authenticatedContext.getCloudContext().getLocation()
                    .getAvailabilityZone()
                    .value())
            .setFilter(String.format("name=%s-*", instanceNamePrefix));
}
 
Example #22
Source File: GcpStackUtil.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public static Compute buildCompute(CloudCredential gcpCredential) {
    try {
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        GoogleCredential credential = buildCredential(gcpCredential, httpTransport);
        return new Compute.Builder(
                httpTransport, JSON_FACTORY, null).setApplicationName(gcpCredential.getName())
                .setHttpRequestInitializer(credential)
                .build();
    } catch (Exception e) {
        LOGGER.warn("Error occurred while building Google Compute access.", e);
        throw new CredentialVerificationException("Error occurred while building Google Compute access.", e);
    }
}
 
Example #23
Source File: BaragonServiceModule.java    From Baragon with Apache License 2.0 5 votes vote down vote up
@Provides
@Singleton
@Named(GOOGLE_CLOUD_COMPUTE_SERVICE)
public Optional<Compute> provideComputeService(BaragonConfiguration config) throws Exception {
  if (!config.getGoogleCloudConfiguration().isEnabled()) {
    return Optional.absent();
  }
  HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
  JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

  GoogleCredential credential = null;

  if (config.getGoogleCloudConfiguration().getGoogleCredentialsFile() != null) {
    File credentialsFile = new File(config.getGoogleCloudConfiguration().getGoogleCredentialsFile());
    credential = GoogleCredential.fromStream(
        new FileInputStream(credentialsFile)
    );
  } else if (config.getGoogleCloudConfiguration().getGoogleCredentials() != null) {
    credential = GoogleCredential.fromStream(
        new ByteArrayInputStream(config.getGoogleCloudConfiguration().getGoogleCredentials().getBytes("UTF-8"))
    );
  } else {
    throw new RuntimeException("Must specify googleCloudCredentials or googleCloudCredentialsFile when using google cloud api");
  }

  if (credential.createScopedRequired()) {
    credential =
        credential.createScoped(Collections.singletonList("https://www.googleapis.com/auth/cloud-platform"));
  }

  return Optional.of(new Compute.Builder(httpTransport, jsonFactory, credential)
      .setApplicationName("BaragonService")
      .build());
}
 
Example #24
Source File: GoogleAccountValidator.java    From halyard with Apache License 2.0 5 votes vote down vote up
@Override
public void validate(ConfigProblemSetBuilder p, GoogleAccount n) {
  DaemonTaskHandler.message(
      "Validating " + n.getNodeName() + " with " + GoogleAccountValidator.class.getSimpleName());

  String jsonKey = getJsonKey(p, n);

  GoogleNamedAccountCredentials credentials =
      n.getNamedAccountCredentials(halyardVersion, jsonKey, p);
  if (credentials == null) {
    return;
  } else {
    credentialsList.add(credentials);
  }

  try {
    Compute compute = credentials.getCompute();

    compute.projects().get(n.getProject()).execute();

    for (String imageProject : n.getImageProjects()) {
      compute.projects().get(imageProject).execute();
    }
  } catch (IOException e) {
    p.addProblem(
        Severity.ERROR,
        "Failed to load project \"" + n.getProject() + "\": " + e.getMessage() + ".");
  }

  validateUserDataFile(p, n);
}
 
Example #25
Source File: GceApi.java    From simpleci with MIT License 5 votes vote down vote up
private Compute createApi(GoogleComputeProvider provider) throws IOException, GeneralSecurityException {
    GoogleCredential credential = GoogleCredential
            .fromStream(new ByteArrayInputStream(provider.gcAccount.serviceAccount.getBytes(StandardCharsets.UTF_8)))
            .createScoped(Collections.singleton(ComputeScopes.COMPUTE));

    NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    return new Compute.Builder(
            httpTransport, JSON_FACTORY, null).setApplicationName(APPLICATION_NAME)
                                              .setHttpRequestInitializer(credential).build();
}
 
Example #26
Source File: GceApi.java    From simpleci with MIT License 5 votes vote down vote up
private Operation createDisk(GoogleComputeProvider provider, Compute compute, String name, String imageName) throws IOException {
    Disk disk = new Disk();
    disk.setName(name)
        .setSizeGb((long) provider.diskSize)
        .setSourceSnapshot(String.format("projects/%s/global/snapshots/%s",
                provider.project, imageName))
        .setType(String.format("projects/%s/zones/%s/diskTypes/%s",
                provider.project, provider.zone, provider.diskType))
        .setZone(String.format("projects/%s/zones/%s",
                provider.project, provider.zone));
    Compute.Disks.Insert operation = compute.disks().insert(
            provider.project, provider.zone, disk);
    return operation.execute();
}
 
Example #27
Source File: GceApi.java    From simpleci with MIT License 5 votes vote down vote up
private void waitForOperation(GoogleComputeProvider provider, Compute compute, Operation diskOperation) throws IOException {
    while (true) {
        Operation currentOperation = compute
                .zoneOperations()
                .get(provider.project, provider.zone, diskOperation.getName())
                .execute();
        if (currentOperation.getStatus().equals("DONE")) {
            return;
        }
    }
}
 
Example #28
Source File: ComputeEngineSample.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static Operation deleteInstance(Compute compute, String instanceName) throws Exception {
  System.out.println(
      "================== Deleting Instance " + instanceName + " ==================");
  Compute.Instances.Delete delete =
      compute.instances().delete(PROJECT_ID, ZONE_NAME, instanceName);
  return delete.execute();
}
 
Example #29
Source File: GoogleProviderUtils.java    From halyard with Apache License 2.0 5 votes vote down vote up
static String defaultServiceAccount(AccountDeploymentDetails<GoogleAccount> details) {
  GoogleAccount account = details.getAccount();
  String project = account.getProject();
  Compute compute = getCompute(details);

  try {
    return compute.projects().get(project).execute().getDefaultServiceAccount();
  } catch (IOException e) {
    throw new HalException(FATAL, "Unable to get default compute service account");
  }
}
 
Example #30
Source File: GoogleProviderUtils.java    From halyard with Apache License 2.0 5 votes vote down vote up
static void waitOnZoneOperation(Compute compute, String project, String zone, Operation operation)
    throws IOException {
  waitOnOperation(
      () -> {
        try {
          return compute.zoneOperations().get(project, zone, operation.getName()).execute();
        } catch (IOException e) {
          throw new HalException(FATAL, "Operation failed: " + e);
        }
      });
}