public void ProcessGetMediaServiceByNameShouldReturnOneMatchingEntry()
        {
            Mock <IMediaServicesClient> clientMock = new Mock <IMediaServicesClient>();


            const string expectedName = "WAMS Account 1";
            MediaServicesAccountGetResponse detail = new MediaServicesAccountGetResponse
            {
                Account = new MediaServicesAccount()
                {
                    AccountName = expectedName
                }
            };

            clientMock.Setup(f => f.GetMediaServiceAsync(detail.Account.AccountName)).Returns(Task.Factory.StartNew(() => detail));

            // Test
            GetAzureMediaServiceCommand getAzureMediaServiceCommand = new GetAzureMediaServiceCommand
            {
                CommandRuntime      = new MockCommandRuntime(),
                MediaServicesClient = clientMock.Object,
                Name = expectedName
            };

            AzureSession.SetCurrentContext(new AzureSubscription {
                Id = new Guid(SubscriptionId)
            }, null, null);
            getAzureMediaServiceCommand.ExecuteCmdlet();
            Assert.Equal(1, ((MockCommandRuntime)getAzureMediaServiceCommand.CommandRuntime).OutputPipeline.Count);
            MediaServiceAccountDetails accounts = (MediaServiceAccountDetails)((MockCommandRuntime)getAzureMediaServiceCommand.CommandRuntime).OutputPipeline.FirstOrDefault();

            Assert.NotNull(accounts);
            Assert.Equal(expectedName, accounts.Name);
        }
        public void TestGetMediaServiceAsync()
        {
            FakeHttpMessageHandler        fakeHttpHandler;
            MediaServicesManagementClient clientWithHandler = CreateMediaManagementClientWithFakeHttpMessageHandler(out fakeHttpHandler);

            const string responseText = @"
            {
            ""AccountName"":""testps"",
            ""AccountKey"":""primarykey"",
            ""AccountKeys"":{""Primary"":""primarykey"",""Secondary"":""secondarykey""},
            ""StorageAccountName"":""psstorage"",
            ""AccountRegion"":""West US""
            }";

            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new FakeHttpContent(responseText)
            };

            fakeHttpHandler.Send = request => response;

            MediaServicesClient target = new MediaServicesClient(null,
                                                                 clientWithHandler,
                                                                 StorageClient.WithHandler(new FakeHttpMessageHandler()));

            MediaServicesAccountGetResponse result = target.GetMediaServiceAsync(AccountName).Result;

            Assert.Equal("primarykey", result.Account.StorageAccountKeys.Primary);
            Assert.Equal("secondarykey", result.Account.StorageAccountKeys.Secondary);
            Assert.Equal("testps", result.Account.AccountName);
            Assert.Equal("psstorage", result.Account.StorageAccountName);
        }
Exemple #3
0
 public MediaServiceAccountDetails(MediaServicesAccountGetResponse response)
 {
     this.AccountKeys        = response.Account.StorageAccountKeys;
     this.Location           = response.Account.AccountRegion;
     this.StorageAccountName = response.Account.StorageAccountName;
     this.Name = response.Account.AccountName;
 }
        public void TestGetMediaServiceAsync()
        {
            FakeHttpMessageHandler        fakeHttpHandler;
            MediaServicesManagementClient clientWithHandler = CreateMediaManagementClientWithFakeHttpMessageHandler(out fakeHttpHandler);

            const string responseText = @"<AccountDetails xmlns='http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
                                    <AccountKey>primarykey</AccountKey><AccountKeys>
                                    <Primary>primarykey</Primary>
                                    <Secondary>secondarykey</Secondary>
                                    </AccountKeys><AccountName>testps</AccountName>
                                    <AccountRegion>West US</AccountRegion>
                                    <StorageAccountName>psstorage</StorageAccountName>
                                </AccountDetails>";

            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new FakeHttpContent(responseText)
            };

            fakeHttpHandler.Send = request => response;

            MediaServicesClient target = new MediaServicesClient(null,
                                                                 clientWithHandler,
                                                                 StorageClient.WithHandler(new FakeHttpMessageHandler()));

            MediaServicesAccountGetResponse result = target.GetMediaServiceAsync(AccountName).Result;

            Assert.AreEqual("primarykey", result.StorageAccountKeys.Primary);
            Assert.AreEqual("secondarykey", result.StorageAccountKeys.Secondary);
            Assert.AreEqual("testps", result.AccountName);
            Assert.AreEqual("psstorage", result.StorageAccountName);
        }
        public void RegenerateMediaServicesAccountTest()
        {
            // Setup
            Mock <IMediaServicesClient> clientMock = new Mock <IMediaServicesClient>();

            const string newKey       = "newkey";
            const string expectedName = "testacc";

            clientMock.Setup(f => f.RegenerateMediaServicesAccountAsync(expectedName, MediaServicesKeyType.Primary)).Returns(
                Task.Factory.StartNew(() => new OperationResponse {
                StatusCode = HttpStatusCode.OK
            }));

            MediaServicesAccountGetResponse detail = new MediaServicesAccountGetResponse
            {
                Account = new MediaServicesAccount {
                    AccountName        = expectedName,
                    StorageAccountKeys = new MediaServicesAccount.AccountKeys
                    {
                        Primary = newKey
                    }
                }
            };

            clientMock.Setup(f => f.GetMediaServiceAsync(expectedName)).Returns(Task.Factory.StartNew(() => detail));

            // Test
            NewAzureMediaServiceKeyCommand command = new NewAzureMediaServiceKeyCommand
            {
                CommandRuntime      = new MockCommandRuntime(),
                Name                = expectedName,
                KeyType             = MediaServicesKeyType.Primary,
                MediaServicesClient = clientMock.Object,
            };

            command.ExecuteCmdlet();
            Assert.Equal(1, ((MockCommandRuntime)command.CommandRuntime).OutputPipeline.Count);
            string key = (string)((MockCommandRuntime)command.CommandRuntime).OutputPipeline.FirstOrDefault();

            Assert.Equal(newKey, key);
        }
        /// <summary>
        /// The Get Media Services Account operation gets detailed information
        /// about a media services account in Windows Azure.  (see
        /// http://msdn.microsoft.com/en-us/library/windowsazure/dn166974.aspx
        /// for more information)
        /// </summary>
        /// <param name='accountName'>
        /// The name of the Media Services account.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The Get Media Services Account operation response.
        /// </returns>
        public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.MediaServices.Models.MediaServicesAccountGetResponse> GetAsync(string accountName, CancellationToken cancellationToken)
        {
            // Validate
            if (accountName == null)
            {
                throw new ArgumentNullException("accountName");
            }

            // Tracing
            bool   shouldTrace  = CloudContext.Configuration.Tracing.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = Tracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("accountName", accountName);
                Tracing.Enter(invocationId, this, "GetAsync", tracingParameters);
            }

            // Construct URL
            string url = new Uri(this.Client.BaseUri, "/").ToString() + this.Client.Credentials.SubscriptionId + "/services/mediaservices/Accounts/" + accountName;

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2011-10-01");

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        Tracing.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        Tracing.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false), CloudExceptionType.Json);
                        if (shouldTrace)
                        {
                            Tracing.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    MediaServicesAccountGetResponse result = null;
                    // Deserialize Response
                    cancellationToken.ThrowIfCancellationRequested();
                    string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                    result = new MediaServicesAccountGetResponse();
                    JToken responseDoc = JToken.Parse(responseContent);

                    if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                    {
                        MediaServicesAccount accountInstance = new MediaServicesAccount();
                        result.Account = accountInstance;

                        JToken accountNameValue = responseDoc["AccountName"];
                        if (accountNameValue != null && accountNameValue.Type != JTokenType.Null)
                        {
                            string accountNameInstance = (string)accountNameValue;
                            accountInstance.AccountName = accountNameInstance;
                        }

                        JToken accountKeyValue = responseDoc["AccountKey"];
                        if (accountKeyValue != null && accountKeyValue.Type != JTokenType.Null)
                        {
                            string accountKeyInstance = (string)accountKeyValue;
                            accountInstance.AccountKey = accountKeyInstance;
                        }

                        JToken accountKeysValue = responseDoc["AccountKeys"];
                        if (accountKeysValue != null && accountKeysValue.Type != JTokenType.Null)
                        {
                            MediaServicesAccount.AccountKeys accountKeysInstance = new MediaServicesAccount.AccountKeys();
                            accountInstance.StorageAccountKeys = accountKeysInstance;

                            JToken primaryValue = accountKeysValue["Primary"];
                            if (primaryValue != null && primaryValue.Type != JTokenType.Null)
                            {
                                string primaryInstance = (string)primaryValue;
                                accountKeysInstance.Primary = primaryInstance;
                            }

                            JToken secondaryValue = accountKeysValue["Secondary"];
                            if (secondaryValue != null && secondaryValue.Type != JTokenType.Null)
                            {
                                string secondaryInstance = (string)secondaryValue;
                                accountKeysInstance.Secondary = secondaryInstance;
                            }
                        }

                        JToken accountRegionValue = responseDoc["AccountRegion"];
                        if (accountRegionValue != null && accountRegionValue.Type != JTokenType.Null)
                        {
                            string accountRegionInstance = (string)accountRegionValue;
                            accountInstance.AccountRegion = accountRegionInstance;
                        }

                        JToken storageAccountNameValue = responseDoc["StorageAccountName"];
                        if (storageAccountNameValue != null && storageAccountNameValue.Type != JTokenType.Null)
                        {
                            string storageAccountNameInstance = (string)storageAccountNameValue;
                            accountInstance.StorageAccountName = storageAccountNameInstance;
                        }
                    }

                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        Tracing.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
Exemple #7
0
        /// <summary>
        /// The Get Media Services Account operation gets detailed information
        /// about a media services account in Windows Azure.  (see
        /// http://msdn.microsoft.com/en-us/library/windowsazure/dn166974.aspx
        /// for more information)
        /// </summary>
        /// <param name='accountName'>
        /// Required. The name of the Media Services account.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The Get Media Services Account operation response.
        /// </returns>
        public async Task <MediaServicesAccountGetResponse> GetAsync(string accountName, CancellationToken cancellationToken)
        {
            // Validate
            if (accountName == null)
            {
                throw new ArgumentNullException("accountName");
            }

            // Tracing
            bool   shouldTrace  = TracingAdapter.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("accountName", accountName);
                TracingAdapter.Enter(invocationId, this, "GetAsync", tracingParameters);
            }

            // Construct URL
            string url = "";

            url = url + "/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/services/mediaservices/Accounts/";
            url = url + Uri.EscapeDataString(accountName);
            string baseUrl = this.Client.BaseUri.AbsoluteUri;

            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2011-10-01");

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    MediaServicesAccountGetResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new MediaServicesAccountGetResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            MediaServicesAccount accountInstance = new MediaServicesAccount();
                            result.Account = accountInstance;

                            JToken accountNameValue = responseDoc["AccountName"];
                            if (accountNameValue != null && accountNameValue.Type != JTokenType.Null)
                            {
                                string accountNameInstance = ((string)accountNameValue);
                                accountInstance.AccountName = accountNameInstance;
                            }

                            JToken accountKeyValue = responseDoc["AccountKey"];
                            if (accountKeyValue != null && accountKeyValue.Type != JTokenType.Null)
                            {
                                string accountKeyInstance = ((string)accountKeyValue);
                                accountInstance.AccountKey = accountKeyInstance;
                            }

                            JToken accountKeysValue = responseDoc["AccountKeys"];
                            if (accountKeysValue != null && accountKeysValue.Type != JTokenType.Null)
                            {
                                MediaServicesAccount.AccountKeys accountKeysInstance = new MediaServicesAccount.AccountKeys();
                                accountInstance.StorageAccountKeys = accountKeysInstance;

                                JToken primaryValue = accountKeysValue["Primary"];
                                if (primaryValue != null && primaryValue.Type != JTokenType.Null)
                                {
                                    string primaryInstance = ((string)primaryValue);
                                    accountKeysInstance.Primary = primaryInstance;
                                }

                                JToken secondaryValue = accountKeysValue["Secondary"];
                                if (secondaryValue != null && secondaryValue.Type != JTokenType.Null)
                                {
                                    string secondaryInstance = ((string)secondaryValue);
                                    accountKeysInstance.Secondary = secondaryInstance;
                                }
                            }

                            JToken accountRegionValue = responseDoc["AccountRegion"];
                            if (accountRegionValue != null && accountRegionValue.Type != JTokenType.Null)
                            {
                                string accountRegionInstance = ((string)accountRegionValue);
                                accountInstance.AccountRegion = accountRegionInstance;
                            }

                            JToken storageAccountNameValue = responseDoc["StorageAccountName"];
                            if (storageAccountNameValue != null && storageAccountNameValue.Type != JTokenType.Null)
                            {
                                string storageAccountNameInstance = ((string)storageAccountNameValue);
                                accountInstance.StorageAccountName = storageAccountNameInstance;
                            }
                        }
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }