com.google.api.services.deploymentmanager.deploymentmanager

pbpqsu0x  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(318)

我希望使用deployment manager自动提供google云资源。只有我想使用javaapi库而不是gcloud。
特别需要使用deploymentmanager提供以下组件。
云sql-mssql
云运行服务
vpc网络,带有用于将云运行连接到云sql的连接器。
我想 com.google.api.services.deploymentmanager.DeploymentManager 这是一个起点,但我不是很确定。
这就是我目前所拥有的

Builder builder = new Builder(
                com.google.api.client.googleapis.javanet.GoogleNetHttpTransport.newTrustedTransport(),
                new com.google.api.client.json.gson.GsonFactory(), null);
        builder.setApplicationName("spatial-cat-319209");
        builder.setDeploymentManagerRequestInitializer(new DeploymentManagerRequestInitializer());
        DeploymentManager deploymentManager = builder.build();

        deploymentManager.deployments().list("spatial-cat-319209").entrySet().stream().forEach(entry -> {
            System.out.println(entry.getKey() + " = " + entry.getValue());
        });

        System.out.println(System.getProperty("GOOGLE_APPLICATION_CREDENTIALS"));
        System.out.println(System.getenv("GOOGLE_APPLICATION_CREDENTIALS"));

        deploymentManager.deployments().list("spatial-cat-319209").execute();

我在最后一行中得到以下错误:

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
GET https://deploymentmanager.googleapis.com/deploymentmanager/v2/projects/spatial-cat-319209/global/deployments
{
  "code": 401,
  "errors": [
    {
      "domain": "global",
      "location": "Authorization",
      "locationType": "header",
      "message": "Login Required.",
      "reason": "required"
    }
  ],
  "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
  "status": "UNAUTHENTICATED"
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:118)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:37)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:428)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1111)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:514)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:455)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:565)

我的环境变量google\u application\u credentials指向一个包含服务帐户密钥的文件(使用 gcloud auth application-default login 指挥部)
任何见解都会非常有用。

rkttyhzu

rkttyhzu1#

好的,看起来我已经用下面的代码完成了。
我已经演示了如何使用processbuilder和deploymentmanager api使用gcloud命令。我必须使用googlecredentials读取服务帐户密钥文件并将其传递给httpcredentialsadapter。
很难找到,但这本指南派上了用场。发布一个链接,作为后来偶然路过的人的参考。
让我知道这是否可以做得更好。

GoogleCredentials googleCredentials = GoogleCredentials
                .fromStream(new FileInputStream(System.getenv("GOOGLE_APPLICATION_CREDENTIALS")));
        HttpRequestInitializer httpRequestInitializer = new HttpCredentialsAdapter(googleCredentials);

        Builder builder = new Builder(
                com.google.api.client.googleapis.javanet.GoogleNetHttpTransport.newTrustedTransport(),
                com.google.api.client.json.gson.GsonFactory.getDefaultInstance(), httpRequestInitializer);
        builder.setApplicationName("spatial-cat-319209");
        builder.setDeploymentManagerRequestInitializer(new DeploymentManagerRequestInitializer());
        DeploymentManager deploymentManager = builder.build();

        deploymentManager.deployments().list("spatial-cat-319209").entrySet().stream().forEach(entry -> {
            System.out.println(entry.getKey() + " = " + entry.getValue());
        });

        System.out.println(System.getProperty("GOOGLE_APPLICATION_CREDENTIALS"));
        System.out.println(System.getenv("GOOGLE_APPLICATION_CREDENTIALS"));
        System.out.println("Listing deployments in project using gcloud command ...");
        // Runtime.getRuntime().exec("gcloud deployment-manager deployments list");
        ProcessBuilder processBuilder = new ProcessBuilder();
        Process process = processBuilder.command("powershell", "gcloud", "deployment-manager", "deployments", "list")
                .start();
        InputStreamReader isr = new InputStreamReader(process.getInputStream());
        BufferedReader rdr = new BufferedReader(isr);
        String line = "";
        while ((line = rdr.readLine()) != null) {
            System.out.println(line);
        }

        isr = new InputStreamReader(process.getErrorStream());
        rdr = new BufferedReader(isr);
        while ((line = rdr.readLine()) != null) {
            System.out.println(line);
        }
        int rc = process.waitFor(); // Wait for the process to complete
        System.out.println("Return Code = " + rc);

        System.out.println("Listing deployments in project using DeploymentManager API...");

        DeploymentsListResponse deploymentsListResponse = deploymentManager.deployments().list("spatial-cat-319209")
                .execute();

        System.out.println("Number of deployments = " + deploymentsListResponse.size());
        deploymentsListResponse.forEach((depl, a) -> System.out.println(depl + " - " + a));

        System.out.println("Done");

相关问题