<commit_before>﻿// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Reflection;
using System.Security;
using System.Threading;
using System.Threading.Tasks;
using ServiceFabricProperties = Microsoft.Azure.Commands.ServiceFabric.Properties;
using Newtonsoft.Json.Linq;
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.ServiceFabric.Common;
using Microsoft.Azure.Commands.ServiceFabric.Models;
using Microsoft.Azure.Management.Compute.Models;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.ResourceManager.Models;
using Microsoft.Azure.Management.ServiceFabric;
using Microsoft.WindowsAzure.Commands.Common;
using Newtonsoft.Json;
using OperatingSystem = Microsoft.Azure.Commands.ServiceFabric.Models.OperatingSystem;
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;

namespace Microsoft.Azure.Commands.ServiceFabric.Commands
{
    [Cmdlet(VerbsCommon.New, CmdletNoun.AzureRmServiceFabricCluster, SupportsShouldProcess = true, DefaultParameterSetName = ByDefaultArmTemplate),
     OutputType(typeof(PSDeploymentResult))]
    public class NewAzureRmServiceFabricCluster : ServiceFabricClusterCertificateCmdlet
    {
        public const string WindowsTemplateRelativePath = @"Template\Windows";
        public const string LinuxTemplateRelativePath = @"Template\Linux";
        public const string ParameterFileName = @"parameter.json";
        public const string TemplateFileName = @"template.json";
        
        public readonly Dictionary<OperatingSystem, string> OsToVmSkuString = new Dictionary<OperatingSystem, string>()
        {
            {OperatingSystem.WindowsServer2012R2Datacenter, "2012-R2-Datacenter"},
            {OperatingSystem.UbuntuServer1604, "16.04"},
            {OperatingSystem.WindowsServer2016DatacenterwithContainers, "2016-Datacenter-with-Containers"},
            {OperatingSystem.WindowsServer2016Datacenter, "2016-Datacenter"}
        };

        private string resourceLocation;
        public override string KeyVaultResouceGroupLocation
        {
            get
            {
                return this.resourceLocation;
            }
        }

        public const string ErrorFormat = "Error: Code={0}; Message={1}\r\n";
        public const string DefaultPublicDnsFormat = "{0}.{1}.cloudapp.azure.com";

        private readonly string DefaultDurability = DurabilityLevel.Bronze.ToString();

        private string adminUserName = string.Empty; 
        private string reliabilityLevel = ReliabilityLevel.Bronze.ToString();
        private string domainNameLabel = string.Empty;

        private string reliabilityLevelParameter = string.Empty;
        private string durabilityLevelParameter = string.Empty;
        private string clusterNameParameter = string.Empty;
        private string vmInstanceParameter = string.Empty;
        private string adminPasswordParameter = string.Empty;
        private string adminUserParameter = string.Empty;
        private string locationParameter = string.Empty;
        private string skuParameter = string.Empty;
        private string vmImageSkuParameter = string.Empty;

        private string thumbprintParameter = string.Empty;
        private string keyVaultParameter = string.Empty;
        private string certificateUrlParameter = string.Empty;

        private string domainNameLabelParameter = string.Empty;

        /// <summary>
        /// Resource group name
        /// </summary>
        /// 
        [Parameter(Mandatory = true, Position = 0, ParameterSetName = ExistingKeyVault, ValueFromPipelineByPropertyName = true,
            HelpMessage = "Specify the name of the resource group.")]
        [Parameter(Mandatory = true, Position = 0, ParameterSetName = ByNewPfxAndVaultName, ValueFromPipelineByPropertyName = true,
            HelpMessage = "Specify the name of the resource group.")]
        [Parameter(Mandatory = true, Position = 0, ParameterSetName = ByExistingPfxAndVaultName, ValueFromPipelineByPropertyName = true,
            HelpMessage = "Specify the name of the resource group.")]
        [Parameter(Mandatory = true, Position = 0, ParameterSetName = ByDefaultArmTemplate, ValueFromPipelineByPropertyName = true,
            HelpMessage = "Specify the name of the resource group.")]
        [ResourceGroupCompleter]
        [ValidateNotNullOrEmpty()]
        public override string ResourceGroupName { get; set; }

        [Parameter(Mandatory = true, ParameterSetName = ByExistingPfxAndVaultName, ValueFromPipeline = true,
                   HelpMessage = "The path to the template file.")]
        [Parameter(Mandatory = true, ParameterSetName = ByNewPfxAndVaultName, ValueFromPipeline = true,
                   HelpMessage = "The path to the template file.")]
        [Parameter(Mandatory = true, ParameterSetName = ExistingKeyVault, ValueFromPipeline = true,
                   HelpMessage = "The path to the template file.")]
        [ValidateNotNullOrEmpty]
        public string TemplateFile { get; set; }

        [Parameter(Mandatory = true, ParameterSetName = ExistingKeyVault, ValueFromPipeline = true,
                   HelpMessage = "The path to the template parameter file.")]
        [Parameter(Mandatory = true, ParameterSetName = ByNewPfxAndVaultName, ValueFromPipeline = true,
                   HelpMessage = "The path to the template parameter file.")]
        [Parameter(Mandatory = true, ParameterSetName = ByExistingPfxAndVaultName, ValueFromPipeline = true,
                   HelpMessage = "The path to the template parameter file.")]
        [ValidateNotNullOrEmpty]
        public string ParameterFile { get; set; }

        [Parameter(Mandatory = true, ValueFromPipeline = true, ParameterSetName = ByExistingPfxAndVaultName,
                HelpMessage = "The existing certificate file path for the primary cluster certificate")]
        [ValidateNotNullOrEmpty]
        [Alias("Source")]
        public override string CertificateFile { get; set; }

        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByNewPfxAndVaultName,
           HelpMessage = "The folder of the new certificate file to be created")]
        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByDefaultArmTemplate,
           HelpMessage = "The folder of the new certificate file to be created")]
        [ValidateNotNullOrEmpty]
        [Alias("Destination")]
        public override string CertificateOutputFolder { get; set; }

        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByExistingPfxAndVaultName,
                  HelpMessage = "The password of the certificate file")]
        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByNewPfxAndVaultName,
                  HelpMessage = "The password of the certificate file")]
        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByDefaultArmTemplate,
                  HelpMessage = "The password of the certificate file")]
        [ValidateNotNullOrEmpty]
        [Alias("CertPassword")]
        public override SecureString CertificatePassword { get; set; }

        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByExistingPfxAndVaultName,
                HelpMessage = "The existing certificate file path for the secondary cluster certificate")]
        [ValidateNotNullOrEmpty]
        [Alias("SecSource")]
        public string SecondaryCertificateFile { get; set; }

        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByExistingPfxAndVaultName,
                 HelpMessage = "The password of the certificate file")]
        [ValidateNotNullOrEmpty]
        [Alias("SecCertPassword")]
        public virtual SecureString SecondaryCertificatePassword { get; set; }

        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByNewPfxAndVaultName,
              HelpMessage = "Azure key vault resource group name, it not given it will be defaulted to resource group name")]
        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByExistingPfxAndVaultName,
              HelpMessage = "Azure key vault resource group name, it not given it will be defaulted to resource group name")]
        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByDefaultArmTemplate,
              HelpMessage = "Azure key vault resource group name, it not given it will be defaulted to resource group name")]
        [ValidateNotNullOrEmpty]
        public override string KeyVaultResouceGroupName { get; set; }

        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByNewPfxAndVaultName,
                HelpMessage = "Azure key vault name, it not given it will be defaulted to the resource group name")]
        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByExistingPfxAndVaultName,
                HelpMessage = "Azure key vault name, it not given it will be defaulted to the resource group name")]
        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByDefaultArmTemplate,
                HelpMessage = "Azure key vault name, it not given it will be defaulted to the resource group name")]
        [ValidateNotNullOrEmpty]
        public override string KeyVaultName { get; set; }

        #region ByDefaultArmTemplate

        [Parameter(Mandatory = true, ParameterSetName = ByDefaultArmTemplate, ValueFromPipeline = true,
                   HelpMessage = "The resource group location")]
        public string Location { get; set; }

        [Parameter(Mandatory = false, ParameterSetName = ByDefaultArmTemplate, ValueFromPipeline = true,
                   HelpMessage = "Specify the name of the cluster, if not given it will be same as resource group name")]
        [ValidateNotNullOrEmpty()]
        [ValidatePattern("^[a-z][a-z0-9-]{4,23}[a-z0-9]$")]
        [Alias("ClusterName")]<commit_msg>Add LocationCompleter to ServiceFabric cmdlets<commit_after>﻿// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Reflection;
using System.Security;
using System.Threading;
using System.Threading.Tasks;
using ServiceFabricProperties = Microsoft.Azure.Commands.ServiceFabric.Properties;
using Newtonsoft.Json.Linq;
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.ServiceFabric.Common;
using Microsoft.Azure.Commands.ServiceFabric.Models;
using Microsoft.Azure.Management.Compute.Models;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.ResourceManager.Models;
using Microsoft.Azure.Management.ServiceFabric;
using Microsoft.WindowsAzure.Commands.Common;
using Newtonsoft.Json;
using OperatingSystem = Microsoft.Azure.Commands.ServiceFabric.Models.OperatingSystem;
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;

namespace Microsoft.Azure.Commands.ServiceFabric.Commands
{
    [Cmdlet(VerbsCommon.New, CmdletNoun.AzureRmServiceFabricCluster, SupportsShouldProcess = true, DefaultParameterSetName = ByDefaultArmTemplate),
     OutputType(typeof(PSDeploymentResult))]
    public class NewAzureRmServiceFabricCluster : ServiceFabricClusterCertificateCmdlet
    {
        public const string WindowsTemplateRelativePath = @"Template\Windows";
        public const string LinuxTemplateRelativePath = @"Template\Linux";
        public const string ParameterFileName = @"parameter.json";
        public const string TemplateFileName = @"template.json";
        
        public readonly Dictionary<OperatingSystem, string> OsToVmSkuString = new Dictionary<OperatingSystem, string>()
        {
            {OperatingSystem.WindowsServer2012R2Datacenter, "2012-R2-Datacenter"},
            {OperatingSystem.UbuntuServer1604, "16.04"},
            {OperatingSystem.WindowsServer2016DatacenterwithContainers, "2016-Datacenter-with-Containers"},
            {OperatingSystem.WindowsServer2016Datacenter, "2016-Datacenter"}
        };

        private string resourceLocation;
        public override string KeyVaultResouceGroupLocation
        {
            get
            {
                return this.resourceLocation;
            }
        }

        public const string ErrorFormat = "Error: Code={0}; Message={1}\r\n";
        public const string DefaultPublicDnsFormat = "{0}.{1}.cloudapp.azure.com";

        private readonly string DefaultDurability = DurabilityLevel.Bronze.ToString();

        private string adminUserName = string.Empty; 
        private string reliabilityLevel = ReliabilityLevel.Bronze.ToString();
        private string domainNameLabel = string.Empty;

        private string reliabilityLevelParameter = string.Empty;
        private string durabilityLevelParameter = string.Empty;
        private string clusterNameParameter = string.Empty;
        private string vmInstanceParameter = string.Empty;
        private string adminPasswordParameter = string.Empty;
        private string adminUserParameter = string.Empty;
        private string locationParameter = string.Empty;
        private string skuParameter = string.Empty;
        private string vmImageSkuParameter = string.Empty;

        private string thumbprintParameter = string.Empty;
        private string keyVaultParameter = string.Empty;
        private string certificateUrlParameter = string.Empty;

        private string domainNameLabelParameter = string.Empty;

        /// <summary>
        /// Resource group name
        /// </summary>
        /// 
        [Parameter(Mandatory = true, Position = 0, ParameterSetName = ExistingKeyVault, ValueFromPipelineByPropertyName = true,
            HelpMessage = "Specify the name of the resource group.")]
        [Parameter(Mandatory = true, Position = 0, ParameterSetName = ByNewPfxAndVaultName, ValueFromPipelineByPropertyName = true,
            HelpMessage = "Specify the name of the resource group.")]
        [Parameter(Mandatory = true, Position = 0, ParameterSetName = ByExistingPfxAndVaultName, ValueFromPipelineByPropertyName = true,
            HelpMessage = "Specify the name of the resource group.")]
        [Parameter(Mandatory = true, Position = 0, ParameterSetName = ByDefaultArmTemplate, ValueFromPipelineByPropertyName = true,
            HelpMessage = "Specify the name of the resource group.")]
        [ResourceGroupCompleter]
        [ValidateNotNullOrEmpty()]
        public override string ResourceGroupName { get; set; }

        [Parameter(Mandatory = true, ParameterSetName = ByExistingPfxAndVaultName, ValueFromPipeline = true,
                   HelpMessage = "The path to the template file.")]
        [Parameter(Mandatory = true, ParameterSetName = ByNewPfxAndVaultName, ValueFromPipeline = true,
                   HelpMessage = "The path to the template file.")]
        [Parameter(Mandatory = true, ParameterSetName = ExistingKeyVault, ValueFromPipeline = true,
                   HelpMessage = "The path to the template file.")]
        [ValidateNotNullOrEmpty]
        public string TemplateFile { get; set; }

        [Parameter(Mandatory = true, ParameterSetName = ExistingKeyVault, ValueFromPipeline = true,
                   HelpMessage = "The path to the template parameter file.")]
        [Parameter(Mandatory = true, ParameterSetName = ByNewPfxAndVaultName, ValueFromPipeline = true,
                   HelpMessage = "The path to the template parameter file.")]
        [Parameter(Mandatory = true, ParameterSetName = ByExistingPfxAndVaultName, ValueFromPipeline = true,
                   HelpMessage = "The path to the template parameter file.")]
        [ValidateNotNullOrEmpty]
        public string ParameterFile { get; set; }

        [Parameter(Mandatory = true, ValueFromPipeline = true, ParameterSetName = ByExistingPfxAndVaultName,
                HelpMessage = "The existing certificate file path for the primary cluster certificate")]
        [ValidateNotNullOrEmpty]
        [Alias("Source")]
        public override string CertificateFile { get; set; }

        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByNewPfxAndVaultName,
           HelpMessage = "The folder of the new certificate file to be created")]
        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByDefaultArmTemplate,
           HelpMessage = "The folder of the new certificate file to be created")]
        [ValidateNotNullOrEmpty]
        [Alias("Destination")]
        public override string CertificateOutputFolder { get; set; }

        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByExistingPfxAndVaultName,
                  HelpMessage = "The password of the certificate file")]
        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByNewPfxAndVaultName,
                  HelpMessage = "The password of the certificate file")]
        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByDefaultArmTemplate,
                  HelpMessage = "The password of the certificate file")]
        [ValidateNotNullOrEmpty]
        [Alias("CertPassword")]
        public override SecureString CertificatePassword { get; set; }

        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByExistingPfxAndVaultName,
                HelpMessage = "The existing certificate file path for the secondary cluster certificate")]
        [ValidateNotNullOrEmpty]
        [Alias("SecSource")]
        public string SecondaryCertificateFile { get; set; }

        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByExistingPfxAndVaultName,
                 HelpMessage = "The password of the certificate file")]
        [ValidateNotNullOrEmpty]
        [Alias("SecCertPassword")]
        public virtual SecureString SecondaryCertificatePassword { get; set; }

        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByNewPfxAndVaultName,
              HelpMessage = "Azure key vault resource group name, it not given it will be defaulted to resource group name")]
        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByExistingPfxAndVaultName,
              HelpMessage = "Azure key vault resource group name, it not given it will be defaulted to resource group name")]
        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByDefaultArmTemplate,
              HelpMessage = "Azure key vault resource group name, it not given it will be defaulted to resource group name")]
        [ValidateNotNullOrEmpty]
        public override string KeyVaultResouceGroupName { get; set; }

        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByNewPfxAndVaultName,
                HelpMessage = "Azure key vault name, it not given it will be defaulted to the resource group name")]
        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByExistingPfxAndVaultName,
                HelpMessage = "Azure key vault name, it not given it will be defaulted to the resource group name")]
        [Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ByDefaultArmTemplate,
                HelpMessage = "Azure key vault name, it not given it will be defaulted to the resource group name")]
        [ValidateNotNullOrEmpty]
        public override string KeyVaultName { get; set; }

        #region ByDefaultArmTemplate

        [Parameter(Mandatory = true, ParameterSetName = ByDefaultArmTemplate, ValueFromPipeline = true,
                   HelpMessage = "The resource group location")]
        [LocationCompleter("Microsoft.ServiceFabric/clusters")]
        public string Location { get; set; }

        [Parameter(Mandatory = false, ParameterSetName = ByDefaultArmTemplate, ValueFromPipeline = true,
                   HelpMessage = "Specify the name of the cluster, if not given it will be same as resource group name")]
        [ValidateNotNullOrEmpty()]
        [ValidatePattern("^[a-z][a-z0-9-]{4,23}[a-z0-9]$")]
        [Alias("ClusterName")]
{'fraction_non_alphanumeric': 0.09135162185523861, 'fraction_numerical': 0.004822346572039489, 'mean_word_length': 3.1886432926829267, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6005142', 'n_tokens_mistral': 5386, 'n_tokens_neox': 5243, 'n_words': 1602}