Corrections to removal of custom fields:

- Custom fields were not removed from all types of objects the UI allows
  setting them for, which meant they were left behind in the other_config.
- Only remove a custom field if it is set on an object, otherwise we hammer
  the server with unnecessary requests.

Signed-off-by: Konstantina Chremmou <Konstantina.Chremmou@cloud.com>
This commit is contained in:
Konstantina Chremmou 2023-01-05 22:23:21 +00:00
parent 423ee7bd81
commit bc40d47ba6
2 changed files with 47 additions and 36 deletions

View File

@ -46,14 +46,14 @@ namespace XenAdmin.CustomFields
/// "XenCenter.CustomFields.foo1" value
/// "XenCenter.CustomFields.foo2" value
/// </summary>
public class CustomFieldsManager
public static class CustomFieldsManager
{
#region These functions deal with caching the list of custom fields
private static readonly CustomFieldsCache customFieldsCache = new CustomFieldsCache();
private const String CUSTOM_FIELD_DELIM = ".";
public const String CUSTOM_FIELD_BASE_KEY = "XenCenter.CustomFields";
public const String CUSTOM_FIELD = "CustomField:";
public static event Action CustomFieldsChanged;
@ -109,15 +109,52 @@ namespace XenAdmin.CustomFields
public static void RemoveCustomField(Session session, IXenConnection connection, CustomFieldDefinition definition)
{
List<CustomFieldDefinition> customFields = customFieldsCache.GetCustomFields(connection);
if (customFields.Remove(definition))
{
SaveCustomFields(session, connection, customFields);
// Remove from all Objects
RemoveCustomFieldsFrom(session, connection.Cache.VMs, definition);
RemoveCustomFieldsFrom(session, connection.Cache.Hosts, definition);
RemoveCustomFieldsFrom(session, connection.Cache.Pools, definition);
RemoveCustomFieldsFrom(session, connection.Cache.SRs, definition);
// theoretically any object type with other_config may have a custom field set,
// but the object types more likely to have one are those shown on the treeview,
// i.e. pools, hosts, VMs (including snapshots and templates), SRs, VDIs, and networks
string customFieldKey = GetCustomFieldKey(definition);
foreach (var pool in connection.Cache.Pools)
{
if (pool.other_config.ContainsKey(customFieldKey))
Pool.remove_from_other_config(session, pool.opaque_ref, customFieldKey);
}
foreach (var host in connection.Cache.Hosts)
{
if (host.other_config.ContainsKey(customFieldKey))
Host.remove_from_other_config(session, host.opaque_ref, customFieldKey);
}
foreach (var vm in connection.Cache.VMs)
{
if (vm.other_config.ContainsKey(customFieldKey))
VM.remove_from_other_config(session, vm.opaque_ref, customFieldKey);
}
foreach (var sr in connection.Cache.SRs)
{
if (sr.other_config.ContainsKey(customFieldKey))
SR.remove_from_other_config(session, sr.opaque_ref, customFieldKey);
}
foreach (var vdi in connection.Cache.VDIs)
{
if (vdi.other_config.ContainsKey(customFieldKey))
VDI.remove_from_other_config(session, vdi.opaque_ref, customFieldKey);
}
foreach (var network in connection.Cache.Networks)
{
if (network.other_config.ContainsKey(customFieldKey))
XenAPI.Network.remove_from_other_config(session, network.opaque_ref, customFieldKey);
}
}
}
@ -155,25 +192,14 @@ namespace XenAdmin.CustomFields
return CUSTOM_FIELD_BASE_KEY + CUSTOM_FIELD_DELIM + customFieldDefinition.Name;
}
private static void RemoveCustomFieldsFrom(Session session, IEnumerable<IXenObject> os, CustomFieldDefinition customFieldDefinition)
{
InvokeHelper.AssertOffEventThread();
string customFieldKey = GetCustomFieldKey(customFieldDefinition);
foreach (IXenObject o in os)
{
Helpers.RemoveFromOtherConfig(session, o, customFieldKey);
}
}
private static void SaveCustomFields(Session session, IXenConnection connection, List<CustomFieldDefinition> customFields)
{
Pool pool = Helpers.GetPoolOfOne(connection);
if (pool != null)
{
String customFieldXML = GetCustomFieldDefinitionXML(customFields);
Helpers.SetGuiConfig(session, pool, CUSTOM_FIELD_BASE_KEY, customFieldXML);
string customFieldXML = GetCustomFieldDefinitionXML(customFields);
Pool.remove_from_gui_config(session, pool.opaque_ref, CUSTOM_FIELD_BASE_KEY);
Pool.add_to_gui_config(session, pool.opaque_ref, CUSTOM_FIELD_BASE_KEY, customFieldXML);
}
}

View File

@ -609,21 +609,6 @@ namespace XenAdmin.Core
return o.Get("gui_config") as Dictionary<String, String>;
}
public static void SetGuiConfig(Session session, IXenObject o, String key, String value)
{
//Program.AssertOffEventThread();
o.Do("remove_from_gui_config", session, o.opaque_ref, key);
o.Do("add_to_gui_config", session, o.opaque_ref, key, value);
}
public static void RemoveFromGuiConfig(Session session, IXenObject o, string key)
{
//Program.AssertOffEventThread();
o.Do("remove_from_gui_config", session, o.opaque_ref, key);
}
#endregion
public enum DataSourceCategory