-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetch.go
More file actions
123 lines (98 loc) · 2.73 KB
/
fetch.go
File metadata and controls
123 lines (98 loc) · 2.73 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package fetch
import (
"context"
"errors"
"io"
"iter"
"github.com/aserto-dev/ds-load/plugins/azureadb2c/pkg/azureclient"
"github.com/aserto-dev/ds-load/sdk/common"
"github.com/aserto-dev/ds-load/sdk/common/js"
"github.com/aserto-dev/ds-load/sdk/fetcher"
"github.com/aserto-dev/msgraph-sdk-go/models"
kiota "github.com/microsoft/kiota-serialization-json-go"
)
type Fetcher struct {
azureClient *azureclient.AzureADClient
Groups bool
userProps []string
groupProps []string
}
func New(ctx context.Context, client *azureclient.AzureADClient, userProps, groupProps []string) (*Fetcher, error) {
return &Fetcher{
azureClient: client,
userProps: userProps,
groupProps: groupProps,
}, nil
}
func (f *Fetcher) WithGroups(groups bool) *Fetcher {
f.Groups = groups
return f
}
func (f *Fetcher) Fetch(ctx context.Context, outputWriter, errorWriter io.Writer) error {
jsonWriter := js.NewJSONArrayWriter(outputWriter)
defer jsonWriter.Close()
if f.Groups {
for obj, err := range f.fetchGroups(ctx) {
if err != nil {
common.WriteErrorWithExitCode(errorWriter, err, 1)
}
if err := jsonWriter.Write(obj); err != nil {
_, _ = errorWriter.Write([]byte(err.Error()))
}
}
}
for user, err := range f.fetchUsers(ctx) {
if err != nil {
common.WriteErrorWithExitCode(errorWriter, err, 1)
}
if err := jsonWriter.Write(user); err != nil {
_, _ = errorWriter.Write([]byte(err.Error()))
}
}
return nil
}
func (f *Fetcher) fetchUsers(ctx context.Context) iter.Seq2[map[string]any, error] {
aadUsers, err := f.azureClient.ListUsers(ctx, f.Groups, f.userProps)
if err != nil {
return fetcher.YieldError(err)
}
return fetcher.YieldMap(aadUsers, func(object any) ([]byte, error) {
user, ok := object.(models.Userable)
if !ok {
return nil, errors.ErrUnsupported
}
writer := kiota.NewJsonSerializationWriter()
err := user.Serialize(writer)
if err != nil {
return nil, err
}
objBytes, err := writer.GetSerializedContent()
if err != nil {
return nil, err
}
objString := "{" + string(objBytes) + "}"
return []byte(objString), nil
})
}
func (f *Fetcher) fetchGroups(ctx context.Context) iter.Seq2[map[string]any, error] {
aadGroups, err := f.azureClient.ListGroups(ctx, f.groupProps)
if err != nil {
return fetcher.YieldError(err)
}
return fetcher.YieldMap(aadGroups, func(object any) ([]byte, error) {
group, ok := object.(models.Groupable)
if !ok {
return nil, errors.ErrUnsupported
}
writer := kiota.NewJsonSerializationWriter()
if err := group.Serialize(writer); err != nil {
return nil, err
}
objBytes, err := writer.GetSerializedContent()
if err != nil {
return nil, err
}
objString := "{" + string(objBytes) + "}"
return []byte(objString), nil
})
}