Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using Microsoft.Azure.WebJobs;
- using Microsoft.Azure.WebJobs.Extensions.Http;
- using Microsoft.Azure.WebJobs.Host;
- using Microsoft.Azure; // Namespace for CloudConfigurationManager
- using Microsoft.WindowsAzure.Storage; // Namespace for CloudStorageAccount
- using Microsoft.WindowsAzure.Storage.Blob; // Namespace for Blob storage types
- using System.IO;
- using Newtonsoft.Json.Linq;
- using Newtonsoft;
- using Newtonsoft.Json;
- using System;
- using System.Net.Http.Headers;
- namespace RemoteConfiguration
- {
- public static class GetRemoteConfiguration
- {
- [FunctionName("GetRemoteConfiguration")]
- public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "head", "get", "post", Route = "GetRemoteConfiguration/{farm}/{tenant}/{component}")]HttpRequestMessage req, string farm, string tenant, TraceWriter log, string component)
- {
- log.Info("C# HTTP trigger function processed a request.");
- if (string.IsNullOrEmpty(farm) || string.IsNullOrEmpty(tenant))
- {
- return req.CreateErrorResponse(HttpStatusCode.BadRequest, "you failed to provide a 'farm' and/or 'tenant' parameters");
- }
- var componentName = component?.ToString();
- if (componentName == "defaults") componentName = "";
- // Parse the connection string and return a reference to the storage account.
- CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=mtstcc1storage;AccountKey=NWOlKhOzFphjqUwxzMcLtKzv7YfJXHR7eTNsvk6IHdBGW8ZjgEQBpSBCq3chC9Xt64SQf0T8B18hDfIq3xI4Lw==;EndpointSuffix=core.windows.net");
- var client = storageAccount.CreateCloudBlobClient();
- CloudBlobContainer container = null;
- try {
- container = client.GetContainerReference(farm);
- }
- catch
- {
- return req.CreateErrorResponse(HttpStatusCode.BadRequest, "The requested farm container doesn't exist");
- }
- CloudBlockBlob defaultsBlob = container.GetBlockBlobReference(string.Format("m.defaults_{0}.json", tenant));
- CloudBlockBlob mergedFileBlob = null;
- //Merge requested component with the defaults.
- if (!string.IsNullOrEmpty(componentName)) {
- var componentConfiguration = string.Format("m.overrides_{0}_{1}.json", tenant, component);
- CloudBlockBlob overridesBlob = container.GetBlockBlobReference(componentConfiguration);
- mergedFileBlob = container.GetBlockBlobReference(string.Format("merged{0}{1}{2}.json", farm, tenant, component));
- string contentDefaults;
- using (var memoryStream = new MemoryStream())
- {
- try
- {
- defaultsBlob.DownloadToStream(memoryStream);
- }
- catch
- {
- return req.CreateErrorResponse(HttpStatusCode.BadRequest, "The requested defaults configuration doesn't exist for this farm");
- }
- contentDefaults = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
- }
- string contentOverrides;
- using (var memoryStream = new MemoryStream())
- {
- try
- {
- overridesBlob.DownloadToStream(memoryStream);
- }
- catch
- {
- return req.CreateErrorResponse(HttpStatusCode.BadRequest, "The requested defaults configuration doesn't exist for this farm");
- }
- contentOverrides = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
- }
- JObject defaults = null;
- JObject overrides = null;
- try
- {
- defaults = JObject.Parse(contentDefaults);
- overrides = JObject.Parse(contentOverrides);
- }
- catch
- {
- return req.CreateErrorResponse(HttpStatusCode.BadRequest, "The requested farm configuration couldn't be fetched. Check if it exists");
- }
- var mergeSettings = new JsonMergeSettings
- {
- MergeArrayHandling = MergeArrayHandling.Union,
- MergeNullValueHandling = MergeNullValueHandling.Merge
- };
- defaults.Merge(overrides, mergeSettings);
- mergedFileBlob.UploadText(defaults.ToString(Formatting.None));
- }
- var contraints = new SharedAccessBlobPolicy();
- contraints.SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5);
- contraints.SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10);
- contraints.Permissions = SharedAccessBlobPermissions.Read;
- var sasBlobToken = "";
- Stream stream = null;
- if (string.IsNullOrEmpty(componentName)) {
- sasBlobToken = defaultsBlob.GetSharedAccessSignature(contraints);
- stream = defaultsBlob.OpenRead();
- } else {
- sasBlobToken = mergedFileBlob.GetSharedAccessSignature(contraints);
- stream = mergedFileBlob.OpenRead();
- }
- HttpResponseMessage response;
- response = req.CreateResponse(HttpStatusCode.OK);
- response.Content = new StreamContent(stream);
- response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
- return response;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement