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

2019-09-13 10:29发布

问题:

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?

回答1:

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



回答2:

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