【发布时间】:2021-07-26 12:54:31
【问题描述】:
我正在尝试通过过滤 gcloud 输出中的对等互连名称来检查 VPC 对等互连是否存在,但无法完全让 gcloud 命令与我的过滤器一起使用......我想要做的是这个:
gcloud compute networks peerings list --project=<project> --flatten="peerings" --filter="peerings.name:<the_peering_I_want_to_check_for>"
或gcloud compute networks peerings list --project=<project> --flatten="peerings[].name" --filter="peerings.name:<the_peering_I_want_to_check_for>"
但这两个都返回错误:
Invalid value for field 'filter' ..... Invalid list filter expression
一直在关注here 上的建议 - 在自定义列表上进行过滤以响应,其中讨论了嵌套资源的过滤
这是一个输出示例,我想从中检索 peerings.name
gcloud compute networks peerings list --project=<project> --format="flattened(peerings)"
---
peerings[0].importCustomRoutes: False
peerings[0].name: the_peering_I_want_to_check_for
peerings[1].importCustomRoutes: False
peerings[1].name: Antother_peering_that_i_don't_care_about_but_not_in_a_mean_way
谢谢!
【问题讨论】:
-
或者你可以使用
gcloud --format=json和jq -
@mbha-phoenix 的建议很好。
--format=JSON然后使用jq提供了一种格式化和过滤 JSON 的通用方法 -
gcloud一般确实提供了足够的功能,因此如果您不想这样做,则无需使用jq(请参阅其他评论)。但是,IMO,这些gcloud命令具有挑战性。当您使用--flatten时,您通常会(!)想要提供整个列表,即--flatten=peerings[]以展平列表元素。这可能是您在第一个示例中需要更改的全部内容。 -
非常感谢 cmets.. 我可以通过 jq 来完成,甚至可以通过管道连接到 grep,但希望了解如何自行使用 gcloud 的过滤器选项, ,无论我使用什么(即使按照建议使用
flatten=peerings[],它也会给出相同的错误:ERROR: (gcloud.compute.networks.peerings.list) HTTPError 400: Invalid value for field 'filter': '[].peerings[].name eq ".*\b<the_peering_I_want_to_check_for>\b.*"'. Invalid list filter expression.
标签: google-cloud-platform gcloud