/// <summary> /// Create a Nut for a specific application account and in a specific configuration container. /// </summary> /// <param name="account">The application account we are creating the nut for.</param> /// <param name="container">The container we are inserting the nut into (e.g. connectionstrings, endpoints, appconfig, etc).</param> /// <param name="nut">Busta nut.</param> /// <returns>True if the nut was added and false if it already exists, and therefore was not added/updated.</returns> public static bool CreateNut(string account, string container, Nut nut) { bool nutAdded = false; Mongo mongo = new Mongo(); mongo.Connect(); var db = mongo.GetDatabase(WellKnownDb.AppConfiguration); var collection = db.GetCollection(container); var doc = new Document(); doc["account"] = account; doc["name"] = nut.Key; doc["value"] = nut.Value; if (nut.Properties != null) { foreach (var k in nut.Properties.Keys) { doc.Add(k, nut.Properties[k]); } } if (collection.FindOne(doc) == null) { collection.Insert(doc); nutAdded = true; } return nutAdded; }
static void ConfigAdd(string[] args) { var squirrel = new SquirrelClient(new Uri("http://localhost:49622/squirrel"), "crm"); var nut = new Nut { Table="ConnectionStrings", Key = "test", Value = "<endpoint xmlns='testing'><b>testing</b></endpoint>" }; squirrel.Add(nut); }
public void Add(Nut nut) { Nut response = null; string uri = string.Format("{0}/{1}/{2}", Address.AbsoluteUri.ToLower(), Account.ToLower(), nut.Table.ToLower()); var client = new HttpClient(uri); MemoryStream ms = new MemoryStream(); DataContractSerializer s = new DataContractSerializer(typeof(Nut)); s.WriteObject(ms, nut); ms.Position = 0; HttpResponseMessage r = client.Post(uri, HttpContent.Create(ms.ToArray(), "application/xml")); ms.Close(); r.EnsureStatusIsSuccessful(); if (r.StatusCode == System.Net.HttpStatusCode.OK) { nut.Uri = uri; } }
static void Main(string[] args) { var squirrel = new SquirrelClient(new Uri("http://localhost/squirrel"), "crm"); Nut nut = new Nut() { Table="ConnectionStrings", Key = "intranetdb", Value = "this is my development intranet db connectionstring", Properties = new Dictionary<string,string>() }; nut.Properties.Add("environment", "Development"); squirrel.Add(nut); var cs = squirrel.Get("ConnectionStrings", "aem"); Console.WriteLine(cs.Value); }
public void Nut_CreateNut_ShouldCreate() { var nut = new Nut() { Database = TestAccount, Key = Guid.NewGuid().ToString(), Value = Guid.NewGuid().ToString() }; bool created = SquirrelBusinessLogic.CreateNut(TestAccount, TestContainer, nut); Assert.IsTrue(created); }
public void Nut_FindNut_NutShouldNotBeNull() { var nut = new Nut() { Database = TestAccount, Key = Guid.NewGuid().ToString(), Value = Guid.NewGuid().ToString() }; SquirrelBusinessLogic.CreateNut(TestAccount, TestContainer, nut); var result = SquirrelBusinessLogic.FindNut(TestAccount, TestContainer, nut.Key, new Dictionary<string, string>()); Assert.IsNotNull(result); }
public void Create(string account, string container, Nut nut) { bool success = false; try { success = SquirrelBusinessLogic.CreateNut(account, container, nut); } catch (Exception eax) { throw new WebFaultException<string>(eax.Message, System.Net.HttpStatusCode.InternalServerError); } finally { if (success) { WebOperationContext.Current.OutgoingResponse.SetStatusAsCreated(CreateUri(account, container, nut)); } } }
protected override void ProcessRecord() { try { var uri = Uri; var tokens = uri.Split('/'); var account = tokens[1]; var table = tokens[0]; var name = tokens[2]; var squirrel = new SquirrelClient(new Uri("http://localhost/squirrel"), account); var nut = new Nut() { Key = name, Table = table, Value = Value }; squirrel.Add(nut); WriteObject("Nut added"); } catch (Exception eax) { WriteError(new ErrorRecord(eax, "", ErrorCategory.InvalidOperation, this)); } }
public static List<Nut> FindAllInContainer(string account, string container, IDictionary<string,string> properties) { List<Nut> nuts = new List<Nut>(); Mongo mongo = new Mongo(); mongo.Connect(); var db = mongo.GetDatabase(WellKnownDb.AppConfiguration); var collection = db.GetCollection(container); var query = new Document(); query["account"] = account; foreach (string k in properties.Keys) { var t = properties[k]; if (k == "value") { query["value"] = t as string; } else { query[k] = properties[k]; } } var result = collection.Find(query); foreach (var r in result.Documents) { var nut = new Nut { Database = WellKnownDb.AppConfiguration, Table = container, Key = r["name"] as string, Properties = new Dictionary<string, string>() }; nut.Value = r["value"] as string; nut.Uri = string.Format("/{0}/{1}/{2}", account, container, r["name"]); foreach (var j in r.Keys) { nut.Properties.Add(j, r[j] as string); } nuts.Add(nut); } return nuts; }
public static Nut FindNut(string account, string container, string name, IDictionary<string,string> properties) { Nut nut = null; Mongo mongo = new Mongo(); mongo.Connect(); var db = mongo.GetDatabase(WellKnownDb.AppConfiguration); var collection = db.GetCollection(container); var query = new Document(); query["name"] = name; query["account"] = account; foreach (string k in properties.Keys) { query[k] = properties[k]; } var result = collection.FindOne(query); if (result != null) { nut = new Nut { Database = WellKnownDb.AppConfiguration, Table = container, Key = name, Properties = new Dictionary<string, string>() }; nut.Value = result["value"] as string; nut.Uri = string.Format("/{0}/{1}/{2}", account, container, name); foreach (var j in result.Keys) { nut.Properties.Add(j, result[j] as string); } } return nut; }
private Uri CreateUri(string account, string table, Nut nut) { string t = string.Format("{0}/{1}/{2}/{3}", WebOperationContext.Current.IncomingRequest.UriTemplateMatch.BaseUri, account, table, nut.Key); return new Uri(t); }