Skip to content

Commit

Permalink
Merge pull request #205 from jfrog/fix-policy-api-error-handling
Browse files Browse the repository at this point in the history
Remove incorrect error handling when deleting policy
  • Loading branch information
alexhung authored Jun 14, 2024
2 parents 4564372 + 0ee46a6 commit e375250
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.8.1 (June 14, 2024)

BUG FIXES:

* resource/xray_\*\_policy: Fix incorrect error handling when deleting a policy that is still attached to a watch. This leads to the resource being deleted even though the policy can't be deleted. PR: [#205](https://github.com/jfrog/terraform-provider-xray/pull/205)

## 2.8.0 (May 30, 2024)

IMPROVEMENTS:
Expand Down
28 changes: 19 additions & 9 deletions pkg/xray/resource/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ type Policy struct {
Modified string `json:"modified,omitempty"` // Omitempty is used because the field is computed
}

type PolicyError struct {
Error string `json:"error"`
}

func unpackPolicy(d *schema.ResourceData) (*Policy, error) {
policy := new(Policy)

Expand Down Expand Up @@ -754,12 +758,16 @@ func resourceXrayPolicyCreate(ctx context.Context, d *schema.ResourceData, m int
return diag.FromErr(err)
}

resp, err := req.SetBody(policy).Post("xray/api/v2/policies")
var policyError PolicyError
resp, err := req.
SetBody(policy).
SetError(&policyError).
Post("xray/api/v2/policies")
if err != nil {
return diag.FromErr(err)
}
if resp.IsError() {
return diag.Errorf("%s", resp.String())
return diag.Errorf("%s", policyError.Error)
}

d.SetId(policy.Name)
Expand All @@ -775,9 +783,11 @@ func resourceXrayPolicyRead(ctx context.Context, d *schema.ResourceData, m inter
return diag.FromErr(err)
}

var policyError PolicyError
resp, err := req.
SetResult(&policy).
SetPathParam("name", d.Id()).
SetError(&policyError).
Get("xray/api/v2/policies/{name}")
if err != nil {
return diag.FromErr(err)
Expand All @@ -787,7 +797,7 @@ func resourceXrayPolicyRead(ctx context.Context, d *schema.ResourceData, m inter
return diag.Errorf("policy (%s) not found, removing from state", d.Id())
}
if resp.IsError() {
return diag.Errorf("%s", resp.String())
return diag.Errorf("%s", policyError.Error)
}

return packPolicy(policy, d)
Expand All @@ -804,17 +814,19 @@ func resourceXrayPolicyUpdate(ctx context.Context, d *schema.ResourceData, m int
return diag.FromErr(err)
}

var policyError PolicyError
resp, err := req.
SetBody(policy).
SetPathParams(map[string]string{
"name": d.Id(),
}).
SetError(&policyError).
Put("xray/api/v2/policies/{name}")
if err != nil {
return diag.FromErr(err)
}
if resp.IsError() {
return diag.Errorf("%s", resp.String())
return diag.Errorf("%s", policyError.Error)
}

d.SetId(policy.Name)
Expand All @@ -832,20 +844,18 @@ func resourceXrayPolicyDelete(ctx context.Context, d *schema.ResourceData, m int
return diag.FromErr(err)
}

// Warning or errors can be collected in a slice type
var policyError PolicyError
resp, err := req.
SetPathParams(map[string]string{
"name": d.Id(),
}).
SetError(&policyError).
Delete("xray/api/v2/policies/{name}")
if err != nil {
return diag.FromErr(err)
}
if resp.StatusCode() == http.StatusInternalServerError {
d.SetId("")
}
if resp.IsError() {
return diag.Errorf("%s", resp.String())
return diag.Errorf("%s", policyError.Error)
}

d.SetId("")
Expand Down
6 changes: 3 additions & 3 deletions pkg/xray/resource/resource_xray_security_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ const securityPolicyMinSeverity = `resource "xray_security_policy" "{{ .resource
name = "{{ .rule_name }}"
priority = 1
criteria {
min_severity = "{{ .min_severity }}"
min_severity = "{{ .min_severity }}"
}
actions {
block_release_bundle_distribution = {{ .block_release_bundle_distribution }}
Expand All @@ -1194,7 +1194,7 @@ const securityPolicyExposures = `resource "xray_security_policy" "{{ .resource_n
name = "{{ .rule_name }}"
priority = 1
criteria {
exposures {
exposures {
min_severity = "{{ .exposures_min_severity }}"
secrets = {{ .exposures_secrets }}
applications = {{ .exposures_applications }}
Expand Down Expand Up @@ -1225,7 +1225,7 @@ const securityPolicyFixVersionDep = `resource "xray_security_policy" "{{ .resour
name = "{{ .rule_name }}"
priority = 1
criteria {
min_severity = "{{ .min_severity }}"
min_severity = "{{ .min_severity }}"
fix_version_dependant = {{ .fix_version_dependant }}
}
actions {
Expand Down

0 comments on commit e375250

Please sign in to comment.