コード例 #1
0
 /// <summary>
 /// Creates new or update existing identity provider configuration.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.ApiManagement.IIdentityProviderOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group.
 /// </param>
 /// <param name='serviceName'>
 /// Required. The name of the Api Management service.
 /// </param>
 /// <param name='identityProviderName'>
 /// Required. Identifier of the identity provider.
 /// </param>
 /// <param name='parameters'>
 /// Required. Create parameters.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static Task <AzureOperationResponse> CreateAsync(this IIdentityProviderOperations operations, string resourceGroupName, string serviceName, string identityProviderName, IdentityProviderCreateParameters parameters)
 {
     return(operations.CreateAsync(resourceGroupName, serviceName, identityProviderName, parameters, CancellationToken.None));
 }
        /// <summary>
        /// Creates new or update existing identity provider configuration.
        /// </summary>
        /// <param name='resourceGroupName'>
        /// Required. The name of the resource group.
        /// </param>
        /// <param name='serviceName'>
        /// Required. The name of the Api Management service.
        /// </param>
        /// <param name='identityProviderName'>
        /// Required. Identifier of the identity provider.
        /// </param>
        /// <param name='parameters'>
        /// Required. Create parameters.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// A standard service response including an HTTP status code and
        /// request ID.
        /// </returns>
        public async Task <AzureOperationResponse> CreateAsync(string resourceGroupName, string serviceName, string identityProviderName, IdentityProviderCreateParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (serviceName == null)
            {
                throw new ArgumentNullException("serviceName");
            }
            if (identityProviderName == null)
            {
                throw new ArgumentNullException("identityProviderName");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            if (parameters.ClientId == null)
            {
                throw new ArgumentNullException("parameters.ClientId");
            }
            if (parameters.ClientSecret == null)
            {
                throw new ArgumentNullException("parameters.ClientSecret");
            }

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

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("resourceGroupName", resourceGroupName);
                tracingParameters.Add("serviceName", serviceName);
                tracingParameters.Add("identityProviderName", identityProviderName);
                tracingParameters.Add("parameters", parameters);
                TracingAdapter.Enter(invocationId, this, "CreateAsync", tracingParameters);
            }

            // Construct URL
            string url = "";

            url = url + "/subscriptions/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/resourceGroups/";
            url = url + Uri.EscapeDataString(resourceGroupName);
            url = url + "/providers/";
            url = url + "Microsoft.ApiManagement";
            url = url + "/service/";
            url = url + Uri.EscapeDataString(serviceName);
            url = url + "/identityProviders/";
            url = url + Uri.EscapeDataString(identityProviderName);
            List <string> queryParameters = new List <string>();

            queryParameters.Add("api-version=2016-10-10");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            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.Put;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers

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

                // Serialize Request
                string requestContent = null;
                JToken requestDoc     = null;

                JObject identityProviderCreateParametersValue = new JObject();
                requestDoc = identityProviderCreateParametersValue;

                identityProviderCreateParametersValue["clientId"] = parameters.ClientId;

                identityProviderCreateParametersValue["clientSecret"] = parameters.ClientSecret;

                if (parameters.AllowedTenants != null)
                {
                    if (parameters.AllowedTenants is ILazyCollection == false || ((ILazyCollection)parameters.AllowedTenants).IsInitialized)
                    {
                        JArray allowedTenantsArray = new JArray();
                        foreach (string allowedTenantsItem in parameters.AllowedTenants)
                        {
                            allowedTenantsArray.Add(allowedTenantsItem);
                        }
                        identityProviderCreateParametersValue["allowedTenants"] = allowedTenantsArray;
                    }
                }

                requestContent      = requestDoc.ToString(Newtonsoft.Json.Formatting.Indented);
                httpRequest.Content = new StringContent(requestContent, Encoding.UTF8);
                httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

                // 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.Created && statusCode != HttpStatusCode.NoContent)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    AzureOperationResponse result = null;
                    // Deserialize Response
                    result            = new AzureOperationResponse();
                    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();
                }
            }
        }
コード例 #3
0
 /// <summary>
 /// Creates new or update existing identity provider configuration.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.ApiManagement.IIdentityProviderOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group.
 /// </param>
 /// <param name='serviceName'>
 /// Required. The name of the Api Management service.
 /// </param>
 /// <param name='identityProviderName'>
 /// Required. Identifier of the identity provider.
 /// </param>
 /// <param name='parameters'>
 /// Required. Create parameters.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static AzureOperationResponse Create(this IIdentityProviderOperations operations, string resourceGroupName, string serviceName, string identityProviderName, IdentityProviderCreateParameters parameters)
 {
     return(Task.Factory.StartNew((object s) =>
     {
         return ((IIdentityProviderOperations)s).CreateAsync(resourceGroupName, serviceName, identityProviderName, parameters);
     }
                                  , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }
        public void IdentityProviderCreateListUpdateDelete()
        {
            TestUtilities.StartTest("SmapiFunctionalTests", "IdentityProviderCreateListUpdateDelete");

            try
            {
                // create facebook external identity provider
                string clientId = TestUtilities.GenerateName("clientId");
                string clientSecret = TestUtilities.GenerateName("clientSecret");

                var identityProviderCreateParameters = new IdentityProviderCreateParameters(clientId, clientSecret);

                var createResponse = ApiManagementClient.IdentityProvider.Create(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    IdentityProviderTypeContract.Facebook.ToString("g"),
                    identityProviderCreateParameters);

                Assert.NotNull(createResponse);
                Assert.Equal(HttpStatusCode.Created, createResponse.StatusCode);

                // get to check it was created
                var getResponse = ApiManagementClient.IdentityProvider.Get(ResourceGroupName, ApiManagementServiceName, IdentityProviderTypeContract.Facebook.ToString("g"));

                Assert.NotNull(getResponse);
                Assert.NotNull(getResponse.Value);
                Assert.NotNull(getResponse.Value.ClientId);
                Assert.NotNull(getResponse.Value.ClientSecret);
                Assert.Equal(IdentityProviderTypeContract.Facebook, getResponse.Value.Type);

                var listIdentityProviders = ApiManagementClient.IdentityProvider.List(ResourceGroupName, ApiManagementServiceName, null);

                Assert.NotNull(listIdentityProviders);
                Assert.NotNull(listIdentityProviders.Result);

                // there should be one identity Provider
                Assert.True(listIdentityProviders.Result.Count >= 1);

                // patch identity provider
                string patchedSecret = TestUtilities.GenerateName("clientSecret");
                var patchResponse = ApiManagementClient.IdentityProvider.Update(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    IdentityProviderTypeContract.Facebook.ToString("g"),
                    new IdentityProviderUpdateParameters
                    {
                        ClientSecret = patchedSecret
                    }, 
                    getResponse.ETag);

                Assert.NotNull(patchResponse);

                // get to check it was patched
                getResponse = ApiManagementClient.IdentityProvider.Get(ResourceGroupName, ApiManagementServiceName, IdentityProviderTypeContract.Facebook.ToString("g"));

                Assert.NotNull(getResponse);
                Assert.NotNull(getResponse.Value);
                Assert.Equal(IdentityProviderTypeContract.Facebook, getResponse.Value.Type);
                Assert.Equal(patchedSecret, getResponse.Value.ClientSecret);

                // delete the identity provider
                var deleteResponse = ApiManagementClient.IdentityProvider.Delete(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    IdentityProviderTypeContract.Facebook.ToString("g"),
                    getResponse.ETag);

                Assert.NotNull(deleteResponse);
                Assert.Equal(HttpStatusCode.NoContent, deleteResponse.StatusCode);

                // get the deleted identity provider to make sure it was deleted
                try
                {
                    ApiManagementClient.IdentityProvider.Get(ResourceGroupName, ApiManagementServiceName, IdentityProviderTypeContract.Facebook.ToString("g"));
                    throw new Exception("This code should not have been executed.");
                }
                catch (CloudException ex)
                {
                    Assert.Equal(HttpStatusCode.NotFound, ex.Response.StatusCode);
                }
            }
            finally
            {
                TestUtilities.EndTest();
            }
        }