mirror of
https://github.com/fleetdm/fleet
synced 2026-05-23 08:58:41 +00:00
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:
parent
f2f0b82eaa
commit
e6bbb768d3
3 changed files with 26 additions and 0 deletions
1
changes/21104-gitops-team-conflict
Normal file
1
changes/21104-gitops-team-conflict
Normal file
|
|
@ -0,0 +1 @@
|
|||
Improved fleetctl gitops error message when trying to change team name to a team that already exists.
|
||||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue