public SubscriptionDataExtended(Subscription subscription, SubscriptionData subscriptionData,
     string description, Operation operation)
 {
     OperationDescription = description;
     OperationStatus = operation.Status;
     OperationId = operation.OperationTrackingId;
     SubscriptionName = subscriptionData.SubscriptionName;
     SubscriptionId = subscriptionData.SubscriptionId;
     Certificate = subscriptionData.Certificate;
     CurrentStorageAccount = subscriptionData.CurrentStorageAccount;
     ServiceEndpoint = subscriptionData.ServiceEndpoint;
     SqlAzureServiceEndpoint = subscriptionData.SqlAzureServiceEndpoint;
     IsDefault = subscriptionData.IsDefault;
     AccountAdminLiveEmailId = subscription.AccountAdminLiveEmailId;
     CurrentCoreCount = subscription.CurrentCoreCount;
     CurrentHostedServices = subscription.CurrentHostedServices;
     CurrentStorageAccounts = subscription.CurrentStorageAccounts;
     CurrentDnsServers = subscription.CurrentDnsServers;
     CurrentLocalNetworkSites = subscription.CurrentLocalNetworkSites;
     CurrentVirtualNetworkSites = subscription.CurrentVirtualNetworkSites;
     MaxCoreCount = subscription.MaxCoreCount;
     MaxHostedServices = subscription.MaxHostedServices;
     MaxStorageAccounts = subscription.MaxStorageAccounts;
     MaxDnsServers = subscription.MaxDnsServers;
     MaxLocalNetworkSites = subscription.MaxLocalNetworkSites;
     MaxVirtualNetworkSites = subscription.MaxVirtualNetworkSites;
     ServiceAdminLiveEmailId = subscription.ServiceAdminLiveEmailId;
     SubscriptionRealName = subscription.SubscriptionName;
     SubscriptionStatus = subscription.SubscriptionStatus;
 }
        public static CloudStorageAccount GetCurrentCloudStorageAccount(IServiceManagement channel, SubscriptionData subscriptionData)
        {
            if (String.IsNullOrEmpty(subscriptionData.CurrentStorageAccount))
            {
                return null;
            }

            if (subscriptionData.CurrentCloudStorageAccount != null)
            {
                return subscriptionData.CurrentCloudStorageAccount;
            }

            CloudStorageAccount currentStorage = null;
            using (new OperationContextScope(channel.ToContextChannel()))
            {
                var storageService = channel.GetStorageService(subscriptionData.SubscriptionId, subscriptionData.CurrentStorageAccount);
                var storageServiceKeys = channel.GetStorageKeys(subscriptionData.SubscriptionId, subscriptionData.CurrentStorageAccount);
                if (storageService != null && storageServiceKeys != null)
                {
                    string connectionString = General.BuildConnectionString("https", storageService.ServiceName, storageServiceKeys.StorageServiceKeys.Primary, storageService.StorageServiceProperties.Endpoints[0].Replace("http://", "https://"), storageService.StorageServiceProperties.Endpoints[2].Replace("http://", "https://"), storageService.StorageServiceProperties.Endpoints[1].Replace("http://", "https://"));
                    currentStorage = CloudStorageAccount.Parse(connectionString);
                }
            }

            subscriptionData.CurrentCloudStorageAccount = currentStorage;
            return currentStorage;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates new WebsitesClient.
        /// </summary>
        /// <param name="subscription">The Windows Azure subscription data object</param>
        /// <param name="logger">The logger action</param>
        public WebsitesClient(SubscriptionData subscription, Action<string> logger)
        {
            SubscriptionId = subscription.SubscriptionId;
            Logger = logger;
            WebsiteChannel = ServiceManagementHelper.CreateServiceManagementChannel<IWebsitesServiceManagement>(
                ConfigurationConstants.WebHttpBinding(),
                new Uri(subscription.ServiceEndpoint),
                subscription.Certificate,
                new HttpRestMessageInspector(logger));

            ServiceManagementChannel = ServiceManagementHelper.CreateServiceManagementChannel<IServiceManagement>(
                ConfigurationConstants.WebHttpBinding(),
                new Uri(subscription.ServiceEndpoint),
                subscription.Certificate,
                new HttpRestMessageInspector(logger));
        }
Exemplo n.º 4
0
        public static CloudStorageAccount GetCurrentCloudStorageAccount(
            IServiceManagement channel,
            SubscriptionData subscriptionData)
        {
            if (String.IsNullOrEmpty(subscriptionData.CurrentStorageAccount))
            {
                return null;
            }

            if (subscriptionData.CurrentCloudStorageAccount != null)
            {
                return subscriptionData.CurrentCloudStorageAccount;
            }

            SetCurrentCloudStorageAccount(channel, subscriptionData);

            return subscriptionData.CurrentCloudStorageAccount;
        }
Exemplo n.º 5
0
 /// <summary>
 /// Creates new instance from CloudServiceClient.
 /// </summary>
 /// <param name="subscription">The subscription data</param>
 /// <param name="debugStream">Action used to log http requests/responses</param>
 /// <param name="verboseStream">Action used to log detailed client progress</param>
 /// <param name="warningStream">Action used to log warning messages</param>
 public CloudServiceClient(
     SubscriptionData subscription,
     string currentLocation = null,
     Action<string> debugStream = null,
     Action<string> verboseStream = null,
     Action<string> warningStream = null)
 {
     Subscription = subscription;
     subscriptionId = subscription.SubscriptionId;
     CurrentDirectory = currentLocation;
     DebugStream = debugStream;
     VerboseStream = verboseStream;
     WarningeStream = warningStream;
     HeadersInspector = new HeadersInspector();
     ServiceManagementChannel = ChannelHelper.CreateServiceManagementChannel<IServiceManagement>(
         ConfigurationConstants.WebHttpBinding(),
         new Uri(subscription.ServiceEndpoint),
         subscription.Certificate,
         new HttpRestMessageInspector(DebugStream),
         HeadersInspector);
     CloudBlobUtility = new CloudBlobUtility();
 }
Exemplo n.º 6
0
        /// <summary>
        /// Creates new WebsitesClient.
        /// </summary>
        /// <param name="subscription">The Windows Azure subscription data object</param>
        /// <param name="logger">The logger action</param>
        public WebsitesClient(SubscriptionData subscription, Action<string> logger)
        {
            subscriptionId = subscription.SubscriptionId;
            Subscription = subscription;
            Logger = logger;
            HeadersInspector = new HeadersInspector();
            HeadersInspector.RequestHeaders.Add(ServiceManagement.Constants.VersionHeaderName, WebsitesServiceVersion);
            HeadersInspector.RequestHeaders.Add(ApiConstants.UserAgentHeaderName, ApiConstants.UserAgentHeaderValue);
            HeadersInspector.RemoveHeaders.Add(ApiConstants.VSDebuggerCausalityDataHeaderName);
            WebsiteChannel = ChannelHelper.CreateChannel<IWebsitesServiceManagement>(
                ConfigurationConstants.WebHttpBinding(),
                new Uri(subscription.ServiceEndpoint),
                subscription.Certificate,
                HeadersInspector,
                new HttpRestMessageInspector(logger));

            ServiceManagementChannel = ChannelHelper.CreateServiceManagementChannel<IServiceManagement>(
                ConfigurationConstants.WebHttpBinding(),
                new Uri(subscription.ServiceEndpoint),
                subscription.Certificate,
                new HttpRestMessageInspector(logger));

            cloudServiceClient = new CloudServiceClient(subscription, debugStream: logger);
        }
        /// <summary>
        /// Connect to Azure SQL Server using certificate authentication.
        /// </summary>
        /// <param name="serverName">The name of the server to connect to</param>
        /// <param name="subscriptionData">The subscription data to use for authentication</param>
        /// <returns>A new <see cref="ServerDataServiceCertAuth"/> context,
        /// or <c>null</c> if an error occurred.</returns>
        internal ServerDataServiceCertAuth GetServerDataServiceByCertAuth(
            string serverName,
            SubscriptionData subscriptionData)
        {
            ServerDataServiceCertAuth context = null;

            try
            {
                context = ServerDataServiceCertAuth.Create(serverName, subscriptionData);
            }
            catch (ArgumentException e)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(this, string.Empty, e);

                context = null;
            }

            return context;
        }
Exemplo n.º 8
0
        /// <summary>
        /// Sets the default subscription properties based on the cmdlet parameters.
        /// </summary>
        /// <param name="subscription">The subscription where to set the properties.</param>
        /// <param name="subscriptionId">The subscription's identifier.</param>
        /// <param name="certificate">The subscription's certificate.</param>
        /// <param name="serviceEndpoint">The subscription's service endpoint</param>
        /// <param name="currentStorageAccount">The current storage account.</param>
        private void SetCommonSettingsProcess(SubscriptionData subscription, string subscriptionId, X509Certificate2 certificate, string serviceEndpoint, string currentStorageAccount)
        {
            if (subscriptionId != null)
            {
                subscription.SubscriptionId = subscriptionId;
            }

            if (certificate != null)
            {
                subscription.Certificate = certificate;
            }

            if (serviceEndpoint != null)
            {
                subscription.ServiceEndpoint = serviceEndpoint;
            }

            if (currentStorageAccount != null)
            {
                subscription.NullCurrentStorageAccount(); // next time it is retrieved get the right account info.
                subscription.CurrentStorageAccount = currentStorageAccount;
            }
        }
Exemplo n.º 9
0
        public void ImportSubscriptionsFile(string subscriptionsDataFile, X509Certificate2 publishSettingsCertificate)
        {
            var subscriptions = General.DeserializeXmlFile<Subscriptions>(subscriptionsDataFile, string.Format(Resources.InvalidSubscriptionsDataSchema, subscriptionsDataFile));
            if (subscriptions != null && subscriptions.Subscription != null)
            {
                Subscriptions = subscriptions.Subscription
                    .Select(subscription =>
                    {
                        X509Certificate2 certificate = null;
                        if (subscription.Thumbprint != null)
                        {
                            if (publishSettingsCertificate != null && subscription.Thumbprint == publishSettingsCertificate.Thumbprint)
                            {
                                certificate = publishSettingsCertificate;
                            }
                            else
                            {
                                certificate = General.GetCertificateFromStore(subscription.Thumbprint);
                            }
                        }

                        var subscriptionData = new SubscriptionData
                        {
                            SubscriptionName = subscription.name,
                            SubscriptionId = subscription.SubscriptionId,
                            Certificate = certificate,
                            ServiceEndpoint = subscription.ServiceEndpoint,
                            SqlAzureServiceEndpoint = subscription.SQLAzureServiceEndpoint,
                            CurrentStorageAccount = subscription.CurrentStorageAccount
                        };

                        return subscriptionData;
                    })
                    .ToDictionary(subscriptionData => subscriptionData.SubscriptionName, StringComparer.OrdinalIgnoreCase);
            }
        }
Exemplo n.º 10
0
        private static void SetCurrentCloudStorageAccount(IServiceManagement channel, SubscriptionData subscriptionData)
        {
            CloudStorageAccount currentStorage = null;
            using (new OperationContextScope((IContextChannel)channel))
            {
                var storageService = channel.GetStorageService(
                    subscriptionData.SubscriptionId,
                    subscriptionData.CurrentStorageAccount);
                var storageServiceKeys = channel.GetStorageKeys(
                    subscriptionData.SubscriptionId,
                    subscriptionData.CurrentStorageAccount);

                if (storageService != null && storageServiceKeys != null)
                {
                    currentStorage = new CloudStorageAccount(new StorageCredentials(
                        storageService.ServiceName,
                        storageServiceKeys.StorageServiceKeys.Primary),
                        General.CreateHttpsEndpoint(storageService.StorageServiceProperties.Endpoints[0]),
                        General.CreateHttpsEndpoint(storageService.StorageServiceProperties.Endpoints[1]),
                        General.CreateHttpsEndpoint(storageService.StorageServiceProperties.Endpoints[2]));
                }
            }

            subscriptionData.CurrentCloudStorageAccount = currentStorage;
        }
 protected override void WriteSubscription(SubscriptionData subscription)
 {
     Messages.Add(subscription);
 }
Exemplo n.º 12
0
        public static void SetTestSettings()
        {
            vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();
            CredentialHelper.GetTestSettings(Resource.TestSettings);

            vmPowershellCmdlets.RemoveAzureSubscriptions();
            vmPowershellCmdlets.ImportAzurePublishSettingsFile(CredentialHelper.PublishSettingsFile);

            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.CurrentStorageAccount == null && !string.IsNullOrEmpty(CredentialHelper.DefaultStorageName))
            {
                SetDefaultStorage();
            }

            imageName = vmPowershellCmdlets.GetAzureVMImageName(new[] { "Windows" }, false); // Get-AzureVMImage
            if (String.IsNullOrEmpty(imageName))
            {
                Console.WriteLine("No image is selected!");
            }
            else
            {
                Console.WriteLine("Image Name: {0}", imageName);
            }
        }
Exemplo n.º 13
0
        public static void SetDefaultStorage()
        {
            if (!string.IsNullOrEmpty(GetDefaultStorage(CredentialHelper.DefaultStorageName, CredentialHelper.Location)))
            {
                defaultAzureSubscription = vmPowershellCmdlets.SetAzureSubscription(defaultAzureSubscription.SubscriptionName, CredentialHelper.DefaultStorageName);

                storageAccountKey = vmPowershellCmdlets.GetAzureStorageAccountKey(defaultAzureSubscription.CurrentStorageAccount);
                Assert.AreEqual(defaultAzureSubscription.CurrentStorageAccount, storageAccountKey.StorageAccountName);
                blobUrlRoot = (vmPowershellCmdlets.GetAzureStorageAccount(defaultAzureSubscription.CurrentStorageAccount)[0].Endpoints.ToArray())[0];
            }
            else
            {
                Console.WriteLine("Unable to get the default storege account");
            }
        }
 protected virtual void WriteSubscription(SubscriptionData subscriptionData)
 {
     WriteObject(subscriptionData, true);
 }
Exemplo n.º 15
0
 public static void SetCurrentSubscription(this PSCmdlet cmdlet, SubscriptionData subscription)
 {
     SessionManager.SetVariable(cmdlet, ManagementConstants.CurrentSubscriptionEnvironmentVariable, subscription);
 }
 private void WriteExtendedSubscription(SubscriptionData subscriptionData, string subscriptionsDataFile)
 {
     SubscriptionData currentSubscription = this.GetCurrentSubscription();
     this.SetCurrentSubscription(subscriptionData.SubscriptionName, subscriptionsDataFile);
     InitChannelCurrentSubscription();
     using (new OperationContextScope(Channel.ToContextChannel()))
     {
         try
         {
             var subprops = RetryCall(s => Channel.GetSubscription(subscriptionData.SubscriptionId));
             Operation operation = GetOperation();
             var subscriptionDataExtended = new SubscriptionDataExtended(subprops,
                                                                         subscriptionData,
                                                                         CommandRuntime.ToString(),
                                                                         operation);
             WriteSubscription(subscriptionDataExtended);
         }
         catch (CommunicationException ex)
         {
             WriteErrorDetails(ex);
         }
         finally
         {
             if (currentSubscription != null && currentSubscription.Certificate != null && currentSubscription.SubscriptionId != null)
             {
                 this.SetCurrentSubscription(currentSubscription.SubscriptionName, subscriptionsDataFile);
             }
         }
     }
 }
 public static void SetCurrentSubscription(this PSCmdlet cmdlet, SubscriptionData subscription)
 {
     SessionManager.SetVariable(cmdlet, ManagementConstants.CurrentSubscriptionEnvironmentVariable, subscription);
 }
        public static void AssemblyInit(TestContext context)
        {
            vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();
            vmPowershellCmdlets.ImportAzurePublishSettingsFile();
            if (string.IsNullOrEmpty(Resource.DefaultSubscriptionName))
            {
                Console.WriteLine("No subscription is selected!");
            }
            else
            {
                defaultAzureSubscription = vmPowershellCmdlets.SetDefaultAzureSubscription(Resource.DefaultSubscriptionName);
                Assert.AreEqual(Resource.DefaultSubscriptionName, defaultAzureSubscription.SubscriptionName);

                if (defaultAzureSubscription.CurrentStorageAccount == null || Utilities.CheckRemove(vmPowershellCmdlets.GetAzureStorageAccount, defaultAzureSubscription.CurrentStorageAccount))
                {
                    string defaultStorage = Utilities.GetUniqueShortName("storage");
                    vmPowershellCmdlets.NewAzureStorageAccount(defaultStorage, Resource.Location);
                    defaultAzureSubscription = vmPowershellCmdlets.SetAzureSubscription(defaultAzureSubscription.SubscriptionName, defaultStorage);
                }

                storageAccountKey = vmPowershellCmdlets.GetAzureStorageAccountKey(defaultAzureSubscription.CurrentStorageAccount);
                Assert.AreEqual(defaultAzureSubscription.CurrentStorageAccount, storageAccountKey.StorageAccountName);
                blobUrlRoot = (vmPowershellCmdlets.GetAzureStorageAccount(defaultAzureSubscription.CurrentStorageAccount)[0].Endpoints.ToArray())[0];

                locationName = vmPowershellCmdlets.GetAzureLocationName(new[] { Resource.Location }, false); // Get-AzureLocation
                if (String.IsNullOrEmpty(locationName))
                {
                    Console.WriteLine("No location is selected!");
                }
                Console.WriteLine("Location Name: {0}", locationName);

                imageName = vmPowershellCmdlets.GetAzureVMImageName(new[] { "Windows", "testvmimage" }, false); // Get-AzureVMImage
                if (String.IsNullOrEmpty(imageName))
                {
                    Console.WriteLine("No image is selected!");
                }
                Console.WriteLine("Image Name: {0}", imageName);
            }
        }
        /// <summary>
        /// Creates and returns a new instance of the <see cref="ServerDataServiceCertAuth"/> class
        /// which connects to the specified server using the specified subscription credentials.
        /// </summary>
        /// <param name="serverName">The name of the server to connect to</param>
        /// <param name="subscriptionData">The information used to connect and authenticate</param>
        /// <returns>An instance of <see cref="ServerDataServiceCertAuth"/> class.</returns>
        public static ServerDataServiceCertAuth Create(
            string serverName,
            SubscriptionData subscriptionData)
        {
            if (string.IsNullOrEmpty(serverName))
            {
                throw new ArgumentException("serverName");
            }

            if (subscriptionData == null)
            {
                throw new ArgumentNullException("subscriptionData");
            }

            // Create a new ServerDataServiceCertAuth object to be used
            return new ServerDataServiceCertAuth(
                new Uri(subscriptionData.ServiceEndpoint),
                subscriptionData.SubscriptionId,
                serverName,
                subscriptionData.Certificate);
        }
        public void TestSaveSubscriptions()
        {
            for (var i = 0; i < Data.ValidPublishSettings.Count; i++)
            {
                var globalSettingsManager = GlobalSettingsManager.CreateFromPublishSettings(GlobalPathInfo.GlobalSettingsDirectory, null, Data.ValidPublishSettings[i]);

                var subscriptionsManager = SubscriptionsManager.Import(
                    Data.ValidSubscriptionsData[i],
                    globalSettingsManager.PublishSettings,
                    globalSettingsManager.Certificate);

                var newSubscription = new SubscriptionData
                {
                    SubscriptionName = "newsubscription",
                    IsDefault = false,
                    SubscriptionId = "id"
                };

                subscriptionsManager.Subscriptions[newSubscription.SubscriptionName] = newSubscription;
                subscriptionsManager.SaveSubscriptions(Path.Combine(GlobalPathInfo.GlobalSettingsDirectory, "test.xml"));

                var newSubscriptionsManager = SubscriptionsManager.Import(
                    Path.Combine(GlobalPathInfo.GlobalSettingsDirectory, "test.xml"),
                    globalSettingsManager.PublishSettings,
                    globalSettingsManager.Certificate);

                var addedSubscription = newSubscriptionsManager.Subscriptions.Values.Single(
                    subscription => subscription.SubscriptionName == newSubscription.SubscriptionName);

                Assert.AreEqual(newSubscription.SubscriptionId, addedSubscription.SubscriptionId);

                globalSettingsManager.DeleteGlobalSettingsManager();
            }
        }
        public void TestSetup()
        {
            GlobalPathInfo.GlobalSettingsDirectory = Data.AzureSdkAppDir;
            CmdletSubscriptionExtensions.SessionManager = new InMemorySessionManager();

            storageService = new StorageService()
            {
                ServiceName = storageName,
                StorageServiceKeys = new StorageServiceKeys()
                {
                    Primary = "MNao3bm7t7B/x+g2/ssh9HnG0mEh1QV5EHpcna8CetYn+TSRoA8/SBoH6B3Ufwtnz3jZLSw9GEUuCTr3VooBWq==",
                    Secondary = "secondaryKey"
                },
                StorageServiceProperties = new StorageServiceProperties()
                {
                    Endpoints = new EndpointList()
                    {
                        "http://awesome.blob.core.windows.net/",
                        "http://awesome.queue.core.windows.net/",
                        "http://awesome.table.core.windows.net/"
                    }
                }
            };

            deployment = new Deployment()
            {
                DeploymentSlot = DeploymentSlotType.Production,
                Name = "mydeployment",
                PrivateID = "privateId",
                Status = DeploymentStatus.Starting,
                RoleInstanceList = new RoleInstanceList()
                {
                    new RoleInstance()
                    {
                        InstanceStatus = RoleInstanceStatus.ReadyRole,
                        RoleName = "Role1",
                        InstanceName = "Instance_Role1"
                    }
                }
            };

            cloudService = new HostedService()
            {
                ServiceName = serviceName,
                Deployments = new DeploymentList()
            };
            subscription = new SubscriptionData()
            {
                Certificate = It.IsAny<X509Certificate2>(),
                IsDefault = true,
                ServiceEndpoint = "https://www.azure.com",
                SubscriptionId = Guid.NewGuid().ToString(),
                SubscriptionName = Data.Subscription1
            };

            serviceManagementChannelMock = new Mock<IServiceManagement>();
            serviceManagementChannelMock.Setup(f => f.EndGetHostedServiceWithDetails(It.IsAny<IAsyncResult>()))
                .Returns(cloudService);
            serviceManagementChannelMock.Setup(f => f.EndGetStorageService((It.IsAny<IAsyncResult>())))
                .Returns(storageService);
            serviceManagementChannelMock.Setup(f => f.EndGetStorageKeys(It.IsAny<IAsyncResult>()))
                .Returns(storageService);
            serviceManagementChannelMock.Setup(f => f.EndGetDeploymentBySlot(It.IsAny<IAsyncResult>()))
                .Returns(deployment);

            cloudBlobUtilityMock = new Mock<CloudBlobUtility>();
            cloudBlobUtilityMock.Setup(f => f.UploadPackageToBlob(
                serviceManagementChannelMock.Object,
                It.IsAny<string>(),
                It.IsAny<string>(),
                It.IsAny<string>(),
                It.IsAny<BlobRequestOptions>())).Returns(new Uri("http://www.packageurl.azure.com"));

            client = new CloudServiceClient(subscription)
            {
                ServiceManagementChannel = serviceManagementChannelMock.Object,
                CloudBlobUtility = cloudBlobUtilityMock.Object
            };
        }