-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
provider "azuread" { | ||
// Define environment variables | ||
// ARM_TENANT_ID, ARM_CLIENT_ID, ARM_CLIENT_SECRET, ARM_SUBSCRIPTION_ID (dummy) | ||
} | ||
|
||
variable "config" { | ||
type = object({ domain = string, name = string, user_count = number, group_count = number }) | ||
default = { domain = "l0wdev.onmicrosoft.com", name = "azureadtf", user_count = 100, group_count = 10 } | ||
} | ||
|
||
provider random {} | ||
|
||
resource "random_password" "password" { | ||
length = 16 | ||
} | ||
|
||
resource "azuread_user" "demo_users" { | ||
count = var.config.user_count | ||
user_principal_name = "${var.config.name}user${count.index}@${var.config.domain}" | ||
display_name = "${var.config.name} user ${count.index}" | ||
mail_nickname = "${var.config.name}user${count.index}" | ||
password = random_password.password.result | ||
} | ||
|
||
resource "azuread_group" "demo_groups" { | ||
count = var.config.group_count | ||
name = "${var.config.name} group ${count.index}" | ||
} | ||
|
||
resource "azuread_group_member" "demo_group_user_members" { | ||
count = var.config.user_count | ||
group_object_id = azuread_group.demo_groups[count.index % var.config.group_count].id | ||
member_object_id = azuread_user.demo_users[count.index].id | ||
} | ||
|
||
resource "azuread_group_member" "demo_group_group_members" { | ||
count = var.config.group_count - 1 | ||
group_object_id = azuread_group.demo_groups[count.index].id | ||
member_object_id = azuread_group.demo_groups[count.index + 1].id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
msgraphgo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/google/uuid" | ||
"github.com/yaegashi/msgraph.go/jsonx" | ||
"github.com/yaegashi/msgraph.go/msauth" | ||
P "github.com/yaegashi/msgraph.go/ptr" | ||
msgraph "github.com/yaegashi/msgraph.go/v1.0" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
func dump(o interface{}) { | ||
enc := jsonx.NewEncoder(os.Stdout) | ||
enc.SetIndent("", " ") | ||
enc.Encode(o) | ||
} | ||
|
||
func main() { | ||
var tenantDomain, tenantID, clientID, clientSecret string | ||
var userCount, groupCount int | ||
var clean bool | ||
flag.StringVar(&tenantDomain, "tenant-domain", "l0wdev.onmicrosoft.com", "Tenant Domain") | ||
flag.StringVar(&tenantID, "tenant-id", os.Getenv("ARM_TENANT_ID"), "Tenant ID") | ||
flag.StringVar(&clientID, "client-id", os.Getenv("ARM_CLIENT_ID"), "Client ID") | ||
flag.StringVar(&clientSecret, "client-secret", os.Getenv("ARM_CLIENT_SECRET"), "Client Secret") | ||
flag.IntVar(&userCount, "user-count", 100, "User count") | ||
flag.IntVar(&groupCount, "group-count", 10, "Group count") | ||
flag.BoolVar(&clean, "clean", false, "Clean") | ||
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) | ||
|
||
if clean { | ||
for i := 0; i < userCount; i++ { | ||
r := graphClient.Users().Request() | ||
r.Filter(fmt.Sprintf("mailNickname eq 'msgraphgouser%d'", i)) | ||
users, _ := r.Get(ctx) | ||
for _, user := range users { | ||
log.Printf("Delete user %d", i) | ||
graphClient.Users().ID(*user.ID).Request().Delete(ctx) | ||
} | ||
} | ||
for i := 0; i < groupCount; i++ { | ||
r := graphClient.Groups().Request() | ||
r.Filter(fmt.Sprintf("mailNickname eq 'msgraphgogroup%d'", i)) | ||
groups, _ := r.Get(ctx) | ||
for _, group := range groups { | ||
log.Printf("Delete group %d", i) | ||
graphClient.Groups().ID(*group.ID).Request().Delete(ctx) | ||
} | ||
} | ||
return | ||
} | ||
|
||
users := make([]*msgraph.User, userCount) | ||
for i := 0; i < userCount; i++ { | ||
user := &msgraph.User{ | ||
AccountEnabled: P.Bool(true), | ||
UserPrincipalName: P.String(fmt.Sprintf("msgraphgouser%d@%s", i, tenantDomain)), | ||
MailNickname: P.String(fmt.Sprintf("msgraphgouser%d", i)), | ||
DisplayName: P.String(fmt.Sprintf("msgraphgo user %d", i)), | ||
PasswordProfile: &msgraph.PasswordProfile{ | ||
ForceChangePasswordNextSignIn: P.Bool(false), | ||
Password: P.String(uuid.New().String()), | ||
}, | ||
} | ||
log.Printf("Create user %d", i) | ||
users[i], err = graphClient.Users().Request().Add(ctx, user) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
groups := make([]*msgraph.Group, groupCount) | ||
for i := 0; i < groupCount; i++ { | ||
group := &msgraph.Group{ | ||
SecurityEnabled: P.Bool(true), | ||
MailEnabled: P.Bool(false), | ||
MailNickname: P.String(fmt.Sprintf("msgraphgogroup%d", i)), | ||
DisplayName: P.String(fmt.Sprintf("msgraphgo group %d", i)), | ||
} | ||
log.Printf("Create group %d", i) | ||
groups[i], err = graphClient.Groups().Request().Add(ctx, group) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
for i := 0; i < userCount; i++ { | ||
member := users[i] | ||
group := groups[i%groupCount] | ||
log.Printf("Add user %d to group %d", i, i%groupCount) | ||
reqObj := map[string]interface{}{ | ||
"@odata.id": graphClient.DirectoryObjects().ID(*member.ID).Request().URL(), | ||
} | ||
r := graphClient.Groups().ID(*group.ID).Members().Request() | ||
err := r.JSONRequest(ctx, "POST", "/$ref", reqObj, nil) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
for i := 0; i < groupCount-1; i++ { | ||
member := groups[i+1] | ||
group := groups[i] | ||
log.Printf("Add group %d to group %d", i+1, i) | ||
reqObj := map[string]interface{}{ | ||
"@odata.id": graphClient.DirectoryObjects().ID(*member.ID).Request().URL(), | ||
} | ||
r := graphClient.Groups().ID(*group.ID).Members().Request() | ||
err := r.JSONRequest(ctx, "POST", "/$ref", reqObj, nil) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
provider "msgraph" { | ||
// Define environment variables: | ||
// ARM_TENANT_ID, ARM_CLIENT_ID, ARM_CLIENT_SECRET | ||
} | ||
|
||
variable "config" { | ||
type = object({ domain = string, name = string, user_count = number, group_count = number }) | ||
default = { domain = "l0wdev.onmicrosoft.com", name = "msgraphtf", user_count = 100, group_count = 10 } | ||
} | ||
|
||
resource "msgraph_user" "demo_users" { | ||
count = var.config.user_count | ||
user_principal_name = "${var.config.name}user${count.index}@${var.config.domain}" | ||
display_name = "${var.config.name} user ${count.index}" | ||
mail_nickname = "${var.config.name}user${count.index}" | ||
account_enabled = true | ||
} | ||
|
||
resource "msgraph_group" "demo_groups" { | ||
count = var.config.group_count | ||
display_name = "${var.config.name} group ${count.index}" | ||
mail_nickname = "${var.config.name}group${count.index}" | ||
} | ||
|
||
resource "msgraph_group_member" "demo_group_user_members" { | ||
count = var.config.user_count | ||
group_id = msgraph_group.demo_groups[count.index % var.config.group_count].id | ||
member_id = msgraph_user.demo_users[count.index].id | ||
} | ||
|
||
resource "msgraph_group_member" "demo_group_group_members" { | ||
count = var.config.group_count - 1 | ||
group_id = msgraph_group.demo_groups[count.index].id | ||
member_id = msgraph_group.demo_groups[count.index + 1].id | ||
} |