Skip to content

Commit 0e8557b

Browse files
authored
Merge pull request #389 from SumoLogic/ayiu_SUMO-194738_fgp_api_not_enabled
SUMO-194738: changing terraform code to deal with HTTP 404 + "api_not_enabled" error code
2 parents f4b3077 + 845ff0f commit 0e8557b

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
## 2.17.0 (Unreleased)
1+
## 2.16.2 (Unreleased)
2+
BUG FIXES:
3+
* Monitor Folder provider now handles more error codes: "api_not_enabled", in addition to: "not_implemented_yet" (GH-389)
24

35
## 2.16.1 (June 6, 2022)
46

sumologic/resource_sumologic_monitors_library_folder.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,14 @@ func resourceSumologicMonitorsLibraryFolderRead(d *schema.ResourceData, meta int
159159
return nil
160160
}
161161

162-
fgpResponse, fgpErr := c.GetCmfFgp(fgpTargetType, folder.ID)
163-
if fgpErr != nil {
162+
fgpResponse, fgpGetErr := c.GetCmfFgp(fgpTargetType, folder.ID)
163+
if fgpGetErr != nil {
164164
// if FGP endpoint is not enabled (not implemented), we should suppress this error
165-
if !HasErrorCode(fgpErr.Error(), "not_implemented_yet") {
166-
return fgpErr
165+
suppressedErrorCode := HasErrorCode(fgpGetErr.Error(), []string{"not_implemented_yet", "api_not_enabled"})
166+
if suppressedErrorCode == "" {
167+
return fgpGetErr
167168
} else {
168-
log.Printf("[WARN] FGP Feature has not been enabled yet. Suppressing \"not_implemented_yet\" error under GetCmfFgp operation.")
169+
log.Printf("[WARN] FGP Feature has not been enabled yet. Suppressing \"%s\" error under GetCmfFgp operation.", suppressedErrorCode)
169170
}
170171
} else {
171172
CmfFgpPermStmtsSetToResource(d, fgpResponse.PermissionStatements)
@@ -203,15 +204,16 @@ func resourceSumologicMonitorsLibraryFolderUpdate(d *schema.ResourceData, meta i
203204
return convErr
204205
}
205206

206-
// reading FGP from Backend to reconcille
207+
// reading FGP from Backend to reconcile
207208
fgpGetResponse, fgpGetErr := c.GetCmfFgp(fgpTargetType, monitorFolder.ID)
208209
if fgpGetErr != nil {
209210
// if FGP endpoint is not enabled (not implemented) and FGP feature is not used,
210211
// we should suppress this error
211-
if !HasErrorCode(fgpGetErr.Error(), "not_implemented_yet") && len(permStmts) == 0 {
212+
suppressedErrorCode := HasErrorCode(fgpGetErr.Error(), []string{"not_implemented_yet", "api_not_enabled"})
213+
if suppressedErrorCode == "" && len(permStmts) == 0 {
212214
return fgpGetErr
213215
} else {
214-
log.Printf("[WARN] FGP Feature has not been enabled yet. Suppressing \"not_implemented_yet\" error under GetCmfFgp operation.")
216+
log.Printf("[WARN] FGP Feature has not been enabled yet. Suppressing \"%s\" error under GetCmfFgp operation.", suppressedErrorCode)
215217
}
216218
}
217219

sumologic/sumologic_client.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ func (s *Client) Put(urlPath string, payload interface{}) ([]byte, error) {
235235
}
236236

237237
func (s *Client) Get(urlPath string) ([]byte, string, error) {
238+
return s.GetWithErrOpt(urlPath, false)
239+
}
240+
241+
func (s *Client) GetWithErrOpt(urlPath string, return404Err bool) ([]byte, string, error) {
238242
relativeURL, _ := url.Parse(urlPath)
239243
sumoURL := s.BaseURL.ResolveReference(relativeURL)
240244

@@ -260,7 +264,11 @@ func (s *Client) Get(urlPath string) ([]byte, string, error) {
260264
}
261265

262266
if resp.StatusCode == 404 {
263-
return nil, "", nil
267+
if return404Err {
268+
return nil, "", errors.New(string(d))
269+
} else {
270+
return nil, "", nil
271+
}
264272
} else if resp.StatusCode >= 400 {
265273
return nil, "", errors.New(string(d))
266274
}
@@ -341,19 +349,21 @@ func NewClient(accessID, accessKey, authJwt, environment, base_url string, admin
341349
return &client, nil
342350
}
343351

344-
func HasErrorCode(errorJsonStr string, errorCode string) bool {
352+
func HasErrorCode(errorJsonStr string, errorCodeChoices []string) string {
345353
var apiError ApiError
346354
jsonErr := json.Unmarshal([]byte(errorJsonStr), &apiError)
347355
if jsonErr != nil {
348356
// when fail to unmarshal JSON, we should consider the errorCode is not found
349-
return false
357+
return ""
350358
}
351359
for i := range apiError.Errors {
352-
if apiError.Errors[i].Code == errorCode {
353-
return true
360+
for j := range errorCodeChoices {
361+
if apiError.Errors[i].Code == errorCodeChoices[j] {
362+
return errorCodeChoices[j]
363+
}
354364
}
355365
}
356-
return false
366+
return ""
357367
}
358368

359369
type Error struct {
@@ -362,7 +372,9 @@ type Error struct {
362372
Detail string `json:"detail"`
363373
}
364374

365-
// e.g. {"id":"8UQOI-82VTR-YBQ8G","errors":[{"code":"not_implemented_yet","message":"Not implemented yet"}]}
375+
// e.g.:
376+
// {"id":"8UQOI-82VTR-YBQ8G","errors":[{"code":"not_implemented_yet","message":"Not implemented yet"}]}
377+
// {"id":"RO4X1-BZW7P-Q8KJF","errors":[{"code":"api_not_enabled","message":"This API is not enabled for your organization."}]}
366378
type ApiError struct {
367379
Id string `json:"id"`
368380
Errors []Error `json:"errors"`

sumologic/sumologic_cmffgp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func (s *Client) GetCmfFgp(targetType string, targetId string) (*CmfFgpResponse,
1010

1111
// e.g. "v1/monitors/0000000000000003/permissions"
1212
url := fmt.Sprintf("v1/%s/%s/permissions", targetType, targetId)
13-
data, _, err := s.Get(url)
13+
data, _, err := s.GetWithErrOpt(url, true)
1414
if err != nil {
1515
return nil, err
1616
}

0 commit comments

Comments
 (0)