|
12 | 12 | // limitations under the License.
|
13 | 13 | // ----------------------------------------------------------------------------------
|
14 | 14 |
|
| 15 | +using Microsoft.Azure.Management.Internal.Resources.Utilities.Models; |
15 | 16 | using Microsoft.Azure.Management.Network;
|
| 17 | +using System; |
| 18 | +using System.Linq; |
| 19 | +using System.Management.Automation; |
16 | 20 | using System.Net;
|
17 | 21 | using MNM = Microsoft.Azure.Management.Network.Models;
|
18 | 22 |
|
@@ -42,5 +46,80 @@ public bool IsFlowLogPresent(string resourceGroupName, string name, string flowL
|
42 | 46 |
|
43 | 47 | return true;
|
44 | 48 | }
|
| 49 | + |
| 50 | + public bool IsValidResourceId(ResourceIdentifier id, string expectedResourceType, bool validateParent = false, string expectedParentType = null) |
| 51 | + { |
| 52 | + if (id == null || string.IsNullOrEmpty(id.ResourceName) || string.IsNullOrEmpty(id.ResourceGroupName) || string.IsNullOrEmpty(id.Subscription) |
| 53 | + || !string.Equals(id.ResourceType, expectedResourceType)) |
| 54 | + { |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + if (validateParent) |
| 59 | + { |
| 60 | + if (string.IsNullOrEmpty(id.ParentResource)) |
| 61 | + { |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + string[] tokens = id.ParentResource.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); |
| 66 | + if (tokens.Count() != 2 || (!string.IsNullOrEmpty(expectedParentType) && !string.Equals(tokens[0], expectedParentType))) |
| 67 | + { |
| 68 | + return false; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
| 75 | + public void ValidateFlowLogParameters(string targetResourceId, string storageId, int? formatVersion, string formatType, |
| 76 | + bool enableTrafficAnalytics, string trafficAnalyticsWorkspaceId, int? trafficAnalyticsInterval, int? retentionPolicyDays) |
| 77 | + { |
| 78 | + ResourceIdentifier targetResourceInfo = new ResourceIdentifier(targetResourceId); |
| 79 | + if (!this.IsValidResourceId(targetResourceInfo, "Microsoft.Network/networkSecurityGroups")) |
| 80 | + { |
| 81 | + throw new PSArgumentException(Properties.Resources.InvalidTargetResourceId); |
| 82 | + } |
| 83 | + |
| 84 | + ResourceIdentifier storageAccountInfo = new ResourceIdentifier(storageId); |
| 85 | + if (!this.IsValidResourceId(storageAccountInfo, "Microsoft.Storage/storageAccounts")) |
| 86 | + { |
| 87 | + throw new PSArgumentException(Properties.Resources.InvalidStorageId); |
| 88 | + } |
| 89 | + |
| 90 | + if (formatVersion != null && (formatVersion < 0 || formatVersion > 2)) |
| 91 | + { |
| 92 | + throw new PSArgumentException(Properties.Resources.InvalidFlowLogFormatVersion); |
| 93 | + } |
| 94 | + |
| 95 | + if (!string.IsNullOrEmpty(formatType) && !string.Equals(formatType, "JSON", StringComparison.OrdinalIgnoreCase)) |
| 96 | + { |
| 97 | + throw new PSArgumentException(Properties.Resources.InvalidFlowLogFormatVersion); |
| 98 | + } |
| 99 | + |
| 100 | + if (enableTrafficAnalytics && string.IsNullOrEmpty(trafficAnalyticsWorkspaceId)) |
| 101 | + { |
| 102 | + throw new PSArgumentException(Properties.Resources.TrafficAnalyticsWorkspaceResourceIdIsMissing); |
| 103 | + } |
| 104 | + |
| 105 | + if (trafficAnalyticsInterval != null && trafficAnalyticsInterval != 10 && trafficAnalyticsInterval != 60) |
| 106 | + { |
| 107 | + throw new PSArgumentException(Properties.Resources.InvalidTrafficAnalyticsInterval); |
| 108 | + } |
| 109 | + |
| 110 | + if (!string.IsNullOrEmpty(trafficAnalyticsWorkspaceId)) |
| 111 | + { |
| 112 | + ResourceIdentifier workspaceInfo = new ResourceIdentifier(trafficAnalyticsWorkspaceId); |
| 113 | + if (!this.IsValidResourceId(workspaceInfo, "Microsoft.OperationalInsights/workspaces")) |
| 114 | + { |
| 115 | + throw new PSArgumentException(Properties.Resources.InvalidWorkspaceResourceId); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if (retentionPolicyDays != null && retentionPolicyDays < 0) |
| 120 | + { |
| 121 | + throw new PSArgumentException(Properties.Resources.InvalidTrafficAnalyticsInterval); |
| 122 | + } |
| 123 | + } |
45 | 124 | }
|
46 | 125 | }
|
0 commit comments