-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.go
146 lines (130 loc) · 3.67 KB
/
main.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"github.com/google/uuid"
msgraph "github.com/yaegashi/msgraph.go/beta"
"github.com/yaegashi/msgraph.go/jsonx"
"github.com/yaegashi/msgraph.go/msauth"
P "github.com/yaegashi/msgraph.go/ptr"
"golang.org/x/oauth2"
)
// Default ID and secret: you should replace them with your own
const (
defaultTenantID = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
defaultClientID = "YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYYY"
defaultClientSecret = "ZZZZZZZZZZZZZZZZZZZZZZZZ"
)
// Default user: you should replace it with your own
var defaultUser = &msgraph.User{
AccountEnabled: P.Bool(true),
UserPrincipalName: P.String("[email protected]"),
MailNickname: P.String("hoge-moge"),
DisplayName: P.String("Hoge Moge"),
GivenName: P.String("Hoge"),
Surname: P.String("Moge"),
PasswordProfile: &msgraph.PasswordProfile{
ForceChangePasswordNextSignIn: P.Bool(false),
Password: P.String(uuid.New().String()),
},
}
// Default group: you should replace it with your own
var defaultGroup = &msgraph.Group{
SecurityEnabled: P.Bool(true),
MailEnabled: P.Bool(false),
MailNickname: P.String("fuga"),
DisplayName: P.String("Fuga"),
}
func dump(o interface{}) {
enc := jsonx.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.Encode(o)
}
func main() {
var tenantID, clientID, clientSecret string
flag.StringVar(&tenantID, "tenant-id", defaultTenantID, "Tenant ID")
flag.StringVar(&clientID, "client-id", defaultClientID, "Client ID")
flag.StringVar(&clientSecret, "client-secret", defaultClientSecret, "Client Secret")
flag.Parse()
ctx := context.Background()
m := msauth.NewManager()
scopes := []string{msauth.DefaultMSGraphScope}
ts, err := m.ClientCredentialsGrant(ctx, tenantID, clientID, clientSecret, scopes)
if err != nil {
log.Fatal(err)
}
httpClient := oauth2.NewClient(ctx, ts)
graphClient := msgraph.NewClient(httpClient)
{
// Search and delete default user
r := graphClient.Users().Request()
r.Filter(fmt.Sprintf("userPrincipalName eq '%s'", *defaultUser.UserPrincipalName))
users, _ := r.Get(ctx)
for _, user := range users {
log.Printf("Delete user %s", *user.ID)
graphClient.Users().ID(*user.ID).Request().Delete(ctx)
}
}
{
// Search and delete default group
r := graphClient.Groups().Request()
r.Filter(fmt.Sprintf("mailNickname eq '%s'", *defaultGroup.MailNickname))
groups, _ := r.Get(ctx)
for _, group := range groups {
log.Printf("Delete group %s", *group.ID)
graphClient.Groups().ID(*group.ID).Request().Delete(ctx)
}
}
var u *msgraph.User
{
log.Printf("Create user %s", *defaultUser.UserPrincipalName)
u, err = graphClient.Users().Request().Add(ctx, defaultUser)
if err != nil {
log.Fatal(err)
}
dump(u)
}
var g *msgraph.Group
{
log.Printf("Create group %s", *defaultGroup.DisplayName)
g, err = graphClient.Groups().Request().Add(ctx, defaultGroup)
if err != nil {
log.Fatal(err)
}
dump(g)
}
{
log.Printf("Add member %s to group %s", *u.ID, *g.ID)
reqObj := map[string]interface{}{
"@odata.id": graphClient.DirectoryObjects().ID(*u.ID).Request().URL(),
}
r := graphClient.Groups().ID(*g.ID).Members().Request()
err := r.JSONRequest(ctx, "POST", "/$ref", reqObj, nil)
if err != nil {
log.Fatal(err)
}
}
{
log.Printf("Show user %s", *u.ID)
r := graphClient.Users().ID(*u.ID).Request()
r.Expand("memberOf")
users, err := r.Get(ctx)
if err != nil {
log.Fatal(err)
}
dump(users)
}
{
log.Printf("Show group %s", *g.ID)
r := graphClient.Groups().ID(*g.ID).Request()
r.Expand("members")
groups, err := r.Get(ctx)
if err != nil {
log.Fatal(err)
}
dump(groups)
}
}