/// <summary> /// The Create Service principal operation adds a new service principal /// in the subscription. /// </summary> /// <param name='operations'> /// Reference to the /// Microsoft.WindowsAzure.Management.ISubscriptionServicePrincipalOperations. /// </param> /// <param name='parameters'> /// Required. Parameters supplied to the Create Service Principal /// operation. /// </param> /// <returns> /// A standard service response including an HTTP status code and /// request ID. /// </returns> public static Task <AzureOperationResponse> CreateAsync(this ISubscriptionServicePrincipalOperations operations, SubscriptionServicePrincipalCreateParameters parameters) { return(operations.CreateAsync(parameters, CancellationToken.None)); }
/// <summary> /// The Create Service principal operation adds a new service principal /// in the subscription. /// </summary> /// <param name='parameters'> /// Required. Parameters supplied to the Create Service Principal /// operation. /// </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(SubscriptionServicePrincipalCreateParameters parameters, CancellationToken cancellationToken) { // Validate if (parameters == null) { throw new ArgumentNullException("parameters"); } if (parameters.Description != null && parameters.Description.Length > 256) { throw new ArgumentOutOfRangeException("parameters.Description"); } if (parameters.ServicePrincipalId == null) { throw new ArgumentNullException("parameters.ServicePrincipalId"); } if (parameters.ServicePrincipalId.Length > 256) { throw new ArgumentOutOfRangeException("parameters.ServicePrincipalId"); } // Tracing bool shouldTrace = TracingAdapter.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = TracingAdapter.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("parameters", parameters); TracingAdapter.Enter(invocationId, this, "CreateAsync", tracingParameters); } // Construct URL string url = ""; url = url + "/"; if (this.Client.Credentials.SubscriptionId != null) { url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId); } url = url + "/servicePrincipals"; 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.Post; httpRequest.RequestUri = new Uri(url); // Set Headers httpRequest.Headers.Add("x-ms-version", "2018-06-01"); // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Serialize Request string requestContent = null; XDocument requestDoc = new XDocument(); XElement servicePrincipalElement = new XElement(XName.Get("ServicePrincipal", "http://schemas.microsoft.com/windowsazure")); requestDoc.Add(servicePrincipalElement); XElement servicePrincipalIdElement = new XElement(XName.Get("ServicePrincipalId", "http://schemas.microsoft.com/windowsazure")); servicePrincipalIdElement.Value = parameters.ServicePrincipalId; servicePrincipalElement.Add(servicePrincipalIdElement); if (parameters.Description != null) { XElement descriptionElement = new XElement(XName.Get("Description", "http://schemas.microsoft.com/windowsazure")); descriptionElement.Value = parameters.Description; servicePrincipalElement.Add(descriptionElement); } if (parameters.ExtendedProperties != null) { if (parameters.ExtendedProperties is ILazyCollection == false || ((ILazyCollection)parameters.ExtendedProperties).IsInitialized) { XElement extendedPropertiesDictionaryElement = new XElement(XName.Get("ExtendedProperties", "http://schemas.microsoft.com/windowsazure")); foreach (KeyValuePair <string, string> pair in parameters.ExtendedProperties) { string extendedPropertiesKey = pair.Key; string extendedPropertiesValue = pair.Value; XElement extendedPropertiesElement = new XElement(XName.Get("ExtendedProperty", "http://schemas.microsoft.com/windowsazure")); extendedPropertiesDictionaryElement.Add(extendedPropertiesElement); XElement extendedPropertiesKeyElement = new XElement(XName.Get("Name", "http://schemas.microsoft.com/windowsazure")); extendedPropertiesKeyElement.Value = extendedPropertiesKey; extendedPropertiesElement.Add(extendedPropertiesKeyElement); XElement extendedPropertiesValueElement = new XElement(XName.Get("Value", "http://schemas.microsoft.com/windowsazure")); extendedPropertiesValueElement.Value = extendedPropertiesValue; extendedPropertiesElement.Add(extendedPropertiesValueElement); } servicePrincipalElement.Add(extendedPropertiesDictionaryElement); } } requestContent = requestDoc.ToString(); httpRequest.Content = new StringContent(requestContent, Encoding.UTF8); httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/xml"); // 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, 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(); } } }
/// <summary> /// The Create Service principal operation adds a new service principal /// in the subscription. /// </summary> /// <param name='operations'> /// Reference to the /// Microsoft.WindowsAzure.Management.ISubscriptionServicePrincipalOperations. /// </param> /// <param name='parameters'> /// Required. Parameters supplied to the Create Service Principal /// operation. /// </param> /// <returns> /// A standard service response including an HTTP status code and /// request ID. /// </returns> public static AzureOperationResponse Create(this ISubscriptionServicePrincipalOperations operations, SubscriptionServicePrincipalCreateParameters parameters) { return(Task.Factory.StartNew((object s) => { return ((ISubscriptionServicePrincipalOperations)s).CreateAsync(parameters); } , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult()); }