|
| 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 | + |
| 15 | +using System; |
| 16 | +using System.Collections.Generic; |
| 17 | +using System.Management.Automation; |
| 18 | +using Microsoft.Azure.Commands.Insights.OutputClasses; |
| 19 | +using Microsoft.Azure.Management.Monitor; |
| 20 | +using Microsoft.Azure.Management.Monitor.Models; |
| 21 | +using Microsoft.WindowsAzure.Commands.Utilities.Common; |
| 22 | + |
| 23 | +namespace Microsoft.Azure.Commands.Insights.Diagnostics |
| 24 | +{ |
| 25 | + [Cmdlet("New", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "DiagnosticSetting"), OutputType(typeof(PSServiceDiagnosticSettings))] |
| 26 | + public class NewAzureRmDiagnosticSettingCommand : ManagementCmdletBase |
| 27 | + { |
| 28 | + public const string StorageAccountIdParamName = "StorageAccountId"; |
| 29 | + public const string ServiceBusRuleIdParamName = "ServiceBusRuleId"; |
| 30 | + public const string EventHubNameParamName = "EventHubName"; |
| 31 | + public const string EventHubRuleIdParamName = "EventHubAuthorizationRuleId"; |
| 32 | + public const string WorkspacetIdParamName = "WorkspaceId"; |
| 33 | + public const string EnabledParamName = "Enabled"; |
| 34 | + public const string EnableLogParamName = "EnableLog"; |
| 35 | + public const string EnableMetricsParamName = "EnableMetrics"; |
| 36 | + |
| 37 | + #region Parameters declarations |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Gets or sets the resourceId parameter of the cmdlet |
| 41 | + /// </summary> |
| 42 | + [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource id")] |
| 43 | + [ValidateNotNullOrEmpty] |
| 44 | + public string TargetResourceId { get; set; } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Gets or sets the resourceId parameter of the cmdlet |
| 48 | + /// </summary> |
| 49 | + [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The name of the diagnostic setting. Defaults to 'service'")] |
| 50 | + [ValidateNotNullOrEmpty] |
| 51 | + public string Name { get; set; } = "service"; |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Gets or sets the storage account parameter of the cmdlet |
| 55 | + /// </summary> |
| 56 | + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The storage account id")] |
| 57 | + public string StorageAccountId { get; set; } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Gets or sets the service bus rule id parameter of the cmdlet |
| 61 | + /// </summary> |
| 62 | + [Parameter( Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The service bus rule id")] |
| 63 | + public string ServiceBusRuleId { get; set; } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Gets or sets the service bus rule id parameter of the cmdlet |
| 67 | + /// </summary> |
| 68 | + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The service bus rule id")] |
| 69 | + public string EventHubName { get; set; } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Gets or sets the event hub authorization rule id parameter of the cmdlet |
| 73 | + /// </summary> |
| 74 | + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The event hub rule id")] |
| 75 | + public string EventHubAuthorizationRuleId { get; set; } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Gets or sets the OMS workspace Id |
| 79 | + /// </summary> |
| 80 | + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource Id of the Log Analytics workspace to send logs/metrics to")] |
| 81 | + public string WorkspaceId { get; set; } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Gets or sets the retention in days |
| 85 | + /// </summary> |
| 86 | + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The value indicating whether to export (to ODS) to resource-specific (if present) or to AzureDiagnostics (default, not present)")] |
| 87 | + public SwitchParameter DedicatedLogAnalyticsDestinationType { get; set; } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Set list of diagnostic settings |
| 91 | + /// </summary> |
| 92 | + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Metric settings or Log settings")] |
| 93 | + public PSDiagnosticDetailSettings[] Setting { get; set; } |
| 94 | + |
| 95 | + #endregion |
| 96 | + |
| 97 | + protected override void ProcessRecordInternal() |
| 98 | + { |
| 99 | + Validate(); |
| 100 | + |
| 101 | + IList<MetricSettings> metrics = new List<MetricSettings>(); |
| 102 | + IList<LogSettings> logs = new List<LogSettings>(); |
| 103 | + |
| 104 | + if (this.IsParameterBound(c => c.Setting) && Setting.Length != 0) |
| 105 | + { |
| 106 | + foreach (PSDiagnosticDetailSettings setting in Setting) |
| 107 | + { |
| 108 | + switch (setting.CategoryType) |
| 109 | + { |
| 110 | + case PSDiagnosticSettingCategoryType.Metrics: |
| 111 | + metrics.Add(((PSMetricSettings)setting).GetMetricSetting()); |
| 112 | + break; |
| 113 | + case PSDiagnosticSettingCategoryType.Logs: |
| 114 | + logs.Add(((PSLogSettings)setting).GetLogSetting()); |
| 115 | + break; |
| 116 | + default: |
| 117 | + throw new ArgumentException("Invalid diagnostic setting type"); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + PSServiceDiagnosticSettings DiagnosticSetting = new PSServiceDiagnosticSettings(id: this.TargetResourceId + "/diagnosticSettings/" + this.Name, name: this.Name) |
| 123 | + { |
| 124 | + StorageAccountId = this.IsParameterBound(c => c.StorageAccountId) ? this.StorageAccountId : null, |
| 125 | + ServiceBusRuleId = this.IsParameterBound(c => c.ServiceBusRuleId) ? this.ServiceBusRuleId : null, |
| 126 | + EventHubName = this.IsParameterBound(c => c.EventHubName) ? this.EventHubName : null, |
| 127 | + EventHubAuthorizationRuleId = this.IsParameterBound(c => c.EventHubAuthorizationRuleId) ? this.EventHubAuthorizationRuleId : null, |
| 128 | + WorkspaceId = this.IsParameterBound(c => c.WorkspaceId) ? this.WorkspaceId : null, |
| 129 | + LogAnalyticsDestinationType = this.IsParameterBound(c => c.DedicatedLogAnalyticsDestinationType) ? "Dedicated" : null, |
| 130 | + Metrics = metrics, |
| 131 | + Logs = logs |
| 132 | + }; |
| 133 | + |
| 134 | + WriteObject(DiagnosticSetting); |
| 135 | + } |
| 136 | + |
| 137 | + protected void Validate() |
| 138 | + { |
| 139 | + if (!this.IsParameterBound(c => c.StorageAccountId) && |
| 140 | + !this.IsParameterBound(c => c.ServiceBusRuleId) && |
| 141 | + !this.IsParameterBound(c => c.EventHubName) && |
| 142 | + !this.IsParameterBound(c => c.EventHubAuthorizationRuleId) && |
| 143 | + !this.IsParameterBound(c => c.WorkspaceId)) |
| 144 | + { |
| 145 | + throw new ArgumentException("No operation is specified, please specify storage account Id/service bus rule Id/eventhub name/eventhub authorization rule Id/workspace Id"); |
| 146 | + } |
| 147 | + |
| 148 | + if (!this.IsParameterBound(c => c.WorkspaceId) && this.IsParameterBound(c => c.DedicatedLogAnalyticsDestinationType)) |
| 149 | + { |
| 150 | + throw new ArgumentException("Please provide workspace Id if want to use dedicated log analytics destination type"); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments