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

Support retrieving directory roles by their template ID #101

Merged
merged 1 commit into from
Sep 14, 2021
Merged
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
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