Ejemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NodeConfigurationService"/> class.
 /// </summary>
 /// <param name="context">
 /// The context.
 /// </param>
 /// <param name="logging">
 /// The logging.
 /// </param>
 public NodeConfigurationService(NodeRepositoryContext context, INodeRepositoryLogging logging)
 {
     this.Context = context;
     this.ConfigurationPropertyRepository = context.Set <ConfigurationProperty>();
     this.NodeRepository = context.Set <Node>();
     this.Logging        = logging;
 }
 public ConfigurationPropertiesController(NodeRepositoryContext context, INodeConfigurationService nodeService, INodeRepositoryLogging logging)
 {
     this.Context        = context;
     this.NodeRepository = context.Set <Node>();
     this.ConfigurationPropertiesRepository = context.Set <ConfigurationProperty>();
     this.NodeService = nodeService;
     this.Logging     = logging;
 }
Ejemplo n.º 3
0
        public static void IncludeConfigurationProperties(
            this IEnumerable <Node> nodes,
            NodeRepositoryContext context,
            bool force = false)
        {
            var allProperties = context.Set <ConfigurationProperty>();

            foreach (var node in nodes)
            {
                if (node.ConfigurationPropertiesPopulated && !force)
                {
                    continue;
                }

                var properties = allProperties.GetPropertiesForNode(node);
                foreach (var property in properties)
                {
                    switch (property.Type)
                    {
                    case PropertyType.Node:
                        if (node.NodeProperties.All(p => p.Id != property.Id))
                        {
                            node.NodeProperties.Add(property);
                        }

                        break;

                    case PropertyType.LocalAgent:
                        if (node.LocalAgentProperties.All(p => p.Id != property.Id))
                        {
                            node.LocalAgentProperties.Add(property);
                        }

                        break;

                    case PropertyType.Bootstrap:
                        if (node.BootstrapProperties.All(p => p.Id != property.Id))
                        {
                            node.BootstrapProperties.Add(property);
                        }

                        break;

                    case PropertyType.ResourceVersion:
                        if (node.ResourceVersionProperties.All(r => r.Id != property.Id))
                        {
                            node.ResourceVersionProperties.Add(property);
                        }

                        break;
                    }
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// The get detail view.
        /// </summary>
        /// <returns>
        /// The <see cref="NodeDetailView"/>.
        /// </returns>
        /// <summary>
        /// The merge.
        /// </summary>
        /// <param name="configurationData">
        /// The configuration data.
        /// </param>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// </exception>
        public bool Merge(Hashtable configurationData, NodeRepositoryContext context)
        {
            if (!configurationData.ContainsKey("NodeName"))
            {
                throw new InvalidOperationException(
                          "Cannot process not configuration that is missing the NodeName property.");
            }

            this.Name = configurationData["NodeName"].ToString();

            var isDirty            = false;
            var excludedProperties = new[]
            {
                "MaintenanceSchedule", "Roles", "NodeProperties", "LocalAgentProperties", "ResourceVersionProperties",
                "BootstrapProperties", "IsInMaintenance"
            };
            var properties =
                this.GetType()
                .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                .Where(p => !excludedProperties.Contains(p.Name))
                .ToArray();

            foreach (DictionaryEntry entry in configurationData)
            {
                // Determine if the property is on the top-level Node object
                var currentProperty = properties.FirstOrDefault(p => p.Name == (string)entry.Key);
                if (currentProperty != null)
                {
                    if (currentProperty?.GetValue(this)?.ToString() != entry.Value.ToString())
                    {
                        currentProperty.SetValue(this, entry.Value);
                        isDirty = true;
                    }

                    continue;
                }

                // Handle resource versions
                if ((string)entry.Key == "ResourceVersionProperties")
                {
                    var table = (entry.Value as JObject)?.ToObject <Hashtable>();
                    if (table == null)
                    {
                        continue;
                    }

                    foreach (DictionaryEntry resource in table)
                    {
                        var property = this.ResourceVersionProperties.FirstOrDefault(r => r.Name == (string)resource.Key);
                        if (property == null)
                        {
                            var rprop = new ConfigurationProperty
                            {
                                Name   = (string)resource.Key,
                                Scope  = PropertyScope.Node,
                                Target = this.NodeName,
                                Type   = PropertyType.ResourceVersion,
                                Value  = (string)resource.Value
                            };
                            this.ResourceVersionProperties.Add(rprop);
                            context.ConfigurationProperties.Add(rprop);
                            isDirty = true;
                        }
                        else
                        {
                            if ((string)resource.Value != (string)property.Value)
                            {
                                if (property.Scope == PropertyScope.Node)
                                {
                                    property.Value = (string)resource.Value;
                                }
                                else
                                {
                                    var rprop = new ConfigurationProperty
                                    {
                                        Name   = (string)resource.Key,
                                        Scope  = PropertyScope.Node,
                                        Target = this.NodeName,
                                        Type   = PropertyType.ResourceVersion,
                                        Value  = (string)resource.Value
                                    };
                                    this.ResourceVersionProperties.Add(rprop);
                                    context.ConfigurationProperties.Add(rprop);
                                }

                                isDirty = true;
                            }
                        }
                    }

                    var allResourceProps = this.ResourceVersionProperties.Where(r => r.Scope == PropertyScope.Node).ToList();
                    allResourceProps.ForEach(
                        r =>
                    {
                        if (!table.ContainsKey(r.Name))
                        {
                            this.ResourceVersionProperties.Remove(r);
                            isDirty = true;
                        }
                    });
                    continue;
                }

                // Handle maintenance schedules
                if ((string)entry.Key == "MaintenanceSchedule")
                {
                    // try
                    // {
                    var token       = entry.Value as JToken;
                    var json        = token?.ToString();
                    var newSchedule = JsonConvert.DeserializeObject <MaintenanceScheduleView>(
                        json,
                        new JsonSerializerSettings {
                        TypeNameHandling = TypeNameHandling.Objects
                    });

                    if (newSchedule == null)
                    {
                        continue;
                    }

                    if (this.MaintenanceSchedule == null)
                    {
                        this.MaintenanceSchedule = new MaintenanceSchedule();
                    }

                    if (this.MaintenanceSchedule.TimeZoneId == null ||
                        this.MaintenanceSchedule.TimeZoneId != newSchedule.TimeZone)
                    {
                        this.MaintenanceSchedule.TimeZoneId = newSchedule.TimeZone;
                        isDirty = true;
                    }

                    var newScheduleData = newSchedule.GetScheduleData();
                    if (this.MaintenanceSchedule.ScheduleData != newScheduleData)
                    {
                        this.MaintenanceSchedule.ScheduleData = newScheduleData;
                        isDirty = true;
                    }

                    continue;
                }

                if ((string)entry.Key == "Roles")
                {
                    var currentRoleNames = this.Roles.Select(r => r.Name).ToArray();
                    var roles            = (entry.Value as JArray)?.ToObject <IEnumerable <string> >().ToArray();

                    if (roles != null)
                    {
                        foreach (var role in roles.Where(role => !currentRoleNames.Contains(role)))
                        {
                            this.Roles.Add(new Role {
                                Name = role, Node = this
                            });
                            isDirty = true;
                        }

                        foreach (var removeRole in currentRoleNames.Where(role => !roles.Contains(role)))
                        {
                            var toRemove = this.Roles.FirstOrDefault(r => r.Name == removeRole);
                            this.Roles.Remove(toRemove);
                            context?.Set <Role>()?.Remove(toRemove);
                            isDirty = true;
                        }
                    }

                    continue;
                }

                // Must be a node property
                var nodeProperty = this.NodeProperties.FirstOrDefault(p => p.Name == (string)entry.Key);
                if (nodeProperty == null)
                {
                    var cProp = new ConfigurationProperty
                    {
                        Name   = (string)entry.Key,
                        Target = this.Name,
                        Scope  = PropertyScope.Node,
                        Type   = PropertyType.Node,
                        Value  = entry.Value
                    };
                    this.NodeProperties.Add(cProp);
                    context?.Set <ConfigurationProperty>()?.Add(cProp);
                    isDirty = true;
                }
                else
                {
                    var localDirty   = false;
                    var table        = entry.Value as IDictionary;
                    var destTable    = nodeProperty.Value as IDictionary;
                    var isTableDirty = this.CompareDictionary(table, destTable);
                    if (isTableDirty.HasValue)
                    {
                        localDirty = isTableDirty.Value;
                    }
                    else
                    {
                        var entryValue = JsonConvert.SerializeObject(
                            entry.Value,
                            new JsonSerializerSettings {
                            TypeNameHandling = TypeNameHandling.Objects
                        });
                        if (nodeProperty.SerializedValue != entryValue)
                        {
                            localDirty = true;
                        }
                    }
                    if (localDirty)
                    {
                        if (nodeProperty.Scope == PropertyScope.Node)
                        {
                            nodeProperty.Value = entry.Value;
                            isDirty            = true;
                        }
                        else
                        {
                            var cProp = new ConfigurationProperty
                            {
                                Name   = (string)entry.Key,
                                Target = this.Name,
                                Scope  = PropertyScope.Node,
                                Type   = PropertyType.Node,
                                Value  = entry.Value
                            };
                            this.NodeProperties.Add(cProp);
                            context?.Set <ConfigurationProperty>()?.Add(cProp);
                            isDirty = true;
                        }
                    }
                }
            }

            // Check if all node properties are needed
            var propsToRemove =
                this.NodeProperties.Where(
                    nodeProp => !configurationData.ContainsKey(nodeProp.Name) && nodeProp.Scope == PropertyScope.Node)
                .ToList();

            propsToRemove.ForEach(
                n =>
            {
                this.NodeProperties.Remove(n);
                context?.Set <ConfigurationProperty>()?.Remove(n);
                isDirty = true;
            });

            return(isDirty);
        }