/// <summary> /// Create a CDS database. /// </summary> /// <param name="environmentName">The name of the environment to create the CDS database in.</param> /// <param name="cdsDatabase">Configuration information regarding the new CDS database.</param> /// <returns>Information regarding the PowerApps environment after the CDS database creation.</returns> public PowerAppsEnvironment CreateCdsDatabase(string environmentName, CreatePowerAppsCdsDatabaseRequest cdsDatabase) { LogInformation($"Looking for existing Common Data Services database for PowerApps environment with id '{environmentName}'..."); PowerAppsEnvironment findEnvironment = (PowerAppsEnvironment)GetEnvironmentsAsync(environmentName).Result; if (findEnvironment != null && findEnvironment.Properties != null && findEnvironment.Properties.LinkedEnvironmentMetadata != null) { LogInformation($"Common Data Services database for PowerApps environment with id '{environmentName}' already exists."); return(findEnvironment); } LogInformation($"Creating Common Data Services database for PowerApps environment '{environmentName}'..."); AzureResponseBase response = CreateCdsDatabaseAsync( environmentName, cdsDatabase).Result; DateTime startTime = DateTime.Now; DateTime currentTime = DateTime.Now; TimeSpan timeDiff = currentTime - startTime; int timeoutInSeconds = 300; HttpResponseMessage httpResponse = null; while (httpResponse == null || (httpResponse.StatusCode != HttpStatusCode.OK && httpResponse.StatusCode != HttpStatusCode.NotFound && httpResponse.StatusCode != HttpStatusCode.InternalServerError && timeDiff.TotalSeconds < timeoutInSeconds && !response.AlreadyExists)) { LogInformation("Sleeping until next poll..."); Thread.Sleep(5000); LogInformation("Polling for Common Data Service database creation completion..."); httpResponse = GetLocationAsync(Audience, new Uri(response.LocationUri)).Result; currentTime = DateTime.Now; timeDiff = currentTime - startTime; } LogInformation("Common Data Service database creation completed."); return(JsonConvert.DeserializeObject <PowerAppsEnvironment>(httpResponse.Content.ReadAsStringAsync().Result)); }
public void CreateCdsDatabaseSuccess() { string expectedEnvironmentName = Guid.NewGuid().ToString(); string expectedOperationId = Guid.NewGuid().ToString(); string expectedBaseLanguage = "1033"; string expectedCurrency = "USD"; string expectedLocation = "unitedstates"; string expectedDisplayName = "TestEnvironment"; string expectedResourceId = Guid.NewGuid().ToString(); string expectedFriendlyName = "TestEnvironment"; string expectedUniqueName = "orgtest4"; string expectedDomainName = "orgtest5"; string expectedRequestUri = $"https://api.bap.microsoft.com/providers/Microsoft.BusinessAppPlatform/scopes/admin/environments/{expectedEnvironmentName}?$expand=permissions&api-version=2016-11-01"; string expectedRequestUri2 = $"https://api.bap.microsoft.com/providers/Microsoft.BusinessAppPlatform/environments/{expectedEnvironmentName}/provisionInstance?api-version=2018-01-01"; string expectedOperationRequestUri = $"https://api.bap.microsoft.com/providers/Microsoft.BusinessAppPlatform/environments/{expectedEnvironmentName}/provisionOperations/{expectedOperationId}?api-version=2018-01-01"; string responseFilePath = @"./data/templates/responses/powerApps/environmentNoCds.json"; string responseFilePath2 = @"./data/templates/responses/powerApps/environment.json"; HttpRequestMessage expectedRequest = TestHelper.CreateHttpRequest( HttpMethod.Get, expectedRequestUri); _httpClient.RegisterExpectedRequest(new ExpectedRequest(expectedRequest)); _httpClient.RegisterExpectedRequest(new ExpectedRequest(expectedRequest)); HttpRequestMessage expectedRequest2 = TestHelper.CreateHttpRequest( HttpMethod.Post, expectedRequestUri2); _httpClient.RegisterExpectedRequest(new ExpectedRequest(expectedRequest2)); HttpRequestMessage expectedRequest3 = TestHelper.CreateHttpRequest( HttpMethod.Get, expectedOperationRequestUri); _httpClient.RegisterExpectedRequest(new ExpectedRequest(expectedRequest3)); _httpClient.RegisterExpectedRequest(new ExpectedRequest(expectedRequest3)); HttpResponseMessage expectedResponse = TestHelper.CreateHttpResponse( HttpStatusCode.OK, null, responseFilePath, "application/json", new Dictionary <string, string>() { { "environmentName", expectedEnvironmentName }, { "location", expectedLocation }, { "displayName", expectedDisplayName }, }); _httpClient.RegisterExpectedResponse( expectedRequestUri, new ExpectedResponse(expectedResponse)); _httpClient.RegisterExpectedResponse( expectedRequestUri, new ExpectedResponse(expectedResponse)); HttpResponseMessage expectedResponse2 = TestHelper.CreateHttpResponse( HttpStatusCode.Accepted, new Dictionary <string, string>() { { "Location", expectedOperationRequestUri }, }, responseFilePath2, "application/json", new Dictionary <string, string>() { { "environmentName", expectedEnvironmentName }, { "location", expectedLocation }, { "displayName", expectedDisplayName }, { "crmResourceId", string.Empty }, { "crmFriendlyName", string.Empty }, { "crmUniqueName", string.Empty }, { "crmDomainName", string.Empty }, }); _httpClient.RegisterExpectedResponse( expectedRequestUri2, new ExpectedResponse(expectedResponse2)); _httpClient.RegisterExpectedResponse( expectedOperationRequestUri, new ExpectedResponse(expectedResponse2)); HttpResponseMessage expectedResponse3 = TestHelper.CreateHttpResponse( HttpStatusCode.OK, null, responseFilePath2, "application/json", new Dictionary <string, string>() { { "environmentName", expectedEnvironmentName }, { "location", expectedLocation }, { "displayName", expectedDisplayName }, { "crmResourceId", expectedResourceId }, { "crmFriendlyName", expectedFriendlyName }, { "crmUniqueName", expectedUniqueName }, { "crmDomainName", expectedDomainName }, }); _httpClient.RegisterExpectedResponse( expectedOperationRequestUri, new ExpectedResponse(expectedResponse3)); IPowerAppsClient client = new PowerAppsClient(_tokenProvider); CreatePowerAppsCdsDatabaseRequest cdsDatabase = new CreatePowerAppsCdsDatabaseRequest() { BaseLanguage = expectedBaseLanguage, Currency = new PowerAppsCdsDatabaseCurrencyMinimal() { Code = expectedCurrency, }, }; PowerAppsEnvironment response = client.CreateCdsDatabase( expectedEnvironmentName, cdsDatabase); Assert.IsNotNull(response, "The response should not be null!"); Assert.IsNotNull(response.Properties, "The response Properties member should not be null!"); Assert.AreEqual(expectedEnvironmentName, response.Name, $"Unexpected name ('{expectedEnvironmentName}' != '{response.Name}')"); Assert.AreEqual(expectedLocation, response.Location, $"Unexpected location ('{expectedLocation}' != '{response.Location}')"); Assert.AreEqual(expectedDisplayName, response.Properties.DisplayName, $"Unexpected location ('{expectedDisplayName}' != '{response.Properties.DisplayName}')"); Assert.AreEqual(expectedDisplayName, response.Properties.DisplayName, $"Unexpected location ('{expectedDisplayName}' != '{response.Properties.DisplayName}')"); Assert.IsNotNull(response.Properties.LinkedEnvironmentMetadata, "The response Properties.LinkedEnvironmentMetadata member should not be null!"); Assert.AreEqual(expectedResourceId, response.Properties.LinkedEnvironmentMetadata.ResourceId, $"Unexpected resource id ('{expectedResourceId}' != '{response.Properties.LinkedEnvironmentMetadata.ResourceId}')"); Assert.AreEqual(expectedFriendlyName, response.Properties.LinkedEnvironmentMetadata.FriendlyName, $"Unexpected friendly name ('{expectedFriendlyName}' != '{response.Properties.LinkedEnvironmentMetadata.FriendlyName}')"); Assert.AreEqual(expectedUniqueName, response.Properties.LinkedEnvironmentMetadata.UniqueName, $"Unexpected unique name ('{expectedUniqueName}' != '{response.Properties.LinkedEnvironmentMetadata.UniqueName}')"); Assert.AreEqual(expectedDomainName, response.Properties.LinkedEnvironmentMetadata.DomainName, $"Unexpected domain name ('{expectedDomainName}' != '{response.Properties.LinkedEnvironmentMetadata.DomainName}')"); }
/// <summary> /// Creates a CDS database. /// </summary> /// <param name="environmentName">The name of the environment to create the CDS database in.</param> /// <param name="cdsDatabase">Configuration information regarding the CDS database to create.</param> /// <returns>The response content as an AzureResponseBase object.</returns> public async Task <AzureResponseBase> CreateCdsDatabaseAsync(string environmentName, CreatePowerAppsCdsDatabaseRequest cdsDatabase) { LogInformation($"Looking for existing Common Data Services database for PowerApps environment with id '{environmentName}'..."); PowerAppsEnvironment findEnvironment = (PowerAppsEnvironment) await GetEnvironmentsAsync(environmentName); if (findEnvironment != null && findEnvironment.Properties != null && findEnvironment.Properties.LinkedEnvironmentMetadata != null) { LogInformation($"Common Data Services database for PowerApps environment with id '{environmentName}' already exists."); return(new AzureResponseBase() { AlreadyExists = true, }); } LogInformation("Acquiring access token..."); IHttpClient httpClient = TokenProvider.GetHttpClient(Audience); HttpContent body = new StringContent( JsonConvert.SerializeObject(cdsDatabase), Encoding.UTF8, "application/json"); LogInformation($"Creating Common Data Services database for PowerApps environment '{environmentName}'..."); HttpResponseMessage response = await httpClient.PostAsync( _newCdsDatabaseUri.Replace("{environmentName}", environmentName), body); AzureResponseBase result = new AzureResponseBase(); if (response.IsSuccessStatusCode) { if (response.Headers.Contains("Azure-AsyncOperation")) { result.AzureAsyncOperationUri = response.Headers.GetValues("Azure-AsyncOperation").First(); } else if (response.Headers.Contains("Location")) { result.LocationUri = response.Headers.GetValues("Location").First(); } if (response.Headers.Contains("Retry-After")) { result.RetryAfter = int.Parse(response.Headers.GetValues("Retry-After").First()); } await response.Content.ReadAsStringAsync(); } else { LogError($"ERROR: ({response.StatusCode}) {response.ReasonPhrase}"); throw new RequestException(response); } return(result); }