From b22d78915cd064aba374d549b58cc143426974e5 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Mon, 12 Jun 2023 10:13:08 +0000 Subject: [PATCH] Showing error details for quota exceeded error for gce operations (#8030) * Showing error details for quota exceeded error for gce operations * Modify the compute_operation to show quota exceeded error details Signed-off-by: Modular Magician --- .changelog/8030.txt | 3 + google/services/compute/compute_operation.go | 8 +++ .../compute/compute_operation_test.go | 62 +++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 .changelog/8030.txt diff --git a/.changelog/8030.txt b/.changelog/8030.txt new file mode 100644 index 00000000000..737235060cd --- /dev/null +++ b/.changelog/8030.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +compute: surface error details for Compute Operation with quota exceeded errors. +``` diff --git a/google/services/compute/compute_operation.go b/google/services/compute/compute_operation.go index 82e54c43515..653368e885c 100644 --- a/google/services/compute/compute_operation.go +++ b/google/services/compute/compute_operation.go @@ -140,6 +140,14 @@ func writeOperationError(w io.StringWriter, opError *compute.OperationErrorError var link *compute.HelpLink for _, ed := range opError.ErrorDetails { + if opError.Code == "QUOTA_EXCEEDED" && ed.QuotaInfo != nil { + w.WriteString("\tmetric name = " + ed.QuotaInfo.MetricName + "\n") + w.WriteString("\tlimit name = " + ed.QuotaInfo.LimitName + "\n") + if ed.QuotaInfo.Dimensions != nil { + w.WriteString("\tdimensions = " + fmt.Sprint(ed.QuotaInfo.Dimensions) + "\n") + } + break + } if lm == nil && ed.LocalizedMessage != nil { lm = ed.LocalizedMessage } diff --git a/google/services/compute/compute_operation_test.go b/google/services/compute/compute_operation_test.go index 420cf1306bc..c55ab571562 100644 --- a/google/services/compute/compute_operation_test.go +++ b/google/services/compute/compute_operation_test.go @@ -15,6 +15,10 @@ const ( localizedMsgTmpl = "LocalizedMessage%d message" helpLinkDescriptionTmpl = "Help%dLink%d Description" helpLinkUrlTmpl = "https://help%d.com/link%d" + quotaExceededMsg = "Quota DISKS_TOTAL_GB exceeded. Limit: 1100.0 in region us-central1." + quotaExceededCode = "QUOTA_EXCEEDED" + quotaMetricName = "compute.googleapis.com/disks_total_storage" + quotaLimitName = "DISKS-TOTAL-GB-per-project-region" ) var locales = []string{"en-US", "es-US", "es-ES", "es-MX", "de-DE"} @@ -53,6 +57,27 @@ func buildOperationError(numLocalizedMsg int, numHelpWithLinks []int) compute.Op } +func buildOperationErrorQuotaExceeded(withDetails bool, withDimensions bool) compute.OperationError { + opError := &compute.OperationErrorErrors{Message: quotaExceededMsg, Code: quotaExceededCode} + opErrorErrors := []*compute.OperationErrorErrors{opError} + if withDetails { + quotaInfo := &compute.QuotaExceededInfo{ + MetricName: quotaMetricName, + LimitName: quotaLimitName, + Limit: 1100, + } + if withDimensions { + quotaInfo.Dimensions = map[string]string{"region": "us-central1"} + } + opError.ErrorDetails = append(opError.ErrorDetails, + &compute.OperationErrorErrorsErrorDetails{ + QuotaInfo: quotaInfo, + }) + } + + return compute.OperationError{Errors: opErrorErrors} +} + func omitAlways(numLocalizedMsg int, numHelpWithLinks []int) []string { var omits []string @@ -180,6 +205,43 @@ func TestComputeOperationError_Error(t *testing.T) { }, expectOmits: append(omitAlways(2, []int{1}), []string{}...), }, + { + name: "QuotaMessageOnly", + input: buildOperationErrorQuotaExceeded(false, false), + expectContains: []string{ + "Quota DISKS_TOTAL_GB exceeded. Limit: 1100.0 in region us-central1.", + }, + expectOmits: append(omitAlways(0, []int{}), []string{ + "metric name = compute.googleapis.com/disks_total_storage", + }...), + }, + { + name: "QuotaMessageWithDetailsNoDimensions", + input: buildOperationErrorQuotaExceeded(true, false), + expectContains: []string{ + "Quota DISKS_TOTAL_GB exceeded. Limit: 1100.0 in region us-central1.", + "metric name = compute.googleapis.com/disks_total_storage", + "limit name = DISKS-TOTAL-GB-per-project-region", + }, + expectOmits: append(omitAlways(0, []int{}), []string{ + "dimensions = map[region:us-central1]", + }...), + }, + { + name: "QuotaMessageWithDetailsWithDimensions", + input: buildOperationErrorQuotaExceeded(true, true), + expectContains: []string{ + "Quota DISKS_TOTAL_GB exceeded. Limit: 1100.0 in region us-central1.", + "metric name = compute.googleapis.com/disks_total_storage", + "limit name = DISKS-TOTAL-GB-per-project-region", + "dimensions = map[region:us-central1]", + }, + expectOmits: append(omitAlways(0, []int{}), []string{ + "LocalizedMessage1", + "Help1Link1 Description", + "https://help1.com/link1", + }...), + }, } for _, tc := range testCases {