【发布时间】:2019-01-22 21:15:13
【问题描述】:
假设我想使用gcloud 删除我的一些 GCP 项目资源。如果我有他们名字的记录,我可以在一个 bash/node/python 脚本中将它们全部删除。问题是我需要能够区分“OK”错误和那些不是。例如,如果我删除了一个不存在的资源,gcloud 会报告错误,并且我的代码没有可靠的方法来确定这是 404。在这种情况下,404 是好的。我希望资源消失,它消失了。 我如何可靠地确定 gcloud 发出的错误类型?
我查看了--log-http,虽然它确实输出了 http 错误响应正文,但我假设没有未来的证明方法可以将其解析出来。
gcloud compute instance-templates delete trash -q --log-http
...
== body start ==
...
{
"error": {
"errors": [
{
"domain": "global",
"reason": "notFound",
"message": "The resource 'projects/mine/global/instanceTemplates/trash' was not found"
}
],
"code": 404,
"message": "The resource 'projects/mine/global/instanceTemplates/trash' was not found"
}
}
----------------------
ERROR: (gcloud.compute.instance-templates.delete) Could not fetch resource:
- The resource 'projects/mine/global/instanceTemplates/trash' was not found
我还尝试在我的 gcloud 配置中设置 show_structured_logs 属性。这产生的输出仍然不够详细。
gcloud config set show_structured_logs always
gcloud compute instance-templates delete trash -q
{
"version": "0.0.1",
"verbosity": "ERROR",
"timestamp": "2018-11-12T07:00:51.505Z",
"message": "(gcloud.compute.instance-templates.delete) Could not fetch resource:\n - The resource 'projects/mine/global/instanceTemplates/trash' was not found\n"
}
我目前的解决方案是在输出中查找关键短语。对于这个例子,我会寻找was not found。这可行,但它很hacky,并且不可靠,正如here所证实的那样。
【问题讨论】:
标签: google-cloud-platform gcloud