From ad22fe22868b087bb21349f788a7c71c1b4b8be8 Mon Sep 17 00:00:00 2001 From: Tom Bamford Date: Mon, 13 Sep 2021 18:11:30 +0100 Subject: [PATCH] Support retrieving directory roles by their template ID --- msgraph/directory_roles.go | 27 +++++++++++++++++++++++++++ msgraph/directory_roles_test.go | 15 +++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/msgraph/directory_roles.go b/msgraph/directory_roles.go index 5612da1d..d78735d9 100644 --- a/msgraph/directory_roles.go +++ b/msgraph/directory_roles.go @@ -79,6 +79,33 @@ func (c *DirectoryRolesClient) Get(ctx context.Context, id string) (*DirectoryRo return &dirRole, status, nil } +// GetByTemplateId retrieves a DirectoryRole manifest for a DirectoryRoleTemplate id. +func (c *DirectoryRolesClient) GetByTemplateId(ctx context.Context, templateId string) (*DirectoryRole, int, error) { + resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{ + ValidStatusCodes: []int{http.StatusOK}, + Uri: Uri{ + Entity: fmt.Sprintf("/directoryRoles/roleTemplateId=%s", templateId), + HasTenantId: true, + }, + }) + if err != nil { + return nil, status, fmt.Errorf("DirectoryRolesClient.BaseClient.Get(): %v", err) + } + + defer resp.Body.Close() + respBody, err := io.ReadAll(resp.Body) + if err != nil { + return nil, status, fmt.Errorf("io.ReadAll(): %v", err) + } + + var dirRole DirectoryRole + if err := json.Unmarshal(respBody, &dirRole); err != nil { + return nil, status, fmt.Errorf("json.Unmarshal(): %v", err) + } + + return &dirRole, status, nil +} + // ListMembers retrieves the members of the specified directory role. // id is the object ID of the directory role. func (c *DirectoryRolesClient) ListMembers(ctx context.Context, id string) (*[]string, int, error) { diff --git a/msgraph/directory_roles_test.go b/msgraph/directory_roles_test.go index 512f98a6..60c979ac 100644 --- a/msgraph/directory_roles_test.go +++ b/msgraph/directory_roles_test.go @@ -38,6 +38,7 @@ func TestDirectoryRolesClient(t *testing.T) { directoryRoles := testDirectoryRolesClient_List(t, dirRolesClient) directoryRole := (*directoryRoles)[0] testDirectoryRolesClient_Get(t, dirRolesClient, *directoryRole.ID) + testDirectoryRolesClient_GetByTemplateId(t, dirRolesClient, *directoryRole.RoleTemplateId) // create a new test group which can be later assigned as a member of the previously listed directory role newGroup := msgraph.Group{ @@ -89,6 +90,20 @@ func testDirectoryRolesClient_Get(t *testing.T, c DirectoryRolesClientTest, id s return } +func testDirectoryRolesClient_GetByTemplateId(t *testing.T, c DirectoryRolesClientTest, templateId string) (directoryRole *msgraph.DirectoryRole) { + directoryRole, status, err := c.client.GetByTemplateId(c.connection.Context, templateId) + if err != nil { + t.Fatalf("DirectoryRolesClient.Get(): %v", err) + } + if status < 200 || status >= 300 { + t.Fatalf("DirectoryRolesClient.Get(): invalid status: %d", status) + } + if directoryRole == nil { + t.Fatal("DirectoryRolesClient.Get(): directoryRole was nil") + } + return +} + func testDirectoryRolesClient_ListMembers(t *testing.T, c DirectoryRolesClientTest, id string) (members *[]string) { members, status, err := c.client.ListMembers(c.connection.Context, id) if err != nil {