private Task <StorageServiceGetResponse> CreateGetResponse(string serviceName)
        {
            Task <StorageServiceGetResponse> resultTask;
            var data = accounts.FirstOrDefault(a => a.Name == serviceName);

            if (data != null)
            {
                var storageServiceGetResponse = new StorageServiceGetResponse
                {
                    ServiceName = data.Name,
                    Properties  = new StorageServiceProperties
                    {
                        Endpoints =
                        {
                            new Uri(data.BlobEndpoint),
                            new Uri(data.QueueEndpoint),
                            new Uri(data.TableEndpoint)
                        }
                    }
                };
                resultTask = Tasks.FromResult(storageServiceGetResponse);
            }
            else
            {
                resultTask = Tasks.FromException <StorageServiceGetResponse>(ClientMocks.Make404Exception());
            }
            return(resultTask);
        }
Beispiel #2
0
        public override void ExecuteCmdlet()
        {
            MediaServicesClient = MediaServicesClient ?? new MediaServicesClient(CurrentSubscription, WriteDebug);

            StorageAccountGetKeysResponse storageKeysResponse = null;
            Uri    storageEndPoint   = null;
            string storageAccountKey = null;

            CatchAggregatedExceptionFlattenAndRethrow(() => { storageKeysResponse = MediaServicesClient.GetStorageServiceKeysAsync(StorageAccountName).Result; });
            storageAccountKey = storageKeysResponse.PrimaryKey;

            StorageServiceGetResponse storageGetResponse = null;

            CatchAggregatedExceptionFlattenAndRethrow(() => { storageGetResponse = MediaServicesClient.GetStorageServicePropertiesAsync(StorageAccountName).Result; });

            if (storageGetResponse.Properties != null && storageGetResponse.Properties.Endpoints.Count > 0)
            {
                storageEndPoint = storageGetResponse.Properties.Endpoints[0];
            }
            else
            {
                throw new Exception(string.Format(Resources.EndPointNotFoundForBlobStorage, Name));
            }

            AccountCreationResult result = null;
            var request = new MediaServicesAccountCreateParameters()
            {
                AccountName            = Name,
                BlobStorageEndpointUri = storageEndPoint,
                Region             = Location,
                StorageAccountKey  = storageAccountKey,
                StorageAccountName = StorageAccountName
            };

            CatchAggregatedExceptionFlattenAndRethrow(() => { result = new AccountCreationResult(MediaServicesClient.CreateNewAzureMediaServiceAsync(request).Result); });
            WriteObject(result, false);
        }
        public void NewMediaServiceAccountShouldPassWithValidParameters()
        {
            // Setup
            Mock <IMediaServicesClient> clientMock = new Mock <IMediaServicesClient>();

            const string storageAccountName     = "teststorage";
            const string storageAccountKey      = "key";
            const string accountName            = "testaccount";
            const string region                 = "West US";
            const string blobStorageEndpointUri = "http://awesome.blob.core.windows.net/";

            MediaServicesAccountCreateParameters request = new MediaServicesAccountCreateParameters
            {
                AccountName            = accountName,
                BlobStorageEndpointUri = new Uri(blobStorageEndpointUri),
                Region             = region,
                StorageAccountKey  = storageAccountKey,
                StorageAccountName = storageAccountName
            };

            clientMock.Setup(f => f.CreateNewAzureMediaServiceAsync(It.Is <MediaServicesAccountCreateParameters>(creationRequest => request.AccountName == accountName))).Returns(
                Task.Factory.StartNew(() => new MediaServicesAccountCreateResponse
            {
                AccountId      = Guid.NewGuid().ToString(),
                AccountName    = request.AccountName,
                SubscriptionId = Guid.NewGuid().ToString()
            }));


            clientMock.Setup(f => f.GetStorageServiceKeysAsync(storageAccountName)).Returns(
                Task.Factory.StartNew(() => new StorageAccountGetKeysResponse
            {
                PrimaryKey   = storageAccountKey,
                SecondaryKey = storageAccountKey
            }));


            clientMock.Setup(f => f.GetStorageServicePropertiesAsync(storageAccountName)).Returns(Task.Factory.StartNew(() =>
            {
                StorageServiceGetResponse response = new StorageServiceGetResponse
                {
                    Properties = new StorageServiceProperties()
                };
                response.Properties.Endpoints.Add(new Uri(blobStorageEndpointUri));
                return(response);
            }));

            // Test
            NewAzureMediaServiceCommand command = new NewAzureMediaServiceCommand
            {
                CommandRuntime      = new MockCommandRuntime(),
                Name                = accountName,
                Location            = region,
                StorageAccountName  = storageAccountName,
                MediaServicesClient = clientMock.Object,
            };

            command.ExecuteCmdlet();
            Assert.AreEqual(1, ((MockCommandRuntime)command.CommandRuntime).OutputPipeline.Count);
            AccountCreationResult accountCreationResult = (AccountCreationResult)((MockCommandRuntime)command.CommandRuntime).OutputPipeline.FirstOrDefault();

            Assert.IsNotNull(accountCreationResult);
            Assert.AreEqual(accountName, accountCreationResult.Name);
        }
 private Task<StorageServiceGetResponse> CreateGetResponse(string serviceName)
 {
     Task<StorageServiceGetResponse> resultTask;
     var data = accounts.FirstOrDefault(a => a.Name == serviceName);
     if (data != null)
     {
         var storageServiceGetResponse = new StorageServiceGetResponse
         {
             ServiceName = data.Name,
             Properties = new StorageServiceProperties
             {
                 Endpoints =
                 {
                     new Uri(data.BlobEndpoint),
                     new Uri(data.QueueEndpoint),
                     new Uri(data.TableEndpoint)
                 }
             }
         };
         resultTask = Tasks.FromResult(storageServiceGetResponse);
     }
     else
     {
         resultTask = Tasks.FromException<StorageServiceGetResponse>(ClientMocks.Make404Exception());
     }
     return resultTask;
 }