【问题标题】:Google Cloud Platform - How to filter instances.list by tag?Google Cloud Platform - 如何按标签过滤 instance.list?
【发布时间】:2018-11-21 17:12:58
【问题描述】:

我正在尝试按标签名称过滤实例列表

什么与 gcloud 完美配合,例如

gcloud compute instances list --filter 'tags.items=firewall-client-mongodb'

不适用于始终返回 400 的 API:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalid",
    "message": "Invalid value for field 'filter': 'tags.items = test'. Invalid list filter expression."
   }
  ],
  "code": 400,
  "message": "Invalid value for field 'filter': 'tags.items = test'. Invalid list filter expression."
 }
}

有没有办法按标签或任何用户定义的变量过滤实例?我正在测试 https://cloud.google.com/compute/docs/reference/rest/v1/instances/list

【问题讨论】:

标签: google-compute-engine google-apis-explorer


【解决方案1】:

从今天开始,以下命令应该现在可以工作了:

gcloud compute instances list \
--filter="tags.items=${TAG}" \
--project=${PROJECT}

#=>

NAME           ZONE        MACHINE_TYPE
my-machine     us-west1-c  f1-micro

和:

gcloud compute instances list \
--project=${PROJECT} \
--zone=us-west1-c

#=>

NAME           ZONE        MACHINE_TYPE
my-machine     us-west1-c  f1-micro

奇怪的是,它确实似乎与两个 --filter--zones 标志正常工作:

gcloud compute instances list \
--filter="tags.items=${TAG}" \
--project=${PROJECT} \
--zones=us-west1-c

#=>

ERROR: (gcloud.compute.instances.list) Some requests did not succeed:
 - Invalid value for field 'filter': 'tags.items eq ".*\b${TAG}\b.*"'. Invalid list filter expression.

底层 API 调用需要提供zone;如果使用 --filter 标志,此命令将总是失败。

此问题已提交here,如果您想查看其状态。

【讨论】:

    【解决方案2】:

    我能够重现该问题并遇到与您相同的错误。这似乎是 API 的问题。 我建议您使用此 link 为问题跟踪器创建新的 Compute Engine 问题。

    【讨论】:

      【解决方案3】:

      似乎gcloud --filter 实现被破坏了。尝试在括号中引用您的搜索模式:

      gcloud ... --filter='term=(item-1 item-2)'
      

      破折号 (-) 可能会破坏 --format ((

      官方文档广泛地将值括在括号中:

      喜欢:

      列出带有 my-tag 或 my-other-tag 标签的 Compute Engine 实例资源:

      gcloud compute instances list  --filter="tags.items=(my-tag,my-other-tag)"
      

      【讨论】:

      • 谢谢你 - 我可以确认这也适用于具有多个标签的容器图像:gcloud container images list-tags us.gcr.io/my-registry/my-image --filter='tags=(671)' --format=json 有效,而 filter='tags.items=671' 无效。
      猜你喜欢
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多