在针对Node JS的Google Cloud Compute库中,如何在options.filter
方法中使用getVMs
使用正则表达式进行过滤?
文件:https://googlecloudplatform.github.io/google-cloud-node/#/docs/compute/0.7.1/compute?method=getVMs
我在下面的尝试返回一个空数组,文档中没有明确的例子(然而!)。我的目标是获得以“prefix”作为名称的一个实例。
有101个实例,这是第101个。
compute.getVMs({
maxResults: 100,
filter: 'name eq ^prefix'
});
搜索过滤器的格式为{name} {comparison} {filterString}。 name:要比较的字段的名称比较:比较运算符,eq(等于)或ne(不等于)filterString:要过滤的字符串。对于字符串字段,这可以是正则表达式。
您在模式的末尾缺少“。$”。基本上你需要指定你想要“匹配以'前缀'开头的字符串,后跟''前缀'之后的任意数量的字符,直到整个字符串的结尾”。你可以抑制'整个字符串的结尾'特殊字符($),但你不能抑制'匹配任意数量的字符'特殊字符(。)。这是一个工作示例:
const Compute = require('@google-cloud/compute');
const gce = new Compute({
projectId: 'your-project-id-here'
});
gce.getVMs({
maxResults: 100,
filter: 'name eq ^prefix.*'
}, function(err, vms) {
console.log(vms);
});