ChaosSearch Storage API - .NET
createObjectGroup API for ChaosSearch
This section provides an example C#/.NET script that enables users to automate the process of creating object groups all the way through initializing indexing.
Create Object Group API
This section shows an example of the createObjectGroup API.
The API request will:
- Create an Object Group
- Set the filter for grouping logs
- Create Live indexing set-up
- Define a retention policy
- Start Indexing
Note
Users must update the
accessKey
andsecretKey
fields with ChaosSearch API keys.
Example C#/.NET API
using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
namespace ChaosAPI
{
public class Program
{
public static string url = "https://local.chaossearch.io/Bucket/createObjectGroup";
public static string accessKey = "<your accesskey from CHAOS UI>";
public static string secretkey = "<your secretkey from CHAOS UI>";
public static string awsRegion = "us-east-1";
public static string awsServiceName = "s3";
public static void startIndexing(string objectGroupName)
{
string url = "https://local.chaossearch.io/Bucket/model";
HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Post, url);
msg.Headers.Host = msg.RequestUri.Host;
// Get and save dates ready for further use.
DateTimeOffset utcNowSaved = DateTimeOffset.UtcNow;
//Expects this format in the header Sun, 05 Apr 2020 17:16:05 GMT
string reqDate = utcNowSaved.ToString("ddd, dd MMM yyyy HH:mm:ss") + " GMT";
// Add to request header
msg.Headers.Add("Date", reqDate);
// Start Indexing
string reqJson = "{\"BucketName\":\"" + objectGroupName + "\",\"ModelMode\":0}";
msg.Content = new StringContent(reqJson);
Console.WriteLine(reqJson);
string stringToSign = "POST\n\n" + "text/plain; charset=UTF-8\n" + reqDate + "\n/Bucket/model";
HMACSHA1 hmac = new HMACSHA1(Encoding.UTF8.GetBytes(secretkey));
var signature = System.Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign.ToString())));
msg.Headers.Add("Authorization", "AWS " + accessKey + ":" + signature);
// Invoke the request with HttpClient.
HttpClient client = new HttpClient();
HttpResponseMessage result = client.SendAsync(msg).Result;
Console.WriteLine(result.ToString());
if (result.IsSuccessStatusCode)
{
Console.WriteLine("Indexing started for " + objectGroupName);
}
}
public static void Main(string[] args)
{
//Simple example creating an Object Group and starting Indexing
HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Post, url);
msg.Headers.Host = msg.RequestUri.Host;
// Get and save dates ready for further use.
DateTimeOffset utcNowSaved = DateTimeOffset.UtcNow;
//Expects this format in the header Sun, 05 Apr 2020 17:16:05 GMT
string reqDate = utcNowSaved.ToString("ddd, dd MMM yyyy HH:mm:ss") + " GMT";
// Add to requeat header
msg.Headers.Add("Date", reqDate);
string s3bucket = "s3-bucket"; //Bucket contain data that CHOAS has access to (and is visible in UI)
string objectGroupName = "ogTest"; //Object Group Name to create
//JSON for the Object Group, Filter, Type, Live Indexing, Retention
var reqJson = "{\"bucket\":\"" + objectGroupName + "\",\"source\":\"" + s3bucket + "\",\"format\":{\"_type\":\"JSON\",\"stripPrefix\":true},\"interval\":{\"mode\":0,\"column\":0},\"indexRetention\":-1,\"filter\":[{\"field\":\"key\",\"regex\":\".*\"}],\"options\":{\"ignoreIrregular\":true,\"compression\":\"GZIP\"}}";
msg.Content = new StringContent(reqJson);
//Amazon https://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html
//Below is minimum needed to sign for CHAOS with appropriate new lines
string stringToSign = "POST\n\n" + "text/plain; charset=UTF-8\n" + reqDate + "\n/Bucket/createObjectGroup";
HMACSHA1 hmac = new HMACSHA1(Encoding.UTF8.GetBytes(secretkey));
var signature = System.Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign.ToString())));
msg.Headers.Add("Authorization", "AWS " + accessKey + ":" + signature);
// Invoke the request with HttpClient.
HttpClient client = new HttpClient();
HttpResponseMessage result = client.SendAsync(msg).Result;
Console.WriteLine(result.ToString());
if (result.IsSuccessStatusCode) //200 on OK & 400 when already exists
{
Console.WriteLine(objectGroupName + "created successfully.");
startIndexing(objectGroupName);
}
}
}
}
Updated about 1 year ago