Skip to content

Commit 326e07f

Browse files
Showing error details for quota exceeded error for gce operations (#8030) (#14879)
* 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 <[email protected]>
1 parent 2f105f1 commit 326e07f

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

.changelog/8030.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: surface error details for Compute Operation with quota exceeded errors.
3+
```

google/services/compute/compute_operation.go

+8
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ func writeOperationError(w io.StringWriter, opError *compute.OperationErrorError
140140
var link *compute.HelpLink
141141

142142
for _, ed := range opError.ErrorDetails {
143+
if opError.Code == "QUOTA_EXCEEDED" && ed.QuotaInfo != nil {
144+
w.WriteString("\tmetric name = " + ed.QuotaInfo.MetricName + "\n")
145+
w.WriteString("\tlimit name = " + ed.QuotaInfo.LimitName + "\n")
146+
if ed.QuotaInfo.Dimensions != nil {
147+
w.WriteString("\tdimensions = " + fmt.Sprint(ed.QuotaInfo.Dimensions) + "\n")
148+
}
149+
break
150+
}
143151
if lm == nil && ed.LocalizedMessage != nil {
144152
lm = ed.LocalizedMessage
145153
}

google/services/compute/compute_operation_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const (
1515
localizedMsgTmpl = "LocalizedMessage%d message"
1616
helpLinkDescriptionTmpl = "Help%dLink%d Description"
1717
helpLinkUrlTmpl = "https://help%d.com/link%d"
18+
quotaExceededMsg = "Quota DISKS_TOTAL_GB exceeded. Limit: 1100.0 in region us-central1."
19+
quotaExceededCode = "QUOTA_EXCEEDED"
20+
quotaMetricName = "compute.googleapis.com/disks_total_storage"
21+
quotaLimitName = "DISKS-TOTAL-GB-per-project-region"
1822
)
1923

2024
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
5357

5458
}
5559

60+
func buildOperationErrorQuotaExceeded(withDetails bool, withDimensions bool) compute.OperationError {
61+
opError := &compute.OperationErrorErrors{Message: quotaExceededMsg, Code: quotaExceededCode}
62+
opErrorErrors := []*compute.OperationErrorErrors{opError}
63+
if withDetails {
64+
quotaInfo := &compute.QuotaExceededInfo{
65+
MetricName: quotaMetricName,
66+
LimitName: quotaLimitName,
67+
Limit: 1100,
68+
}
69+
if withDimensions {
70+
quotaInfo.Dimensions = map[string]string{"region": "us-central1"}
71+
}
72+
opError.ErrorDetails = append(opError.ErrorDetails,
73+
&compute.OperationErrorErrorsErrorDetails{
74+
QuotaInfo: quotaInfo,
75+
})
76+
}
77+
78+
return compute.OperationError{Errors: opErrorErrors}
79+
}
80+
5681
func omitAlways(numLocalizedMsg int, numHelpWithLinks []int) []string {
5782
var omits []string
5883

@@ -180,6 +205,43 @@ func TestComputeOperationError_Error(t *testing.T) {
180205
},
181206
expectOmits: append(omitAlways(2, []int{1}), []string{}...),
182207
},
208+
{
209+
name: "QuotaMessageOnly",
210+
input: buildOperationErrorQuotaExceeded(false, false),
211+
expectContains: []string{
212+
"Quota DISKS_TOTAL_GB exceeded. Limit: 1100.0 in region us-central1.",
213+
},
214+
expectOmits: append(omitAlways(0, []int{}), []string{
215+
"metric name = compute.googleapis.com/disks_total_storage",
216+
}...),
217+
},
218+
{
219+
name: "QuotaMessageWithDetailsNoDimensions",
220+
input: buildOperationErrorQuotaExceeded(true, false),
221+
expectContains: []string{
222+
"Quota DISKS_TOTAL_GB exceeded. Limit: 1100.0 in region us-central1.",
223+
"metric name = compute.googleapis.com/disks_total_storage",
224+
"limit name = DISKS-TOTAL-GB-per-project-region",
225+
},
226+
expectOmits: append(omitAlways(0, []int{}), []string{
227+
"dimensions = map[region:us-central1]",
228+
}...),
229+
},
230+
{
231+
name: "QuotaMessageWithDetailsWithDimensions",
232+
input: buildOperationErrorQuotaExceeded(true, true),
233+
expectContains: []string{
234+
"Quota DISKS_TOTAL_GB exceeded. Limit: 1100.0 in region us-central1.",
235+
"metric name = compute.googleapis.com/disks_total_storage",
236+
"limit name = DISKS-TOTAL-GB-per-project-region",
237+
"dimensions = map[region:us-central1]",
238+
},
239+
expectOmits: append(omitAlways(0, []int{}), []string{
240+
"LocalizedMessage1",
241+
"Help1Link1 Description",
242+
"https://help1.com/link1",
243+
}...),
244+
},
183245
}
184246

185247
for _, tc := range testCases {

0 commit comments

Comments
 (0)