Improved fleetctl gitops error message when trying to change team name to a team that already exists. (#21214)

#21104 
# Checklist for submitter


- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/Committing-Changes.md#changes-files)
for more information.
- [x] Added/updated tests
- [x] Manual QA for all new/changed functionality
This commit is contained in:
Victor Lyuboslavsky 2024-08-09 15:25:19 +02:00 committed by GitHub
parent f2f0b82eaa
commit e6bbb768d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 26 additions and 0 deletions

View file

@ -0,0 +1 @@
Improved fleetctl gitops error message when trying to change team name to a team that already exists.

View file

@ -665,6 +665,9 @@ func TestFullTeamGitOps(t *testing.T) {
// Team
var savedTeam *fleet.Team
ds.TeamByNameFunc = func(ctx context.Context, name string) (*fleet.Team, error) {
if name == "Conflict" {
return &fleet.Team{}, nil
}
if savedTeam != nil && savedTeam.Name == name {
return savedTeam, nil
}
@ -825,7 +828,15 @@ func TestFullTeamGitOps(t *testing.T) {
assert.Equal(t, newTeamName, savedTeam.Name)
assert.Equal(t, baseFilename, *savedTeam.Filename)
// Try to change team name again, but this time the new name conflicts with an existing team
t.Setenv("TEST_TEAM_NAME", "Conflict")
_, err = runAppNoChecks([]string{"gitops", "-f", file, "--dry-run"})
assert.ErrorContains(t, err, "team name already exists")
_, err = runAppNoChecks([]string{"gitops", "-f", file})
assert.ErrorContains(t, err, "team name already exists")
// Now clear the settings
t.Setenv("TEST_TEAM_NAME", newTeamName)
tmpFile, err := os.CreateTemp(t.TempDir(), "*.yml")
require.NoError(t, err)
secret := "TestSecret"

View file

@ -868,6 +868,20 @@ func (svc *Service) ApplyTeamSpecs(ctx context.Context, specs []*fleet.TeamSpec,
if err != nil && !fleet.IsNotFound(err) {
return nil, err
}
if team != nil && team.Name != spec.Name {
// If user is trying to change team name, check that the new name is not already taken.
_, err = svc.ds.TeamByName(ctx, spec.Name)
switch {
case err == nil:
return nil, fleet.NewInvalidArgumentError("name",
fmt.Sprintf("cannot change team name from '%s' (filename: %s) to '%s' because team name already exists", team.Name,
*spec.Filename, spec.Name))
case fleet.IsNotFound(err):
// OK
default:
return nil, err
}
}
}
var create bool
if team == nil {