Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cockpit): add data source for source #2856

Merged
merged 5 commits into from
Dec 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(cockpit): refacto fetch all
jremy42 committed Dec 20, 2024
commit 6b4d1e02f98900d3f4377f34ffc6da1659e22ffe
24 changes: 6 additions & 18 deletions internal/services/cockpit/source_data_source.go
Original file line number Diff line number Diff line change
@@ -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
@@ -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
}

Loading