@@ -95,71 +95,62 @@ func dataSourceSysdigSecureZone() *schema.Resource {
9595}
9696
9797func dataSourceSysdigSecureZoneRead (ctx context.Context , d * schema.ResourceData , m any ) diag.Diagnostics {
98- client , err := getZoneClient (m .(SysdigClients ))
98+ clientV2 , err := getZoneV2Client (m .(SysdigClients ))
9999 if err != nil {
100100 return diag .FromErr (err )
101101 }
102- clientv2 , err := getZoneV2Client (m .(SysdigClients ))
103- if err != nil {
104- return diag .FromErr (err )
105- }
106- var zone * v2.Zone
107102 var zoneV2 * v2.ZoneV2
108103 zoneIDRaw , hasZoneID := d .GetOk ("id" )
109104 if hasZoneID {
110105 zoneID , err := strconv .Atoi (zoneIDRaw .(string ))
111- if err != nil {
112- return diag .FromErr (fmt .Errorf ("invalid zone id: %s" , err ))
113- }
114- zone , err = client .GetZoneByID (ctx , zoneID )
115106 if err != nil {
116107 return diag .FromErr (fmt .Errorf ("error fetching zone by ID: %s" , err ))
117108 }
118- zoneV2 , err = clientv2 .GetZoneV2 (ctx , zoneID )
109+ zoneV2 , err = clientV2 .GetZoneV2 (ctx , zoneID )
119110 if err != nil {
120111 return diag .FromErr (fmt .Errorf ("error fetching zone v2 by ID: %s" , err ))
121112 }
122113 } else if nameRaw , hasName := d .GetOk ("name" ); hasName {
123114 name := nameRaw .(string )
124- zones , err := client . GetZones (ctx , name )
115+ zones , err := clientV2 . GetZonesV2 (ctx , name )
125116 if err != nil {
126117 return diag .FromErr (fmt .Errorf ("error fetching zones: %s" , err ))
127118 }
128119 for _ , z := range zones {
129120 if z .Name == name {
130- zone = & z
121+ zoneV2 = & z
131122 break
132123 }
133124 }
134- if zone == nil {
125+ if zoneV2 == nil {
135126 return diag .FromErr (fmt .Errorf ("zone with name '%s' not found" , name ))
136127 }
137- zoneV2 , err = clientv2 .GetZoneV2 (ctx , zone .ID )
128+ zoneV2 , err = clientV2 .GetZoneV2 (ctx , zoneV2 .ID )
138129 if err != nil {
139130 return diag .FromErr (fmt .Errorf ("error fetching zones: %s" , err ))
140131 }
141132 } else {
142133 return diag .FromErr (fmt .Errorf ("either id or name must be specified" ))
143134 }
144135
145- d .SetId (fmt .Sprintf ("%d" , zone .ID ))
146- _ = d .Set (SchemaNameKey , zone .Name )
147- _ = d .Set (SchemaDescriptionKey , zone .Description )
148- _ = d .Set (SchemaIsSystemKey , zone .IsSystem )
149- _ = d .Set (SchemaAuthorKey , zone .Author )
150- _ = d .Set (SchemaLastModifiedBy , zone .LastModifiedBy )
151- _ = d .Set (SchemaLastUpdated , time .UnixMilli (zone .LastUpdated ).Format (time .RFC3339 ))
136+ d .SetId (fmt .Sprintf ("%d" , zoneV2 .ID ))
137+ _ = d .Set (SchemaNameKey , zoneV2 .Name )
138+ _ = d .Set (SchemaDescriptionKey , zoneV2 .Description )
139+ _ = d .Set (SchemaIsSystemKey , zoneV2 .IsSystem )
140+ _ = d .Set (SchemaAuthorKey , zoneV2 .Author )
141+ _ = d .Set (SchemaLastModifiedBy , zoneV2 .LastModifiedBy )
142+ _ = d .Set (SchemaLastUpdated , time .UnixMilli (zoneV2 .LastUpdated ).Format (time .RFC3339 ))
152143
153- if err := d .Set (SchemaScopeKey , getZoneScopes (zone . Scopes , zoneV2 )); err != nil {
144+ if err := d .Set (SchemaScopeKey , getZoneScopes (zoneV2 )); err != nil {
154145 return diag .FromErr (fmt .Errorf ("error setting scope: %s" , err ))
155146 }
156147
157148 return nil
158149}
159150
160- func getZoneScopes (legacyScopes []v2. ZoneScope , zoneV2 * v2.ZoneV2 ) []any {
151+ func getZoneScopes (zoneV2 * v2.ZoneV2 ) []any {
161152 // Build expression lookup by filter ID from the v2 response.
162- exprByID := make (map [ int ][] any )
153+ out := make ([] any , 0 )
163154 if zoneV2 != nil {
164155 for _ , s := range zoneV2 .Scopes {
165156 for _ , f := range s .Filters {
@@ -168,26 +159,16 @@ func getZoneScopes(legacyScopes []v2.ZoneScope, zoneV2 *v2.ZoneV2) []any {
168159 for _ , e := range f .Expressions {
169160 exprs = append (exprs , flattenExpressionV2 (e ))
170161 }
171- exprByID [f .ID ] = exprs
162+ m := map [string ]any {
163+ SchemaIDKey : f .ID ,
164+ SchemaTargetTypeKey : f .ResourceType ,
165+ SchemaRulesKey : f .Rules ,
166+ }
167+ m [SchemaExpressionKey ] = exprs
168+ out = append (out , m )
172169 }
173170 }
174171 }
175172 }
176-
177- // Merge: v1 scope ← v2 expressions by ID.
178- // If a scope's ID has no v2 match (rollout not complete, or filter has no
179- // expressions), the scope gets rules only — graceful degradation.
180- out := make ([]any , 0 , len (legacyScopes ))
181- for _ , scope := range legacyScopes {
182- m := map [string ]any {
183- SchemaIDKey : scope .ID ,
184- SchemaTargetTypeKey : scope .TargetType ,
185- SchemaRulesKey : scope .Rules ,
186- }
187- if exprs , ok := exprByID [scope .ID ]; ok {
188- m [SchemaExpressionKey ] = exprs
189- }
190- out = append (out , m )
191- }
192173 return out
193174}
0 commit comments