|
| 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 Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; |
| 16 | +using Microsoft.Azure.Commands.Synapse.Common; |
| 17 | +using Microsoft.Azure.Commands.Synapse.Models; |
| 18 | +using Microsoft.WindowsAzure.Commands.Utilities.Common; |
| 19 | +using Newtonsoft.Json; |
| 20 | +using System.Collections.Generic; |
| 21 | +using System.IO; |
| 22 | +using System.Linq; |
| 23 | +using System.Management.Automation; |
| 24 | + |
| 25 | +namespace Microsoft.Azure.Commands.Synapse |
| 26 | +{ |
| 27 | + [Cmdlet(VerbsData.Export, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + SynapseConstants.SynapsePrefix + SynapseConstants.SparkConfiguration, |
| 28 | + DefaultParameterSetName = ExportByName)] |
| 29 | + [OutputType(typeof(FileInfo))] |
| 30 | + public class ExportAzureSynapseSparkConfiguration : SynapseArtifactsCmdletBase |
| 31 | + { |
| 32 | + private const string ExportByName = "ExportByName"; |
| 33 | + private const string ExportByObject = "ExportByObject"; |
| 34 | + private const string ExportByInputObject = "ExportByInputObject"; |
| 35 | + |
| 36 | + [Parameter(ValueFromPipelineByPropertyName = false, ParameterSetName = ExportByName, |
| 37 | + Mandatory = true, HelpMessage = HelpMessages.WorkspaceName)] |
| 38 | + [ResourceNameCompleter(ResourceTypes.Workspace, "ResourceGroupName")] |
| 39 | + [ValidateNotNullOrEmpty] |
| 40 | + public override string WorkspaceName { get; set; } |
| 41 | + |
| 42 | + [Parameter(ValueFromPipeline = true, ParameterSetName = ExportByObject, |
| 43 | + Mandatory = true, HelpMessage = HelpMessages.WorkspaceObject)] |
| 44 | + [ValidateNotNull] |
| 45 | + public PSSynapseWorkspace WorkspaceObject { get; set; } |
| 46 | + |
| 47 | + [Parameter(ValueFromPipelineByPropertyName = false, Mandatory = false, ParameterSetName = ExportByName, |
| 48 | + HelpMessage = HelpMessages.SparkConfigurationName)] |
| 49 | + [Parameter(ValueFromPipelineByPropertyName = false, Mandatory = false, ParameterSetName = ExportByObject, |
| 50 | + HelpMessage = HelpMessages.SparkConfigurationName)] |
| 51 | + [ValidateNotNullOrEmpty] |
| 52 | + [Alias("SparkConfigurationName")] |
| 53 | + public string Name { get; set; } |
| 54 | + |
| 55 | + [Parameter(ValueFromPipeline = true, ParameterSetName = ExportByInputObject, |
| 56 | + Mandatory = true, HelpMessage = HelpMessages.SparkConfigurationObject)] |
| 57 | + [ValidateNotNull] |
| 58 | + public PSSparkConfigurationResource InputObject { get; set; } |
| 59 | + |
| 60 | + [Parameter(ValueFromPipelineByPropertyName = false, Mandatory = true, HelpMessage = HelpMessages.OutputFolder)] |
| 61 | + [ValidateNotNullOrEmpty] |
| 62 | + public string OutputFolder { get; set; } |
| 63 | + |
| 64 | + [Parameter(Mandatory = false, HelpMessage = HelpMessages.AsJob)] |
| 65 | + public SwitchParameter AsJob { get; set; } |
| 66 | + |
| 67 | + public override void ExecuteCmdlet() |
| 68 | + { |
| 69 | + if (this.IsParameterBound(c => c.WorkspaceObject)) |
| 70 | + { |
| 71 | + this.WorkspaceName = this.WorkspaceObject.Name; |
| 72 | + } |
| 73 | + |
| 74 | + var fileExtension = ".json"; |
| 75 | + if (this.IsParameterBound(c => c.InputObject)) |
| 76 | + { |
| 77 | + WriteToFile(this.InputObject); |
| 78 | + WriteObject(new FileInfo(Path.Combine(this.OutputFolder, this.InputObject.Name + fileExtension))); |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + if (this.IsParameterBound(c => c.Name)) |
| 83 | + { |
| 84 | + var sparkConfiguration = new PSSparkConfigurationResource(SynapseAnalyticsClient.GetSparkConfiguration(this.Name), this.WorkspaceName); |
| 85 | + WriteToFile(sparkConfiguration); |
| 86 | + WriteObject(new FileInfo(Path.Combine(this.OutputFolder, sparkConfiguration.Name + fileExtension))); |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + var infoList = new List<FileInfo>(); |
| 91 | + var sparkConfigurations = SynapseAnalyticsClient.GetSparkConfigurationByWorkspace() |
| 92 | + .Select(element => new PSSparkConfigurationResource(element, this.WorkspaceName)); |
| 93 | + foreach (var sparkConfiguration in sparkConfigurations) |
| 94 | + { |
| 95 | + WriteToFile(sparkConfiguration); |
| 96 | + infoList.Add(new FileInfo(Path.Combine(this.OutputFolder, sparkConfiguration.Name + fileExtension))); |
| 97 | + } |
| 98 | + WriteObject(infoList, true); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private void WriteToFile(PSSparkConfigurationResource sparkConfigurationResource) |
| 104 | + { |
| 105 | + string json = Newtonsoft.Json.JsonConvert.SerializeObject(sparkConfigurationResource.Properties, Formatting.Indented); |
| 106 | + File.WriteAllText(Path.Combine(this.OutputFolder, sparkConfigurationResource.Name + ".json"), json); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments