How to get SoftLayer virtual guest by tags, what&#

2019-09-13 10:21发布

I can get virtual guests by masks like "id", "hostname", "primaryIpAddress" etc. However, I need to get virtual guests with specific tags. What's the mask to use to retrieve tagged virtual guests?

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-09-13 10:35

you need to use Objectfilter:

https://sldn.softlayer.com/article/object-filters

see this example using RESTFul request

URL : https://$USERNAME:$APIKEY@api.softlayer.com/rest/v3/SoftLayer_Account/getVirtualGuests?objectFilter={"virtualGuests":{"tagReferences":{"tag":{"name":{"operation":"in","options":[{"name":"data","value":["$tag1","$tag2"]}]}}}}}

Method: GET

Note: replace $tag1 and $tag2 with the tags you wish 

Regards

查看更多
Bombasti
3楼-- · 2019-09-13 10:46

I got some help. Here is the Ruby code to do it:

client = SoftLayer::Client.new(username: USERNAME, api_key: API_KEY)
account_service = client['SoftLayer_Account']
object_filter = SoftLayer::ObjectFilter.new
object_filter.set_criteria_for_key_path(
'virtualGuests.tagReferences.tag.name', 'operation' => 'in',
'options' => [{
'name' => 'data',
'value' => tagsToFind
}])
mask = 'mask[tagReferences[tag[name]]]'
result = account_service.object_mask(mask).
object_filter(object_filter).getVirtualGuests
查看更多
登录 后发表回答