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

Implementation for retrying requests #57

Merged
merged 4 commits into from
Jun 8, 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
7 changes: 4 additions & 3 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ func main() {

users, _, err := client.List(ctx, "")
if err != nil {
log.Fatal(err)
log.Println(err)
return
}
if users == nil {
log.Fatalln("bad API response, nil result received")
log.Println("bad API response, nil result received")
return
}

for _, user := range *users {
fmt.Printf("%s: %s <%s>\n", *user.ID, *user.DisplayName, *user.UserPrincipalName)
}
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module github.com/manicminer/hamilton
go 1.16

require (
github.com/golang/protobuf v1.4.3 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/hashicorp/go-uuid v1.0.2
github.com/hashicorp/go-version v1.2.1
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
golang.org/x/net v0.0.0-20201209123823-ac852fbbde11 // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
github.com/hashicorp/go-version v1.3.0
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c
google.golang.org/appengine v1.6.7 // indirect
)
355 changes: 344 additions & 11 deletions go.sum

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions msgraph/app_role_assignments.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ func (c *AppRoleAssignmentsClient) List(ctx context.Context, id string) (*[]AppR
// Remove removes a app role assignment.
func (c *AppRoleAssignmentsClient) Remove(ctx context.Context, id, appRoleAssignmentId string) (int, error) {
_, status, _, err := c.BaseClient.Delete(ctx, DeleteHttpRequestInput{
ValidStatusCodes: []int{http.StatusNoContent},
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusNoContent},
Uri: Uri{
Entity: fmt.Sprintf("/%s/%s/appRoleAssignments/%s", c.resourceType, id, appRoleAssignmentId),
HasTenantId: true,
Expand Down Expand Up @@ -105,8 +106,9 @@ func (c *AppRoleAssignmentsClient) Assign(ctx context.Context, clientServicePrin
return nil, status, fmt.Errorf("json.Marshal(): %v", err)
}
resp, status, _, err := c.BaseClient.Post(ctx, PostHttpRequestInput{
Body: body,
ValidStatusCodes: []int{http.StatusCreated},
Body: body,
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusCreated},
Uri: Uri{
Entity: fmt.Sprintf("/%s/%s/appRoleAssignments", c.resourceType, clientServicePrincipalId),
HasTenantId: true,
Expand Down
57 changes: 32 additions & 25 deletions msgraph/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ func (c *ApplicationsClient) Create(ctx context.Context, application Application
// Get retrieves an Application manifest.
func (c *ApplicationsClient) Get(ctx context.Context, id string) (*Application, int, error) {
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
ValidStatusCodes: []int{http.StatusOK},
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: fmt.Sprintf("/applications/%s", id),
HasTenantId: true,
Expand All @@ -113,7 +114,8 @@ func (c *ApplicationsClient) Get(ctx context.Context, id string) (*Application,
// 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},
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: fmt.Sprintf("/directory/deletedItems/%s", id),
HasTenantId: true,
Expand Down Expand Up @@ -145,8 +147,9 @@ func (c *ApplicationsClient) Update(ctx context.Context, application Application
return status, fmt.Errorf("json.Marshal(): %v", err)
}
_, status, _, err = c.BaseClient.Patch(ctx, PatchHttpRequestInput{
Body: body,
ValidStatusCodes: []int{http.StatusNoContent},
Body: body,
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusNoContent},
Uri: Uri{
Entity: fmt.Sprintf("/applications/%s", *application.ID),
HasTenantId: true,
Expand All @@ -161,7 +164,8 @@ func (c *ApplicationsClient) Update(ctx context.Context, application Application
// Delete removes an Application.
func (c *ApplicationsClient) Delete(ctx context.Context, id string) (int, error) {
_, status, _, err := c.BaseClient.Delete(ctx, DeleteHttpRequestInput{
ValidStatusCodes: []int{http.StatusNoContent},
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusNoContent},
Uri: Uri{
Entity: fmt.Sprintf("/applications/%s", id),
HasTenantId: true,
Expand All @@ -177,7 +181,8 @@ func (c *ApplicationsClient) Delete(ctx context.Context, id string) (int, error)
// id is the object ID of the application.
func (c *ApplicationsClient) DeletePermanently(ctx context.Context, id string) (int, error) {
_, status, _, err := c.BaseClient.Delete(ctx, DeleteHttpRequestInput{
ValidStatusCodes: []int{http.StatusNoContent},
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusNoContent},
Uri: Uri{
Entity: fmt.Sprintf("/directory/deletedItems/%s", id),
HasTenantId: true,
Expand Down Expand Up @@ -229,8 +234,9 @@ func (c *ApplicationsClient) AddPassword(ctx context.Context, applicationId stri
return nil, status, fmt.Errorf("json.Marshal(): %v", err)
}
resp, status, _, err := c.BaseClient.Post(ctx, PostHttpRequestInput{
Body: body,
ValidStatusCodes: []int{http.StatusOK, http.StatusCreated},
Body: body,
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusOK, http.StatusCreated},
Uri: Uri{
Entity: fmt.Sprintf("/applications/%s/addPassword", applicationId),
HasTenantId: true,
Expand Down Expand Up @@ -263,8 +269,9 @@ func (c *ApplicationsClient) RemovePassword(ctx context.Context, applicationId s
return status, fmt.Errorf("json.Marshal(): %v", err)
}
_, status, _, err = c.BaseClient.Post(ctx, PostHttpRequestInput{
Body: body,
ValidStatusCodes: []int{http.StatusOK, http.StatusNoContent},
Body: body,
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusOK, http.StatusNoContent},
Uri: Uri{
Entity: fmt.Sprintf("/applications/%s/removePassword", applicationId),
HasTenantId: true,
Expand All @@ -280,7 +287,8 @@ func (c *ApplicationsClient) RemovePassword(ctx context.Context, applicationId s
// id is the object ID of the application.
func (c *ApplicationsClient) ListOwners(ctx context.Context, id string) (*[]string, int, error) {
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
ValidStatusCodes: []int{http.StatusOK},
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: fmt.Sprintf("/applications/%s/owners", id),
Params: url.Values{"$select": []string{"id"}},
Expand Down Expand Up @@ -316,7 +324,8 @@ func (c *ApplicationsClient) ListOwners(ctx context.Context, id string) (*[]stri
// ownerId is the object ID of the owning object.
func (c *ApplicationsClient) GetOwner(ctx context.Context, applicationId, ownerId string) (*string, int, error) {
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
ValidStatusCodes: []int{http.StatusOK},
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: fmt.Sprintf("/applications/%s/owners/%s/$ref", applicationId, ownerId),
Params: url.Values{"$select": []string{"id,url"}},
Expand Down Expand Up @@ -356,10 +365,8 @@ func (c *ApplicationsClient) AddOwners(ctx context.Context, application *Applica
for _, owner := range *application.Owners {
// don't fail if an owner already exists
checkOwnerAlreadyExists := func(resp *http.Response, o *odata.OData) bool {
if resp.StatusCode == http.StatusBadRequest {
if o.Error != nil {
return o.Error.Match(odata.ErrorAddedObjectReferencesAlreadyExist)
}
if resp.StatusCode == http.StatusBadRequest && o.Error != nil {
return o.Error.Match(odata.ErrorAddedObjectReferencesAlreadyExist)
}
return false
}
Expand All @@ -374,9 +381,10 @@ func (c *ApplicationsClient) AddOwners(ctx context.Context, application *Applica
return status, fmt.Errorf("json.Marshal(): %v", err)
}
_, status, _, err = c.BaseClient.Post(ctx, PostHttpRequestInput{
Body: body,
ValidStatusCodes: []int{http.StatusNoContent},
ValidStatusFunc: checkOwnerAlreadyExists,
Body: body,
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusNoContent},
ValidStatusFunc: checkOwnerAlreadyExists,
Uri: Uri{
Entity: fmt.Sprintf("/applications/%s/owners/$ref", *application.ID),
HasTenantId: true,
Expand Down Expand Up @@ -408,18 +416,17 @@ func (c *ApplicationsClient) RemoveOwners(ctx context.Context, applicationId str

// despite the above check, sometimes owners are just gone
checkOwnerGone := func(resp *http.Response, o *odata.OData) bool {
if resp.StatusCode == http.StatusBadRequest {
if o.Error != nil {
return o.Error.Match(odata.ErrorRemovedObjectReferencesDoNotExist)
}
if resp.StatusCode == http.StatusBadRequest && o.Error != nil {
return o.Error.Match(odata.ErrorRemovedObjectReferencesDoNotExist)
}
return false
}

var err error
_, status, _, err = c.BaseClient.Delete(ctx, DeleteHttpRequestInput{
ValidStatusCodes: []int{http.StatusNoContent},
ValidStatusFunc: checkOwnerGone,
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusNoContent},
ValidStatusFunc: checkOwnerGone,
Uri: Uri{
Entity: fmt.Sprintf("/applications/%s/owners/%s/$ref", applicationId, ownerId),
HasTenantId: true,
Expand Down
Loading