使用 Java 暂停 Google 云平台实例

Suspend Google Cloud Platform instance using Java

我有一个 Java Web 应用程序,我在其中创建 GCP VM 实例并对其执行操作。

我使用 Java Compute Engine API 来执行创建、启动、停止等任务

我想表演:

Compute compute = getComputeObj();
Suspend suspend = compute.instances().suspend(getProjectId(), getOrder().getAvailabilityZone(), getOrder().getMachineName());
Operation response = suspend .execute();

但是,我无法使用此 API“暂停”一台机器,因为“暂停”处于测试版(它不是 Compute Engine v1 API 的一部分,因此它不是Java 包装器)。

据我所知,我有两个选项可以解决这个问题,但我都无法完成:

  1. 创建一个继承Computeclass的Javaclass“ComputeBeta”并实现一个Suspendclass来执行操作。这是有问题的,因为原始 Compute class 中的 URL 字段是最终字段,所以我无法将 URL 从“v1”更改为“beta”url.

  2. 执行到 GCP beta API 的“常规”http 连接以暂停机器。这里的问题是我无法找到 API 的正确身份验证语法,并且收到了 401 响应。到目前为止我尝试了什么:

String serviceAccountKey = getServiceAccountKey();

String baseUrl = "https://compute.googleapis.com";

String endpoint = "/compute/beta/projects/" + getOrder().getProject() 
                + "/zones/" + getOrder().getAvailabilityZone() + "/instances/" 
                + getOrder().getMachineName() + "/suspend";

URL serverUrl = new URL(baseUrl + endpoint);

httpConnection = (HttpsURLConnection)serverUrl.openConnection();
httpConnection.setRequestMethod(HttpMethods.POST);
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);


httpConnection.setFixedLengthStreamingMode(length);

httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
httpConnection.setRequestProperty("Authorization", "Bearer " + serviceAccountKey);
httpConnection.connect();

int responseCode = httpConnection.getResponseCode();
String responseMessage = httpConnection.getResponseMessage();

我们将不胜感激任何关于如何前进的帮助!

您不必像这样使用您的服务帐户密钥。此密钥允许您创建凭据,然后用它生成令牌。

 GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
// You can use explicitly your service account key file like this
// GoogleCredentials credentials = GoogleCredentials.fromStream(this.getClass().getResourceAsStream("my_key_file.json"));

 String token = credentials.refreshAccessToken().getTokenValue();
...
 httpConnection.setRequestProperty("Authorization", "Bearer " + token);

你也可以使用Google传输工厂来避免手动添加授权header

 GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
// You can use explicitly your service account key file like this
// GoogleCredentials credentials = GoogleCredentials.fromStream(this.getClass().getResourceAsStream("my_key_file.json"));

HttpRequestFactory factory = new NetHttpTransport().createRequestFactory(new HttpCredentialsAdapter(credentials));
// Here a GET request, but you can build a POST request also
HttpRequest request = factory.buildGetRequest(new GenericUrl("YOUR_URL"));
HttpResponse httpResponse = request.execute();
System.out.println(CharStreams.toString(new InputStreamReader(httpResponse.getContent(), Charsets.UTF_8)));

如果你有依赖问题(通常不是因为你使用了包含它的计算引擎库),你可以添加这个依赖

        <dependency>
            <groupId>com.google.auth</groupId>
            <artifactId>google-auth-library-oauth2-http</artifactId>
            <version>0.21.1</version>
        </dependency>