protected void InitializeStorageContext(string storageAccountName)
        {
            // lookup storage account by name and retrieve key
            var storageService = this.StorageClient.StorageAccounts.Get(storageAccountName);

            if (storageService == null)
            {
                ThrowTerminatingError(new ErrorRecord(
                                          new Exception("ServiceExtensionCannotFindStorageAccountName"),
                                          string.Empty,
                                          ErrorCategory.InvalidData,
                                          null));
            }
            var storageKeys = this.StorageClient.StorageAccounts.GetKeys(storageService.StorageAccount.Name);

            if (storageKeys == null || storageKeys.PrimaryKey == null || storageKeys.SecondaryKey == null)
            {
                ThrowTerminatingError(new ErrorRecord(
                                          new Exception("ServiceExtensionCannotFindStorageAccountKey"),
                                          string.Empty,
                                          ErrorCategory.InvalidData,
                                          null));
            }
            string storageAccountKey = storageKeys.PrimaryKey != null ? storageKeys.PrimaryKey : storageKeys.SecondaryKey;

            // now that key has been retrieved, initialize context and return
            StorageCredentials  creds = new StorageCredentials(storageAccountName, storageAccountKey);
            CloudStorageAccount csa   = new WindowsAzure.Storage.CloudStorageAccount(creds, true);

            StorageContext = new AzureStorageContext(csa);
        }
        public void AzureServiceDiagnosticsExtensionTest()
        {
            StartTest(MethodBase.GetCurrentMethod().Name, testStartTime);

            // Choose the package and config files from local machine
            string packageName = Convert.ToString(TestContext.DataRow["packageName"]);
            string configName = Convert.ToString(TestContext.DataRow["configName"]);
            var packagePath1 = new FileInfo(Directory.GetCurrentDirectory() + "\\" + packageName);
            var configPath1 = new FileInfo(Directory.GetCurrentDirectory() + "\\" + configName);

            Assert.IsTrue(File.Exists(packagePath1.FullName), "VHD file not exist={0}", packagePath1);
            Assert.IsTrue(File.Exists(configPath1.FullName), "VHD file not exist={0}", configPath1);

            string deploymentName = "deployment1";
            string deploymentLabel = "label1";
            DeploymentInfoContext result;

            string storage = defaultAzureSubscription.CurrentStorageAccountName;
            string daConfig = @"da.xml";

            string defaultExtensionId = string.Format("Default-{0}-Production-Ext-0", Utilities.PaaSDiagnosticsExtensionName);

            serviceName = Utilities.GetUniqueShortName(serviceNamePrefix);
            vmPowershellCmdlets.NewAzureService(serviceName, serviceName, locationName);
            Console.WriteLine("service, {0}, is created.", serviceName);

            vmPowershellCmdlets.NewAzureDeployment(serviceName, packagePath1.FullName, configPath1.FullName, DeploymentSlotType.Production, deploymentLabel, deploymentName, false, false);

            result = vmPowershellCmdlets.GetAzureDeployment(serviceName, DeploymentSlotType.Production);
            pass = Utilities.PrintAndCompareDeployment(result, serviceName, deploymentName, deploymentLabel, DeploymentSlotType.Production, null, 2);
            Console.WriteLine("successfully deployed the package");

            string storageKey = vmPowershellCmdlets.GetAzureStorageAccountKey(storage).Primary;

            StorageCredentials creds = new StorageCredentials(storage, storageKey);
            CloudStorageAccount csa = new WindowsAzure.Storage.CloudStorageAccount(creds, true);
            var storageContext = new AzureStorageContext(csa);

            vmPowershellCmdlets.SetAzureServiceDiagnosticsExtension(serviceName, storageContext, daConfig, null, null);

            DiagnosticExtensionContext resultContext = vmPowershellCmdlets.GetAzureServiceDiagnosticsExtension(serviceName)[0];

            VerifyDiagExtContext(resultContext, "AllRoles", defaultExtensionId, storage, daConfig);

            vmPowershellCmdlets.RemoveAzureServiceDiagnosticsExtension(serviceName, true);

            Assert.AreEqual(vmPowershellCmdlets.GetAzureServiceDiagnosticsExtension(serviceName).Count, 0);

            vmPowershellCmdlets.RemoveAzureDeployment(serviceName, DeploymentSlotType.Production, true);

            pass &= Utilities.CheckRemove(vmPowershellCmdlets.GetAzureDeployment, serviceName, DeploymentSlotType.Production);
        }
        public void VipSwapWithDiagnosticsExtensionTest()
        {
            StartTest(MethodBase.GetCurrentMethod().Name, testStartTime);

            // Choose the package and config files from local machine
            string packageName = Convert.ToString(TestContext.DataRow["packageName"]);
            string configName = Convert.ToString(TestContext.DataRow["configName"]);
            var packagePath = new FileInfo(Directory.GetCurrentDirectory() + "\\" + packageName);
            var configPath = new FileInfo(Directory.GetCurrentDirectory() + "\\" + configName);

            Assert.IsTrue(File.Exists(packagePath.FullName), "Package file not exist={0}", packagePath);
            Assert.IsTrue(File.Exists(configPath.FullName), "Config file not exist={0}", configPath);

            string deploymentName = "deployment1";
            string deploymentLabel = "label1";
            DeploymentInfoContext result;

            string storage = defaultAzureSubscription.CurrentStorageAccountName;
            string daConfig = @"da.xml";

            string storageKey = vmPowershellCmdlets.GetAzureStorageAccountKey(storage).Primary;
            StorageCredentials creds = new StorageCredentials(storage, storageKey);
            CloudStorageAccount csa = new WindowsAzure.Storage.CloudStorageAccount(creds, true);
            var storageContext = new AzureStorageContext(csa);

            serviceName = Utilities.GetUniqueShortName(serviceNamePrefix);
            vmPowershellCmdlets.NewAzureService(serviceName, serviceName, locationName);
            Console.WriteLine("service, {0}, is created.", serviceName);

            // deploy staging
            vmPowershellCmdlets.NewAzureDeployment(serviceName, packagePath.FullName, configPath.FullName, DeploymentSlotType.Staging, deploymentLabel, deploymentName, false, false);
            result = vmPowershellCmdlets.GetAzureDeployment(serviceName, DeploymentSlotType.Staging);
            pass = Utilities.PrintAndCompareDeployment(result, serviceName, deploymentName, deploymentLabel, DeploymentSlotType.Staging, null, 2);
            Console.WriteLine("successfully deployed the package");

            vmPowershellCmdlets.SetAzureServiceDiagnosticsExtension(serviceName, storageContext, daConfig, null, slot: DeploymentSlotType.Staging);
            DiagnosticExtensionContext resultContext = vmPowershellCmdlets.GetAzureServiceDiagnosticsExtension(serviceName, slot: DeploymentSlotType.Staging)[0];
            VerifyDiagExtContext(resultContext, "AllRoles", "Default-PaaSDiagnostics-Staging-Ext-0", storage, daConfig);

            // swap staging -> production
            // production will be retain diagnosting config from staging, named Default-PaaSDiagnostics-Staging-Ext-0
            vmPowershellCmdlets.MoveAzureDeployment(serviceName);

            // deploy a new staging
            deploymentName = "deployment2";
            deploymentLabel = "label2";

            vmPowershellCmdlets.NewAzureDeployment(serviceName, packagePath.FullName, configPath.FullName, DeploymentSlotType.Staging, deploymentLabel, deploymentName, false, false);
            result = vmPowershellCmdlets.GetAzureDeployment(serviceName, DeploymentSlotType.Staging);
            pass = Utilities.PrintAndCompareDeployment(result, serviceName, deploymentName, deploymentLabel, DeploymentSlotType.Staging, null, 2);
            Console.WriteLine("successfully deployed the package");

            // should detect that Default-PaaSDiagnostics-Staging-Ext-0 is in use 
            vmPowershellCmdlets.SetAzureServiceDiagnosticsExtension(serviceName, storageContext, daConfig, null, slot: DeploymentSlotType.Staging);
            DiagnosticExtensionContext resultContext2 = vmPowershellCmdlets.GetAzureServiceDiagnosticsExtension(serviceName, slot: DeploymentSlotType.Staging)[0];
            VerifyDiagExtContext(resultContext2, "AllRoles", "Default-PaaSDiagnostics-Staging-Ext-1", storage, daConfig);

            // execute again to make sure max number of extensions will handled correctly (1 for production and 1 for staging, 1 unused)
            // should not fail due to ExtensionIdLiveCycleCount limit
            vmPowershellCmdlets.SetAzureServiceDiagnosticsExtension(serviceName, storageContext, daConfig, null, slot: DeploymentSlotType.Staging);
            DiagnosticExtensionContext resultContext3 = vmPowershellCmdlets.GetAzureServiceDiagnosticsExtension(serviceName, slot: DeploymentSlotType.Staging)[0];

            // azure splits config from All Roles to specific role in that case, so role name should not be validated
            VerifyDiagExtContext(resultContext3, null, "Default-PaaSDiagnostics-Staging-Ext-2", storage, daConfig);
            
            vmPowershellCmdlets.RemoveAzureService(serviceName, true);
        }
        protected void InitializeStorageContext(string storageAccountName)
        {
            // lookup storage account by name and retrieve key 
            var storageService = this.StorageClient.StorageAccounts.Get(storageAccountName);
            if (storageService == null)
            {
                ThrowTerminatingError(new ErrorRecord(
                    new Exception("ServiceExtensionCannotFindStorageAccountName"),
                    string.Empty,
                    ErrorCategory.InvalidData,
                    null));
            }
            var storageKeys = this.StorageClient.StorageAccounts.GetKeys(storageService.StorageAccount.Name);
            if (storageKeys == null || storageKeys.PrimaryKey == null || storageKeys.SecondaryKey == null)
            {
                ThrowTerminatingError(new ErrorRecord(
                    new Exception("ServiceExtensionCannotFindStorageAccountKey"),
                    string.Empty,
                    ErrorCategory.InvalidData,
                    null));
            }
            string storageAccountKey = storageKeys.PrimaryKey != null ? storageKeys.PrimaryKey : storageKeys.SecondaryKey;

            // now that key has been retrieved, initialize context and return
            StorageCredentials creds = new StorageCredentials(storageAccountName, storageAccountKey);
            CloudStorageAccount csa = new WindowsAzure.Storage.CloudStorageAccount(creds, true);
            StorageContext = new AzureStorageContext(csa);
        }