update resource path

This commit is contained in:
neha-Gupta1 2023-08-01 13:47:42 +05:30
parent 8e35c81197
commit 0f41256010
5 changed files with 145 additions and 42 deletions

View File

@ -30,8 +30,8 @@ const (
ExchangeMetadataService // exchangeMetadata ExchangeMetadataService // exchangeMetadata
OneDriveMetadataService // onedriveMetadata OneDriveMetadataService // onedriveMetadata
SharePointMetadataService // sharepointMetadata SharePointMetadataService // sharepointMetadata
TeamsService // teams GroupsService // groups
TeamsMetadataService // teamsMetadata GroupsMetadataService // groupsMetadata
) )
func toServiceType(service string) ServiceType { func toServiceType(service string) ServiceType {

View File

@ -15,11 +15,13 @@ func _() {
_ = x[ExchangeMetadataService-4] _ = x[ExchangeMetadataService-4]
_ = x[OneDriveMetadataService-5] _ = x[OneDriveMetadataService-5]
_ = x[SharePointMetadataService-6] _ = x[SharePointMetadataService-6]
_ = x[GroupsService-7]
_ = x[GroupsMetadataService-8]
} }
const _ServiceType_name = "UnknownServiceexchangeonedrivesharepointexchangeMetadataonedriveMetadatasharepointMetadata" const _ServiceType_name = "UnknownServiceexchangeonedrivesharepointexchangeMetadataonedriveMetadatasharepointMetadatagroupsgroupsMetadata"
var _ServiceType_index = [...]uint8{0, 14, 22, 30, 40, 56, 72, 90} var _ServiceType_index = [...]uint8{0, 14, 22, 30, 40, 56, 72, 90, 96, 110}
func (i ServiceType) String() string { func (i ServiceType) String() string {
if i < 0 || i >= ServiceType(len(_ServiceType_index)-1) { if i < 0 || i >= ServiceType(len(_ServiceType_index)-1) {

View File

@ -23,21 +23,21 @@ const (
// controller // controller
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
func (c Client) Teams() Teams { func (c Client) Groups() Groups {
return Teams{c} return Groups{c}
} }
// On creation of each Teams team a corrsponding group gets created. // On creation of each Teams team a corrsponding group gets created.
// The group acts as the protected resource, and all teams data like events, // The group acts as the protected resource, and all teams data like events,
// drive and mail messages are owned by that group. // drive and mail messages are owned by that group.
// Teams is an interface-compliant provider of the client. // Groups is an interface-compliant provider of the client.
type Teams struct { type Groups struct {
Client Client
} }
// GetAllTeams retrieves all groups. // GetAllGroups retrieves all groups.
func (c Teams) GetAll( func (c Groups) GetAll(
ctx context.Context, ctx context.Context,
errs *fault.Bus, errs *fault.Bus,
) ([]models.Groupable, error) { ) ([]models.Groupable, error) {
@ -46,13 +46,26 @@ func (c Teams) GetAll(
return nil, err return nil, err
} }
return getGroups(ctx, true, errs, service) return getGroups(ctx, func(ctx context.Context, g models.Groupable) bool { return true }, errs, service)
}
// GetTeams retrieves all Teams.
func (c Groups) GetTeams(
ctx context.Context,
errs *fault.Bus,
) ([]models.Groupable, error) {
service, err := c.Service()
if err != nil {
return nil, err
}
return getGroups(ctx, FetchOnlyTeams, errs, service)
} }
// GetAll retrieves all groups. // GetAll retrieves all groups.
func getGroups( func getGroups(
ctx context.Context, ctx context.Context,
getOnlyTeams bool, filterGroupsData func(ctx context.Context, g models.Groupable) bool,
errs *fault.Bus, errs *fault.Bus,
service graph.Servicer, service graph.Servicer,
) ([]models.Groupable, error) { ) ([]models.Groupable, error) {
@ -64,7 +77,7 @@ func getGroups(
iter, err := msgraphgocore.NewPageIterator[models.Groupable]( iter, err := msgraphgocore.NewPageIterator[models.Groupable](
resp, resp,
service.Adapter(), service.Adapter(),
models.CreateTeamCollectionResponseFromDiscriminatorValue) models.CreateGroupCollectionResponseFromDiscriminatorValue)
if err != nil { if err != nil {
return nil, graph.Wrap(ctx, err, "creating groups iterator") return nil, graph.Wrap(ctx, err, "creating groups iterator")
} }
@ -83,8 +96,7 @@ func getGroups(
if err != nil { if err != nil {
el.AddRecoverable(ctx, graph.Wrap(ctx, err, "validating groups")) el.AddRecoverable(ctx, graph.Wrap(ctx, err, "validating groups"))
} else { } else {
isTeam := IsTeam(ctx, item) if filterGroupsData(ctx, item) {
if !getOnlyTeams || isTeam {
groups = append(groups, item) groups = append(groups, item)
} }
} }
@ -99,7 +111,7 @@ func getGroups(
return groups, el.Failure() return groups, el.Failure()
} }
func IsTeam(ctx context.Context, g models.Groupable) bool { func FetchOnlyTeams(ctx context.Context, g models.Groupable) bool {
log := logger.Ctx(ctx) log := logger.Ctx(ctx)
if g.GetAdditionalData()[ResourceProvisioningOptions] != nil { if g.GetAdditionalData()[ResourceProvisioningOptions] != nil {
@ -120,8 +132,8 @@ func IsTeam(ctx context.Context, g models.Groupable) bool {
return false return false
} }
// GetID retrieves team by groupID/teamID. // GetID retrieves group by groupID.
func (c Teams) GetByID( func (c Groups) GetByID(
ctx context.Context, ctx context.Context,
identifier string, identifier string,
) (models.Groupable, error) { ) (models.Groupable, error) {
@ -137,7 +149,27 @@ func (c Teams) GetByID(
return nil, err return nil, err
} }
if !IsTeam(ctx, resp) { return resp, graph.Stack(ctx, err).OrNil()
}
// GetTeamByID retrieves group by groupID.
func (c Groups) GetTeamByID(
ctx context.Context,
identifier string,
) (models.Groupable, error) {
service, err := c.Service()
if err != nil {
return nil, err
}
resp, err := service.Client().Groups().ByGroupId(identifier).Get(ctx, nil)
if err != nil {
err := graph.Wrap(ctx, err, "getting group by id")
return nil, err
}
if !FetchOnlyTeams(ctx, resp) {
err := clues.New("given teamID is not related to any team") err := clues.New("given teamID is not related to any team")
return nil, err return nil, err

View File

@ -17,18 +17,18 @@ import (
"github.com/alcionai/corso/src/pkg/services/m365/api" "github.com/alcionai/corso/src/pkg/services/m365/api"
) )
type TeamsUnitSuite struct { type GroupUnitSuite struct {
tester.Suite tester.Suite
} }
func TestTeamsUnitSuite(t *testing.T) { func TestGroupsUnitSuite(t *testing.T) {
suite.Run(t, &TeamsUnitSuite{Suite: tester.NewUnitSuite(t)}) suite.Run(t, &GroupUnitSuite{Suite: tester.NewUnitSuite(t)})
} }
func (suite *TeamsUnitSuite) TestValidateGroup() { func (suite *GroupUnitSuite) TestValidateGroup() {
team := models.NewTeam() group := models.NewGroup()
team.SetDisplayName(ptr.To("testgroup")) group.SetDisplayName(ptr.To("testgroup"))
team.SetId(ptr.To("testID")) group.SetId(ptr.To("testID"))
tests := []struct { tests := []struct {
name string name string
@ -41,7 +41,7 @@ func (suite *TeamsUnitSuite) TestValidateGroup() {
args: func() *models.Group { args: func() *models.Group {
s := models.NewGroup() s := models.NewGroup()
s.SetId(ptr.To("id")) s.SetId(ptr.To("id"))
s.SetDisplayName(ptr.To("testTeam")) s.SetDisplayName(ptr.To("testgroup"))
return s return s
}(), }(),
errCheck: assert.NoError, errCheck: assert.NoError,
@ -59,7 +59,7 @@ func (suite *TeamsUnitSuite) TestValidateGroup() {
name: "No ID", name: "No ID",
args: func() *models.Group { args: func() *models.Group {
s := models.NewGroup() s := models.NewGroup()
s.SetDisplayName(ptr.To("testTeam")) s.SetDisplayName(ptr.To("testgroup"))
return s return s
}(), }(),
errCheck: assert.Error, errCheck: assert.Error,
@ -80,47 +80,60 @@ func (suite *TeamsUnitSuite) TestValidateGroup() {
} }
} }
type TeamsIntgSuite struct { type GroupsIntgSuite struct {
tester.Suite tester.Suite
its intgTesterSetup its intgTesterSetup
} }
func TestTeamsIntgSuite(t *testing.T) { func TestGroupsIntgSuite(t *testing.T) {
suite.Run(t, &TeamsIntgSuite{ suite.Run(t, &GroupsIntgSuite{
Suite: tester.NewIntegrationSuite( Suite: tester.NewIntegrationSuite(
t, t,
[][]string{tconfig.M365AcctCredEnvs}), [][]string{tconfig.M365AcctCredEnvs}),
}) })
} }
func (suite *TeamsIntgSuite) SetupSuite() { func (suite *GroupsIntgSuite) SetupSuite() {
suite.its = newIntegrationTesterSetup(suite.T()) suite.its = newIntegrationTesterSetup(suite.T())
} }
func (suite *TeamsIntgSuite) TestGetAllTeams() { func (suite *GroupsIntgSuite) TestGetAllGroups() {
t := suite.T() t := suite.T()
ctx, flush := tester.NewContext(t) ctx, flush := tester.NewContext(t)
defer flush() defer flush()
teams, err := suite.its.ac. groups, err := suite.its.ac.
Teams(). Groups().
GetAll(ctx, fault.New(true)) GetAll(ctx, fault.New(true))
require.NoError(t, err) require.NoError(t, err)
require.NotZero(t, len(teams), "must have at least one team") require.NotZero(t, len(groups), "must have at least one group")
}
for _, team := range teams { func (suite *GroupsIntgSuite) TestGetAllTeams() {
assert.True(t, api.IsTeam(ctx, team), "must not return non teams groups") t := suite.T()
ctx, flush := tester.NewContext(t)
defer flush()
groups, err := suite.its.ac.
Groups().
GetTeams(ctx, fault.New(true))
require.NoError(t, err)
require.NotZero(t, len(groups), "must have at least one group")
for _, team := range groups {
assert.True(t, api.FetchOnlyTeams(ctx, team), "must not return non groups groups")
} }
} }
func (suite *TeamsIntgSuite) TestTeams_GetByID() { func (suite *GroupsIntgSuite) TestTeams_GetByID() {
var ( var (
t = suite.T() t = suite.T()
teamID = tconfig.M365TeamsID(t) teamID = tconfig.M365TeamsID(t)
) )
teamsAPI := suite.its.ac.Teams() teamsAPI := suite.its.ac.Groups()
table := []struct { table := []struct {
name string name string
@ -164,7 +177,63 @@ func (suite *TeamsIntgSuite) TestTeams_GetByID() {
ctx, flush := tester.NewContext(t) ctx, flush := tester.NewContext(t)
defer flush() defer flush()
_, err := teamsAPI.GetByID(ctx, test.id) _, err := teamsAPI.GetTeamByID(ctx, test.id)
test.expectErr(t, err)
})
}
}
func (suite *GroupsIntgSuite) TestGroups_GetByID() {
var (
t = suite.T()
groupID = tconfig.M365GroupID(t)
)
groupsAPI := suite.its.ac.Groups()
table := []struct {
name string
id string
expectErr func(*testing.T, error)
}{
{
name: "3 part id",
id: groupID,
expectErr: func(t *testing.T, err error) {
assert.NoError(t, err, clues.ToCore(err))
},
},
{
name: "malformed id",
id: uuid.NewString(),
expectErr: func(t *testing.T, err error) {
assert.Error(t, err, clues.ToCore(err))
},
},
{
name: "random id",
id: uuid.NewString() + "," + uuid.NewString(),
expectErr: func(t *testing.T, err error) {
assert.Error(t, err, clues.ToCore(err))
},
},
{
name: "malformed url",
id: "barunihlda",
expectErr: func(t *testing.T, err error) {
assert.Error(t, err, clues.ToCore(err))
},
},
}
for _, test := range table {
suite.Run(test.name, func() {
t := suite.T()
ctx, flush := tester.NewContext(t)
defer flush()
_, err := groupsAPI.GetByID(ctx, test.id)
test.expectErr(t, err) test.expectErr(t, err)
}) })
} }

View File

@ -135,7 +135,7 @@ func newIntegrationTesterSetup(t *testing.T) intgTesterSetup {
// teams // teams
its.teamID = tconfig.M365TeamsID(t) its.teamID = tconfig.M365TeamsID(t)
team, err := its.ac.Teams().GetByID(ctx, its.teamID) team, err := its.ac.Groups().GetTeamByID(ctx, its.teamID)
require.NoError(t, err, clues.ToCore(err)) require.NoError(t, err, clues.ToCore(err))
its.teamID = ptr.Val(team.GetId()) its.teamID = ptr.Val(team.GetId())