diff --git a/src/pkg/services/m365/m365.go b/src/pkg/services/m365/m365.go index 91141696f..b08cf7926 100644 --- a/src/pkg/services/m365/m365.go +++ b/src/pkg/services/m365/m365.go @@ -312,6 +312,86 @@ func SitesMap( return idname.NewCache(itn), nil } +// --------------------------------------------------------------------------- +// Teams +// --------------------------------------------------------------------------- + +// Team is the minimal information required to identify and display a M365 Team. +type Team struct { + ID string + + // DisplayName is the human-readable name of the team. Normally the plaintext name that the + // user provided when they created the team or the updated name if it was changed. + // Ex: displayName: "Testing Team" + DisplayName string +} + +// TeamsCompat returns a list of teams in the specified M365 tenant. +func TeamsCompat(ctx context.Context, acct account.Account) ([]*Team, error) { + errs := fault.New(true) + + us, err := Teams(ctx, acct, errs) + if err != nil { + return nil, err + } + + return us, errs.Failure() +} + +// Teams returns a list of teams in the specified M365 tenant +func Teams(ctx context.Context, acct account.Account, errs *fault.Bus) ([]*Team, error) { + ac, err := makeAC(ctx, acct, path.TeamsService) + if err != nil { + return nil, clues.Stack(err).WithClues(ctx) + } + + return getAllTeams(ctx, ac.Groups()) +} + +// parseUser extracts information from `models.Userable` we care about +func parseTeam(item models.Groupable) (*Team, error) { + if item.GetDisplayName() == nil { + return nil, clues.New("Team missing display name"). + With("Team ID", ptr.Val(item.GetId())) + } + + u := &Team{ + ID: ptr.Val(item.GetId()), + DisplayName: ptr.Val(item.GetDisplayName()), + } + + return u, nil +} + +type getAllGroupers interface { + GetAll(ctx context.Context, filterTeams bool, errs *fault.Bus) ([]models.Groupable, error) +} + +func getAllTeams(ctx context.Context, gas getAllGroupers) ([]*Team, error) { + teams, err := gas.GetAll(ctx, true, fault.New(true)) + // TODO: check this. Label has to be changed + if err != nil { + if clues.HasLabel(err, graph.LabelsNoSharePointLicense) { + return nil, clues.Stack(graph.ErrServiceNotEnabled, err) + } + + return nil, clues.Wrap(err, "retrieving teams") + } + + ret := make([]*Team, 0, len(teams)) + + for _, team := range teams { + t, err := parseTeam(team) + if err != nil { + return nil, clues.Wrap(err, "parsing teams") + } + + ret = append(ret, t) + } + + return ret, nil +} + // --------------------------------------------------------------------------- // helpers // ---------------------------------------------------------------------------