Skip to content

Commit 22fe4ad

Browse files
committed
refactor datasource
1 parent f1c66dc commit 22fe4ad

2 files changed

Lines changed: 24 additions & 156 deletions

File tree

sysdig/data_source_sysdig_secure_zone.go

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -95,71 +95,62 @@ func dataSourceSysdigSecureZone() *schema.Resource {
9595
}
9696

9797
func 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
}

sysdig/data_source_sysdig_secure_zone_unit_test.go

Lines changed: 1 addition & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ import (
77
)
88

99
func TestGetZoneScopes_MatchByID(t *testing.T) {
10-
// Two scopes with the same target_type but different IDs.
11-
// Each must get the correct expressions.
12-
legacyScopes := []v2.ZoneScope{
13-
{ID: 10, TargetType: "kubernetes", Rules: `cluster in ("a")`},
14-
{ID: 20, TargetType: "kubernetes", Rules: `cluster in ("b")`},
15-
}
16-
1710
zoneV2 := &v2.ZoneV2{
1811
Scopes: []v2.ScopeV2{
1912
{
@@ -37,7 +30,7 @@ func TestGetZoneScopes_MatchByID(t *testing.T) {
3730
},
3831
}
3932

40-
result := getZoneScopes(legacyScopes, zoneV2)
33+
result := getZoneScopes(zoneV2)
4134
if len(result) != 2 {
4235
t.Fatalf("expected 2 scopes, got %d", len(result))
4336
}
@@ -61,109 +54,3 @@ func TestGetZoneScopes_MatchByID(t *testing.T) {
6154
}
6255
}
6356

64-
func TestGetZoneScopes_NilV2(t *testing.T) {
65-
legacyScopes := []v2.ZoneScope{
66-
{ID: 1, TargetType: "aws", Rules: `account in ("123")`},
67-
}
68-
69-
result := getZoneScopes(legacyScopes, nil)
70-
if len(result) != 1 {
71-
t.Fatalf("expected 1 scope, got %d", len(result))
72-
}
73-
74-
scope := result[0].(map[string]any)
75-
if scope[SchemaRulesKey] != `account in ("123")` {
76-
t.Errorf("expected rules preserved, got: %v", scope[SchemaRulesKey])
77-
}
78-
if _, hasExpr := scope[SchemaExpressionKey]; hasExpr {
79-
t.Errorf("expected no expressions when v2 is nil, got: %v", scope[SchemaExpressionKey])
80-
}
81-
}
82-
83-
func TestGetZoneScopes_MissingV2ID(t *testing.T) {
84-
// v2 filters have ID=0 (backend hasn't rolled out IDs yet).
85-
// Should degrade gracefully: no expressions attached.
86-
legacyScopes := []v2.ZoneScope{
87-
{ID: 5, TargetType: "aws", Rules: `org in ("o1")`},
88-
}
89-
90-
zoneV2 := &v2.ZoneV2{
91-
Scopes: []v2.ScopeV2{
92-
{
93-
Filters: []v2.FilterV2{
94-
{
95-
ID: 0,
96-
ResourceType: "aws",
97-
Expressions: []v2.ExpressionV2{
98-
{Field: "org", Operator: "in", Values: []string{"o1"}},
99-
},
100-
},
101-
},
102-
},
103-
},
104-
}
105-
106-
result := getZoneScopes(legacyScopes, zoneV2)
107-
if len(result) != 1 {
108-
t.Fatalf("expected 1 scope, got %d", len(result))
109-
}
110-
111-
scope := result[0].(map[string]any)
112-
if _, hasExpr := scope[SchemaExpressionKey]; hasExpr {
113-
t.Errorf("expected no expressions when v2 ID=0, got: %v", scope[SchemaExpressionKey])
114-
}
115-
}
116-
117-
func TestGetZoneScopes_PartialMatch(t *testing.T) {
118-
// 3 v1 scopes, only 2 have v2 matches.
119-
legacyScopes := []v2.ZoneScope{
120-
{ID: 1, TargetType: "aws", Rules: `org in ("o1")`},
121-
{ID: 2, TargetType: "gcp", Rules: `org in ("o2")`},
122-
{ID: 3, TargetType: "azure", Rules: `org in ("o3")`},
123-
}
124-
125-
zoneV2 := &v2.ZoneV2{
126-
Scopes: []v2.ScopeV2{
127-
{
128-
Filters: []v2.FilterV2{
129-
{
130-
ID: 1,
131-
ResourceType: "aws",
132-
Expressions: []v2.ExpressionV2{
133-
{Field: "org", Operator: "in", Values: []string{"o1"}},
134-
},
135-
},
136-
{
137-
ID: 3,
138-
ResourceType: "azure",
139-
Expressions: []v2.ExpressionV2{
140-
{Field: "org", Operator: "in", Values: []string{"o3"}},
141-
},
142-
},
143-
},
144-
},
145-
},
146-
}
147-
148-
result := getZoneScopes(legacyScopes, zoneV2)
149-
if len(result) != 3 {
150-
t.Fatalf("expected 3 scopes, got %d", len(result))
151-
}
152-
153-
for _, raw := range result {
154-
scope := raw.(map[string]any)
155-
id := scope[SchemaIDKey].(int)
156-
_, hasExpr := scope[SchemaExpressionKey]
157-
158-
switch id {
159-
case 1, 3:
160-
if !hasExpr {
161-
t.Errorf("scope ID=%d: expected expressions (matched in v2)", id)
162-
}
163-
case 2:
164-
if hasExpr {
165-
t.Errorf("scope ID=2: expected no expressions (no v2 match)")
166-
}
167-
}
168-
}
169-
}

0 commit comments

Comments
 (0)