API GCP Cloud Asset BatchAssetHistoryRequest возвращает пустой ответ - PullRequest
0 голосов
/ 20 января 2019

Я пытаюсь использовать API облачных активов GCP и получить историю для одной из моих виртуальных машин. Я пытался использовать BatchGetAssetsHistoryRequest как в API Explorer, так и с использованием кода Java, но ответ всегда был пустым. Я просто не знаю, есть ли проблема с тем, как я называю этот API, или Asset API вообще не может быть использован для этой цели.

Я включил API Cloud Asset API, и у меня не возникает проблем с аутентификацией. Ресурс, который я пытаюсь запросить, также существует

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; 
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.gax.grpc.GrpcCallContext;
import com.google.api.gax.rpc.*;
import com.google.api.services.cloudbilling.Cloudbilling;
import com.google.api.services.cloudbilling.model.ListProjectBillingInfoResponse;
import com.google.api.services.cloudbilling.model.ProjectBillingInfo;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.OAuth2Credentials;

import com.google.cloud.asset.v1beta1.*;

import com.google.cloud.asset.v1beta1.stub.AssetServiceStubSettings;
import com.google.protobuf.Timestamp;
import com.google.protobuf.Value;


import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.*;
import java.util.concurrent.ScheduledThreadPoolExecutor;


public class AssetExample {
public static void main(String args[]) throws IOException, 
    GeneralSecurityException {
    // Project ID for this request.
     String project = "mohanh200"; // TODO: Update placeholder value.
     String bucketResourceName = "//compute.googleapis.com/projects/" + project + "/zones/us-central1-c/instances/instance-1";

    Timestamp startTime = 
    Timestamp.newBuilder().setSeconds((System.currentTimeMillis() - 86400L 
  * 2 * 1000 * 30) / 1000).build();

    Timestamp endTime = Timestamp.newBuilder().setSeconds(System.currentTimeMillis() / 1000).build();

    TimeWindow timeWindow = TimeWindow.newBuilder().setStartTime(startTime.toBuilder()).setStartTime(startTime).
            setEndTime(endTime).build();


    try (AssetServiceClient assetServiceClient = createAssetService()) {
        ProjectName parent1 = ProjectName.of("mohanh200");
        ContentType contentType = ContentType.RESOURCE;


        BatchGetAssetsHistoryRequest request = BatchGetAssetsHistoryRequest.newBuilder()
                .setParent(parent1.toString())
                .setContentType(contentType)
                .addAssetNames(bucketResourceName).
                        setReadTimeWindow(timeWindow)
                .build();

        BatchGetAssetsHistoryResponse response = assetServiceClient.batchGetAssetsHistory(request);
        for (int i = 0; i < response.getAssetsList().size(); ++i) {
            TemporalAsset asset = response.getAssets(i);
            if (asset != null) {
                Asset asset1 = asset.getAsset();
                if (asset1.getResource().hasData() && asset1.getResource().getData().getFieldsMap().keySet().contains("machineType")) {
                    Value machineType = asset1.getResource().getData().getFieldsMap().
                            get("machineType");
                    String machineType1 = machineType.getStringValue();
                    System.out.println(machineType1);

                }


            }

        }
        System.out.println("Testing");
    }
}

BatchAssetsHistoryResponse всегда пусто независимо от установленного времени запуска.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...