Skip to content

Commit 798aefa

Browse files
authored
Fix Issue With Handling Missing Policy (#265)
* Fix Issue With Handling Missing Policy * Fix Issue With Handling Missing Policy * Fix Issue With Handling Missing Policy
1 parent e6c1a37 commit 798aefa

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

sysdig/internal/client/secure/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type SysdigSecureClient interface {
1515
CreatePolicy(context.Context, Policy) (Policy, error)
1616
DeletePolicy(context.Context, int) error
1717
UpdatePolicy(context.Context, Policy) (Policy, error)
18-
GetPolicyById(context.Context, int) (Policy, error)
18+
GetPolicyById(context.Context, int) (Policy, int, error)
1919

2020
CreateRule(context.Context, Rule) (Rule, error)
2121
GetRuleByID(context.Context, int) (Rule, error)

sysdig/internal/client/secure/policies.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,20 @@ func (client *sysdigSecureClient) UpdatePolicy(ctx context.Context, policyReques
6767
return PolicyFromJSON(body), nil
6868
}
6969

70-
func (client *sysdigSecureClient) GetPolicyById(ctx context.Context, policyID int) (policy Policy, err error) {
70+
func (client *sysdigSecureClient) GetPolicyById(ctx context.Context, policyID int) (policy Policy, statusCode int, err error) {
7171
response, err := client.doSysdigSecureRequest(ctx, http.MethodGet, client.policyURL(policyID), nil)
7272
if err != nil {
7373
return
7474
}
7575
defer response.Body.Close()
7676

7777
if response.StatusCode != http.StatusOK {
78-
return Policy{}, errorFromResponse(response)
78+
return Policy{}, response.StatusCode, errorFromResponse(response)
7979
}
8080

8181
body, err := io.ReadAll(response.Body)
8282
if err != nil {
8383
return
8484
}
85-
return PolicyFromJSON(body), nil
85+
return PolicyFromJSON(body), http.StatusOK, nil
8686
}

sysdig/resource_sysdig_secure_policy.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sysdig
22

33
import (
44
"context"
5+
"net/http"
56
"strconv"
67
"strings"
78
"time"
@@ -254,11 +255,13 @@ func resourceSysdigPolicyRead(ctx context.Context, d *schema.ResourceData, meta
254255
}
255256

256257
id, _ := strconv.Atoi(d.Id())
257-
policy, err := client.GetPolicyById(ctx, id)
258+
policy, statusCode, err := client.GetPolicyById(ctx, id)
258259

259260
if err != nil {
260261
d.SetId("")
261-
return diag.FromErr(err)
262+
if statusCode == http.StatusNotFound {
263+
return diag.FromErr(err)
264+
}
262265
}
263266

264267
policyToResourceData(&policy, d)

0 commit comments

Comments
 (0)