首页 > 解决方案 > 在 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?

提前感谢您的支持。

问候, 马蒂亚斯

标签: ibm-cloud-infrastructure

解决方案


您可以使用该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,但您可以改进代码并从上面的注释中检索此createArchiveTransactiongetActiveTransaction

考虑到一旦事务完成,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_Account/getPrivateBlockDeviceTemplateGroups/

https://softlayer.github.io/reference/services/SoftLayer_Virtual_Guest/getActiveTransaction/


推荐阅读