From 73e75a01f59bb1d9bf134048edab9bf9a721bb1d Mon Sep 17 00:00:00 2001 From: GUEYE Date: Thu, 20 Apr 2023 10:39:36 +0200 Subject: [PATCH] AWS image cache control --- .../Managers/AwsDeviceModelImageManager.cs | 41 ++----- src/AzureIoTHub.Portal.Server/Startup.cs | 7 ++ .../AwsDeviceModelImageManagerTest.cs | 102 +++--------------- 3 files changed, 27 insertions(+), 123 deletions(-) diff --git a/src/AzureIoTHub.Portal.Infrastructure/Managers/AwsDeviceModelImageManager.cs b/src/AzureIoTHub.Portal.Infrastructure/Managers/AwsDeviceModelImageManager.cs index b425e04c6..663e7c294 100644 --- a/src/AzureIoTHub.Portal.Infrastructure/Managers/AwsDeviceModelImageManager.cs +++ b/src/AzureIoTHub.Portal.Infrastructure/Managers/AwsDeviceModelImageManager.cs @@ -76,7 +76,7 @@ public async Task ChangeDeviceModelImageAsync(string deviceModelId, Stre public Uri ComputeImageUri(string deviceModelId) { - return new Uri($"https://{this.configHandler.AWSBucketName}.s3.{RegionEndpoint.GetBySystemName(this.configHandler.AWSRegion)}.amazonaws.com/{deviceModelId}"); + throw new NotImplementedException(); } private string ComputeImageUrl(string deviceModelId) @@ -183,42 +183,13 @@ public async Task InitializeDefaultImageBlob() } - public async Task SyncImagesCacheControl() + public Task SyncImagesCacheControl() { + /* We don't need an implementation of + this mehod for AWS because new images will processed by the method SetDefaultImageToModel + */ + throw new NotImplementedException(); - this.logger.LogInformation($"Synchronize Cache control images"); - - var listImagesObjects = new ListObjectsRequest - { - BucketName = this.configHandler.AWSBucketName - }; - - //Get All images from AWS S3 - var response = await this.s3Client.ListObjectsAsync(listImagesObjects); - if (response != null && response.HttpStatusCode == System.Net.HttpStatusCode.OK) - { - foreach (var item in response.S3Objects) - { - var copyObjectRequest = new CopyObjectRequest - { - SourceBucket = this.configHandler.AWSBucketName, - SourceKey = item.Key, - DestinationBucket = this.configHandler.AWSBucketName, - DestinationKey = item.Key, - Headers = {CacheControl = $"max-age={this.configHandler.StorageAccountDeviceModelImageMaxAge}, must-revalidate" } - }; - var copyObjectResponse = await this.s3Client.CopyObjectAsync(copyObjectRequest); - if (copyObjectResponse.HttpStatusCode != System.Net.HttpStatusCode.OK) - { - throw new InternalServerErrorException($"Cache control Synchronization failed for object {item}"); - - } - } - } - else - { - throw new InternalServerErrorException($"Can not retreive list of images to synchronize"); - } } } } diff --git a/src/AzureIoTHub.Portal.Server/Startup.cs b/src/AzureIoTHub.Portal.Server/Startup.cs index 7276e7c91..1aa81e693 100644 --- a/src/AzureIoTHub.Portal.Server/Startup.cs +++ b/src/AzureIoTHub.Portal.Server/Startup.cs @@ -448,6 +448,7 @@ public async void Configure(IApplicationBuilder app, IWebHostEnvironment env) await ConfigureAzureAsync(app); break; case CloudProviders.AWS: + await ConfigureAwsAsync(app); break; default: break; @@ -463,6 +464,12 @@ private static async Task ConfigureAzureAsync(IApplicationBuilder app) await EnsureDatabaseCreatedAndUpToDate(app)!; } + private static async Task ConfigureAwsAsync(IApplicationBuilder app) + { + var deviceModelImageManager = app.ApplicationServices.GetService(); + + await deviceModelImageManager?.InitializeDefaultImageBlob()!; + } private static void UseApiExceptionMiddleware(IApplicationBuilder app) { diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Infrastructure/Managers/AwsDeviceModelImageManagerTest.cs b/src/AzureIoTHub.Portal.Tests.Unit/Infrastructure/Managers/AwsDeviceModelImageManagerTest.cs index 340c58e74..82a2bc0cb 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Infrastructure/Managers/AwsDeviceModelImageManagerTest.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Infrastructure/Managers/AwsDeviceModelImageManagerTest.cs @@ -3,6 +3,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Infrastructure.Managers { + using System; using System.Collections.Generic; using System.IO; using System.Net; @@ -402,108 +403,33 @@ public void InitializeDefaultImageBlobShouldThrowsAnExceptionForPutACLStatusCode [Test] - public void SyncImagesCacheControlShouldThrowsAnExceptionForCopyObjectAsyncStatusCode() + public void SyncImagesCacheControlShouldThrowsANotImplmentedException() { // Arrange - var bucketName = "invalid bucket Name for example"; - - var listObjectsResponse = new ListObjectsResponse - { - HttpStatusCode = HttpStatusCode.OK, - S3Objects = new List - { - new S3Object { Key = "image1.jpg" }, - new S3Object { Key = "image2.png" } - } - }; - - _ = this.mockConfigHandler.Setup(handler => handler.AWSBucketName).Returns(bucketName); - _ = this.mockConfigHandler.Setup(handler => handler.StorageAccountDeviceModelImageMaxAge).Returns(Fixture.Create()); + // We just verify that the method throw NotImplmentedExeception() - _ = this.s3ClientMock.Setup(s3 => s3.ListObjectsAsync(It.IsAny(), It.IsAny())) - .ReturnsAsync(listObjectsResponse); - _ = this.s3ClientMock.Setup(s3 => s3.CopyObjectAsync(It.IsAny(), It.IsAny())) - .ReturnsAsync(new CopyObjectResponse - { - HttpStatusCode = HttpStatusCode.BadGateway - }); + // Act + var act = () => this.awsDeviceModelImageManager.SyncImagesCacheControl(); // Assert - _ = Assert.ThrowsAsync(async () => - { - // Act - await this.awsDeviceModelImageManager.SyncImagesCacheControl(); - - }, "Cache control Synchronization failed"); - this.s3ClientMock.VerifyAll(); + _ = act.Should().ThrowAsync(); } - [Test] - public void SyncImagesCacheControlShouldThrowsAnExceptionForListObjectsAsyncStatusCode() - { - // Arrange - var bucketName = "invalid bucket Name for example"; - - var listObjectsResponse = new ListObjectsResponse - { - HttpStatusCode = HttpStatusCode.BadGateway, - S3Objects = new List - { - new S3Object { Key = "image1.jpg" }, - new S3Object { Key = "image2.png" } - } - }; - - _ = this.mockConfigHandler.Setup(handler => handler.AWSBucketName).Returns(bucketName); - _ = this.mockConfigHandler.Setup(handler => handler.StorageAccountDeviceModelImageMaxAge).Returns(Fixture.Create()); - - _ = this.s3ClientMock.Setup(s3 => s3.ListObjectsAsync(It.IsAny(), It.IsAny())) - .ReturnsAsync(listObjectsResponse); + /*===========================*** Tests for ComputeImageUri() **===========================*/ - // Assert - _ = Assert.ThrowsAsync(async () => - { - // Act - await this.awsDeviceModelImageManager.SyncImagesCacheControl(); - - }, "Can not retreive list of images to synchronize"); - this.s3ClientMock.VerifyAll(); - } - [Test] - public async Task SyncImagesCacheControlShouldSucceed() + public void ComputeImageUriShouldThrowsANotImplmentedException() { // Arrange - var bucketName = "invalid bucket Name for example"; - - var listObjectsResponse = new ListObjectsResponse - { - HttpStatusCode = HttpStatusCode.OK, - S3Objects = new List - { - new S3Object { Key = "image1.jpg" }, - new S3Object { Key = "image2.png" } - } - }; - - _ = this.mockConfigHandler.Setup(handler => handler.AWSBucketName).Returns(bucketName); - _ = this.mockConfigHandler.Setup(handler => handler.StorageAccountDeviceModelImageMaxAge).Returns(Fixture.Create()); - - _ = this.s3ClientMock.Setup(s3 => s3.ListObjectsAsync(It.IsAny(), It.IsAny())) - .ReturnsAsync(listObjectsResponse); - - _ = this.s3ClientMock.Setup(s3 => s3.CopyObjectAsync(It.IsAny(), It.IsAny())) - .ReturnsAsync(new CopyObjectResponse - { - HttpStatusCode = HttpStatusCode.OK - }); - - // Act - await this.awsDeviceModelImageManager.SyncImagesCacheControl(); + var deviceModelId = Fixture.Create(); // Assert - this.s3ClientMock.VerifyAll(); + _ = Assert.Throws(() => + { + // Act + _ = this.awsDeviceModelImageManager.ComputeImageUri(deviceModelId); + }); } } }