From 80288bc4ff972d7c13f3fe8c017bc9c9f59a0a5f Mon Sep 17 00:00:00 2001 From: Taras Sologub Date: Thu, 27 May 2021 08:31:11 +0200 Subject: [PATCH 1/6] Get deleted application API --- msgraph/applications.go | 25 +++++++++++++++++++++++++ msgraph/applications_test.go | 15 +++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/msgraph/applications.go b/msgraph/applications.go index 24e3b14e..b23d636c 100644 --- a/msgraph/applications.go +++ b/msgraph/applications.go @@ -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) + } + 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 diff --git a/msgraph/applications_test.go b/msgraph/applications_test.go index c05b725d..7031f62e 100644 --- a/msgraph/applications_test.go +++ b/msgraph/applications_test.go @@ -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) { @@ -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 { From a28935e8dd9030f1c312a542a8fd046362e42792 Mon Sep 17 00:00:00 2001 From: Taras Sologub Date: Thu, 27 May 2021 08:47:02 +0200 Subject: [PATCH 2/6] Get deleted group API --- msgraph/groups.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/msgraph/groups.go b/msgraph/groups.go index 470d0c5b..13c1316f 100644 --- a/msgraph/groups.go +++ b/msgraph/groups.go @@ -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) + } + 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 From 175d3fa36dba94bb49562f4f0e5b235e0efa3550 Mon Sep 17 00:00:00 2001 From: Taras Sologub Date: Thu, 27 May 2021 08:51:17 +0200 Subject: [PATCH 3/6] Get deleted user API --- msgraph/users.go | 24 ++++++++++++++++++++++++ msgraph/users_test.go | 15 +++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/msgraph/users.go b/msgraph/users.go index 149751c3..6911fb68 100644 --- a/msgraph/users.go +++ b/msgraph/users.go @@ -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) + } + 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 diff --git a/msgraph/users_test.go b/msgraph/users_test.go index 2d199bd9..9aaa38fa 100644 --- a/msgraph/users_test.go +++ b/msgraph/users_test.go @@ -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) { @@ -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 { From 783b0ffe0f0b9d1477a1dd4a6edd85ca08643483 Mon Sep 17 00:00:00 2001 From: Tom Bamford Date: Thu, 27 May 2021 16:54:17 +0100 Subject: [PATCH 4/6] Error text --- msgraph/applications.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/msgraph/applications.go b/msgraph/applications.go index b23d636c..405b6095 100644 --- a/msgraph/applications.go +++ b/msgraph/applications.go @@ -120,7 +120,7 @@ func (c *ApplicationsClient) GetDeleted(ctx context.Context, id string) (*Applic }, }) if err != nil { - return nil, status, fmt.Errorf("ApplicationsClient.BaseClient.GetDeleted(): %v", err) + return nil, status, fmt.Errorf("ApplicationsClient.BaseClient.Get(): %v", err) } defer resp.Body.Close() respBody, err := ioutil.ReadAll(resp.Body) From 4f4ca4ae2673bb94d15d66922560d3a9bbe40109 Mon Sep 17 00:00:00 2001 From: Tom Bamford Date: Thu, 27 May 2021 16:54:22 +0100 Subject: [PATCH 5/6] Error text --- msgraph/groups.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/msgraph/groups.go b/msgraph/groups.go index 13c1316f..a26f2c0e 100644 --- a/msgraph/groups.go +++ b/msgraph/groups.go @@ -118,7 +118,7 @@ func (c *GroupsClient) GetDeleted(ctx context.Context, id string) (*Group, int, }, }) if err != nil { - return nil, status, fmt.Errorf("GroupsClient.BaseClient.GetDeleted(): %v", err) + return nil, status, fmt.Errorf("GroupsClient.BaseClient.Get(): %v", err) } defer resp.Body.Close() respBody, err := ioutil.ReadAll(resp.Body) From 10c806b6e3b6818eaaf7400a09e28613ec4889af Mon Sep 17 00:00:00 2001 From: Tom Bamford Date: Thu, 27 May 2021 16:54:27 +0100 Subject: [PATCH 6/6] Error text --- msgraph/users.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/msgraph/users.go b/msgraph/users.go index 6911fb68..6b051a65 100644 --- a/msgraph/users.go +++ b/msgraph/users.go @@ -116,7 +116,7 @@ func (c *UsersClient) GetDeleted(ctx context.Context, id string) (*User, int, er }, }) if err != nil { - return nil, status, fmt.Errorf("UsersClient.BaseClient.GetDeleted(): %v", err) + return nil, status, fmt.Errorf("UsersClient.BaseClient.Get(): %v", err) } defer resp.Body.Close() respBody, err := ioutil.ReadAll(resp.Body)