From c4843c467cf3216afa52bf2604ead47779182b49 Mon Sep 17 00:00:00 2001 From: neha-Gupta1 Date: Fri, 8 Sep 2023 13:22:01 +0530 Subject: [PATCH] channel and message generator for testing --- src/cmd/factory/factory.go | 17 ++++- src/cmd/factory/impl/common.go | 53 ++++++++++++++ src/cmd/factory/impl/groups.go | 64 +++++++++++++++++ src/pkg/services/m365/api/channels.go | 100 ++++++++++++++++++++++++++ 4 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 src/cmd/factory/impl/groups.go diff --git a/src/cmd/factory/factory.go b/src/cmd/factory/factory.go index e7201fc50..cb47ff7ec 100644 --- a/src/cmd/factory/factory.go +++ b/src/cmd/factory/factory.go @@ -32,10 +32,16 @@ var oneDriveCmd = &cobra.Command{ var sharePointCmd = &cobra.Command{ Use: "sharepoint", - Short: "Generate shareopint data", + Short: "Generate sharepoint data", RunE: handleSharePointFactory, } +var groupCmd = &cobra.Command{ + Use: "group", + Short: "Generate group data", + RunE: handleGroupFactory, +} + // ------------------------------------------------------------------------------------------ // CLI command handlers // ------------------------------------------------------------------------------------------ @@ -61,6 +67,8 @@ func main() { fs.StringVar(&impl.Tenant, "tenant", "", "m365 tenant containing the user") fs.StringVar(&impl.Site, "site", "", "sharepoint site owning the new data") fs.StringVar(&impl.User, "user", "", "m365 user owning the new data") + fs.StringVar(&impl.Group, "group", "", "groupID of group owning the new data") + fs.StringVar(&impl.SecondaryUser, "secondaryuser", "", "m365 secondary user owning the new data") fs.IntVar(&impl.Count, "count", 0, "count of items to produce") cobra.CheckErr(factoryCmd.MarkPersistentFlagRequired("count")) @@ -73,6 +81,8 @@ func main() { impl.AddOneDriveCommands(oneDriveCmd) factoryCmd.AddCommand(sharePointCmd) impl.AddSharePointCommands(sharePointCmd) + factoryCmd.AddCommand(groupCmd) + impl.AddGroupsCommands(groupCmd) if err := factoryCmd.ExecuteContext(ctx); err != nil { logger.Flush(ctx) @@ -99,3 +109,8 @@ func handleSharePointFactory(cmd *cobra.Command, args []string) error { Err(cmd.Context(), impl.ErrNotYetImplemented) return cmd.Help() } + +func handleGroupFactory(cmd *cobra.Command, args []string) error { + Err(cmd.Context(), impl.ErrNotYetImplemented) + return cmd.Help() +} diff --git a/src/cmd/factory/impl/common.go b/src/cmd/factory/impl/common.go index bec56762f..022f25f0d 100644 --- a/src/cmd/factory/impl/common.go +++ b/src/cmd/factory/impl/common.go @@ -9,6 +9,7 @@ import ( "github.com/alcionai/clues" "github.com/google/uuid" + "github.com/microsoftgraph/msgraph-sdk-go/models" "github.com/alcionai/corso/src/cli/print" "github.com/alcionai/corso/src/internal/common/dttm" @@ -32,6 +33,7 @@ import ( "github.com/alcionai/corso/src/pkg/fault" "github.com/alcionai/corso/src/pkg/path" "github.com/alcionai/corso/src/pkg/selectors" + "github.com/alcionai/corso/src/pkg/services/m365/api" ) var ( @@ -41,6 +43,7 @@ var ( Tenant string User string SecondaryUser string + Group string ) // TODO: ErrGenerating = clues.New("not all items were successfully generated") @@ -455,3 +458,53 @@ func generateAndRestoreDriveItems( return ctrl.ConsumeRestoreCollections(ctx, rcc, collections, errs, ctr) } + +// Channel data creation +func generateAndCreateChannelItems( + ctrl *m365.Controller, + protectedResource idname.Provider, + acct account.Account, + tenantID, destFldr string, + intCount int, + errs *fault.Bus, + ctr *count.Bus, +) ( + *details.Details, + error, +) { + ctx, flush := tester.NewContext(nil) + defer flush() + teamID := protectedResource.ID() + + account, err := acct.M365Config() + if err != nil { + return nil, clues.Wrap(err, "creating api client").WithClues(ctx) + } + + ac, err := api.NewClient(account, control.Options{}) + if err != nil { + return nil, clues.Wrap(err, "creating api client").WithClues(ctx) + } + + chCient := ac.Channels() + + channel, err := chCient.CreateChannel(ctx, protectedResource.ID(), destFldr) + if err != nil { + return nil, clues.Wrap(err, "creating api client").WithClues(ctx) + } + + channelID := ptr.Val(channel.GetId()) + + for i := 0; i < intCount; i++ { + body := models.ItemBody{} + content := fmt.Sprintf("Hello new automated message: %d", 0) + + body.SetContent(&content) + _, err := chCient.PostChannelMessage(ctx, teamID, channelID, &body) + if err != nil { + return nil, clues.Wrap(err, "creating api client").WithClues(ctx) + } + } + + return &details.Details{}, nil +} diff --git a/src/cmd/factory/impl/groups.go b/src/cmd/factory/impl/groups.go new file mode 100644 index 000000000..225928d2e --- /dev/null +++ b/src/cmd/factory/impl/groups.go @@ -0,0 +1,64 @@ +package impl + +import ( + . "github.com/alcionai/corso/src/cli/print" + "github.com/alcionai/corso/src/cli/utils" + "github.com/alcionai/corso/src/internal/m365/resource" + "github.com/alcionai/corso/src/pkg/count" + "github.com/alcionai/corso/src/pkg/fault" + "github.com/alcionai/corso/src/pkg/logger" + "github.com/alcionai/corso/src/pkg/path" + "github.com/alcionai/corso/src/pkg/selectors" + "github.com/spf13/cobra" +) + +var channelCmd = &cobra.Command{ + Use: "channel", + Short: "Generate groups channel messages", + RunE: handleGroupChannelFactory, +} + +func AddGroupsCommands(cmd *cobra.Command) { + cmd.AddCommand(channelCmd) +} + +func handleGroupChannelFactory(cmd *cobra.Command, args []string) error { + var ( + ctx = cmd.Context() + errs = fault.New(false) + ) + + if utils.HasNoFlagsAndShownHelp(cmd) { + return nil + } + + ctrl, acct, inp, err := getControllerAndVerifyResourceOwner(ctx, resource.Groups, Group, path.GroupsService) + + if err != nil { + return Only(ctx, err) + } + + sel := selectors.NewGroupsBackup([]string{Group}).Selector + sel.SetDiscreteOwnerIDName(inp.ID(), inp.Name()) + + deets, err := generateAndCreateChannelItems( + ctrl, + inp, + acct, + Tenant, + Destination, + Count, + errs, + count.New()) + if err != nil { + return Only(ctx, err) + } + + for _, e := range errs.Recovered() { + logger.CtxErr(ctx, err).Error(e.Error()) + } + + deets.PrintEntries(ctx) + + return nil +} diff --git a/src/pkg/services/m365/api/channels.go b/src/pkg/services/m365/api/channels.go index 8a176d4a3..7fc1f8c91 100644 --- a/src/pkg/services/m365/api/channels.go +++ b/src/pkg/services/m365/api/channels.go @@ -100,6 +100,52 @@ func (c Channels) GetChannelByName( return cal, nil } +// CreateChannel makes an channels with the name in the team +func (c Channels) CreateChannel( + ctx context.Context, + teamID, channelName string, +) (models.Channelable, error) { + body := models.NewChannel() + body.SetDisplayName(&channelName) + + channel, err := c.Stable. + Client(). + Teams(). + ByTeamIdString(teamID). + Channels(). + Post(ctx, body, nil) + if err != nil { + return nil, graph.Wrap(ctx, err, "creating channel") + } + + return channel, nil +} + +// DeleteChannel removes a channel from team with given teamID +func (c Channels) DeleteChannel( + ctx context.Context, + teamID, channelID string, +) error { + // deletes require unique http clients + // https://github.com/alcionai/corso/issues/2707 + srv, err := NewService(c.Credentials) + if err != nil { + return graph.Stack(ctx, err) + } + + err = srv.Client(). + Teams(). + ByTeamIdString(teamID). + Channels(). + ByChannelIdString(channelID). + Delete(ctx, nil) + if err != nil { + return graph.Stack(ctx, err) + } + + return nil +} + // --------------------------------------------------------------------------- // message // --------------------------------------------------------------------------- @@ -128,6 +174,60 @@ func (c Channels) GetChannelMessage( return message, info, nil } +func (c Channels) PostChannelMessage( + ctx context.Context, + teamID, channelID string, + body *models.ItemBody, +) (models.ChatMessageable, error) { + requestBody := models.NewChatMessage() + requestBody.SetBody(body) + + itm, err := c.Stable. + Client(). + Teams(). + ByTeamIdString(teamID). + Channels(). + ByChannelIdString(channelID). + Messages(). + Post(ctx, requestBody, nil) + if err != nil { + return nil, graph.Wrap(ctx, err, "creating chamailnnel message") + } + + if itm == nil { + return nil, clues.New("nil response channel message creation").WithClues(ctx) + } + + return itm, nil +} + +func (c Channels) DeleteChannelMessage( + ctx context.Context, + teamID, messageID, channelID string, +) error { + // deletes require unique http clients + // https://github.com/alcionai/corso/issues/2707 + srv, err := NewService(c.Credentials) + if err != nil { + return graph.Stack(ctx, err) + } + + err = srv. + Client(). + Teams(). + ByTeamIdString(teamID). + Channels(). + ByChannelIdString(channelID). + Messages(). + ByChatMessageIdString(messageID). + Delete(ctx, nil) + if err != nil { + return graph.Wrap(ctx, err, "deleting channel message") + } + + return nil +} + // --------------------------------------------------------------------------- // Helpers // ---------------------------------------------------------------------------