基于cloud-init标识筛选镜像列表及SoftLayer Java SDK公共镜像筛选指引
Hey there, let's walk through how to tackle both of these tasks with clear, actionable steps:
The approach varies slightly depending on the tool you're using, but the core logic is targeting images tagged with cloud-init. Here are common methods:
SoftLayer CLI Method
If you're using the SoftLayer command-line tool, you can directly filter images by tag with the--tagflag:# 筛选公共镜像中带有cloud-init标记的 slcli image list --public --tag cloud-init # 筛选私有镜像中带有cloud-init标记的,去掉--public参数即可 slcli image list --tag cloud-initREST API Method
When calling the SoftLayer REST API, construct anobjectFilterto target the tag. Send a GET request tohttps://api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest_Block_Device_Template_Group/getAllObjectswith this filter in the request parameters:{ "objectFilter": { "tagReferences": { "tag": { "name": { "operation": "cloud-init" } } } } }Note: Add
"isPublic": {"operation": true}to the filter if you only want to see public images.
First, add the SoftLayer Java SDK to your project. For Maven, include this dependency in your pom.xml:
<dependency> <groupId>com.softlayer.api</groupId> <artifactId>softlayer-api-client</artifactId> <version>0.3.4</version> </dependency>
Then use this code snippet to filter images by both tag and creation date:
import com.softlayer.api.ApiClient; import com.softlayer.api.RestApiClient; import com.softlayer.api.service.virtual.guest.block.device.template.group.VirtualGuestBlockDeviceTemplateGroup; import com.softlayer.api.filter.FilterOperator; import com.softlayer.api.filter.ObjectFilter; import com.softlayer.api.filter.ObjectSort; import java.util.List; public class CloudInitImageFilter { public static void main(String[] args) { // 初始化API客户端,替换为你的SoftLayer用户名和API密钥 ApiClient client = new RestApiClient().withCredentials("YOUR_USERNAME", "YOUR_API_KEY"); VirtualGuestBlockDeviceTemplateGroup.Service imageService = VirtualGuestBlockDeviceTemplateGroup.service(client); // 构建过滤条件 ObjectFilter filter = new ObjectFilter(); // 筛选公共镜像 filter.append("isPublic", FilterOperator.EQUALS, true); // 筛选带有cloud-init标记的镜像 filter.append("tagReferences.tag.name", FilterOperator.EQUALS, "cloud-init"); // 筛选创建时间在2024年1月1日之后的镜像,可根据需求调整日期 filter.append("createDate", FilterOperator.GREATER_THAN_OR_EQUAL, "2024-01-01T00:00:00Z"); // 按创建时间降序排序,最新镜像优先 imageService.setObjectSort(new ObjectSort().append("createDate", ObjectSort.Order.DESC)); imageService.setObjectFilter(filter); try { // 获取符合条件的镜像列表 List<VirtualGuestBlockDeviceTemplateGroup> images = imageService.getAllObjects(); // 遍历输出结果 for (VirtualGuestBlockDeviceTemplateGroup image : images) { System.out.println("镜像名称: " + image.getName()); System.out.println("创建时间: " + image.getCreateDate()); System.out.println("镜像ID: " + image.getId()); System.out.println("---------------------------"); } } catch (Exception e) { e.printStackTrace(); } } }
Key Notes:
- Double-check the tag name: Some images might use variants like
cloud_initinstead ofcloud-init—run a basic list request first to confirm the exact tag if you get no results. - Adjust the date filter: Use
LESS_THAN_OR_EQUALto target older images, or add a second date condition to create a range. - For private images: Remove the
isPublicfilter line from the code.
内容的提问来源于stack exchange,提问作者Malyadri

