Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove custom files when resetting to default #487

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions server/modules/salt/saltstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,23 +661,32 @@ func (store *Saltstore) UpdateSetting(ctx context.Context, setting *model.Settin
}

log.WithFields(log.Fields{
"settingId": setting.Id,
"minionId": setting.NodeId,
"path": path,
"length": len(setting.Value),
"settingId": setting.Id,
"minionId": setting.NodeId,
"settingPath": path,
"settingLength": len(setting.Value),
}).Info("Updating advanced settings to new value")
os.WriteFile(path, []byte(setting.Value), 0600)

} else if setting.File {
path := fmt.Sprintf("%s/local/salt/%s", store.saltstackDir, store.relPathFromId(setting.Id))
if !remove {
log.WithFields(log.Fields{
"settingId": setting.Id,
"settingPath": path,
"settingLength": len(setting.Value),
}).Info("Updating custom file setting to new value")

log.WithFields(log.Fields{
"settingId": setting.Id,
"path": path,
"length": len(setting.Value),
}).Info("Updating custom file setting to new value")
err = os.WriteFile(path, []byte(setting.Value), 0600)
} else {
log.WithFields(log.Fields{
"settingId": setting.Id,
"settingPath": path,
"settingLength": len(setting.Value),
}).Info("Deleting custom file")

err = os.WriteFile(path, []byte(setting.Value), 0600)
err = os.Remove(path)
}
} else {
var path string
if setting.NodeId == "" {
Expand All @@ -691,15 +700,15 @@ func (store *Saltstore) UpdateSetting(ctx context.Context, setting *model.Settin
if err == nil {
if !remove {
log.WithFields(log.Fields{
"settingId": setting.Id,
"path": path,
"length": len(setting.Value),
"settingId": setting.Id,
"settingPath": path,
"settingLength": len(setting.Value),
}).Info("Updating setting to new value")
err = store.updateSetting(mapped, sections, setting)
} else {
log.WithFields(log.Fields{
"settingId": setting.Id,
"path": path,
"settingId": setting.Id,
"settingPath": path,
}).Info("Deleting setting")
_, err = store.deleteSetting(mapped, sections)
}
Expand Down
10 changes: 10 additions & 0 deletions server/modules/salt/saltstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,16 @@ func TestUpdateSetting_UpdateFile(tester *testing.T) {
updated_setting := findSetting(settings, "myapp.foo__txt", "")
assert.Equal(tester, "anything", updated_setting.Default)
assert.Equal(tester, "something", updated_setting.Value)

// Delete setting
err = salt.UpdateSetting(ctx(), setting, true)
assert.NoError(tester, err)

settings, get_err = salt.GetSettings(ctx())
assert.NoError(tester, get_err)
updated_setting = findSetting(settings, "myapp.foo__txt", "")
assert.Equal(tester, "anything", updated_setting.Default)
assert.Equal(tester, "anything", updated_setting.Value)
}

func TestUpdateSetting_UpdateAdvancedFailToParse(tester *testing.T) {
Expand Down
Loading