package m365 import ( "context" "github.com/alcionai/clues" "github.com/microsoftgraph/msgraph-sdk-go/models" "github.com/alcionai/corso/src/internal/common/idname" "github.com/alcionai/corso/src/internal/common/ptr" "github.com/alcionai/corso/src/internal/m365/graph" "github.com/alcionai/corso/src/pkg/account" "github.com/alcionai/corso/src/pkg/fault" "github.com/alcionai/corso/src/pkg/path" ) // Site is the minimal information required to identify and display a SharePoint site. type Site struct { // WebURL is the url for the site, works as an alias for the user name. WebURL string // ID is of the format: .. // for example: contoso.sharepoint.com,abcdeab3-0ccc-4ce1-80ae-b32912c9468d,xyzud296-9f7c-44e1-af81-3c06d0d43007 ID string // DisplayName is the human-readable name of the site. Normally the plaintext name that the // user provided when they created the site, though it can be changed across time. // Ex: webUrl: https://host.com/sites/TestingSite, displayName: "Testing Site" DisplayName string } // Sites returns a list of Sites in a specified M365 tenant func Sites(ctx context.Context, acct account.Account, errs *fault.Bus) ([]*Site, error) { ac, err := makeAC(ctx, acct, path.SharePointService) if err != nil { return nil, clues.Stack(err).WithClues(ctx) } return getAllSites(ctx, ac.Sites()) } func getAllSites( ctx context.Context, ga getAller[models.Siteable], ) ([]*Site, error) { sites, err := ga.GetAll(ctx, fault.New(true)) if err != nil { if clues.HasLabel(err, graph.LabelsNoSharePointLicense) { return nil, clues.Stack(graph.ErrServiceNotEnabled, err) } return nil, clues.Wrap(err, "retrieving sites") } ret := make([]*Site, 0, len(sites)) for _, s := range sites { ps, err := parseSite(s) if err != nil { return nil, clues.Wrap(err, "parsing siteable") } ret = append(ret, ps) } return ret, nil } // parseSite extracts the information from `models.Siteable` we care about func parseSite(item models.Siteable) (*Site, error) { s := &Site{ ID: ptr.Val(item.GetId()), WebURL: ptr.Val(item.GetWebUrl()), DisplayName: ptr.Val(item.GetDisplayName()), } return s, nil } // SitesMap retrieves all sites in the tenant, and returns two maps: one id-to-webURL, // and one webURL-to-id. func SitesMap( ctx context.Context, acct account.Account, errs *fault.Bus, ) (idname.Cacher, error) { sites, err := Sites(ctx, acct, errs) if err != nil { return idname.NewCache(nil), err } itn := make(map[string]string, len(sites)) for _, s := range sites { itn[s.ID] = s.WebURL } return idname.NewCache(itn), nil }