我正在尝试使用GCP的Cloud Asset API并获取其中一台VM的历史记录。我尝试在API Explorer和Java代码上都使用BatchGetAssetsHistoryRequest,但是响应始终为空。我只是不知道我调用此API的方式是否有问题,或者Asset 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始终为空。