Skip to content

Commit

Permalink
Support retrieving directory roles by their template ID
Browse files Browse the repository at this point in the history
  • Loading branch information
manicminer committed Sep 30, 2021
1 parent c8c81e1 commit faa460d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
27 changes: 27 additions & 0 deletions msgraph/directory_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions msgraph/directory_roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit faa460d

Please sign in to comment.