Inheritance: PSAzureSubscription
        private PSAzureSubscriptionExtended ConstructPsAzureSubscriptionExtended(AzureSubscription subscription, IClientFactory clientFactory)
        {
            using (var client = clientFactory.CreateClient<ManagementClient>(Profile, subscription, AzureEnvironment.Endpoint.ServiceManagement))
            {
                var response = client.Subscriptions.Get();
                var environment = ProfileClient.GetEnvironmentOrDefault(subscription.Environment);
                var account = ProfileClient.Profile.Accounts[subscription.Account];
                bool isCert = account.Type == AzureAccount.AccountType.Certificate;
                var psAzureSubscription = new PSAzureSubscription(subscription, ProfileClient.Profile);
                PSAzureSubscriptionExtended result = new PSAzureSubscriptionExtended(psAzureSubscription)
                {
                    AccountAdminLiveEmailId = response.AccountAdminLiveEmailId,
                    ActiveDirectoryUserId = subscription.Account,
                    CurrentCoreCount = response.CurrentCoreCount,
                    CurrentHostedServices = response.CurrentHostedServices,
                    CurrentDnsServers = response.CurrentDnsServers,
                    CurrentLocalNetworkSites = response.CurrentLocalNetworkSites,
                    CurrentStorageAccounts = response.CurrentStorageAccounts,
                    CurrentVirtualNetworkSites = response.CurrentVirtualNetworkSites,
                    MaxCoreCount = response.MaximumCoreCount,
                    MaxDnsServers = response.MaximumDnsServers,
                    MaxHostedServices = response.MaximumHostedServices,
                    MaxLocalNetworkSites = response.MaximumLocalNetworkSites,
                    MaxStorageAccounts = response.MaximumStorageAccounts,
                    MaxVirtualNetworkSites = response.MaximumVirtualNetworkSites,
                    ServiceAdminLiveEmailId = response.ServiceAdminLiveEmailId,
                    SubscriptionRealName = response.SubscriptionName,
                    SubscriptionStatus = response.SubscriptionStatus.ToString(),
                    ServiceEndpoint = environment.GetEndpoint(AzureEnvironment.Endpoint.ServiceManagement),
                    ResourceManagerEndpoint = environment.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager),
                    IsDefault = subscription.GetProperty(AzureSubscription.Property.Default) != null,
                    Account = account,
                    Certificate = isCert ? AzureSession.DataStore.GetCertificate(subscription.Account) : null,
                    CurrentStorageAccountName = subscription.GetProperty(AzureSubscription.Property.StorageAccount)
                };

                return result;
            }
        }
        public static void SetTestSettings()
        {
            // Please remove this line once all tests are done
            System.Net.ServicePointManager.ServerCertificateValidationCallback +=
                delegate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                    System.Security.Cryptography.X509Certificates.X509Chain chain,
                    System.Net.Security.SslPolicyErrors sslPolicyErrors)
                {
                    return true; // **** Always accept
                };

            vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();
            CredentialHelper.GetTestSettings(Resource.TestSettings);

            vmPowershellCmdlets.RemoveAzureSubscriptions();
            var ussouthEnv = vmPowershellCmdlets.GetAzureEnvironment("ussouth");
            if (ussouthEnv != null && ussouthEnv.Count > 0)
            {
                Console.WriteLine("Removing ussouth environment...");
                vmPowershellCmdlets.RunPSScript("Remove-AzureEnvironment -Name ussouth -Force");
            }

            List<PSAzureEnvironment> environments = vmPowershellCmdlets.GetAzureEnvironment();
            var serviceManagementUrl = GetServiceManagementUrl(CredentialHelper.PublishSettingsFile);
            var subscriptionId = GetSubscriptionId(CredentialHelper.PublishSettingsFile);

            foreach (var env in environments)
            {
                if (!string.IsNullOrEmpty(env.ServiceManagementUrl))
                {
                    if (env.ServiceManagementUrl.Equals(serviceManagementUrl))
                    {
                        currentEnvName = env.Name;
                        var curEnv = vmPowershellCmdlets.GetAzureEnvironment(currentEnvName)[0];
                        Console.WriteLine("Using the existing environment: {0}", currentEnvName);
                        Console.WriteLine("PublichSettingsFileUrl: {0}", curEnv.PublishSettingsFileUrl);
                        Console.WriteLine("ServiceManagement: {0}", curEnv.ServiceManagementUrl);
                        Console.WriteLine("ManagementPortalUrl: {0}", curEnv.ManagementPortalUrl);
                        Console.WriteLine("ActiveDirectory: {0}", curEnv.ActiveDirectoryAuthority);
                        Console.WriteLine("ActiveDirectoryServiceEndpointResourceId: {0}", curEnv.ActiveDirectoryServiceEndpointResourceId);
                        Console.WriteLine("ResourceManager: {0}", curEnv.ResourceManagerUrl);
                        Console.WriteLine("Gallery: {0}", curEnv.GalleryUrl);
                        Console.WriteLine("Graph: {0}", curEnv.GalleryUrl);
                        break;
                    }
                }
            }

            if (string.IsNullOrEmpty(currentEnvName))
            {
                Console.WriteLine("Creating new environment... : {0}", TempEnvName);
                var prodEnv = vmPowershellCmdlets.GetAzureEnvironment("AzureCloud")[0];
                vmPowershellCmdlets.RunPSScript(string.Format(
                    @"Add-AzureEnvironment -Name {0} `
                    -PublishSettingsFileUrl {1} `
                    -ServiceEndpoint {2} `
                    -ManagementPortalUrl {3} `
                    -ActiveDirectoryEndpoint {4} `
                    -ActiveDirectoryServiceEndpointResourceId {5} `
                    -ResourceManagerEndpoint {6} `
                    -GalleryEndpoint {7} `
                    -GraphEndpoint {8}",
                    TempEnvName,
                    prodEnv.PublishSettingsFileUrl,
                    serviceManagementUrl,
                    prodEnv.ManagementPortalUrl,
                    prodEnv.ActiveDirectoryAuthority,
                    prodEnv.ActiveDirectoryServiceEndpointResourceId,
                    prodEnv.ResourceManagerUrl,
                    prodEnv.GalleryUrl,
                    prodEnv.GalleryUrl));

                vmPowershellCmdlets.ImportAzurePublishSettingsFile(CredentialHelper.PublishSettingsFile, TempEnvName);
            }
            else
            {
                Console.WriteLine("Using existing environment... : {0}", currentEnvName);
                vmPowershellCmdlets.ImportAzurePublishSettingsFile(CredentialHelper.PublishSettingsFile, currentEnvName);
            }

            var firstSub = vmPowershellCmdlets.GetAzureSubscription(subscriptionId);
            vmPowershellCmdlets.SelectAzureSubscription(firstSub.SubscriptionId);
            defaultAzureSubscription = vmPowershellCmdlets.GetCurrentAzureSubscription();
            CredentialHelper.DefaultSubscriptionName = defaultAzureSubscription.SubscriptionName;

            locationName = vmPowershellCmdlets.GetAzureLocationName(new[] { CredentialHelper.Location }); // Get-AzureLocation

            if (String.IsNullOrEmpty(locationName))
            {
                Console.WriteLine("No location is selected!");
            }
            Console.WriteLine("Location Name: {0}", locationName);

            if (defaultAzureSubscription.CurrentStorageAccountName == null && !string.IsNullOrEmpty(CredentialHelper.DefaultStorageName))
            {
                SetDefaultStorage();
            }

            try
            {
                imageName = vmPowershellCmdlets.GetAzureVMImageName(new[] { "Windows" }, false); // Get-AzureVMImage
            }
            catch
            {
                Console.WriteLine("Error occurred during Get-AzureVMImageName... imageName is not set.");
            }

            if (String.IsNullOrEmpty(imageName))
            {
                Console.WriteLine("No image is selected!");
            }
            else
            {
                Console.WriteLine("Image Name: {0}", imageName);
            }
        }
        public static void SetTestSettings()
        {
            vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();
            CredentialHelper.GetTestSettings(Resource.TestSettings);

            vmPowershellCmdlets.RemoveAzureSubscriptions();
            vmPowershellCmdlets.ImportAzurePublishSettingsFile(CredentialHelper.PublishSettingsFile);
            var firstSub = vmPowershellCmdlets.GetAzureSubscription().First();
            vmPowershellCmdlets.SelectAzureSubscription(firstSub.SubscriptionName);

            if (string.IsNullOrEmpty(CredentialHelper.DefaultSubscriptionName))
            {
                defaultAzureSubscription = vmPowershellCmdlets.GetCurrentAzureSubscription();
                if (string.IsNullOrEmpty(Resource.DefaultSubscriptionName))
                {
                    CredentialHelper.DefaultSubscriptionName = defaultAzureSubscription.SubscriptionName;
                }
            }
            else
            {
                defaultAzureSubscription = vmPowershellCmdlets.SetDefaultAzureSubscription(CredentialHelper.DefaultSubscriptionName);
            }

            locationName = vmPowershellCmdlets.GetAzureLocationName(new[] { CredentialHelper.Location }); // Get-AzureLocation

            if (String.IsNullOrEmpty(locationName))
            {
                Console.WriteLine("No location is selected!");
            }
            Console.WriteLine("Location Name: {0}", locationName);

            if (defaultAzureSubscription.CurrentStorageAccountName == null && !string.IsNullOrEmpty(CredentialHelper.DefaultStorageName))
            {
                SetDefaultStorage();
            }

            try
            {
                imageName = vmPowershellCmdlets.GetAzureVMImageName(new[] { "Windows" }, false); // Get-AzureVMImage
            }
            catch
            {
                Console.WriteLine("Error occurred during Get-AzureVMImageName... imageName is not set.");
            }

            try
            {
                DownloadVhds();
            }
            catch
            {
                Console.WriteLine("Error occurred during downloading vhds...");
            }

            if (String.IsNullOrEmpty(imageName))
            {
                Console.WriteLine("No image is selected!");
            }
            else
            {
                Console.WriteLine("Image Name: {0}", imageName);
            }
        }
 public static void SetDefaultStorage()
 {
     if (!string.IsNullOrEmpty(GetDefaultStorage(CredentialHelper.DefaultStorageName, CredentialHelper.Location)))
     {
         vmPowershellCmdlets.SelectAzureSubscription(defaultAzureSubscription.SubscriptionId);
         defaultAzureSubscription = vmPowershellCmdlets.SetAzureSubscription(defaultAzureSubscription.SubscriptionId, CredentialHelper.DefaultStorageName);
         defaultAzureSubscription.CurrentStorageAccountName = CredentialHelper.DefaultStorageName;
         storageAccountKey = vmPowershellCmdlets.GetAzureStorageAccountKey(CredentialHelper.DefaultStorageName);
         Assert.AreEqual(CredentialHelper.DefaultStorageName, storageAccountKey.StorageAccountName);
         blobUrlRoot = (vmPowershellCmdlets.GetAzureStorageAccount(CredentialHelper.DefaultStorageName)[0].Endpoints.ToArray())[0];
     }
     else
     {
         Console.WriteLine("Unable to get the default storage account");
     }
 }
        public static void SetTestSettings()
        {
            vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();
            CredentialHelper.GetTestSettings(Resource.TestSettings);

            vmPowershellCmdlets.RemoveAzureSubscriptions();
            if (vmPowershellCmdlets.GetAzureEnvironment("ussouth").Count > 0)
            {
                Console.WriteLine("Removing ussouth environment...");
                vmPowershellCmdlets.RunPSScript("Remove-AzureEnvironment -Name ussouth -Force");
            }

            List<AzureEnvironment> environments =  vmPowershellCmdlets.GetAzureEnvironment();
            var serviceManagementUrl = GetServiceManagementUrl(CredentialHelper.PublishSettingsFile);

            foreach (var env in environments)
            {
                var envServiceManagementUrl = (string) env.Endpoints[AzureEnvironment.Endpoint.ServiceManagement];
                if (!string.IsNullOrEmpty(envServiceManagementUrl))
                {
                    if (envServiceManagementUrl.Equals(serviceManagementUrl))
                    {
                        currentEnvName = env.Name;
                        var curEnv = vmPowershellCmdlets.GetAzureEnvironment(currentEnvName)[0];
                        Console.WriteLine("Using the existing environment: {0}", currentEnvName);
                        Console.WriteLine("PublichSettingsFileUrl: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl));
                        Console.WriteLine("ServiceManagement: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.ServiceManagement));
                        Console.WriteLine("ManagementPortalUrl: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.ManagementPortalUrl));
                        Console.WriteLine("ActiveDirectory: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectory));
                        Console.WriteLine("ActiveDirectoryServiceEndpointResourceId: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId));
                        Console.WriteLine("ResourceManager: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager));
                        Console.WriteLine("Gallery: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.Gallery));
                        Console.WriteLine("Graph: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.Graph));
                        break;
                    }
                }
            }

            if (string.IsNullOrEmpty(currentEnvName))
            {
                Console.WriteLine("Creating new environment... : {0}", TempEnvName);
                var prodEnv = vmPowershellCmdlets.GetAzureEnvironment("AzureCloud")[0];
                vmPowershellCmdlets.RunPSScript(string.Format(
                    @"Add-AzureEnvironment -Name {0} `
                    -PublishSettingsFileUrl {1} `
                    -ServiceEndpoint {2} `
                    -ManagementPortalUrl {3} `
                    -ActiveDirectoryEndpoint {4} `
                    -ActiveDirectoryServiceEndpointResourceId {5} `
                    -ResourceManagerEndpoint {6} `
                    -GalleryEndpoint {7} `
                    -GraphEndpoint {8}",
                    TempEnvName,
                    prodEnv.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl),
                    serviceManagementUrl,
                    prodEnv.GetEndpoint(AzureEnvironment.Endpoint.ManagementPortalUrl),
                    prodEnv.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectory),
                    prodEnv.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId),
                    prodEnv.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager),
                    prodEnv.GetEndpoint(AzureEnvironment.Endpoint.Gallery),
                    prodEnv.GetEndpoint(AzureEnvironment.Endpoint.Graph)));

                vmPowershellCmdlets.ImportAzurePublishSettingsFile(CredentialHelper.PublishSettingsFile, TempEnvName);
            }
            else
            {
                Console.WriteLine("Using existing environment... : {0}", currentEnvName);
                vmPowershellCmdlets.ImportAzurePublishSettingsFile(CredentialHelper.PublishSettingsFile, currentEnvName);
            }

            var firstSub = vmPowershellCmdlets.GetAzureSubscription().First();
            vmPowershellCmdlets.SelectAzureSubscription(firstSub.SubscriptionName);

            if (string.IsNullOrEmpty(CredentialHelper.DefaultSubscriptionName))
            {
                defaultAzureSubscription = vmPowershellCmdlets.GetCurrentAzureSubscription();
                if (string.IsNullOrEmpty(Resource.DefaultSubscriptionName))
                {
                    CredentialHelper.DefaultSubscriptionName = defaultAzureSubscription.SubscriptionName;
                }
            }
            else
            {
                defaultAzureSubscription = vmPowershellCmdlets.SetDefaultAzureSubscription(CredentialHelper.DefaultSubscriptionName);
            }

            locationName = vmPowershellCmdlets.GetAzureLocationName(new[] { CredentialHelper.Location }); // Get-AzureLocation

            if (String.IsNullOrEmpty(locationName))
            {
                Console.WriteLine("No location is selected!");
            }
            Console.WriteLine("Location Name: {0}", locationName);

            if (defaultAzureSubscription.CurrentStorageAccountName == null && !string.IsNullOrEmpty(CredentialHelper.DefaultStorageName))
            {
                SetDefaultStorage();
            }

            try
            {
                imageName = vmPowershellCmdlets.GetAzureVMImageName(new[] { "Windows" }, false); // Get-AzureVMImage
            }
            catch
            {
                Console.WriteLine("Error occurred during Get-AzureVMImageName... imageName is not set.");
            }

            if (String.IsNullOrEmpty(imageName))
            {
                Console.WriteLine("No image is selected!");
            }
            else
            {
                Console.WriteLine("Image Name: {0}", imageName);
            }
        }