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

Get deleted API for apps, groups, and users #51

Merged
merged 6 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
25 changes: 25 additions & 0 deletions msgraph/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,31 @@ func (c *ApplicationsClient) Get(ctx context.Context, id string) (*Application,
return &application, status, nil
}

// GetDeleted retrieves a deleted Application manifest.
// id is the object ID of the application.
func (c *ApplicationsClient) GetDeleted(ctx context.Context, id string) (*Application, int, error) {
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: fmt.Sprintf("/directory/deletedItems/%s", id),
HasTenantId: true,
},
})
if err != nil {
return nil, status, fmt.Errorf("ApplicationsClient.BaseClient.GetDeleted(): %v", err)
manicminer marked this conversation as resolved.
Show resolved Hide resolved
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("ioutil.ReadAll(): %v", err)
}
var application Application
if err := json.Unmarshal(respBody, &application); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}
return &application, status, nil
}

// Update amends the manifest of an existing Application.
func (c *ApplicationsClient) Update(ctx context.Context, application Application) (int, error) {
var status int
Expand Down
15 changes: 15 additions & 0 deletions msgraph/applications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestApplicationsClient(t *testing.T) {
testApplicationsClient_List(t, c)
testApplicationsClient_Delete(t, c, *app.ID)
testApplicationsClient_ListDeleted(t, c, *app.ID)
testApplicationsClient_GetDeleted(t, c, *app.ID)
}

func TestApplicationsClient_groupMembershipClaims(t *testing.T) {
Expand Down Expand Up @@ -109,6 +110,20 @@ func testApplicationsClient_Get(t *testing.T, c ApplicationsClientTest, id strin
return
}

func testApplicationsClient_GetDeleted(t *testing.T, c ApplicationsClientTest, id string) (application *msgraph.Application) {
application, status, err := c.client.GetDeleted(c.connection.Context, id)
if err != nil {
t.Fatalf("ApplicationsClient.GetDeleted(): %v", err)
}
if status < 200 || status >= 300 {
t.Fatalf("ApplicationsClient.GetDeleted(): invalid status: %d", status)
}
if application == nil {
t.Fatal("ApplicationsClient.GetDeleted(): application was nil")
}
return
}

func testApplicationsClient_Delete(t *testing.T, c ApplicationsClientTest, id string) {
status, err := c.client.Delete(c.connection.Context, id)
if err != nil {
Expand Down
24 changes: 24 additions & 0 deletions msgraph/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,30 @@ func (c *GroupsClient) Get(ctx context.Context, id string) (*Group, int, error)
return &group, status, nil
}

// GetDeleted retrieves a deleted O365 Group.
func (c *GroupsClient) GetDeleted(ctx context.Context, id string) (*Group, int, error) {
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: fmt.Sprintf("/directory/deletedItems/%s", id),
HasTenantId: true,
},
})
if err != nil {
return nil, status, fmt.Errorf("GroupsClient.BaseClient.GetDeleted(): %v", err)
manicminer marked this conversation as resolved.
Show resolved Hide resolved
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("ioutil.ReadAll(): %v", err)
}
var group Group
if err := json.Unmarshal(respBody, &group); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}
return &group, status, nil
}

// Update amends an existing Group.
func (c *GroupsClient) Update(ctx context.Context, group Group) (int, error) {
var status int
Expand Down
24 changes: 24 additions & 0 deletions msgraph/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@ func (c *UsersClient) Get(ctx context.Context, id string) (*User, int, error) {
return &user, status, nil
}

// GetDeleted retrieves a deleted User.
func (c *UsersClient) GetDeleted(ctx context.Context, id string) (*User, int, error) {
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: fmt.Sprintf("/directory/deletedItems/%s", id),
HasTenantId: true,
},
})
if err != nil {
return nil, status, fmt.Errorf("UsersClient.BaseClient.GetDeleted(): %v", err)
manicminer marked this conversation as resolved.
Show resolved Hide resolved
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("ioutil.ReadAll(): %v", err)
}
var user User
if err := json.Unmarshal(respBody, &user); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}
return &user, status, nil
}

// Update amends an existing User.
func (c *UsersClient) Update(ctx context.Context, user User) (int, error) {
var status int
Expand Down
15 changes: 15 additions & 0 deletions msgraph/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func TestUsersClient(t *testing.T) {

testUsersClient_Delete(t, c, *user.ID)
testUsersClient_ListDeleted(t, c, *user.ID)
testUsersClient_GetDeleted(t, c, *user.ID)
}

func testUsersClient_Create(t *testing.T, c UsersClientTest, u msgraph.User) (user *msgraph.User) {
Expand Down Expand Up @@ -126,6 +127,20 @@ func testUsersClient_Get(t *testing.T, c UsersClientTest, id string) (user *msgr
return
}

func testUsersClient_GetDeleted(t *testing.T, c UsersClientTest, id string) (user *msgraph.User) {
user, status, err := c.client.GetDeleted(c.connection.Context, id)
if err != nil {
t.Fatalf("UsersClient.GetDeleted(): %v", err)
}
if status < 200 || status >= 300 {
t.Fatalf("UsersClient.GetDeleted(): invalid status: %d", status)
}
if user == nil {
t.Fatal("UsersClient.GetDeleted(): user was nil")
}
return
}

func testUsersClient_Delete(t *testing.T, c UsersClientTest, id string) {
status, err := c.client.Delete(c.connection.Context, id)
if err != nil {
Expand Down