Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. using System.Linq;
  2. using System.Net;
  3. using System.Net.Http;
  4. using Microsoft.Azure.WebJobs;
  5. using Microsoft.Azure.WebJobs.Extensions.Http;
  6. using Microsoft.Azure.WebJobs.Host;
  7.  
  8. using Microsoft.Azure; // Namespace for CloudConfigurationManager
  9. using Microsoft.WindowsAzure.Storage; // Namespace for CloudStorageAccount
  10. using Microsoft.WindowsAzure.Storage.Blob; // Namespace for Blob storage types
  11. using System.IO;
  12. using Newtonsoft.Json.Linq;
  13. using Newtonsoft;
  14. using Newtonsoft.Json;
  15. using System;
  16. using System.Net.Http.Headers;
  17.  
  18. namespace RemoteConfiguration
  19. {
  20. public static class GetRemoteConfiguration
  21. {
  22. [FunctionName("GetRemoteConfiguration")]
  23.  
  24. 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)
  25. {
  26. log.Info("C# HTTP trigger function processed a request.");
  27.  
  28. if (string.IsNullOrEmpty(farm) || string.IsNullOrEmpty(tenant))
  29. {
  30. return req.CreateErrorResponse(HttpStatusCode.BadRequest, "you failed to provide a 'farm' and/or 'tenant' parameters");
  31. }
  32.  
  33. var componentName = component?.ToString();
  34.  
  35. if (componentName == "defaults") componentName = "";
  36.  
  37. // Parse the connection string and return a reference to the storage account.
  38. CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=mtstcc1storage;AccountKey=NWOlKhOzFphjqUwxzMcLtKzv7YfJXHR7eTNsvk6IHdBGW8ZjgEQBpSBCq3chC9Xt64SQf0T8B18hDfIq3xI4Lw==;EndpointSuffix=core.windows.net");
  39.  
  40. var client = storageAccount.CreateCloudBlobClient();
  41.  
  42. CloudBlobContainer container = null;
  43. try {
  44. container = client.GetContainerReference(farm);
  45. }
  46. catch
  47. {
  48. return req.CreateErrorResponse(HttpStatusCode.BadRequest, "The requested farm container doesn't exist");
  49. }
  50.  
  51. CloudBlockBlob defaultsBlob = container.GetBlockBlobReference(string.Format("m.defaults_{0}.json", tenant));
  52. CloudBlockBlob mergedFileBlob = null;
  53.  
  54. //Merge requested component with the defaults.
  55. if (!string.IsNullOrEmpty(componentName)) {
  56. var componentConfiguration = string.Format("m.overrides_{0}_{1}.json", tenant, component);
  57.  
  58. CloudBlockBlob overridesBlob = container.GetBlockBlobReference(componentConfiguration);
  59.  
  60. mergedFileBlob = container.GetBlockBlobReference(string.Format("merged{0}{1}{2}.json", farm, tenant, component));
  61.  
  62. string contentDefaults;
  63. using (var memoryStream = new MemoryStream())
  64. {
  65. try
  66. {
  67. defaultsBlob.DownloadToStream(memoryStream);
  68. }
  69. catch
  70. {
  71. return req.CreateErrorResponse(HttpStatusCode.BadRequest, "The requested defaults configuration doesn't exist for this farm");
  72. }
  73.  
  74. contentDefaults = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
  75. }
  76.  
  77. string contentOverrides;
  78. using (var memoryStream = new MemoryStream())
  79. {
  80. try
  81. {
  82. overridesBlob.DownloadToStream(memoryStream);
  83. }
  84. catch
  85. {
  86. return req.CreateErrorResponse(HttpStatusCode.BadRequest, "The requested defaults configuration doesn't exist for this farm");
  87. }
  88.  
  89. contentOverrides = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
  90. }
  91.  
  92. JObject defaults = null;
  93. JObject overrides = null;
  94.  
  95. try
  96. {
  97. defaults = JObject.Parse(contentDefaults);
  98. overrides = JObject.Parse(contentOverrides);
  99. }
  100. catch
  101. {
  102. return req.CreateErrorResponse(HttpStatusCode.BadRequest, "The requested farm configuration couldn't be fetched. Check if it exists");
  103. }
  104.  
  105. var mergeSettings = new JsonMergeSettings
  106. {
  107. MergeArrayHandling = MergeArrayHandling.Union,
  108. MergeNullValueHandling = MergeNullValueHandling.Merge
  109. };
  110.  
  111. defaults.Merge(overrides, mergeSettings);
  112.  
  113. mergedFileBlob.UploadText(defaults.ToString(Formatting.None));
  114. }
  115.  
  116. var contraints = new SharedAccessBlobPolicy();
  117. contraints.SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5);
  118. contraints.SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10);
  119. contraints.Permissions = SharedAccessBlobPermissions.Read;
  120.  
  121. var sasBlobToken = "";
  122. Stream stream = null;
  123. if (string.IsNullOrEmpty(componentName)) {
  124. sasBlobToken = defaultsBlob.GetSharedAccessSignature(contraints);
  125. stream = defaultsBlob.OpenRead();
  126. } else {
  127. sasBlobToken = mergedFileBlob.GetSharedAccessSignature(contraints);
  128. stream = mergedFileBlob.OpenRead();
  129. }
  130.  
  131. HttpResponseMessage response;
  132. response = req.CreateResponse(HttpStatusCode.OK);
  133. response.Content = new StreamContent(stream);
  134. response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
  135.  
  136. return response;
  137. }
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement