I'm currently struggling to make use of the Google Cloud Logging API with PowerShell, but I'm running into issues with the JSON format.
The links I'm presently using are: https://cloud.google.com/logging/docs/logs-based-metrics/counter-metrics https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics/create
Essentially, I'm trying to create a new custom logging metric within a Google Cloud project. I know there's something wrong with the JSON format, but I honestly don't know what, so perhaps someone here could advise. My code is as follows:
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
}
}
When checking $ErrResp.error.message, I see the following output:
Invalid JSON payload received. Unknown name "Filter" at 'metric': Cannot find field.
Invalid JSON payload received. Unknown name "Description" at 'metric': Cannot find field.
Invalid JSON payload received. Unknown name "Name" at 'metric': Cannot find field.
Any help with this would be greatly appreciated!