/// <summary> /// Converts an object to a GremlinVertex allowing more control over how the id, partitionKey, and label properties are set. /// </summary> /// <param name="idProperty">Which property of the object should be used for id of the GremlinVertex, defaults to "id"</param> /// <param name="partitionKeyProperty">Which property should be used for partitionKey of the GremlinVertex, defaults to "partitionKey"</param> /// <param name="vertexLabel">What value should be used for the label property, defaults to the Type name of the object</param> /// <returns> /// A new instance of a GremlinVertex /// with its id and partitionKey property values set to and <typeparamref name="idProperty"/> and <typeparamref name="partitionKeyProeprty"/> properties respectively /// and the label property set to the value of <typeparamref name="label"/> /// </returns> public static GremlinVertex ToGremlinVertex(this object obj, string idProperty, string partitionKeyProperty, string vertexLabel) { if (string.IsNullOrWhiteSpace(idProperty)) { throw new ArgumentException($"{nameof(idProperty)} cannot be Null or Empty", nameof(idProperty)); } if (string.IsNullOrWhiteSpace(partitionKeyProperty)) { throw new ArgumentException($"{nameof(partitionKeyProperty)} cannot be Null or Empty", nameof(partitionKeyProperty)); } if (string.IsNullOrWhiteSpace(vertexLabel)) { throw new ArgumentException($"{nameof(vertexLabel)} cannot be Null or Empty", nameof(vertexLabel)); } var gv = new GremlinVertex(obj.GetPropertyValue(idProperty).ToString(), vertexLabel); gv.AddProperty(new GremlinVertexProperty("partitionKey", obj.GetPropertyValue(partitionKeyProperty))); //Get a list of all Properties, except where the name is "id" or "partitionKey" var props = obj.GetType().GetProperties() .Where(x => !x.Name.Equals("id", StringComparison.InvariantCultureIgnoreCase) && !x.Name.Equals("partitionKey", StringComparison.InvariantCultureIgnoreCase)); foreach (var prop in props) { gv.AddProperty(new GremlinVertexProperty(prop.Name, prop.GetValue(obj))); } return(gv); }
private static GremlinVertex BuildGremlinVertex(string vertexLabel, string id, object partitionKey, IEnumerable <Tuple <string, object> > vertexProperties) { if (string.IsNullOrWhiteSpace(vertexLabel)) { throw new ArgumentNullException($"{nameof(vertexLabel)} cannot be Null or Empty", nameof(vertexLabel)); } if (string.IsNullOrWhiteSpace(id)) { throw new ArgumentNullException($"{nameof(id)} cannot be Null or Empty", nameof(id)); } if (partitionKey == null) { throw new ArgumentNullException($"{nameof(partitionKey)} cannot be Null or Empty", nameof(partitionKey)); } GremlinVertex gv = new GremlinVertex(id, vertexLabel); gv.AddProperty(new GremlinVertexProperty("partitionKey", partitionKey)); foreach ((string name, object value) in vertexProperties) { gv.AddProperty(new GremlinVertexProperty(name, value)); } return(gv); }
public static void DirectoryRoleMembership(DirectoryRole _, List <GroupMember> members) { var gremlinVertices = new List <GremlinVertex>(); var gremlinEdges = new List <GremlinEdge>(); var vertex = new GremlinVertex(_.Id, nameof(Models.BloodHound.DirectoryRole)); vertex.AddProperty(CosmosDbHelper.CollectionPartitionKey, _.Id.GetHashCode()); vertex.AddProperty(nameof(_.DisplayName), _.DisplayName?.ToUpper() ?? string.Empty); gremlinVertices.Add(vertex); members.ForEach(member => { gremlinEdges.Add(new GremlinEdge( _.Id + member.Id, "MemberOf", member.Id, _.Id, nameof(User), nameof(Models.BloodHound.DirectoryRole), member.Id.GetHashCode(), _.Id.GetHashCode())); }); CosmosDbHelper.RunImportVerticesBlock.Post(gremlinVertices); CosmosDbHelper.RunImportEdgesBlock.Post(gremlinEdges); }
public static IEnumerable <GremlinVertex> GenerateVerticesCustom(long count) { // CosmosDB currently doesn't support documents with id length > 1000 GremlinVertex vBad = new GremlinVertex(getLongId(), "vertex"); vBad.AddProperty(ConfigurationManager.AppSettings["CollectionPartitionKey"], 0); yield return(vBad); string dirFile = @"..\..\data\vertices"; foreach (string fileName in Directory.GetFiles(dirFile)) { string jsonValue = System.IO.File.ReadAllText(fileName); var RootObjects = JsonConvert.DeserializeObject <List <RootObject> >(jsonValue); foreach (var rootObject in RootObjects) { //GremlinVertex v = new GremlinVertex(rootObject.Name, "vertex"); GremlinVertex v = new GremlinVertex(rootObject.Name + " " + rootObject.pk, "vertex"); v.AddProperty(ConfigurationManager.AppSettings["CollectionPartitionKey"], rootObject.pk); //System.Diagnostics.Debug.WriteLine("rootObject.Name: "+ rootObject.Name); v.AddProperty("Name", rootObject.Name); //v.AddProperty("Age", rootObject.Age); yield return(v); } } }
private static GremlinVertex ToCosmosDBVertex(INode node) { var vertex = new GremlinVertex(node.Id.ToString(), node.Labels.First()); AddProperties(node.Properties, (n, v) => vertex.AddProperty(n, v)); return(vertex); }
public static void DeviceOwners(Device _, List <DirectoryObject> ownerList, List <DirectoryRole> directoryRoles) { try { var gremlinVertices = new List <GremlinVertex>(); var gremlinEdges = new List <GremlinEdge>(); var deviceOwnerGroups = directoryRoles.Where(__ => AzureActiveDirectoryHelper.DeviceOwnerGroupDisplayNames.Contains(__.DisplayName)); var vertex = new GremlinVertex(_.Id, nameof(Computer)); vertex.AddProperty(CosmosDbHelper.CollectionPartitionKey, _.Id.GetHashCode()); vertex.AddProperty(nameof(_.DisplayName), _.DisplayName?.ToUpper() ?? string.Empty); gremlinVertices.Add(vertex); ownerList.ForEach(__ => { var user = (User)__; var gremlinEdge = new GremlinEdge( user.Id + _.Id, "AdminTo", user.Id, _.Id, nameof(User), nameof(Computer), user.Id.GetHashCode(), _.Id.GetHashCode()); gremlinEdges.Add(gremlinEdge); }); deviceOwnerGroups.ForEach(directoryRole => { var gremlinEdge = new GremlinEdge( directoryRole.Id + _.Id, "AdminTo", directoryRole.Id, _.Id, nameof(DirectoryRole), nameof(Computer), directoryRole.Id.GetHashCode(), _.Id.GetHashCode()); gremlinEdges.Add(gremlinEdge); }); CosmosDbHelper.RunImportVerticesBlock.Post(gremlinVertices); CosmosDbHelper.RunImportEdgesBlock.Post(gremlinEdges); } catch (ClientException ex) { throw new Exception(ex.Message); } }
internal static GremlinVertex CreateGremlinVertex(string id, string label, Dictionary <string, object> properties) { var vertex = new GremlinVertex(id, label); foreach (var property in properties) { vertex.AddProperty(property.Key, property.Value); } return(vertex); }
public static void Users(User user) { var gremlinVertices = new List <GremlinVertex>(); var userVertex = new GremlinVertex(user.Id, nameof(User)); userVertex.AddProperty(CosmosDbHelper.CollectionPartitionKey, user.Id.GetHashCode()); userVertex.AddProperty(nameof(user.UserPrincipalName), user.UserPrincipalName ?? string.Empty); userVertex.AddProperty(nameof(user.Mail), user.Mail ?? string.Empty); userVertex.AddProperty(nameof(user.DisplayName), user.DisplayName?.ToUpper() ?? string.Empty); gremlinVertices.Add(userVertex); CosmosDbHelper.RunImportVerticesBlock.Post(gremlinVertices); }
public void BuildsFromNewtonsoftDeserializedObject() { const string idval = "id"; const string pkval = "pk value"; string serializedObject = JsonConvert.SerializeObject(new { Id = idval, partitionKey = pkval }); dynamic deserializedObject = JsonConvert.DeserializeObject(serializedObject); GremlinVertex gv = ((object)deserializedObject).ToGremlinVertex(); Assert.AreEqual(idval, gv.Id); Assert.AreEqual(pkval, gv.GetVertexProperties("partitionKey").FirstOrDefault().Value.ToString()); }
private async Task RunBulkImportVerticesAsync(GraphBulkExecutor graphbulkExecutor, VertexSpec vertexSpec) { var tokenSource = new CancellationTokenSource(); var token = tokenSource.Token; BulkImportResponse response = null; List <GremlinVertex> vObjects = null; int currentCount = 0; while (currentCount < vertexSpec.count) { vObjects = new List <GremlinVertex>(); for (int i = 0; i < 100000 && currentCount < vertexSpec.count; i++) { string id = createVertexId(vertexSpec.label, currentCount); GremlinVertex v = new GremlinVertex(id, vertexSpec.label); for (int j = 0; j < vertexSpec.numberOfProperties; j++) { v.AddProperty("property_" + j, Guid.NewGuid().ToString()); } v.AddProperty(pkPropertyName, id); currentCount += 1; vObjects.Add(v); } try { response = await graphbulkExecutor.BulkImportAsync( vObjects, enableUpsert : true, disableAutomaticIdGeneration : true, maxConcurrencyPerPartitionKeyRange : null, maxInMemorySortingBatchSize : null, cancellationToken : token); } catch (DocumentClientException de) { Trace.TraceError("Document client exception: {0}", de); } catch (Exception e) { Trace.TraceError("Exception: {0}", e); } } }
public static void AppOwnership(Application app, List <DirectoryObject> owners, string applicationAdministratorRoleId = "534a2975-e5b9-4ea1-b7da-deaec0a7c0aa") { try { var gremlinVertices = new List <GremlinVertex>(); var gremlinEdges = new List <GremlinEdge>(); var vertex = new GremlinVertex(app.AppId, nameof(Models.BloodHound.Application)); vertex.AddProperty(CosmosDbHelper.CollectionPartitionKey, app.AppId.GetHashCode()); vertex.AddProperty(nameof(app.DisplayName), app.DisplayName?.ToUpper() ?? string.Empty); gremlinVertices.Add(vertex); owners.ForEach(owner => { var gremlinEdge = new GremlinEdge( owner.Id + app.AppId, "Owner", owner.Id, app.AppId, nameof(User), nameof(Models.BloodHound.Application), owner.Id.GetHashCode(), app.AppId.GetHashCode()); gremlinEdges.Add(gremlinEdge); }); var gremlinEdge2 = new GremlinEdge( applicationAdministratorRoleId + app.AppId, "Owner", applicationAdministratorRoleId, app.AppId, nameof(DirectoryRole), nameof(Models.BloodHound.Application), applicationAdministratorRoleId.GetHashCode(), app.AppId.GetHashCode() ); gremlinEdges.Add(gremlinEdge2); CosmosDbHelper.RunImportVerticesBlock.Post(gremlinVertices); CosmosDbHelper.RunImportEdgesBlock.Post(gremlinEdges); } catch (Exception e) { Console.WriteLine(e); throw; } }
public void BuildsFromExpandoObjectWithDifferentCasingVariations(string idPropertyName, string partitionKeyPropertyName) { const string idValue = "id"; const string primaryKeyValue = "pk value"; dynamic dynamicObject = new ExpandoObject(); ((IDictionary <string, object>)dynamicObject).Add(idPropertyName, idValue); ((IDictionary <string, object>)dynamicObject).Add(partitionKeyPropertyName, primaryKeyValue); GremlinVertex gv = ((object)dynamicObject).ToGremlinVertex(); Assert.AreEqual(idValue, gv.Id); Assert.AreEqual(primaryKeyValue, gv.GetVertexProperties("partitionKey").FirstOrDefault().Value.ToString()); }
/// <summary> /// Generates the movies vertices. /// </summary> /// <returns></returns> public static IEnumerable <GremlinVertex> GenerateMovieVertices() { // Read the file as one string. var text = File.ReadAllText( ConfigurationManager.AppSettings["dataPath"] + @"\Vertices\movies.json"); var movies = JsonConvert.DeserializeObject <List <Movie> >(text); var vertices = new List <GremlinVertex>(); foreach (var movie in movies) { var v = new GremlinVertex(movie.id, "movie"); v.AddProperty("pk", movie.id); v.AddProperty("title", movie.title); if (movie.adult != null) { v.AddProperty("isAdult", movie.adult); } v.AddProperty("budget", movie.budget ?? -1); v.AddProperty("homepage", movie.homepage ?? string.Empty); v.AddProperty("imdbId", movie.imdb_id ?? string.Empty); v.AddProperty("originalLanguage", movie.original_language ?? string.Empty); v.AddProperty("originalTitle", movie.original_title ?? string.Empty); v.AddProperty("overview", movie.overview ?? string.Empty); v.AddProperty("popularity", movie.popularity ?? -1.0); v.AddProperty("posterPath", movie.poster_path ?? string.Empty); v.AddProperty("releaseDate", movie.release_date ?? DateTime.MinValue.ToUniversalTime()); v.AddProperty("revenue", movie.revenue ?? -1); v.AddProperty("runtime", movie.runtime ?? -1.0); v.AddProperty("status", movie.status ?? string.Empty); v.AddProperty("tagline", movie.tagline ?? string.Empty); if (movie.video != null) { v.AddProperty("isVideo", movie.video); } v.AddProperty("voteAverage", movie.vote_average ?? -1.0); v.AddProperty("voteCount", movie.vote_count ?? -1); vertices.Add(v); } return(vertices); }
private GremlinVertex ToVertex(V v) { var vertex = new GremlinVertex(v.Id, v.Label); // make sure partition key is added, i.e. if partition key is dcName, vertex.AddProperty("dcName", "***"); var propValues = v.GetPropertyValues(vertexProps); if (propValues != null && propValues.Count > 0) { foreach (var key in propValues.Keys) { vertex.AddProperty(key, propValues[key]); } } return(vertex); }
public static IEnumerable <GremlinVertex> ConvertLocationsToVertices(IEnumerable <Location> locations) { foreach (Location location in locations) { string thisIndex = location.LocationID.ToString(); string locTripKey = "location." + thisIndex; String locTripPK = "locations"; GremlinVertex v = new GremlinVertex(locTripKey, "location"); v.AddProperty(ConfigurationManager.AppSettings["CollectionPartitionKey"], locTripPK);//<10k docs so putting in 1 partition v.AddProperty("LocationID", location.LocationID); v.AddProperty("Borough", location.Borough); v.AddProperty("Zone", location.Zone); v.AddProperty("service_zone", location.service_zone); yield return(v); } }
/// <summary> /// Generates the user vertices (from big file - 20M ratings). /// </summary> /// <returns></returns> public static IEnumerable <GremlinVertex> GenerateUserVerticesFull() { var lines = File.ReadAllLines( ConfigurationManager.AppSettings["dataPath"] + @"\ratings.csv"); var vertices = new List <GremlinVertex>(); foreach (var line in lines) { var data = line.Split(','); var v = new GremlinVertex(data[0], "collection"); v.AddProperty("pk", data[0]); v.AddProperty("name", data[0]); vertices.Add(v); } return(vertices); }
public static IEnumerable <GremlinVertex> GenerateVertices(long count) { // CosmosDB currently doesn't support documents with id length > 1000 GremlinVertex vBad = new GremlinVertex(GetLongId(), "vertex"); vBad.AddProperty(ConfigurationManager.AppSettings["CollectionPartitionKey"], 0); yield return(vBad); for (long i = 0; i < count; i++) { GremlinVertex v = new GremlinVertex(i.ToString(), "vertex"); v.AddProperty(ConfigurationManager.AppSettings["CollectionPartitionKey"], i); v.AddProperty("name1", "name" + i); v.AddProperty("name2", i * 2); v.AddProperty("name3", i * 3); v.AddProperty("name4", i + 100); yield return(v); } }
/// <summary> /// Generates the language vertices. /// </summary> /// <returns></returns> public static IEnumerable <GremlinVertex> GenerateLanguageVertices() { // Read the file as one string. var text = File.ReadAllText( ConfigurationManager.AppSettings["dataPath"] + @"\Vertices\languages.json"); var languages = JsonConvert.DeserializeObject <List <SpokenLanguage> >(text); var vertices = new List <GremlinVertex>(); foreach (var language in languages) { var v = new GremlinVertex(language.iso_639_1, "language"); v.AddProperty("pk", language.iso_639_1); v.AddProperty("name", language.name); vertices.Add(v); } return(vertices); }
/// <summary> /// Generates the company vertices. /// </summary> /// <returns></returns> public static IEnumerable <GremlinVertex> GenerateCompanyVertices() { // Read the file as one string. var text = File.ReadAllText( ConfigurationManager.AppSettings["dataPath"] + @"\Vertices\companies.json"); var companies = JsonConvert.DeserializeObject <List <ProductionCompany> >(text); var vertices = new List <GremlinVertex>(); foreach (var company in companies) { var v = new GremlinVertex(company.id, "company"); v.AddProperty("pk", company.id); v.AddProperty("name", company.name); vertices.Add(v); } return(vertices); }
/// <summary> /// Generates the genre vertices. /// </summary> /// <returns></returns> public static IEnumerable <GremlinVertex> GenerateGenreVertices() { // Read the file as one string. var text = File.ReadAllText( ConfigurationManager.AppSettings["dataPath"] + @"\Vertices\genres.json"); var genres = JsonConvert.DeserializeObject <List <Genre> >(text); var vertices = new List <GremlinVertex>(); foreach (var genre in genres) { var v = new GremlinVertex(genre.id, "genre"); v.AddProperty("pk", genre.id); v.AddProperty("name", genre.name); vertices.Add(v); } return(vertices); }
/// <summary> /// Convert to gremlin vertex /// </summary> /// <typeparam name="V"></typeparam> /// <param name="vertex"></param> /// <param name="serializer"></param> /// <returns></returns> public static GremlinVertex ToVertex <V>(this V vertex, JsonSerializer serializer) { var jvertex = vertex.ToJElement(serializer, out var id, out var label, out var pk); var gvertex = new GremlinVertex(id, label); foreach (var prop in jvertex) { switch (prop.Key.ToLowerInvariant()) { case DocumentProperties.IdKey: case DocumentProperties.LabelKey: case DocumentProperties.PartitionKey: break; default: gvertex.AddProperty(prop.Key, prop.Value); break; } } return(gvertex); }
/// <summary> /// Generates the person vertices. /// </summary> /// <returns></returns> public static IEnumerable <GremlinVertex> GeneratePersonVertices() { // Read the file as one string. var text = File.ReadAllText( ConfigurationManager.AppSettings["dataPath"] + @"\Vertices\persons.json"); var persons = JsonConvert.DeserializeObject <List <Person> >(text); var vertices = new List <GremlinVertex>(); foreach (var person in persons) { var v = new GremlinVertex(person.id, "person"); v.AddProperty("pk", person.id); v.AddProperty("name", person.name); v.AddProperty("gender", person.gender); v.AddProperty("profilePath", person.profile_path ?? string.Empty); vertices.Add(v); } return(vertices); }
/// <summary> /// Generates the collection vertices. /// </summary> /// <returns></returns> public static IEnumerable <GremlinVertex> GenerateCollectionVertices() { // Read the file as one string. var text = File.ReadAllText( ConfigurationManager.AppSettings["dataPath"] + @"\Vertices\collections.json"); var collections = JsonConvert.DeserializeObject <List <BelongsToCollection> >(text); var vertices = new List <GremlinVertex>(); foreach (var collection in collections) { var v = new GremlinVertex(collection.id, "collection"); v.AddProperty("pk", collection.id); v.AddProperty("name", collection.name); v.AddProperty("backdropPath", collection.backdrop_path ?? string.Empty); v.AddProperty("posterPath", collection.poster_path ?? string.Empty); vertices.Add(v); } return(vertices); }
public static void GroupMembership <T>(Group _, List <T> groupMembers) where T : GroupMember { try { var gremlinVertices = new List <GremlinVertex>(); var gremlinEdges = new List <GremlinEdge>(); var vertex = new GremlinVertex(_.Id, nameof(AzureAdLateralMovement.Models.BloodHound.Group)); vertex.AddProperty(CosmosDbHelper.CollectionPartitionKey, _.Id.GetHashCode()); vertex.AddProperty(nameof(_.DisplayName), _.DisplayName?.ToUpper() ?? string.Empty); gremlinVertices.Add(vertex); groupMembers.ForEach(member => { var gremlinEdge = new GremlinEdge( _.Id + member.Id, member is GroupOwner ? "Owner" : "MemberOf", member.Id, _.Id, member.Type, nameof(AzureAdLateralMovement.Models.BloodHound.Group), member.Id.GetHashCode(), _.Id.GetHashCode()); gremlinEdges.Add(gremlinEdge); }); CosmosDbHelper.RunImportVerticesBlock.Post(gremlinVertices); CosmosDbHelper.RunImportEdgesBlock.Post(gremlinEdges); } catch (Exception e) { Console.WriteLine(e); throw; } }
public static void Applications( string appDisplayName, string appId, HashSet <string> permissionsSet, HashSet <string> userIdSet, string principalId, string homePage, string appOwnerOrganizationId) { try { var gremlinVertices = new List <GremlinVertex>(); var gremlinEdges = new List <GremlinEdge>(); var vertex = new GremlinVertex(appId, nameof(Models.BloodHound.Application)); vertex.AddProperty(CosmosDbHelper.CollectionPartitionKey, appId.GetHashCode()); vertex.AddProperty(nameof(Application.DisplayName), appDisplayName?.ToUpper() ?? string.Empty); vertex.AddProperty(nameof(permissionsSet), permissionsSet.ToDelimitedString(",") ?? string.Empty); vertex.AddProperty(nameof(principalId), principalId ?? string.Empty); vertex.AddProperty(nameof(homePage), homePage ?? string.Empty); vertex.AddProperty(nameof(appOwnerOrganizationId), appOwnerOrganizationId ?? string.Empty); gremlinVertices.Add(vertex); if (appOwnerOrganizationId != null) { var vertexDomain = new GremlinVertex(appOwnerOrganizationId, nameof(Models.BloodHound.Domain)); vertexDomain.AddProperty(CosmosDbHelper.CollectionPartitionKey, appOwnerOrganizationId.GetHashCode()); vertexDomain.AddProperty(nameof(Application.DisplayName), appOwnerOrganizationId.ToUpper()); gremlinVertices.Add(vertexDomain); gremlinEdges.Add( new GremlinEdge( appOwnerOrganizationId + appId, "Owner", appOwnerOrganizationId, appId, nameof(Models.BloodHound.Domain), nameof(Application), appOwnerOrganizationId.GetHashCode(), appId.GetHashCode() )); } if (permissionsSet.Any(_ => string.Equals(_, "Directory.AccessAsUser.All", StringComparison.OrdinalIgnoreCase))) { userIdSet.ForEach(userId => gremlinEdges.Add( new GremlinEdge( appId + userId, "AccessAsUser", appId, userId, nameof(Models.BloodHound.Application), nameof(User), appId.GetHashCode(), userId.GetHashCode() )) ); } CosmosDbHelper.RunImportVerticesBlock.Post(gremlinVertices); CosmosDbHelper.RunImportEdgesBlock.Post(gremlinEdges); } catch (Exception e) { Console.WriteLine(e); throw; } }
public static void Applications( string appDisplayName, string appId, HashSet <string> permissionsSet, string principalId) { try { var gremlinVertices = new List <GremlinVertex>(); var gremlinEdges = new List <GremlinEdge>(); var vertex = new GremlinVertex(appId, nameof(Application)); vertex.AddProperty(CosmosDbHelper.CollectionPartitionKey, appId.GetHashCode()); vertex.AddProperty(nameof(appDisplayName), appDisplayName?.ToUpper() ?? string.Empty); vertex.AddProperty(nameof(permissionsSet), permissionsSet.ToDelimitedString(",") ?? string.Empty); gremlinVertices.Add(vertex); var outVertexId = principalId ?? "AccessToAllPrincipals"; var gremlinEdge = new GremlinEdge( outVertexId + appId, "Granted", appId, outVertexId, nameof(Models.BloodHound.User), nameof(Application), appId.GetHashCode(), outVertexId.GetHashCode()); gremlinEdge.AddProperty(nameof(permissionsSet), permissionsSet.ToDelimitedString(",") ?? string.Empty); gremlinEdges.Add(gremlinEdge); var mailPermissions = new List <string> { "Mail.Read", "Mail.ReadBasic", "Mail.ReadWrite", "Mail.Read.Shared", "Mail.ReadWrite.Shared", "Mail.Send", "Mail.Send.Shared", "MailboxSettings.Read", "Mail.Read", "Mail.ReadWrite", "Mail.Send", "MailboxSettings.Read", "MailboxSettings.ReadWrite" }; if (permissionsSet.Overlaps(mailPermissions)) { gremlinEdge = new GremlinEdge( appId + "MailBoxes", "CanManipulate", appId, "MailBoxes", nameof(Application), nameof(Application), appId.GetHashCode(), "MailBoxes".GetHashCode()); gremlinEdges.Add(gremlinEdge); } CosmosDbHelper.RunImportVerticesBlock.Post(gremlinVertices); CosmosDbHelper.RunImportEdgesBlock.Post(gremlinEdges); } catch (Exception e) { Console.WriteLine(e); throw; } }
private async Task ProcessTasks() { List <String> documentsToBeImported = new List <string>(); FileReader reader = new FileReader(InputFolder); documentsToBeImported = reader.LoadDocuments(); List <Tuple <String, String, String> > mapping = Utility.XmlToTuples(documentsToBeImported); List <GremlinEdge> edges = new List <GremlinEdge>(); List <GremlinVertex> vertices = new List <GremlinVertex>(); foreach (var tuple in mapping) { string outvertex = tuple.Item1; string invertex = tuple.Item2; string type = tuple.Item3; if (type == "E") { GremlinVertex vertex1 = new GremlinVertex(outvertex, "Emisor"); vertex1.AddProperty("pk", outvertex); GremlinVertex vertex2 = new GremlinVertex(invertex, "CFDI"); vertex2.AddProperty("pk", invertex); if (!vertices.Contains(vertex1)) { vertices.Add(vertex1); } if (!vertices.Contains(vertex2)) { vertices.Add(vertex2); } GremlinEdge edge = new GremlinEdge(outvertex + invertex, outvertex + invertex, outvertex, invertex, outvertex, invertex, outvertex, invertex); if (!edges.Contains(edge)) { edges.Add(edge); } } else if (type == "R") { GremlinVertex vertex1 = new GremlinVertex(outvertex, "CFDI"); vertex1.AddProperty("pk", outvertex); GremlinVertex vertex2 = new GremlinVertex(invertex, "Receptor"); vertex2.AddProperty("pk", invertex); if (!vertices.Contains(vertex1)) { vertices.Add(vertex1); } if (!vertices.Contains(vertex2)) { vertices.Add(vertex2); } GremlinEdge edge = new GremlinEdge(outvertex + invertex, outvertex + invertex, outvertex, invertex, outvertex, invertex, outvertex, invertex); if (!edges.Contains(edge)) { edges.Add(edge); } } } await RunBulkImportAsync(edges, vertices); }
private static GremlinVertex ConvertToGremlinVertex(JObject doc) { GremlinVertex gv = null; if (doc.GetValue("_isEdge") == null) { string docId = (string)doc.GetValue("id"); // add id // add label gv = new GremlinVertex(docId, (string)doc.GetValue("label")); // add pk : Elevating the partition key object as root level object for vertices JArray pk = (JArray)doc.GetValue(ConfigurationManager.AppSettings["DestPartitionKey"]); if (pk.Count > 1) { throw new Exception("Partition key property can't be multi-valued property"); } JObject prop = (JObject)pk[0]; object pkObject = prop.GetValue("_value"); gv.AddProperty(new GremlinVertexProperty(ConfigurationManager.AppSettings["DestPartitionKey"], pkObject)); if (!Program.idPKMapping.ContainsKey(docId)) { idPKMapping.Add(docId, pkObject); } // add ttl if (doc.GetValue("ttl") != null) { gv.AddProperty(new GremlinVertexProperty("ttl", (long)doc.GetValue("ttl"))); } // add all properties foreach (JProperty jp in doc.Properties()) { if (jp.Name == "id" || jp.Name == ConfigurationManager.AppSettings["DestPartitionKey"] || jp.Name == "ttl" || jp.Name == "label" || jp.Name == "_rid" || jp.Name == "_etag" || jp.Name == "_self" || jp.Name == "_ts" || jp.Name == "_attachements" || !(jp.Value is JArray)) { continue; } JArray propArray = jp.Value as JArray; JObject propObject = (JObject)propArray[0]; object propertValue = propObject.GetValue("_value"); gv.AddProperty(new GremlinVertexProperty(jp.Name, propertValue)); } } return(gv); }
public static IEnumerable <GremlinVertex> ConvertGreenTripsToVertices(IEnumerable <GreenTrip> greenTrips, List <GremlinVertex> Veritices, List <GremlinEdge> edges) { foreach (GreenTrip trip in greenTrips) { string thisIndex = GreenTripVertexIndex++.ToString(); string greenTripKey = "greentrip." + thisIndex; String greenTripPK = String.Format("{0}:{1}", trip.VendorID, thisIndex); GremlinVertex v = new GremlinVertex(greenTripKey, "greentrip"); v.AddProperty(ConfigurationManager.AppSettings["CollectionPartitionKey"], greenTripPK); v.AddProperty("vendorid", trip.VendorID); v.AddProperty("lpep_pickup_datetime", trip.lpep_pickup_datetime); v.AddProperty("PULocationID", trip.PULocationID); v.AddProperty("DOLocationID", trip.DOLocationID); //yield return v; Veritices.Add(v); //Add the edges here as will need a reference to the original objects //pickup start GremlinEdge e = new GremlinEdge( "e.pickup." + thisIndex, "has_pickup", greenTripKey, //green trip "location." + trip.PULocationID.ToString(), //location ID "greentrip", "location", greenTripPK, "locations"); edges.Add(e); //now reverse -- not sure if needed (Dont think so unless want to reverse query) //e = new GremlinEdge( // "e.pickup_greentrip." + thisIndex, // "pickup_greentrip", // "location." + trip.PULocationID.ToString(), //location ID // greenTripKey, //green trip // "location", // "greentrip", // "locations", // greenTripPK); //edges.Add(e); //dropoff e = new GremlinEdge( "e.dropoff." + thisIndex, "has_dropoff", greenTripKey, //green trip "location." + trip.DOLocationID.ToString(), //location ID "greentrip", "location", greenTripPK, "locations"); edges.Add(e); //now reverse -- not sure if needed (Dont think so unless want to reverse query) //e = new GremlinEdge( // "e.dropoff_greentrip." + thisIndex, // "dropoff_greentrip", // "location." + trip.DOLocationID.ToString(), //location ID // greenTripKey, //green trip // "location", // "greentrip", // "locations", // greenTripPK); //if (GreenTripVertexIndex == 2) break; //force just 10k records } return(null); }