如何识别脚本中的gcloud错误 [英] how to identify gcloud errors in scripts
问题描述
假设我想使用gcloud
删除一些我的GCP项目资源.如果我记录了它们的名称,则可以在一个bash/node/python脚本中将其全部删除.问题是我需要能够将确定"错误与非确定错误区分开.例如,如果我删除了不存在的资源,则gcloud报告错误,并且我的代码没有可靠的方法来确定这是404.在这种情况下,404是好的.我希望资源消失了,资源也消失了. 如何可靠地确定gcloud发出的错误类型?
Suppose I want to delete some of my GCP project resources using gcloud
. If I have a record of their names, I can delete them all in a single bash/node/python script. The problem is I need to be able to distinguish "OK" errors from those that aren't. For example, if I delete a resource that doesn't exist, gcloud reports an error and my code has no reliable way of determining this was a 404. In this case a 404 is good. I wanted the resource to be gone and it's gone. How do I reliably determine the kind of error gcloud emits?
我研究了 --log-http
,尽管它确实输出了http错误响应正文,我假设没有未来的证明方法可以将其解析出来.
I've looked into --log-http
and while this does output the http error response body, I'm assuming there is no future proof way to parse it out.
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
我还尝试设置 show_structured_logs
属性在我的gcloud配置中.产生的输出仍然不够详细.
I've also tried setting the show_structured_logs
property in my gcloud config. The output this yields is still not detailed enough.
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
. .
My current solution is to just look for key phrases in the output. For this example, I'd look for was not found
. This works but its hacky and is not reliable as confirmed here.
推荐答案
在删除任何内容并从列表中删除一项之前,获取资源列表.这样您将确定它存在
Get a list of your resources before deleting anything and delete an item from the list. This way you will know for sure it exists
您可以解析此命令的输出:
You can parse the output of this command:
gcloud compute instance-templates list
这篇关于如何识别脚本中的gcloud错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!