diff --git a/CHANGELOG.md b/CHANGELOG.md index ea462b86..f8f5a045 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: diff --git a/pkg/xray/resource/policies.go b/pkg/xray/resource/policies.go index a3e55d04..c8dedcb8 100644 --- a/pkg/xray/resource/policies.go +++ b/pkg/xray/resource/policies.go @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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("") diff --git a/pkg/xray/resource/resource_xray_security_policy_test.go b/pkg/xray/resource/resource_xray_security_policy_test.go index fe846d82..1d28534c 100644 --- a/pkg/xray/resource/resource_xray_security_policy_test.go +++ b/pkg/xray/resource/resource_xray_security_policy_test.go @@ -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 }} @@ -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 }} @@ -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 {