ibm-cloud-infrastructure - 在 Guest.Service.createArchiveTransaction 之后获取图像模板的 id
问题描述
使用 Java-Library (com.softlayer.api) 我尝试从 VM 创建一个新的图像模板。创建工作,但不幸的是,我还不能知道图像是什么时候创建的,它的标识符是什么。
我使用 Guest.Service.createArchiveTransaction,它返回一个 Transaction 对象。但我知道这与它有什么关系?我检查了它的内容,但找不到对正在创建的图像模板的引用。
你能告诉我,我怎么能得到这些信息?
下面是我的代码:
Guest.Service service = Guest.service(softlayerAdapter.getRestApiClient(), globalIdentifier);
List<Device> devices = service.getBlockDevices()
.stream().filter(device -> {
Image.Service imageService = Image
.service(softlayerAdapter.getRestApiClient(), device.getDiskImageId());
return !imageService.getMetadataFlag() && !imageService.getLocalDiskFlag();
}).collect(Collectors.toList());
Transaction transaction = service.createArchiveTransaction(imageName, devices, notes);
// How do I know when the order is done and what is the identifier for the image?
提前感谢您的支持。
问候, 马蒂亚斯
解决方案
您可以使用该SoftLayer_Virtual_Guest::getActiveTransaction
方法来了解事务是否仍处于活动状态,它在图像创建完成时返回空值,该方法还返回“averageDuration”详细信息,您可以使用它来估计您需要等待的时间:
Guest.Service service = Guest.service(client, virtualGuestId);
try {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Transaction transaction = service.getActiveTransaction();
String json = gson.toJson(transaction);
System.out.println(json);
} catch (Exception e) {
System.out.println("Unable to retrieve transaction. "
+ e.getMessage());
}
您可以使用从中检索到的事务 IDcreateArchiveTransaction
或您提供getActiveTransaction
的图像名称来查找正在创建的图像模板。
该方法SoftLayer_Account::getPrivateBlockDeviceTemplateGroups
有助于检索所有私有图像,不幸的是 Java 客户端不支持对象过滤器,因此您必须检索所有图像模板并使用 java 本身对其进行过滤。
要查看图像上的活动事务,请使用以下掩码调用 getPrivateBlockDeviceTemplateGroups:
mask[id,children[id,transactionId],name]
它将返回如下结构:
{
"id": 2029237,
"name": "ImageName01",
"children": [
{
"id": 2020209,
"transactionId": 95642323
}
]
},
您需要将 transactionId 值与从createArchiveTransaction
或检索到的值匹配getActiveTransaction
Java 示例
此示例从帐户中检索所有私有图像并查找具有相同 transactionId 的图像。我初始化了变量 transactionId,但您可以改进代码并从上面的注释中检索此createArchiveTransaction
值getActiveTransaction
。
考虑到一旦事务完成,transactionId 将为 null,因此您将无法再通过此值将 VSI 与图像相关联。
ApiClient client = new RestApiClient().withCredentials(username, apiKey);
Account.Service service = Account.service(client);
Long transactionId = new Long(96385227);
service.withMask().blockDeviceTemplateGroups().children();
try{
List<Group> images = service.getObject().getBlockDeviceTemplateGroups();
Gson gson = new Gson();
for( Group img : images){
for(Group child : img.getChildren()){
if(child.getTransactionId() != null && child.getTransactionId().equals(transactionId)){
System.out.println("Image Id: " + child.getId());
System.out.println("Image Name: " + child.getName());
System.out.println("Image Transaction: " + child.getTransactionId());
}
}
}
}catch (Exception e){
System.out.println("Error: " + e);
}
参考:
https://softlayer.github.io/reference/services/SoftLayer_Virtual_Guest/getActiveTransaction/
推荐阅读
- android - onRangeStart() 在阅读段落时停止获取回调(遇到标点符号后)
- cors - Access-Control-Allow-Methods 没有停止 POST 调用
- python - 无法通过 API 响应排序
- python - 用于返回 jitclass 的函数的 Numba jit() 函数签名
- javascript - 如何在window.open javascript中将自定义对象作为参数传递
- django - 如何在django模板中显示其他地方的图像
- vue.js - v-mask 将电话号码屏蔽为巴西格式
- autodesk-forge - VS Code 的 Forge 扩展
- mysql - SQL noob 尝试使用连接进行更新查询
- python - 抓取 HTML 表格。所有类都有相同的名称,但内容不同。如何抓取内容?