Skip to content

Commit

Permalink
User: add func DeleteUser
Browse files Browse the repository at this point in the history
  • Loading branch information
TerraTalpi committed Aug 19, 2021
1 parent e99e511 commit 37a8c3b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
8 changes: 8 additions & 0 deletions GraphClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ func (g *GraphClient) makePATCHAPICall(apiCall string, reqParams getRequestParam
return g.makeAPICall(apiCall, http.MethodPatch, reqParams, body, v)
}

// makeDELETEAPICall performs an API-Call to the msgraph API.
func (g *GraphClient) makeDELETEAPICall(apiCall string, reqParams getRequestParams, v interface{}) error {
return g.makeAPICall(apiCall, http.MethodDelete, reqParams, nil, v)
}

// makeAPICall performs an API-Call to the msgraph API. This func uses sync.Mutex to synchronize all API-calls.
//
// Parameter httpMethod may be http.MethodGet, http.MethodPost or http.MethodPatch
Expand Down Expand Up @@ -223,6 +228,9 @@ func (g *GraphClient) performRequest(req *http.Request, v interface{}) error {
return fmt.Errorf("HTTP response read error: %v of http.Request: %v", err, req.URL)
}

if req.Method == http.MethodDelete { // no content returned when http DELETE is used, e.g. User.DeleteUser()
return nil
}
return json.Unmarshal(body, &v) // return the error of the json unmarshal
}

Expand Down
33 changes: 33 additions & 0 deletions GraphClient_queryOptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type CreateQueryOption func(opts *createQueryOptions)

type UpdateQueryOption func(opts *updateQueryOptions)

type DeleteQueryOption func(opts *deleteQueryOptions)

var (
// GetWithContext - add a context.Context to the HTTP request e.g. to allow cancellation
GetWithContext = func(ctx context.Context) GetQueryOption {
Expand Down Expand Up @@ -77,6 +79,12 @@ var (
opts.ctx = ctx
}
}
// DeleteWithContext - add a context.Context to the HTTP request e.g. to allow cancellation
DeleteWithContext = func(ctx context.Context) DeleteQueryOption {
return func(opts *deleteQueryOptions) {
opts.ctx = ctx
}
}
)

// getQueryOptions allow to optionally pass OData query options
Expand Down Expand Up @@ -197,3 +205,28 @@ func compileUpdateQueryOptions(options []UpdateQueryOption) *updateQueryOptions

return opts
}

// deleteQueryOptions allows to add a context to the request
type deleteQueryOptions struct {
getQueryOptions
}

func (g *deleteQueryOptions) Context() context.Context {
if g.ctx == nil {
return context.Background()
}
return g.ctx
}

func compileDeleteQueryOptions(options []DeleteQueryOption) *deleteQueryOptions {
var opts = &deleteQueryOptions{
getQueryOptions: getQueryOptions{
queryValues: url.Values{},
},
}
for idx := range options {
options[idx](opts)
}

return opts
}
14 changes: 14 additions & 0 deletions User.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ func (u User) UpdateUser(userInput User, opts ...UpdateQueryOption) error {
return err
}

// DeleteUser deletes this user instance at the Microsoft Azure AD. Use with caution.
//
// Reference: https://docs.microsoft.com/en-us/graph/api/user-delete
func (u User) DeleteUser(opts ...DeleteQueryOption) error {
if u.graphClient == nil {
return ErrNotGraphClientSourced
}
resource := fmt.Sprintf("/users/%v", u.ID)

// TODO: check return body, maybe there is some potential success or error message hidden in it?
err := u.graphClient.makeDELETEAPICall(resource, compileDeleteQueryOptions(opts), nil)
return err
}

// PrettySimpleString returns the User-instance simply formatted for logging purposes: {FullName (email) (activePhone)}
func (u User) PrettySimpleString() string {
return fmt.Sprintf("{ %v (%v) (%v) }", u.GetFullName(), u.Mail, u.GetActivePhone())
Expand Down

0 comments on commit 37a8c3b

Please sign in to comment.