PageRenderTime 62ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ResourceManager/Resources/Commands.Resources/Models.ResourceGroups/ResourcesExtensions.cs

https://gitlab.com/jslee1/azure-powershell
C# | 268 lines | 215 code | 40 blank | 13 comment | 32 complexity | 6af0a3f705f8f60a3a167e697cb3c5b3 MD5 | raw file
  1. // ----------------------------------------------------------------------------------
  2. //
  3. // Copyright Microsoft Corporation
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // ----------------------------------------------------------------------------------
  14. using System;
  15. using System.Collections;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Reflection;
  19. using System.Text;
  20. using Microsoft.Azure.Commands.Common.Authentication;
  21. using Microsoft.Azure.Commands.Resources.Models.Authorization;
  22. using Microsoft.Azure.Commands.Tags.Model;
  23. using Microsoft.Azure.Gallery;
  24. using Microsoft.Azure.Management.Authorization.Models;
  25. using Microsoft.Azure.Management.Resources.Models;
  26. using Microsoft.WindowsAzure.Commands.Utilities.Common;
  27. using Newtonsoft.Json;
  28. namespace Microsoft.Azure.Commands.Resources.Models
  29. {
  30. public static class ResourcesExtensions
  31. {
  32. public static PSGalleryItem ToPSGalleryItem(this GalleryItem gallery)
  33. {
  34. PSGalleryItem psGalleryItem = new PSGalleryItem();
  35. foreach (PropertyInfo prop in gallery.GetType().GetProperties())
  36. {
  37. (typeof(PSGalleryItem)).GetProperty(prop.Name).SetValue(psGalleryItem, prop.GetValue(gallery, null), null);
  38. }
  39. return psGalleryItem;
  40. }
  41. public static PSResource ToPSResource(this GenericResourceExtended resource, ResourcesClient client, bool minimal)
  42. {
  43. ResourceIdentifier identifier = new ResourceIdentifier(resource.Id);
  44. return new PSResource
  45. {
  46. Name = identifier.ResourceName,
  47. Location = resource.Location,
  48. ResourceType = identifier.ResourceType,
  49. ResourceGroupName = identifier.ResourceGroupName,
  50. ParentResource = identifier.ParentResource,
  51. Properties = JsonUtilities.DeserializeJson(resource.Properties),
  52. PropertiesText = resource.Properties,
  53. Tags = TagsConversionHelper.CreateTagHashtable(resource.Tags),
  54. Permissions = minimal ? null : client.GetResourcePermissions(identifier),
  55. ResourceId = identifier.ToString()
  56. };
  57. }
  58. public static PSPermission ToPSPermission(this Permission permission)
  59. {
  60. return new PSPermission()
  61. {
  62. Actions = new List<string>(permission.Actions),
  63. NotActions = new List<string>(permission.NotActions)
  64. };
  65. }
  66. private static string ConstructTemplateLinkView(TemplateLink templateLink)
  67. {
  68. if (templateLink == null)
  69. {
  70. return string.Empty;
  71. }
  72. StringBuilder result = new StringBuilder();
  73. result.AppendLine();
  74. result.AppendLine(string.Format("{0, -15}: {1}", "Uri", templateLink.Uri));
  75. result.AppendLine(string.Format("{0, -15}: {1}", "ContentVersion", templateLink.ContentVersion));
  76. return result.ToString();
  77. }
  78. private static string GetEventDataCaller(Dictionary<string, string> claims)
  79. {
  80. string name = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name";
  81. if (claims == null || !claims.ContainsKey(name))
  82. {
  83. return null;
  84. }
  85. else
  86. {
  87. return claims[name];
  88. }
  89. }
  90. public static string ConstructDeploymentVariableTable(Dictionary<string, DeploymentVariable> dictionary)
  91. {
  92. if (dictionary == null)
  93. {
  94. return null;
  95. }
  96. StringBuilder result = new StringBuilder();
  97. if (dictionary.Count > 0)
  98. {
  99. string rowFormat = "{0, -15} {1, -25} {2, -10}\r\n";
  100. result.AppendLine();
  101. result.AppendFormat(rowFormat, "Name", "Type", "Value");
  102. result.AppendFormat(rowFormat, GeneralUtilities.GenerateSeparator(15, "="), GeneralUtilities.GenerateSeparator(25, "="), GeneralUtilities.GenerateSeparator(10, "="));
  103. foreach (KeyValuePair<string, DeploymentVariable> pair in dictionary)
  104. {
  105. result.AppendFormat(rowFormat, pair.Key, pair.Value.Type, pair.Value.Value);
  106. }
  107. }
  108. return result.ToString();
  109. }
  110. public static string ConstructTagsTable(Hashtable[] tags)
  111. {
  112. if (tags == null)
  113. {
  114. return null;
  115. }
  116. Hashtable emptyHashtable = new Hashtable
  117. {
  118. {"Name", string.Empty},
  119. {"Value", string.Empty}
  120. };
  121. StringBuilder resourcesTable = new StringBuilder();
  122. if (tags.Length > 0)
  123. {
  124. int maxNameLength = Math.Max("Name".Length, tags.Where(ht => ht.ContainsKey("Name")).DefaultIfEmpty(emptyHashtable).Max(ht => ht["Name"].ToString().Length));
  125. int maxValueLength = Math.Max("Value".Length, tags.Where(ht => ht.ContainsKey("Value")).DefaultIfEmpty(emptyHashtable).Max(ht => ht["Value"].ToString().Length));
  126. string rowFormat = "{0, -" + maxNameLength + "} {1, -" + maxValueLength + "}\r\n";
  127. resourcesTable.AppendLine();
  128. resourcesTable.AppendFormat(rowFormat, "Name", "Value");
  129. resourcesTable.AppendFormat(rowFormat,
  130. GeneralUtilities.GenerateSeparator(maxNameLength, "="),
  131. GeneralUtilities.GenerateSeparator(maxValueLength, "="));
  132. foreach (Hashtable tag in tags)
  133. {
  134. PSTagValuePair tagValuePair = TagsConversionHelper.Create(tag);
  135. if (tagValuePair != null)
  136. {
  137. if (tagValuePair.Name.StartsWith(TagsClient.ExecludedTagPrefix))
  138. {
  139. continue;
  140. }
  141. if (tagValuePair.Value == null)
  142. {
  143. tagValuePair.Value = string.Empty;
  144. }
  145. resourcesTable.AppendFormat(rowFormat, tagValuePair.Name, tagValuePair.Value);
  146. }
  147. }
  148. }
  149. return resourcesTable.ToString();
  150. }
  151. public static string ConstructPermissionsTable(List<PSPermission> permissions)
  152. {
  153. StringBuilder permissionsTable = new StringBuilder();
  154. if (permissions != null && permissions.Count > 0)
  155. {
  156. int maxActionsLength = Math.Max("Actions".Length, permissions.Where(p => p.Actions != null).DefaultIfEmpty(EmptyPermission).Max(p => p.ActionsString.Length));
  157. int maxNotActionsLength = Math.Max("NotActions".Length, permissions.Where(p => p.NotActions != null).DefaultIfEmpty(EmptyPermission).Max(p => p.NotActionsString.Length));
  158. string rowFormat = "{0, -" + maxActionsLength + "} {1, -" + maxNotActionsLength + "}\r\n";
  159. permissionsTable.AppendLine();
  160. permissionsTable.AppendFormat(rowFormat, "Actions", "NotActions");
  161. permissionsTable.AppendFormat(rowFormat,
  162. GeneralUtilities.GenerateSeparator(maxActionsLength, "="),
  163. GeneralUtilities.GenerateSeparator(maxNotActionsLength, "="));
  164. foreach (PSPermission permission in permissions)
  165. {
  166. permissionsTable.AppendFormat(rowFormat, permission.ActionsString, permission.NotActionsString);
  167. }
  168. }
  169. return permissionsTable.ToString();
  170. }
  171. private static PSPermission EmptyPermission
  172. {
  173. get
  174. {
  175. return new PSPermission()
  176. {
  177. Actions = new List<string>(),
  178. NotActions = new List<string>()
  179. };
  180. }
  181. }
  182. public static PSResourceGroupDeployment ToPSResourceGroupDeployment(this DeploymentExtended result, string resourceGroup)
  183. {
  184. PSResourceGroupDeployment deployment = new PSResourceGroupDeployment();
  185. if (result != null)
  186. {
  187. deployment = CreatePSResourceGroupDeployment(result.Name, resourceGroup, result.Properties);
  188. }
  189. return deployment;
  190. }
  191. private static PSResourceGroupDeployment CreatePSResourceGroupDeployment(
  192. string name,
  193. string gesourceGroup,
  194. DeploymentPropertiesExtended properties)
  195. {
  196. PSResourceGroupDeployment deploymentObject = new PSResourceGroupDeployment();
  197. deploymentObject.DeploymentName = name;
  198. deploymentObject.ResourceGroupName = gesourceGroup;
  199. if (properties != null)
  200. {
  201. deploymentObject.Mode = properties.Mode;
  202. deploymentObject.ProvisioningState = properties.ProvisioningState;
  203. deploymentObject.TemplateLink = properties.TemplateLink;
  204. deploymentObject.Timestamp = properties.Timestamp;
  205. deploymentObject.CorrelationId = properties.CorrelationId;
  206. if (properties.DebugSettingResponse != null && !string.IsNullOrEmpty(properties.DebugSettingResponse.DeploymentDebugDetailLevel))
  207. {
  208. deploymentObject.DeploymentDebugLogLevel = properties.DebugSettingResponse.DeploymentDebugDetailLevel;
  209. }
  210. if (!string.IsNullOrEmpty(properties.Outputs))
  211. {
  212. Dictionary<string, DeploymentVariable> outputs = JsonConvert.DeserializeObject<Dictionary<string, DeploymentVariable>>(properties.Outputs);
  213. deploymentObject.Outputs = outputs;
  214. }
  215. if (!string.IsNullOrEmpty(properties.Parameters))
  216. {
  217. Dictionary<string, DeploymentVariable> parameters = JsonConvert.DeserializeObject<Dictionary<string, DeploymentVariable>>(properties.Parameters);
  218. deploymentObject.Parameters = parameters;
  219. }
  220. if (properties.TemplateLink != null)
  221. {
  222. deploymentObject.TemplateLinkString = ConstructTemplateLinkView(properties.TemplateLink);
  223. }
  224. }
  225. return deploymentObject;
  226. }
  227. }
  228. }