Skip to content

Commit 6b4d1e0

Browse files
committed
feat(cockpit): refacto fetch all
1 parent 1280eba commit 6b4d1e0

File tree

4 files changed

+210
-222
lines changed

4 files changed

+210
-222
lines changed

internal/services/cockpit/source_data_source.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,30 +124,18 @@ func fetchDataSourceByFilters(ctx context.Context, d *schema.ResourceData, meta
124124
if v, ok := d.GetOk("origin"); ok {
125125
req.Origin = cockpit.DataSourceOrigin(v.(string))
126126
}
127-
var allDataSources []*cockpit.DataSource
128-
page := int32(1)
129-
for {
130-
req.Page = &page
131-
req.PageSize = scw.Uint32Ptr(1000)
132127

133-
res, err := api.ListDataSources(req, scw.WithContext(ctx))
134-
if err != nil {
135-
return diag.FromErr(err)
136-
}
137-
138-
allDataSources = append(allDataSources, res.DataSources...)
139-
if len(res.DataSources) < 1000 {
140-
break
141-
}
142-
page++
128+
res, err := api.ListDataSources(req, scw.WithContext(ctx), scw.WithAllPages())
129+
if err != nil {
130+
return diag.FromErr(err)
143131
}
144132

145-
if len(allDataSources) == 0 {
133+
if res.TotalCount == 0 {
146134
return diag.Errorf("no data source found matching the specified criteria")
147135
}
148136

149137
if name, ok := d.GetOk("name"); ok {
150-
for _, ds := range allDataSources {
138+
for _, ds := range res.DataSources {
151139
if ds.Name == name.(string) {
152140
flattenDataSource(d, ds)
153141
return nil
@@ -156,7 +144,7 @@ func fetchDataSourceByFilters(ctx context.Context, d *schema.ResourceData, meta
156144
return diag.Errorf("no data source found with name '%s'", name.(string))
157145
}
158146

159-
flattenDataSource(d, allDataSources[0])
147+
flattenDataSource(d, res.DataSources[0])
160148
return nil
161149
}
162150

0 commit comments

Comments
 (0)