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

Draft:Add manager #378

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
110 changes: 110 additions & 0 deletions internal/services/aadgraph/graph/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package graph

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"regexp"

"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"

"github.com/Azure/go-autorest/autorest"
"github.com/terraform-providers/terraform-provider-azuread/internal/utils"
)

Expand Down Expand Up @@ -69,3 +74,108 @@ func UserGetByMailNickname(ctx context.Context, client *graphrbac.UsersClient, m

return &user, nil
}

type userManager struct {
Url string `json:"url"`
}

func UserGetManager(client *graphrbac.UsersClient, ctx context.Context, objectID string) (managerObjectId string, err error) {
req, err := userGetManagerPreparer(client, ctx, objectID)

if err != nil {
return "", err
}

resp, err := client.GetSender(req)

if err != nil {
return "", err
}

manager := userManager{}
err = json.NewDecoder(resp.Body).Decode(&manager)

if objectID == "a5a66956-d3dc-4e5c-a31e-9039eba62e34" {
log.Println("YYYYYY")
log.Println(manager.Url)
}

if err != nil {
return "", err
}

uuidRegex := regexp.MustCompile(`\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b`)
managerObjectId = uuidRegex.FindString(manager.Url)

return
}

func UserUpdateManager(client *graphrbac.UsersClient, ctx context.Context, userObjectID, mangerObjectID string) error {
req, err := userUpdateManagerPreparer(client, ctx, userObjectID, mangerObjectID)

if err != nil {
return err
}

resp, err := client.UpdateSender(req)

if err != nil {
return err
}

_, err = client.UpdateResponder(resp)

return err
}

func userGetManagerPreparer(client *graphrbac.UsersClient, ctx context.Context, userObjectId string) (*http.Request, error) {
pathParameters := map[string]interface{}{
"upnOrObjectId": autorest.Encode("path", userObjectId),
}

const APIVersion = "1.6"
queryParameters := map[string]interface{}{
"api-version": APIVersion,
}

preparer := autorest.CreatePreparer(
autorest.AsGet(),
autorest.WithBaseURL(client.BaseURI),
autorest.WithPathParameters("/myorganization/users/{upnOrObjectId}/$links/manager", pathParameters),
autorest.WithQueryParameters(queryParameters))
return preparer.Prepare((&http.Request{}).WithContext(ctx))
}

func userUpdateManagerPreparer(client *graphrbac.UsersClient, ctx context.Context, upnOrObjectID, managerObjectID string) (*http.Request, error) {
pathParameters := map[string]interface{}{
"upnOrObjectId": autorest.Encode("path", upnOrObjectID),
"tenantID": autorest.Encode("path", client.TenantID),
}

const APIVersion = "1.6"
queryParameters := map[string]interface{}{
"api-version": APIVersion,
}

if managerObjectID == "" {
preparer := autorest.CreatePreparer(
autorest.AsDelete(),
autorest.WithBaseURL(client.BaseURI),
autorest.WithPathParameters("/{tenantID}/users/{upnOrObjectId}/$links/manager", pathParameters),
autorest.WithQueryParameters(queryParameters))
return preparer.Prepare((&http.Request{}).WithContext(ctx))
} else {
manager := userManager{
Url: fmt.Sprintf("https://graph.windows.net/%s/directoryObjects/%s", client.TenantID, managerObjectID),
}

preparer := autorest.CreatePreparer(
autorest.AsContentType("application/json; charset=utf-8"),
autorest.AsPut(),
autorest.WithBaseURL(client.BaseURI),
autorest.WithPathParameters("/{tenantID}/users/{upnOrObjectId}/$links/manager", pathParameters),
autorest.WithJSON(manager),
autorest.WithQueryParameters(queryParameters))
return preparer.Prepare((&http.Request{}).WithContext(ctx))
}
}
20 changes: 20 additions & 0 deletions internal/services/aadgraph/user_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ func userResource() *schema.Resource {
Computed: true,
Description: "The primary cellular telephone number for the user.",
},

"manager": {
Type: schema.TypeString,
Optional: true,
Description: "The user's manager.",
},
},
}
}
Expand Down Expand Up @@ -386,6 +392,13 @@ func userResourceUpdate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("updating User with ID %q: %+v", d.Id(), err)
}

if d.HasChange("manager") {
err := graph.UserUpdateManager(client, ctx, d.Id(), d.Get("manager").(string))
if err != nil {
return fmt.Errorf("Failed to update users manger: %w", err)
}
}

return userResourceRead(d, meta)
}

Expand All @@ -405,6 +418,13 @@ func userResourceRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("retrieving User with ID %q: %+v", objectId, err)
}

managerObjectId, err := graph.UserGetManager(client, ctx, objectId)

if err != nil {
return fmt.Errorf("Failed to get user's manger: %w", err)
}

d.Set("manager", managerObjectId)
d.Set("user_principal_name", user.UserPrincipalName)
d.Set("display_name", user.DisplayName)
d.Set("given_name", user.GivenName)
Expand Down