/
groups.go
84 lines (63 loc) · 2.09 KB
/
groups.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package datasources
import (
"context"
"fmt"
"github.com/fivetran/terraform-provider-fivetran/fivetran/framework/core"
"github.com/fivetran/terraform-provider-fivetran/fivetran/framework/core/model"
"github.com/hashicorp/terraform-plugin-framework/datasource"
sdk "github.com/fivetran/go-fivetran/groups"
fivetranSchema "github.com/fivetran/terraform-provider-fivetran/fivetran/framework/core/schema"
)
func Groups() datasource.DataSource {
return &groups{}
}
// Ensure the implementation satisfies the desired interfaces.
var _ datasource.DataSourceWithConfigure = &groups{}
type groups struct {
core.ProviderDatasource
}
func (d *groups) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = "fivetran_groups"
}
func (d *groups) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = fivetranSchema.GroupsDatasource()
}
func (d *groups) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
if d.GetClient() == nil {
resp.Diagnostics.AddError(
"Unconfigured Fivetran Client",
"Please report this issue to the provider developers.",
)
return
}
var data model.Groups
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
var respNextCursor string
var groupsResponse sdk.GroupsListResponse
limit := 1000
for {
var err error
var tmpResp sdk.GroupsListResponse
svc := d.GetClient().NewGroupsList()
if respNextCursor == "" {
tmpResp, err = svc.Limit(limit).Do(ctx)
}
if respNextCursor != "" {
tmpResp, err = svc.Limit(limit).Cursor(respNextCursor).Do(ctx)
}
if err != nil {
resp.Diagnostics.AddError(
"Read error.",
fmt.Sprintf("%v; code: %v", err, tmpResp.Code),
)
groupsResponse = sdk.GroupsListResponse{}
}
groupsResponse.Data.Items = append(groupsResponse.Data.Items, tmpResp.Data.Items...)
if tmpResp.Data.NextCursor == "" {
break
}
respNextCursor = tmpResp.Data.NextCursor
}
data.ReadFromResponse(ctx, groupsResponse)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}