public void LinkedServiceCreateFailsWithoutWorkspace() { BasicDelegatingHandler handler = new BasicDelegatingHandler(); using (var undoContext = UndoContext.Current) { undoContext.Start(); var client = TestHelper.GetOperationalInsightsManagementClient(handler); var subId = client.Credentials.SubscriptionId; string linkedServiceName = "Automation"; string workspaceName = TestUtilities.GenerateName("AzSDKTest"); var accountResourceIdFromat = "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Automation/automationAccounts/{2}"; var accountResourceId = string.Format(accountResourceIdFromat, subId, resourceGroupName, "testAccount"); var createParameters = new LinkedServiceCreateOrUpdateParameters { Name = linkedServiceName, Properties = new LinkedServiceProperties() { ResourceId = accountResourceId } }; // Create a linked service on a non-existent workspace TestHelper.VerifyCloudException(HttpStatusCode.NotFound, () => client.LinkedServices.CreateOrUpdate(resourceGroupName, workspaceName, createParameters)); } }
/// <summary> /// Validates a linked service matches the expected properties. Throws assertion exceptions if validation fails. /// </summary> /// <param name="expected">Expected linked service</param> /// <param name="actual">Actual linked service</param> internal static void ValidateLinkedService(LinkedServiceCreateOrUpdateParameters expected, LinkedService actual) { Assert.NotNull(actual); Assert.NotNull(actual.Id); Assert.Equal(LinkedServiceResourceType.ToLower(), actual.Type.ToLower()); Assert.NotNull(actual.Properties); Assert.Equal(expected.Properties.ResourceId.ToLower(), actual.Properties.ResourceId.ToLower()); }
/// <summary> /// Create or update a data factory linkedService. /// </summary> /// <param name='operations'> /// Reference to the /// Microsoft.Azure.Management.DataFactories.ILinkedServiceOperations. /// </param> /// <param name='resourceGroupName'> /// Required. The resource group name of the data factory. /// </param> /// <param name='dataFactoryName'> /// Required. The name of the data factory. /// </param> /// <param name='parameters'> /// Required. The parameters required to create or update a data /// factory linkedService. /// </param> /// <returns> /// The create or update data factory linkedService operation response. /// </returns> public static Task <LinkedServiceCreateOrUpdateResponse> BeginCreateOrUpdateAsync( this ILinkedServiceOperations operations, string resourceGroupName, string dataFactoryName, LinkedServiceCreateOrUpdateParameters parameters) { return(operations.BeginCreateOrUpdateAsync( resourceGroupName, dataFactoryName, parameters, CancellationToken.None)); }
private Core.Models.LinkedServiceCreateOrUpdateParameters ValidateAndConvert(LinkedServiceCreateOrUpdateParameters parameters) { // Validate Ensure.IsNotNull(parameters, "parameters"); Ensure.IsNotNull(parameters.LinkedService, "parameters.LinkedService"); this.ValidateObject(parameters.LinkedService); // Convert Core.Models.LinkedService internalLinkedService = this.Converter.ToCoreType(parameters.LinkedService); return(new Core.Models.LinkedServiceCreateOrUpdateParameters() { LinkedService = internalLinkedService }); }
/// <summary> /// Serializes the given LinkedService into JSON, by mocking a create request to /// exercise the client's serialization logic. /// </summary> /// <param name="item"></param> /// <returns></returns> public static string SerializeLinkedServiceToJson(LinkedService item) { var createParams = new LinkedServiceCreateOrUpdateParameters() { LinkedService = item }; var handler = new MockResourceProviderDelegatingHandler(); var client = DataPipelineManagementClient.GetFakeClient(handler); string resourceGroupName = Guid.NewGuid().ToString("D"); string dataFactoryName = Guid.NewGuid().ToString("D"); client.LinkedServices.BeginCreateOrUpdate(resourceGroupName, dataFactoryName, createParams); return(handler.Json); }
/// <summary> /// Create or update a data factory linkedService. /// </summary> /// <param name='operations'> /// Reference to the /// Microsoft.Azure.Management.DataFactories.ILinkedServiceOperations. /// </param> /// <param name='resourceGroupName'> /// Required. The resource group name of the data factory. /// </param> /// <param name='dataFactoryName'> /// Required. The name of the data factory. /// </param> /// <param name='parameters'> /// Required. The parameters required to create or update a data /// factory linkedService. /// </param> /// <returns> /// The create or update data factory linkedService operation response. /// </returns> public static LinkedServiceCreateOrUpdateResponse BeginCreateOrUpdate( this ILinkedServiceOperations operations, string resourceGroupName, string dataFactoryName, LinkedServiceCreateOrUpdateParameters parameters) { return(Task.Factory.StartNew( s => ((ILinkedServiceOperations)s).BeginCreateOrUpdateAsync( resourceGroupName, dataFactoryName, parameters), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult()); }
public async Task <LinkedServiceCreateOrUpdateResponse> BeginCreateOrUpdateAsync( string resourceGroupName, string dataFactoryName, LinkedServiceCreateOrUpdateParameters parameters, CancellationToken cancellationToken) { Core.Models.LinkedServiceCreateOrUpdateParameters internalParameters = this.ValidateAndConvert(parameters); Core.Models.LinkedServiceCreateOrUpdateResponse response = await this.Client.InternalClient.LinkedServices.BeginCreateOrUpdateAsync( resourceGroupName, dataFactoryName, internalParameters, cancellationToken); return(new LinkedServiceCreateOrUpdateResponse(response, this.Client)); }
/// <summary> /// Create or update a linked service. /// </summary> /// <param name='operations'> /// Reference to the /// Microsoft.Azure.Management.OperationalInsights.ILinkedServiceOperations. /// </param> /// <param name='resourceGroupName'> /// Required. The resource group name of the linked service. /// </param> /// <param name='workspaceName'> /// Required. The name of the parent workspace that will contain the /// linked service /// </param> /// <param name='parameters'> /// Required. The parameters required to create or update a linked /// service. /// </param> /// <returns> /// The create or update linked service operation response. /// </returns> public static Task <LinkedServiceCreateOrUpdateResponse> CreateOrUpdateAsync(this ILinkedServiceOperations operations, string resourceGroupName, string workspaceName, LinkedServiceCreateOrUpdateParameters parameters) { return(operations.CreateOrUpdateAsync(resourceGroupName, workspaceName, parameters, CancellationToken.None)); }
/// <summary> /// Create or update a linked service. /// </summary> /// <param name='resourceGroupName'> /// Required. The resource group name of the linked service. /// </param> /// <param name='workspaceName'> /// Required. The name of the parent workspace that will contain the /// linked service /// </param> /// <param name='parameters'> /// Required. The parameters required to create or update a linked /// service. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// The create or update linked service operation response. /// </returns> public async Task <LinkedServiceCreateOrUpdateResponse> CreateOrUpdateAsync(string resourceGroupName, string workspaceName, LinkedServiceCreateOrUpdateParameters parameters, CancellationToken cancellationToken) { // Validate if (resourceGroupName == null) { throw new ArgumentNullException("resourceGroupName"); } if (workspaceName == null) { throw new ArgumentNullException("workspaceName"); } if (parameters == null) { throw new ArgumentNullException("parameters"); } if (parameters.Name == null) { throw new ArgumentNullException("parameters.Name"); } if (parameters.Properties == null) { throw new ArgumentNullException("parameters.Properties"); } if (parameters.Properties.ResourceId == null) { throw new ArgumentNullException("parameters.Properties.ResourceId"); } // 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("workspaceName", workspaceName); tracingParameters.Add("parameters", parameters); TracingAdapter.Enter(invocationId, this, "CreateOrUpdateAsync", 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/Microsoft.OperationalInsights/workspaces/"; url = url + Uri.EscapeDataString(workspaceName); url = url + "/linkedServices/"; url = url + Uri.EscapeDataString(parameters.Name); List <string> queryParameters = new List <string>(); queryParameters.Add("api-version=2015-11-01-preview"); 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 httpRequest.Headers.Add("x-ms-client-request-id", Guid.NewGuid().ToString()); // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Serialize Request string requestContent = null; JToken requestDoc = null; JObject linkedServiceCreateOrUpdateParametersValue = new JObject(); requestDoc = linkedServiceCreateOrUpdateParametersValue; linkedServiceCreateOrUpdateParametersValue["name"] = parameters.Name; JObject propertiesValue = new JObject(); linkedServiceCreateOrUpdateParametersValue["properties"] = propertiesValue; propertiesValue["resourceId"] = parameters.Properties.ResourceId; 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.OK && statusCode != HttpStatusCode.Created) { 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 LinkedServiceCreateOrUpdateResponse result = null; // Deserialize Response if (statusCode == HttpStatusCode.OK || statusCode == HttpStatusCode.Created) { cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new LinkedServiceCreateOrUpdateResponse(); JToken responseDoc = null; if (string.IsNullOrEmpty(responseContent) == false) { responseDoc = JToken.Parse(responseContent); } if (responseDoc != null && responseDoc.Type != JTokenType.Null) { LinkedService linkedServiceInstance = new LinkedService(); result.LinkedService = linkedServiceInstance; JToken idValue = responseDoc["id"]; if (idValue != null && idValue.Type != JTokenType.Null) { string idInstance = ((string)idValue); linkedServiceInstance.Id = idInstance; } JToken nameValue = responseDoc["name"]; if (nameValue != null && nameValue.Type != JTokenType.Null) { string nameInstance = ((string)nameValue); linkedServiceInstance.Name = nameInstance; } JToken typeValue = responseDoc["type"]; if (typeValue != null && typeValue.Type != JTokenType.Null) { string typeInstance = ((string)typeValue); linkedServiceInstance.Type = typeInstance; } JToken propertiesValue2 = responseDoc["properties"]; if (propertiesValue2 != null && propertiesValue2.Type != JTokenType.Null) { LinkedServiceProperties propertiesInstance = new LinkedServiceProperties(); linkedServiceInstance.Properties = propertiesInstance; JToken resourceIdValue = propertiesValue2["resourceId"]; if (resourceIdValue != null && resourceIdValue.Type != JTokenType.Null) { string resourceIdInstance = ((string)resourceIdValue); propertiesInstance.ResourceId = resourceIdInstance; } } } } 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(); } } }
public void CanCreateUpdateDeleteLinkedService() { BasicDelegatingHandler handler = new BasicDelegatingHandler(); using (var undoContext = UndoContext.Current) { undoContext.Start(); var client = TestHelper.GetOperationalInsightsManagementClient(handler); var subId = client.Credentials.SubscriptionId; var linkedServiceName = "Automation"; var accountResourceIdFromat = "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Automation/automationAccounts/{2}"; var accountResourceId = string.Format(accountResourceIdFromat, subId, resourceGroupName, automationAccountName); // Create a linked service var createParameters = new LinkedServiceCreateOrUpdateParameters { Name = linkedServiceName, Properties = new LinkedServiceProperties() { ResourceId = accountResourceId } }; var createResponse = client.LinkedServices.CreateOrUpdate(resourceGroupName, workspaceName, createParameters); Assert.True(HttpStatusCode.Created == createResponse.StatusCode || HttpStatusCode.OK == createResponse.StatusCode); TestHelper.ValidateLinkedService(createParameters, createResponse.LinkedService); // Get the linked service var getResponse = client.LinkedServices.Get(resourceGroupName, workspaceName, linkedServiceName); Assert.Equal(HttpStatusCode.OK, getResponse.StatusCode); TestHelper.ValidateLinkedService(createParameters, getResponse.LinkedService); // List the linked services in the workspace var listResponse = client.LinkedServices.List(resourceGroupName, workspaceName); Assert.True(HttpStatusCode.OK == listResponse.StatusCode); Assert.Equal(1, listResponse.LinkedServices.Count); Assert.Null(listResponse.NextLink); Assert.Single(listResponse.LinkedServices.Where(w => w.Properties.ResourceId.Equals(accountResourceId, StringComparison.OrdinalIgnoreCase))); var accountResourceId2 = string.Format(accountResourceIdFromat, subId, resourceGroupName, automationAccountName2); var updateParameters = new LinkedServiceCreateOrUpdateParameters { Name = linkedServiceName, Properties = new LinkedServiceProperties() { ResourceId = accountResourceId2 } }; // Perform an update on one of the linked service var updateResponse = client.LinkedServices.CreateOrUpdate(resourceGroupName, workspaceName, updateParameters); Assert.True(HttpStatusCode.OK == updateResponse.StatusCode); TestHelper.ValidateLinkedService(updateParameters, updateResponse.LinkedService); // Delete a linked service var deleteResponse = client.LinkedServices.Delete(resourceGroupName, workspaceName, linkedServiceName); Assert.Equal(HttpStatusCode.OK, deleteResponse.StatusCode); // Verify the linkedService source is gone getResponse = client.LinkedServices.Get(resourceGroupName, workspaceName, linkedServiceName); Assert.Equal(HttpStatusCode.OK, getResponse.StatusCode); Assert.Null(getResponse.LinkedService); } }