Skip to content

Commit

Permalink
feat(cockpit): refacto fetch all
Browse files Browse the repository at this point in the history
  • Loading branch information
jremy42 committed Dec 20, 2024
1 parent 1280eba commit 6b4d1e0
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 222 deletions.
24 changes: 6 additions & 18 deletions internal/services/cockpit/source_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,30 +124,18 @@ func fetchDataSourceByFilters(ctx context.Context, d *schema.ResourceData, meta
if v, ok := d.GetOk("origin"); ok {
req.Origin = cockpit.DataSourceOrigin(v.(string))
}
var allDataSources []*cockpit.DataSource
page := int32(1)
for {
req.Page = &page
req.PageSize = scw.Uint32Ptr(1000)

res, err := api.ListDataSources(req, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

allDataSources = append(allDataSources, res.DataSources...)
if len(res.DataSources) < 1000 {
break
}
page++
res, err := api.ListDataSources(req, scw.WithContext(ctx), scw.WithAllPages())
if err != nil {
return diag.FromErr(err)
}

if len(allDataSources) == 0 {
if res.TotalCount == 0 {
return diag.Errorf("no data source found matching the specified criteria")
}

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

flattenDataSource(d, allDataSources[0])
flattenDataSource(d, res.DataSources[0])
return nil
}

Expand Down
Loading

0 comments on commit 6b4d1e0

Please sign in to comment.