我目前正在努力通过PowerShell使用Google Cloud Logging API,但是遇到JSON格式的问题。
我当前使用的链接是: https://cloud.google.com/logging/docs/logs-based-metrics/counter-metrics https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics/create
基本上,我正在尝试在Google Cloud项目中创建新的自定义日志记录指标。我知道JSON格式有问题,但是老实说我不知道是什么,所以也许有人可以建议。我的代码如下:
Begin{
$Cred = gcloud auth print-access-token
$Headers = @{ Authorization = "Bearer $Cred" }
$URI = "https://logging.googleapis.com/v2/projects/projectname/metrics"
$Body1 = @{
Name = "snapshot-api-createcluster-fail"
Description = "The API call of createCluster fails whilst taking a snapshot."
Filter = '"resource.type="gce_disk" severity>=ERROR protoPayload.methodName="v1.compute.disks.createSnapshot""'
}
$CreateHostJSON = ConvertTo-Json($Body1)
}
Process{
Try{
Invoke-RestMethod -Uri $URI -Method POST -Body $CreateHostJSON -Headers $Headers -ContentType "application/json"
}
Catch{
Write-Error $_.Exception.Message
$streamReader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream())
$ErrResp = $streamReader.ReadToEnd() | ConvertFrom-Json
$streamReader.Close()
Break
}
}
在检查$ ErrResp.error.message时,我看到以下输出:
接收到无效的JSON有效负载。未知名称“过滤器”位于“指标”:找不到字段。
接收到无效的JSON有效负载。未知名称“描述”位于“度量”:找不到字段。
接收到无效的JSON有效负载。未知名称“名称”位于“度量”:找不到字段。
任何帮助,将不胜感激!
答案 0 :(得分:4)
尝试将$Body1
哈希表键设置为小写:
$Body1 = @{
name = "snapshot-api-createcluster-fail"
description = "The API call of createCluster fails whilst taking a snapshot."
filter = '"resource.type="gce_disk" severity>=ERROR protoPayload.methodName="v1.compute.disks.createSnapshot""'
}