Skip to content

Commit 959366b

Browse files
authored
[Fix] RMS: Fix pagination and filter bugs in rms_policy_states_v1 data source (#3315)
[Fix] RMS: Fix pagination and filter bugs in rms_policy_states_v1 data source Summary of the Pull Request Fix two bugs in opentelekomcloud_rms_policy_states_v1 that caused incomplete and incorrect results in environments with paginated policy states: Pagination: Bump gophertelekomcloud SDK which fixes marker-based pagination for RMS compliance (NewPageInfoBase replaces NewSinglePageBase in ListAllUserCompliance/ ListAllRuleCompliance). The old SDK short-circuited NewAllPages() after page 1. Filtering: Pass compliance_state, resource_name, and resource_id as query parameters to the SDK calls for server-side filtering. Remove filterPolicyStates() which returned all unfiltered states when no match was found instead of an empty slice. Also fixes pre-existing acceptance test issues: Add missing filter_by_compliance_state data source block in HCL config Add opentelekomcloud_rms_policy_assignment_evaluate_v1 to make the test self-contained (no longer depends on pre-existing policy states) Split into two test steps with 30s wait for evaluation to complete PR Checklist Refers to: #3316 Tests added/passed. Documentation updated. (N/A — no schema or behavior change from user perspective) Schema updated. Release notes added. Acceptance Steps Performed Prerequisites: RMS Resource Recorder must be enabled on the test account. === RUN TestAccDataSourceRmsPolicyStates_basic === PAUSE TestAccDataSourceRmsPolicyStates_basic === CONT TestAccDataSourceRmsPolicyStates_basic data_source_opentelekomcloud_rms_policy_states_v1_test.go:38: Waiting 30s for policy evaluation to complete... --- PASS: TestAccDataSourceRmsPolicyStates_basic (269.36s) PASS Reviewed-by: Anton Sidelnikov Reviewed-by: Tobias Wohland Reviewed-by: Artem Lifshits
1 parent 24ba784 commit 959366b

File tree

5 files changed

+58
-50
lines changed

5 files changed

+58
-50
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require (
1717
github.com/jmespath/go-jmespath v0.4.0
1818
github.com/keybase/go-crypto v0.0.0-20200123153347-de78d2cb44f4
1919
github.com/mitchellh/go-homedir v1.1.0
20-
github.com/opentelekomcloud/gophertelekomcloud v0.9.6-0.20260323130726-5844a1acfb29
20+
github.com/opentelekomcloud/gophertelekomcloud v0.9.6-0.20260326114400-f18a3f4a5030
2121
github.com/unknwon/com v1.0.1
2222
golang.org/x/crypto v0.46.0
2323
golang.org/x/sync v0.19.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k
167167
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce/go.mod h1:uFMI8w+ref4v2r9jz+c9i1IfIttS/OkmLfrk1jne5hs=
168168
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
169169
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
170-
github.com/opentelekomcloud/gophertelekomcloud v0.9.6-0.20260323130726-5844a1acfb29 h1:xvC7TWnoZM5o3s6NeZkswFwpLPMksGS6XRlmMqdJmHc=
171-
github.com/opentelekomcloud/gophertelekomcloud v0.9.6-0.20260323130726-5844a1acfb29/go.mod h1:la8cQVYopRoEbNe2L7HlGTdLxUQOwIqHp1VHtjE/5qA=
170+
github.com/opentelekomcloud/gophertelekomcloud v0.9.6-0.20260326114400-f18a3f4a5030 h1:EgL/BTE8W86qWHUw6kocYylW1gYbIPLYF+HkZQVLv7I=
171+
github.com/opentelekomcloud/gophertelekomcloud v0.9.6-0.20260326114400-f18a3f4a5030/go.mod h1:la8cQVYopRoEbNe2L7HlGTdLxUQOwIqHp1VHtjE/5qA=
172172
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
173173
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
174174
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

opentelekomcloud/acceptance/rms/data_source_opentelekomcloud_rms_policy_states_v1_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package rms
33
import (
44
"fmt"
55
"testing"
6+
"time"
67

78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -30,6 +31,13 @@ func TestAccDataSourceRmsPolicyStates_basic(t *testing.T) {
3031
ProviderFactories: common.TestAccProviderFactories,
3132
Steps: []resource.TestStep{
3233
{
34+
Config: testDataSourceDataSourceRmsPolicyStates_base(rName),
35+
},
36+
{
37+
PreConfig: func() {
38+
t.Log("Waiting 30s for policy evaluation to complete...")
39+
time.Sleep(30 * time.Second)
40+
},
3341
Config: testDataSourceDataSourceRmsPolicyStates_basic(rName),
3442
Check: resource.ComposeTestCheckFunc(
3543
dc1.CheckResourceExists(),
@@ -76,27 +84,43 @@ resource "opentelekomcloud_rms_policy_assignment_v1" "test" {
7684
listOfAllowedFlavors = "[\"${data.opentelekomcloud_compute_flavor_v2.test.id}\"]"
7785
}
7886
}
87+
88+
resource "opentelekomcloud_rms_policy_assignment_evaluate_v1" "test" {
89+
policy_assignment_id = opentelekomcloud_rms_policy_assignment_v1.test.id
90+
}
7991
`, testAccPolicyAssignment_ecsConfig(name), name, env.OS_REGION_NAME)
8092
}
8193

8294
func testDataSourceDataSourceRmsPolicyStates_basic(name string) string {
8395
return fmt.Sprintf(`
8496
%[1]s
8597
86-
data "opentelekomcloud_rms_policy_states_v1" "basic" {}
98+
data "opentelekomcloud_rms_policy_states_v1" "basic" {
99+
depends_on = [opentelekomcloud_rms_policy_assignment_evaluate_v1.test]
100+
}
101+
102+
data "opentelekomcloud_rms_policy_states_v1" "filter_by_compliance_state" {
103+
compliance_state = "Compliant"
104+
105+
depends_on = [opentelekomcloud_rms_policy_assignment_evaluate_v1.test]
106+
}
87107
88108
data "opentelekomcloud_rms_policy_states_v1" "filter_by_resource_name" {
89109
resource_name = "%[2]s"
90110
91-
depends_on = [opentelekomcloud_compute_instance_v2.test]
111+
depends_on = [opentelekomcloud_rms_policy_assignment_evaluate_v1.test]
92112
}
93113
94114
data "opentelekomcloud_rms_policy_states_v1" "filter_by_resource_id" {
95115
resource_id = opentelekomcloud_compute_instance_v2.test.id
116+
117+
depends_on = [opentelekomcloud_rms_policy_assignment_evaluate_v1.test]
96118
}
97119
98120
data "opentelekomcloud_rms_policy_states_v1" "filter_by_assignment_id" {
99121
policy_assignment_id = opentelekomcloud_rms_policy_assignment_v1.test.id
122+
123+
depends_on = [opentelekomcloud_rms_policy_assignment_evaluate_v1.test]
100124
}
101125
102126
locals {

opentelekomcloud/services/rms/data_opentelekomcloud_rms_policy_states_v1.go

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ func DataSourcePolicyStates() *schema.Resource {
9595
},
9696
}
9797
}
98+
9899
func dataSourcePolicyStatesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
99100
config := meta.(*cfg.Config)
100101
client, err := common.ClientFromCtx(ctx, rmsClientV1, func() (*golangsdk.ServiceClient, error) {
@@ -104,40 +105,44 @@ func dataSourcePolicyStatesRead(ctx context.Context, d *schema.ResourceData, met
104105
return fmterr.Errorf(errCreationRMSV1Client, err)
105106
}
106107

108+
domainID := GetRmsDomainId(client, config)
109+
complianceState := d.Get("compliance_state").(string)
110+
resourceName := d.Get("resource_name").(string)
111+
resourceID := d.Get("resource_id").(string)
112+
107113
var policyStates []compliance.PolicyState
108-
if _, ok := d.GetOk("policy_assignment_id"); ok {
114+
if v, ok := d.GetOk("policy_assignment_id"); ok {
109115
policyStates, err = compliance.ListAllRuleCompliance(client, compliance.ListAllComplianceOpts{
110-
DomainId: GetRmsDomainId(client, config),
111-
PolicyId: d.Get("policy_assignment_id").(string),
116+
DomainId: domainID,
117+
PolicyId: v.(string),
118+
ComplianceState: complianceState,
119+
ResourceName: resourceName,
120+
ResourceId: resourceID,
112121
})
113-
114-
if err != nil {
115-
return diag.FromErr(err)
116-
}
117122
} else {
118123
policyStates, err = compliance.ListAllUserCompliance(client, compliance.ListAllUserComplianceOpts{
119-
DomainId: GetRmsDomainId(client, config),
124+
DomainId: domainID,
125+
ComplianceState: complianceState,
126+
ResourceName: resourceName,
127+
ResourceId: resourceID,
120128
})
121-
122-
if err != nil {
123-
return diag.FromErr(err)
124-
}
129+
}
130+
if err != nil {
131+
return diag.FromErr(err)
125132
}
126133

127-
filteredStates := filterPolicyStates(policyStates, d)
128-
129-
if len(filteredStates) == 0 {
134+
if len(policyStates) == 0 {
130135
return diag.FromErr(fmt.Errorf("policy states not found"))
131136
}
132137

133-
uuid, err := uuid.GenerateUUID()
138+
id, err := uuid.GenerateUUID()
134139
if err != nil {
135140
return diag.Errorf("unable to generate ID: %s", err)
136141
}
137-
d.SetId(uuid)
142+
d.SetId(id)
138143

139-
states := make([]map[string]interface{}, len(filteredStates))
140-
for i, state := range filteredStates {
144+
states := make([]map[string]interface{}, len(policyStates))
145+
for i, state := range policyStates {
141146
states[i] = map[string]interface{}{
142147
"domain_id": state.DomainID,
143148
"region_id": state.RegionID,
@@ -161,29 +166,3 @@ func dataSourcePolicyStatesRead(ctx context.Context, d *schema.ResourceData, met
161166

162167
return diag.FromErr(mErr.ErrorOrNil())
163168
}
164-
165-
func filterPolicyStates(states []compliance.PolicyState, d *schema.ResourceData) []compliance.PolicyState {
166-
var filtered []compliance.PolicyState
167-
168-
for _, state := range states {
169-
if v, ok := d.GetOk("compliance_state"); ok && v.(string) != state.ComplianceState {
170-
continue
171-
}
172-
173-
if v, ok := d.GetOk("resource_name"); ok && v.(string) != state.ResourceName {
174-
continue
175-
}
176-
177-
if v, ok := d.GetOk("resource_id"); ok && v.(string) != state.ResourceID {
178-
continue
179-
}
180-
181-
filtered = append(filtered, state)
182-
}
183-
184-
if len(filtered) == 0 {
185-
return states
186-
}
187-
188-
return filtered
189-
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
**[RMS]** Fix pagination and filter bugs in ``datasource/opentelekomcloud_rms_policy_states_v1`` where only the first page of results was returned and filtering by ``compliance_state`` could return unfiltered data
5+
(`#3315 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/3315>`_)

0 commit comments

Comments
 (0)