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

The following examples show how to use com.google.api.services.compute.ComputeScopes. 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: 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 #2
Source File: ComputeEngineSample.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
  try {
    httpTransport = GoogleNetHttpTransport.newTrustedTransport();

    // Authenticate using Google Application Default Credentials.
    GoogleCredentials credential = GoogleCredentials.getApplicationDefault();
    if (credential.createScopedRequired()) {
      List<String> scopes = new ArrayList<>();
      // Set Google Cloud Storage scope to Full Control.
      scopes.add(ComputeScopes.DEVSTORAGE_FULL_CONTROL);
      // Set Google Compute Engine scope to Read-write.
      scopes.add(ComputeScopes.COMPUTE);
      credential = credential.createScoped(scopes);
    }
    HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credential);
    // Create Compute Engine object for listing instances.
    Compute compute =
        new Compute.Builder(httpTransport, JSON_FACTORY, requestInitializer)
            .setApplicationName(APPLICATION_NAME)
            .build();

    // List out instances, looking for the one created by this sample app.
    boolean foundOurInstance = printInstances(compute);

    Operation op;
    if (foundOurInstance) {
      op = deleteInstance(compute, SAMPLE_INSTANCE_NAME);
    } else {
      op = startInstance(compute, SAMPLE_INSTANCE_NAME);
    }

    // Call Compute Engine API operation and poll for operation completion status
    System.out.println("Waiting for operation completion...");
    Operation.Error error = blockUntilComplete(compute, op, OPERATION_TIMEOUT_MILLIS);
    if (error == null) {
      System.out.println("Success!");
    } else {
      System.out.println(error.toPrettyString());
    }
  } catch (IOException e) {
    System.err.println(e.getMessage());
  } catch (Throwable t) {
    t.printStackTrace();
  }
  System.exit(1);
}