Ejemplo n.º 1
0
        public Object get_record(String clazz, String opaqueRef, bool makeResponse)
        {
            Db.Table table = db.Tables[clazz];

            Type type = table.XapiType;

            if (!table.Rows.ContainsKey(opaqueRef))
            {
                if (makeResponse)
                {
                    return(typeof(Response <>).MakeGenericType(type).GetConstructor(new Type[] { typeof(bool), typeof(String[]) })
                           .Invoke(new Object[] { true, new String[] { Failure.OBJECT_NO_LONGER_EXISTS, opaqueRef } }));
                }

                throw new Exception(Failure.OBJECT_NO_LONGER_EXISTS);
            }

            Db.Row row    = table.Rows[opaqueRef];
            Object result = Activator.CreateInstance(type);

            foreach (string propName in row.Props.Keys)
            {
                FieldInfo info = type.GetField(propName, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                info.SetValue(result, row.Props[propName].XapiObjectValue);
            }

            if (makeResponse)
            {
                return(typeof(Response <>).MakeGenericType(type).GetConstructor(new Type[] { type }).Invoke(new Object[] { result }));
            }

            return(result);
        }
Ejemplo n.º 2
0
        public string CopyObject(string clazz, string opaque_ref)
        {
            if (!db.Tables[clazz].Rows.ContainsKey(opaque_ref))
            {
                return(Helper.NullOpaqueRef);
            }
            Db.Row r = db.Tables[clazz].Rows[opaque_ref].CopyOf();
            string new_opaque_ref = CreateOpaqueRef();

            r.Props["uuid"].XapiObjectValue = Guid.NewGuid().ToString(); // this object needs a new uuid & ref
            db.Tables[clazz].Rows.Add(new_opaque_ref, r);
            return(new_opaque_ref);
        }
Ejemplo n.º 3
0
 private void edit_record(EditTypes editType, string clazz, string opaqueRef, string field, params object[] args)
 {
     Db.Row  row = db.Tables[clazz].Rows[opaqueRef];
     Db.Prop prop;
     if (row.Props.TryGetValue(field, out prop))
     {
         prop.XapiObjectValue = NewValue(editType, prop.XapiObjectValue, args);
     }
     else
     {
         prop             = new Db.Prop(row, field, NewValue(editType, null, args));
         row.Props[field] = prop;
     }
 }
Ejemplo n.º 4
0
        public string CopyObject(string clazz, string opaque_ref)
        {
            if (!db.Tables[clazz].Rows.ContainsKey(opaque_ref))
            {
                return(Helper.NullOpaqueRef);
            }

            Db.Row r = db.Tables[clazz].Rows[opaque_ref].CopyOf();
            string new_opaque_ref = CreateOpaqueRef();

            r.Props["uuid"].XapiObjectValue = Guid.NewGuid().ToString(); // this object needs a new uuid & ref

            if (r.Props.TryGetValue("VBDs", out Db.Prop vbdVal))
            {
                if (vbdVal.XapiObjectValue is string[] vbds)
                {
                    r.Props["VBDs"].XapiObjectValue =
                        vbds.Select(v => $"OpaqueRef:{Guid.NewGuid().ToString()}").ToArray();
                }
            }

            db.Tables[clazz].Rows.Add(new_opaque_ref, r);
            return(new_opaque_ref);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Populates the specified Db with the specified XML document.
        /// </summary>
        /// <param name="db">The <see cref="Db"/> to be populated.</param>
        /// <param name="doc">The <see cref="XmlDocument"/> to be read.</param>
        public void PopulateDbFromXml(Db db, XmlDocument doc)
        {
            Util.ThrowIfParameterNull(db, "db");
            Util.ThrowIfParameterNull(doc, "doc");

            if (db.Tables.Keys.Count > 0)
            {
                throw new ArgumentException("Specified Db should be empty.", "db");
            }

            XmlNode     dataBaseNode = GetDatabaseNode(doc);
            IEnumerable hostNodes    = GetHostNodes(dataBaseNode);

            foreach (XmlNode child in dataBaseNode.ChildNodes)
            {
                if (child.Name == "table")
                {
                    string   tableName = child.Attributes["name"].Value.ToLower();
                    Db.Table table     = db.Tables.Add(tableName);

                    foreach (XmlNode node in child.ChildNodes)
                    {
                        Db.Row row = table.Rows.Add(node.Attributes["ref"].Value);

                        foreach (XmlAttribute a in node.Attributes)
                        {
                            string name = ParsePropertyName(a.Name, tableName);
                            if (string.IsNullOrEmpty(name))
                            {
                                continue;
                            }

                            row.Props.Add(name, SanitizePropertyValue(a.Value));
                        }
                    }

                    if (tableName == "host_metrics" && child.ChildNodes.Count == 0)  // host_metrics table used to be empty: see CA-31223
                    {
                        foreach (XmlNode host in hostNodes)
                        {
                            string opaque_ref = host.Attributes["metrics"].Value;
                            if (table.Rows.ContainsKey(opaque_ref))
                            {
                                // This doesn't happen with real databases, but for some of the hand-edited ones, we've got duplicate
                                // metrics opaquerefs.
                                continue;
                            }

                            Db.Row row = table.Rows.Add(opaque_ref);
                            row.Props.Add("live", "true");
                            row.Props.Add("memory_total", (2L * 1024 * 1024 * 1024).ToString());
                            row.Props.Add("memory_free", (1L * 1024 * 1024 * 1024).ToString());
                        }
                    }
                }
            }

            foreach (string xenApiType in SimpleProxyMethodParser.AllTypes)
            {
                if (!db.Tables.Keys.Contains(xenApiType))
                {
                    db.Tables.Add(xenApiType);
                }
            }
        }