Manual iteration for calendar events (#1628)

## Description

Will fit better with how incremental backups are planned to work in the future as next links can be extracted.

## Type of change

<!--- Please check the type of change your PR introduces: --->
- [x] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Test
- [ ] 💻 CI/Deployment
- [ ] 🐹 Trivial/Minor

## Issue(s)

part of:
* closes #1612 

## Test Plan

- [x] 💪 Manual
- [x]  Unit test
- [ ] 💚 E2E
This commit is contained in:
ashmrtn 2022-12-02 12:44:07 -08:00 committed by GitHub
parent e767bb0b77
commit a8d855a9da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 36 deletions

View File

@ -6,6 +6,7 @@ import (
abs "github.com/microsoft/kiota-abstractions-go" abs "github.com/microsoft/kiota-abstractions-go"
msuser "github.com/microsoftgraph/msgraph-sdk-go/users" msuser "github.com/microsoftgraph/msgraph-sdk-go/users"
mscalendars "github.com/microsoftgraph/msgraph-sdk-go/users/item/calendars" mscalendars "github.com/microsoftgraph/msgraph-sdk-go/users/item/calendars"
mscevents "github.com/microsoftgraph/msgraph-sdk-go/users/item/calendars/item/events"
mscontactfolder "github.com/microsoftgraph/msgraph-sdk-go/users/item/contactfolders" mscontactfolder "github.com/microsoftgraph/msgraph-sdk-go/users/item/contactfolders"
mscontactfolderitem "github.com/microsoftgraph/msgraph-sdk-go/users/item/contactfolders/item" mscontactfolderitem "github.com/microsoftgraph/msgraph-sdk-go/users/item/contactfolders/item"
mscontactfolderchild "github.com/microsoftgraph/msgraph-sdk-go/users/item/contactfolders/item/childfolders" mscontactfolderchild "github.com/microsoftgraph/msgraph-sdk-go/users/item/contactfolders/item/childfolders"
@ -317,6 +318,24 @@ func optionsForContactFoldersItem(
return options, nil return options, nil
} }
// optionsForEvents ensures valid option inputs for exchange.Events
// @return is first call in Events().GetWithRequestConfigurationAndResponseHandler(options, handler)
func optionsForCalendarEvents(moreOps []string) (*mscevents.EventsRequestBuilderGetRequestConfiguration, error) {
selecting, err := buildOptions(moreOps, events)
if err != nil {
return nil, err
}
requestParameters := &mscevents.EventsRequestBuilderGetQueryParameters{
Select: selecting,
}
options := &mscevents.EventsRequestBuilderGetRequestConfiguration{
QueryParameters: requestParameters,
}
return options, nil
}
// optionsForEvents ensures valid option inputs for exchange.Events // optionsForEvents ensures valid option inputs for exchange.Events
// @return is first call in Events().GetWithRequestConfigurationAndResponseHandler(options, handler) // @return is first call in Events().GetWithRequestConfigurationAndResponseHandler(options, handler)
func optionsForEvents(moreOps []string) (*msevents.EventsRequestBuilderGetRequestConfiguration, error) { func optionsForEvents(moreOps []string) (*msevents.EventsRequestBuilderGetRequestConfiguration, error) {

View File

@ -6,8 +6,8 @@ import (
"strings" "strings"
multierror "github.com/hashicorp/go-multierror" multierror "github.com/hashicorp/go-multierror"
msgraphgocore "github.com/microsoftgraph/msgraph-sdk-go-core"
"github.com/microsoftgraph/msgraph-sdk-go/models" "github.com/microsoftgraph/msgraph-sdk-go/models"
msevents "github.com/microsoftgraph/msgraph-sdk-go/users/item/calendars/item/events"
cdelta "github.com/microsoftgraph/msgraph-sdk-go/users/item/contactfolders/item/contacts/delta" cdelta "github.com/microsoftgraph/msgraph-sdk-go/users/item/contactfolders/item/contacts/delta"
mdelta "github.com/microsoftgraph/msgraph-sdk-go/users/item/mailfolders/item/messages/delta" mdelta "github.com/microsoftgraph/msgraph-sdk-go/users/item/mailfolders/item/messages/delta"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -166,50 +166,47 @@ func FetchEventIDsFromCalendar(
gs graph.Service, gs graph.Service,
user, calendarID string, user, calendarID string,
) ([]string, error) { ) ([]string, error) {
ids := []string{} var (
errs *multierror.Error
ids []string
)
response, err := gs.Client(). options, err := optionsForCalendarEvents([]string{"id"})
if err != nil {
return nil, err
}
builder := gs.Client().
UsersById(user). UsersById(user).
CalendarsById(calendarID). CalendarsById(calendarID).
Events().Get(ctx, nil) Events()
if err != nil {
return nil, errors.Wrap(err, support.ConnectorStackErrorTrace(err))
}
pageIterator, err := msgraphgocore.NewPageIterator( for {
response, resp, err := builder.Get(ctx, options)
gs.Adapter(), if err != nil {
models.CreateEventCollectionResponseFromDiscriminatorValue, return nil, errors.Wrap(err, support.ConnectorStackErrorTrace(err))
)
if err != nil {
return nil, errors.Wrap(err, "iterator creation failure during fetchEventIDs")
}
var errs *multierror.Error
err = pageIterator.Iterate(ctx, func(pageItem any) bool {
entry, ok := pageItem.(graph.Idable)
if !ok {
errs = multierror.Append(errs, errors.New("item without GetId() call"))
return true
} }
if entry.GetId() == nil { for _, item := range resp.GetValue() {
errs = multierror.Append(errs, errors.New("item with nil ID")) if item.GetId() == nil {
return true errs = multierror.Append(
errs,
errors.Errorf("event with nil ID in calendar %s", calendarID),
)
// TODO(ashmrtn): Handle fail-fast.
continue
}
ids = append(ids, *item.GetId())
} }
ids = append(ids, *entry.GetId()) nextLink := resp.GetOdataNextLink()
if nextLink == nil || len(*nextLink) == 0 {
break
}
return true builder = msevents.NewEventsRequestBuilder(*nextLink, gs.Adapter())
})
if err != nil {
return nil, errors.Wrap(
err,
support.ConnectorStackErrorTrace(err)+
" :fetching events from calendar "+calendarID,
)
} }
return ids, errs.ErrorOrNil() return ids, errs.ErrorOrNil()