gpt4 book ai didi

com.google.cloud.compute.v1.ZoneOperationClient类的使用及代码示例

转载 作者:知者 更新时间:2024-03-13 12:31:43 28 4
gpt4 key购买 nike

本文整理了Java中com.google.cloud.compute.v1.ZoneOperationClient类的一些代码示例,展示了ZoneOperationClient类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZoneOperationClient类的具体详情如下:
包路径:com.google.cloud.compute.v1.ZoneOperationClient
类名称:ZoneOperationClient

ZoneOperationClient介绍

[英]Service Description: Creates and runs virtual machines on Google Cloud Platform.

This class provides the ability to make remote calls to the backing service through method calls that map to API methods. Sample code to get started:

try (ZoneOperationClient zoneOperationClient = ZoneOperationClient.create()) { 
ProjectZoneOperationName operation = ProjectZoneOperationName.of("[PROJECT]", "[ZONE]", "[OPERATION]"); 
zoneOperationClient.deleteZoneOperation(operation); 
}

Note: close() needs to be called on the zoneOperationClient object to clean up resources such as threads. In the example above, try-with-resources is used, which automatically calls close().

The surface of this class includes several types of Java methods for each of the API's methods:

  1. A "flattened" method. With this type of method, the fields of the request type have been converted into function parameters. It may be the case that not all fields are available as parameters, and not every API method will have a flattened method entry point.
  2. A "request object" method. This type of method only takes one parameter, a request object, which must be constructed before the call. Not every API method will have a request object method.
  3. A "callable" method. This type of method takes no parameters and returns an immutable API callable object, which can be used to initiate calls to the service.

See the individual methods for example code.

Many parameters require resource names to be formatted in a particular way. To assist with these names, this class includes a format method for each type of name, and additionally a parse method to extract the individual identifiers contained within names that are returned.

This class can be customized by passing in a custom instance of ZoneOperationSettings to create(). For example:

To customize credentials:

ZoneOperationSettings zoneOperationSettings = 
ZoneOperationSettings.newBuilder() 
.setCredentialsProvider(FixedCredentialsProvider.create(myCredentials)) 
.build(); 
ZoneOperationClient zoneOperationClient = 
ZoneOperationClient.create(zoneOperationSettings);

To customize the endpoint:

ZoneOperationSettings zoneOperationSettings = 
ZoneOperationSettings.newBuilder().setEndpoint(myEndpoint).build(); 
ZoneOperationClient zoneOperationClient = 
ZoneOperationClient.create(zoneOperationSettings);

[中]服务描述:在谷歌云平台上创建并运行虚拟机。
此类提供了通过映射到API方法的方法调用对支持服务进行远程调用的能力。开始的示例代码:

try (ZoneOperationClient zoneOperationClient = ZoneOperationClient.create()) { 
ProjectZoneOperationName operation = ProjectZoneOperationName.of("[PROJECT]", "[ZONE]", "[OPERATION]"); 
zoneOperationClient.deleteZoneOperation(operation); 
}

注意:需要对zoneOperationClient对象调用close(),以清理线程等资源。在上面的示例中,使用了try with resources,它会自动调用close()。
该类的表面包括针对每个API方法的几种类型的Java方法:
1.“扁平化”方法。使用这种类型的方法,请求类型的字段已转换为函数参数。可能不是所有字段都可以用作参数,也不是每个API方法都有平坦的方法入口点。
1.“请求对象”方法。这种类型的方法只接受一个参数,即请求对象,它必须在调用之前构造。并不是每个API方法都有一个请求对象方法。
1.“可调用”方法。这种类型的方法不接受任何参数,并返回一个不可变的API可调用对象,该对象可用于启动对服务的调用。
有关示例代码,请参见各个方法。
许多参数要求以特定的方式格式化资源名称。为了帮助处理这些名称,这个类为每种类型的名称包括一个格式化方法,另外还有一个解析方法,用于提取返回的名称中包含的各个标识符。
可以通过将ZoneOperationSettings的自定义实例传递给create()来自定义此类。例如:
要自定义凭据,请执行以下操作:

ZoneOperationSettings zoneOperationSettings = 
ZoneOperationSettings.newBuilder() 
.setCredentialsProvider(FixedCredentialsProvider.create(myCredentials)) 
.build(); 
ZoneOperationClient zoneOperationClient = 
ZoneOperationClient.create(zoneOperationSettings);

要自定义端点:

ZoneOperationSettings zoneOperationSettings = 
ZoneOperationSettings.newBuilder().setEndpoint(myEndpoint).build(); 
ZoneOperationClient zoneOperationClient = 
ZoneOperationClient.create(zoneOperationSettings);

代码示例

代码示例来源:origin: googleapis/google-cloud-java

/** Constructs an instance of ZoneOperationClient with default settings. */
public static final ZoneOperationClient create() throws IOException {
 return create(ZoneOperationSettings.newBuilder().build());
}

代码示例来源:origin: googleapis/google-cloud-java

/**
 * Constructs an instance of ZoneOperationClient, using the given settings. The channels are
 * created based on the settings passed in, or defaults for any settings that are not set.
 */
public static final ZoneOperationClient create(ZoneOperationSettings settings)
  throws IOException {
 return new ZoneOperationClient(settings);
}

代码示例来源:origin: googleapis/google-cloud-java

@AfterClass
public static void tearDown() throws Exception {
 client.close();
}

代码示例来源:origin: googleapis/google-cloud-java

/**
 * Deletes the specified zone-specific Operations resource.
 *
 * <p>Sample code:
 *
 * <pre><code>
 * try (ZoneOperationClient zoneOperationClient = ZoneOperationClient.create()) {
 *   ProjectZoneOperationName operation = ProjectZoneOperationName.of("[PROJECT]", "[ZONE]", "[OPERATION]");
 *   zoneOperationClient.deleteZoneOperation(operation.toString());
 * }
 * </code></pre>
 *
 * @param operation Name of the Operations resource to delete.
 * @throws com.google.api.gax.rpc.ApiException if the remote call fails
 */
@BetaApi
public final void deleteZoneOperation(String operation) {
 DeleteZoneOperationHttpRequest request =
   DeleteZoneOperationHttpRequest.newBuilder().setOperation(operation).build();
 deleteZoneOperation(request);
}

代码示例来源:origin: googleapis/google-cloud-java

/**
 * Retrieves the specified zone-specific Operations resource.
 *
 * <p>Sample code:
 *
 * <pre><code>
 * try (ZoneOperationClient zoneOperationClient = ZoneOperationClient.create()) {
 *   ProjectZoneOperationName operation = ProjectZoneOperationName.of("[PROJECT]", "[ZONE]", "[OPERATION]");
 *   Operation response = zoneOperationClient.getZoneOperation(operation.toString());
 * }
 * </code></pre>
 *
 * @param operation Name of the Operations resource to return.
 * @throws com.google.api.gax.rpc.ApiException if the remote call fails
 */
@BetaApi
public final Operation getZoneOperation(String operation) {
 GetZoneOperationHttpRequest request =
   GetZoneOperationHttpRequest.newBuilder().setOperation(operation).build();
 return getZoneOperation(request);
}

代码示例来源:origin: googleapis/google-cloud-java

/**
 * Retrieves a list of Operation resources contained within the specified zone.
 *
 * <p>Sample code:
 *
 * <pre><code>
 * try (ZoneOperationClient zoneOperationClient = ZoneOperationClient.create()) {
 *   ProjectZoneName zone = ProjectZoneName.of("[PROJECT]", "[ZONE]");
 *   for (Operation element : zoneOperationClient.listZoneOperations(zone.toString()).iterateAll()) {
 *     // doThingsWith(element);
 *   }
 * }
 * </code></pre>
 *
 * @param zone Name of the zone for request.
 * @throws com.google.api.gax.rpc.ApiException if the remote call fails
 */
@BetaApi
public final ListZoneOperationsPagedResponse listZoneOperations(String zone) {
 ListZoneOperationsHttpRequest request =
   ListZoneOperationsHttpRequest.newBuilder().setZone(zone).build();
 return listZoneOperations(request);
}

代码示例来源:origin: googleapis/google-cloud-java

/**
 * Retrieves the specified zone-specific Operations resource.
 *
 * <p>Sample code:
 *
 * <pre><code>
 * try (ZoneOperationClient zoneOperationClient = ZoneOperationClient.create()) {
 *   ProjectZoneOperationName operation = ProjectZoneOperationName.of("[PROJECT]", "[ZONE]", "[OPERATION]");
 *   GetZoneOperationHttpRequest request = GetZoneOperationHttpRequest.newBuilder()
 *     .setOperation(operation.toString())
 *     .build();
 *   Operation response = zoneOperationClient.getZoneOperation(request);
 * }
 * </code></pre>
 *
 * @param request The request object containing all of the parameters for the API call.
 * @throws com.google.api.gax.rpc.ApiException if the remote call fails
 */
@BetaApi
public final Operation getZoneOperation(GetZoneOperationHttpRequest request) {
 return getZoneOperationCallable().call(request);
}

代码示例来源:origin: googleapis/google-cloud-java

/**
 * Deletes the specified zone-specific Operations resource.
 *
 * <p>Sample code:
 *
 * <pre><code>
 * try (ZoneOperationClient zoneOperationClient = ZoneOperationClient.create()) {
 *   ProjectZoneOperationName operation = ProjectZoneOperationName.of("[PROJECT]", "[ZONE]", "[OPERATION]");
 *   DeleteZoneOperationHttpRequest request = DeleteZoneOperationHttpRequest.newBuilder()
 *     .setOperation(operation.toString())
 *     .build();
 *   zoneOperationClient.deleteZoneOperation(request);
 * }
 * </code></pre>
 *
 * @param request The request object containing all of the parameters for the API call.
 * @throws com.google.api.gax.rpc.ApiException if the remote call fails
 */
@BetaApi
public final void deleteZoneOperation(DeleteZoneOperationHttpRequest request) {
 deleteZoneOperationCallable().call(request);
}

代码示例来源:origin: googleapis/google-cloud-java

/**
 * Retrieves a list of Operation resources contained within the specified zone.
 *
 * <p>Sample code:
 *
 * <pre><code>
 * try (ZoneOperationClient zoneOperationClient = ZoneOperationClient.create()) {
 *   ProjectZoneName zone = ProjectZoneName.of("[PROJECT]", "[ZONE]");
 *   ListZoneOperationsHttpRequest request = ListZoneOperationsHttpRequest.newBuilder()
 *     .setZone(zone.toString())
 *     .build();
 *   for (Operation element : zoneOperationClient.listZoneOperations(request).iterateAll()) {
 *     // doThingsWith(element);
 *   }
 * }
 * </code></pre>
 *
 * @param request The request object containing all of the parameters for the API call.
 * @throws com.google.api.gax.rpc.ApiException if the remote call fails
 */
@BetaApi
public final ListZoneOperationsPagedResponse listZoneOperations(
  ListZoneOperationsHttpRequest request) {
 return listZoneOperationsPagedCallable().call(request);
}

代码示例来源:origin: googleapis/google-cloud-java

/**
 * Deletes the specified zone-specific Operations resource.
 *
 * <p>Sample code:
 *
 * <pre><code>
 * try (ZoneOperationClient zoneOperationClient = ZoneOperationClient.create()) {
 *   ProjectZoneOperationName operation = ProjectZoneOperationName.of("[PROJECT]", "[ZONE]", "[OPERATION]");
 *   zoneOperationClient.deleteZoneOperation(operation);
 * }
 * </code></pre>
 *
 * @param operation Name of the Operations resource to delete.
 * @throws com.google.api.gax.rpc.ApiException if the remote call fails
 */
@BetaApi
public final void deleteZoneOperation(ProjectZoneOperationName operation) {
 DeleteZoneOperationHttpRequest request =
   DeleteZoneOperationHttpRequest.newBuilder()
     .setOperation(operation == null ? null : operation.toString())
     .build();
 deleteZoneOperation(request);
}

代码示例来源:origin: googleapis/google-cloud-java

/**
 * Retrieves the specified zone-specific Operations resource.
 *
 * <p>Sample code:
 *
 * <pre><code>
 * try (ZoneOperationClient zoneOperationClient = ZoneOperationClient.create()) {
 *   ProjectZoneOperationName operation = ProjectZoneOperationName.of("[PROJECT]", "[ZONE]", "[OPERATION]");
 *   Operation response = zoneOperationClient.getZoneOperation(operation);
 * }
 * </code></pre>
 *
 * @param operation Name of the Operations resource to return.
 * @throws com.google.api.gax.rpc.ApiException if the remote call fails
 */
@BetaApi
public final Operation getZoneOperation(ProjectZoneOperationName operation) {
 GetZoneOperationHttpRequest request =
   GetZoneOperationHttpRequest.newBuilder()
     .setOperation(operation == null ? null : operation.toString())
     .build();
 return getZoneOperation(request);
}

代码示例来源:origin: googleapis/google-cloud-java

/**
 * Retrieves a list of Operation resources contained within the specified zone.
 *
 * <p>Sample code:
 *
 * <pre><code>
 * try (ZoneOperationClient zoneOperationClient = ZoneOperationClient.create()) {
 *   ProjectZoneName zone = ProjectZoneName.of("[PROJECT]", "[ZONE]");
 *   for (Operation element : zoneOperationClient.listZoneOperations(zone).iterateAll()) {
 *     // doThingsWith(element);
 *   }
 * }
 * </code></pre>
 *
 * @param zone Name of the zone for request.
 * @throws com.google.api.gax.rpc.ApiException if the remote call fails
 */
@BetaApi
public final ListZoneOperationsPagedResponse listZoneOperations(ProjectZoneName zone) {
 ListZoneOperationsHttpRequest request =
   ListZoneOperationsHttpRequest.newBuilder()
     .setZone(zone == null ? null : zone.toString())
     .build();
 return listZoneOperations(request);
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
@SuppressWarnings("all")
public void deleteZoneOperationTest() {
 mockService.addNullResponse();
 ProjectZoneOperationName operation =
   ProjectZoneOperationName.of("[PROJECT]", "[ZONE]", "[OPERATION]");
 client.deleteZoneOperation(operation);
 List<String> actualRequests = mockService.getRequestPaths();
 Assert.assertEquals(1, actualRequests.size());
 String apiClientHeaderKey =
   mockService
     .getRequestHeaders()
     .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
     .iterator()
     .next();
 Assert.assertTrue(
   GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
     .matcher(apiClientHeaderKey)
     .matches());
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
@SuppressWarnings("all")
public void getZoneOperationExceptionTest() throws Exception {
 ApiException exception =
   ApiExceptionFactory.createException(
     new Exception(), FakeStatusCode.of(Code.INVALID_ARGUMENT), false);
 mockService.addException(exception);
 try {
  ProjectZoneOperationName operation =
    ProjectZoneOperationName.of("[PROJECT]", "[ZONE]", "[OPERATION]");
  client.getZoneOperation(operation);
  Assert.fail("No exception raised");
 } catch (InvalidArgumentException e) {
  // Expected exception
 }
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
 @SuppressWarnings("all")
 public void listZoneOperationsExceptionTest() throws Exception {
  ApiException exception =
    ApiExceptionFactory.createException(
      new Exception(), FakeStatusCode.of(Code.INVALID_ARGUMENT), false);
  mockService.addException(exception);

  try {
   ProjectZoneName zone = ProjectZoneName.of("[PROJECT]", "[ZONE]");

   client.listZoneOperations(zone);
   Assert.fail("No exception raised");
  } catch (InvalidArgumentException e) {
   // Expected exception
  }
 }
}

代码示例来源:origin: googleapis/google-cloud-java

@BeforeClass
public static void setUp() throws IOException {
 clientSettings =
   ZoneOperationSettings.newBuilder()
     .setTransportChannelProvider(
       ZoneOperationSettings.defaultHttpJsonTransportProviderBuilder()
         .setHttpTransport(mockService)
         .build())
     .setCredentialsProvider(NoCredentialsProvider.create())
     .build();
 client = ZoneOperationClient.create(clientSettings);
}

代码示例来源:origin: googleapis/google-cloud-java

/**
 * Constructs an instance of ZoneOperationClient, using the given stub for making calls. This is
 * for advanced usage - prefer to use ZoneOperationSettings}.
 */
@BetaApi("A restructuring of stub classes is planned, so this may break in the future")
public static final ZoneOperationClient create(ZoneOperationStub stub) {
 return new ZoneOperationClient(stub);
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
@SuppressWarnings("all")
public void deleteZoneOperationExceptionTest() throws Exception {
 ApiException exception =
   ApiExceptionFactory.createException(
     new Exception(), FakeStatusCode.of(Code.INVALID_ARGUMENT), false);
 mockService.addException(exception);
 try {
  ProjectZoneOperationName operation =
    ProjectZoneOperationName.of("[PROJECT]", "[ZONE]", "[OPERATION]");
  client.deleteZoneOperation(operation);
  Assert.fail("No exception raised");
 } catch (InvalidArgumentException e) {
  // Expected exception
 }
}

代码示例来源:origin: googleapis/google-cloud-java

ProjectZoneOperationName.of("[PROJECT]", "[ZONE]", "[OPERATION]");
Operation actualResponse = client.getZoneOperation(operation);
Assert.assertEquals(expectedResponse, actualResponse);

代码示例来源:origin: googleapis/google-cloud-java

ListZoneOperationsPagedResponse pagedListResponse = client.listZoneOperations(zone);

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com