Update label name in configuration profile (#21246)
#21163 Fixed bug where configuration profile was still showing the old label name after the name was updated. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [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:
@@ -227,6 +227,14 @@ func (ds *Datastore) SaveLabel(ctx context.Context, label *fleet.Label, teamFilt
|
||||
if err != nil {
|
||||
return nil, nil, ctxerr.Wrap(ctx, err, "saving label")
|
||||
}
|
||||
|
||||
// Update the label name in mdm_configuration_profile_labels
|
||||
query = `UPDATE mdm_configuration_profile_labels SET label_name = ? WHERE label_id = ?`
|
||||
_, err = ds.writer(ctx).ExecContext(ctx, query, label.Name, label.ID)
|
||||
if err != nil {
|
||||
return nil, nil, ctxerr.Wrap(ctx, err, "updating mdm configuration profile label")
|
||||
}
|
||||
|
||||
return ds.labelDB(ctx, label.ID, teamFilter, ds.writer(ctx))
|
||||
}
|
||||
|
||||
|
||||
@@ -673,6 +673,31 @@ func testLabelsChangeDetails(t *testing.T, db *Datastore) {
|
||||
saved, _, err := db.Label(context.Background(), label.ID, filter)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, label.Name, saved.Name)
|
||||
assert.Equal(t, label.Description, saved.Description)
|
||||
|
||||
// Create an Apple config profile, which should reflect a change in label's name
|
||||
profA, err := db.NewMDMAppleConfigProfile(context.Background(), *generateCP("a", "a", 0))
|
||||
require.NoError(t, err)
|
||||
ExecAdhocSQL(t, db, func(q sqlx.ExtContext) error {
|
||||
_, err := q.ExecContext(context.Background(),
|
||||
"INSERT INTO mdm_configuration_profile_labels (apple_profile_uuid, label_name, label_id) VALUES (?, ?, ?)",
|
||||
profA.ProfileUUID, label.Name, label.ID)
|
||||
return err
|
||||
})
|
||||
label.Name = "changed name"
|
||||
// ApplyLabelSpecs can't update the name -- it simply creates a new label, so we need to call SaveLabel.
|
||||
saved.Name = label.Name
|
||||
saved2, _, err := db.SaveLabel(context.Background(), saved, filter)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, label.Name, saved2.Name)
|
||||
assert.Equal(t, label.Description, saved2.Description)
|
||||
|
||||
var configProfileLabelName string
|
||||
ExecAdhocSQL(t, db, func(q sqlx.ExtContext) error {
|
||||
return sqlx.GetContext(context.Background(), q, &configProfileLabelName,
|
||||
"SELECT label_name FROM mdm_configuration_profile_labels WHERE label_id = ?", label.ID)
|
||||
})
|
||||
assert.Equal(t, label.Name, configProfileLabelName)
|
||||
}
|
||||
|
||||
func setupLabelSpecsTest(t *testing.T, ds fleet.Datastore) []*fleet.LabelSpec {
|
||||
@@ -806,6 +831,17 @@ func testLabelsSave(t *testing.T, db *Datastore) {
|
||||
}
|
||||
label, err = db.NewLabel(context.Background(), label)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create an Apple config profile
|
||||
profA, err := db.NewMDMAppleConfigProfile(context.Background(), *generateCP("a", "a", 0))
|
||||
require.NoError(t, err)
|
||||
ExecAdhocSQL(t, db, func(q sqlx.ExtContext) error {
|
||||
_, err := q.ExecContext(context.Background(),
|
||||
"INSERT INTO mdm_configuration_profile_labels (apple_profile_uuid, label_name, label_id) VALUES (?, ?, ?)",
|
||||
profA.ProfileUUID, label.Name, label.ID)
|
||||
return err
|
||||
})
|
||||
|
||||
label.Name = "changed name"
|
||||
label.Description = "changed description"
|
||||
|
||||
@@ -819,6 +855,13 @@ func testLabelsSave(t *testing.T, db *Datastore) {
|
||||
assert.Equal(t, label.Name, saved.Name)
|
||||
assert.Equal(t, label.Description, saved.Description)
|
||||
assert.Equal(t, 1, saved.HostCount)
|
||||
|
||||
var configProfileLabelName string
|
||||
ExecAdhocSQL(t, db, func(q sqlx.ExtContext) error {
|
||||
return sqlx.GetContext(context.Background(), q, &configProfileLabelName,
|
||||
"SELECT label_name FROM mdm_configuration_profile_labels WHERE label_id = ?", label.ID)
|
||||
})
|
||||
assert.Equal(t, label.Name, configProfileLabelName)
|
||||
}
|
||||
|
||||
func testLabelsQueriesForCentOSHost(t *testing.T, db *Datastore) {
|
||||
|
||||
Reference in New Issue
Block a user