0

我有一个 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“暂停”机器,因为“暂停”处于 beta 版本(它不是 Compute Engine v1 API 的一部分,因此它不是 Java 包装器的一部分)。

正如我所看到的,我有两种选择来解决这个问题,但我都无法完成:

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

  2. 执行到 GCP beta API 的“常规”httpconnection 以暂停机器。这里的问题是我无法找到 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();

任何有关如何前进的帮助将不胜感激!

4

1 回答 1

2

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

 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 Transport 工厂来避免手动添加授权标头

 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>
于 2020-09-30T08:30:17.520 回答