/// <summary> /// Gets an environment based on a display name. /// </summary> /// <param name="displayName">The display name to search for.</param> /// <returns>Information regarding the environment.</returns> public async Task <PowerAppsEnvironment> GetEnvironmentByDisplayNameAsync(string displayName) { LogInformation("Acquiring access token..."); IHttpClient httpClient = TokenProvider.GetHttpClient(Audience); LogInformation($"Acquiring PowerApps environment named '{displayName}'..."); HttpResponseMessage response = await httpClient.GetAsync( _getEnvironmentByDisplayNameUri .Replace("{displayName}", displayName)); PowerAppsEnvironment result = null; if (response.IsSuccessStatusCode) { GetPowerAppsEnvironmentsResponse environments = JsonConvert.DeserializeObject <GetPowerAppsEnvironmentsResponse>(await response.Content.ReadAsStringAsync()); result = environments .Value .Where(x => string.Compare(x.Properties.DisplayName, displayName) == 0 || (x.Properties.LinkedEnvironmentMetadata != null && string.Compare(x.Properties.LinkedEnvironmentMetadata.FriendlyName, displayName) == 0)).FirstOrDefault(); } else { LogError($"ERROR: ({response.StatusCode}) {response.ReasonPhrase}"); throw new RequestException(response); } return(result); }
/// <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); }
public void GetEnvironmentByDisplayNameAsyncSuccess() { string expectedEnvironmentName = Guid.NewGuid().ToString(); 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?$filter=properties.displayName%20eq%20'{expectedDisplayName}'&api-version=2016-11-01"; string responseFilePath = @"./data/templates/responses/powerApps/getEnvironments.json"; HttpRequestMessage expectedRequest = TestHelper.CreateHttpRequest( HttpMethod.Get, expectedRequestUri); _httpClient.RegisterExpectedRequest(new ExpectedRequest(expectedRequest)); HttpResponseMessage expectedResponse = TestHelper.CreateHttpResponse( HttpStatusCode.OK, null, responseFilePath, "application/json", new Dictionary <string, string>() { { "environmentName", expectedEnvironmentName }, { "location", expectedLocation }, { "displayName", expectedDisplayName }, { "crmResourceId", expectedResourceId }, { "crmFriendlyName", expectedFriendlyName }, { "crmUniqueName", expectedUniqueName }, { "crmDomainName", expectedDomainName }, }); _httpClient.RegisterExpectedResponse( expectedRequestUri, new ExpectedResponse(expectedResponse)); IPowerAppsClient client = new PowerAppsClient(_tokenProvider); PowerAppsEnvironment response = (PowerAppsEnvironment)client.GetEnvironmentByDisplayNameAsync(expectedDisplayName).Result; 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 new environment. /// </summary> /// <param name="environment">Configuration information for the new environment.</param> /// <returns>Information regarding the new environment.</returns> public async Task <CreatePowerAppsEnvironmentResponse> CreateEnvironmentAsync(CreatePowerAppsEnvironmentRequest environment) { LogInformation($"Looking for existing PowerApps environment named '{environment.Properties.DisplayName}'..."); PowerAppsEnvironment findEnvironment = await GetEnvironmentByDisplayNameAsync(environment.Properties.DisplayName); if (findEnvironment != null) { LogInformation($"PowerApps environment named '{environment.Properties.DisplayName}' already exists."); return(new CreatePowerAppsEnvironmentResponse() { Id = findEnvironment.Id, Name = findEnvironment.Name, Type = findEnvironment.Type, Location = findEnvironment.Location, Properties = findEnvironment.Properties, }); } LogInformation("Acquiring access token..."); IHttpClient httpClient = TokenProvider.GetHttpClient(Audience); HttpContent body = new StringContent( JsonConvert.SerializeObject(environment), Encoding.UTF8, "application/json"); LogInformation($"Creating PowerApps environment named '{environment.Properties.DisplayName}' in location '{environment.Location}' with SKU '{environment.Properties.EnvironmentSku}'..."); HttpResponseMessage response = await httpClient.PostAsync( _newEnvironmentUri, body); CreatePowerAppsEnvironmentResponse result = null; if (response.IsSuccessStatusCode) { result = JsonConvert.DeserializeObject <CreatePowerAppsEnvironmentResponse>(await response.Content.ReadAsStringAsync()); } else { LogError($"ERROR: ({response.StatusCode}) {response.ReasonPhrase}"); throw new RequestException(response); } return(result); }
/// <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}')"); }