diff --git a/.devops/deploy-pipelines.yml b/.devops/deploy-pipelines.yml index 0405a24..00d9864 100644 --- a/.devops/deploy-pipelines.yml +++ b/.devops/deploy-pipelines.yml @@ -36,21 +36,21 @@ variables: AZURE_SUBSCRIPTION: $(DEV_AZURE_SUBSCRIPTION) APP_NAME: $(DEV_WEB_APP_NAME) STAGE: "d" - RESOURCE_GROUP: 'pagopa-d-weu-shared-rg' + RESOURCE_GROUP: 'pagopa-d-weu-shared-txnm-rg' dockerRegistryServiceConnection: $(DEV_CONTAINER_REGISTRY_SERVICE_CONN) dockerNamespace: $(DEV_CONTAINER_NAMESPACE) ${{ if eq(parameters['ENV'], 'uat') }}: AZURE_SUBSCRIPTION: $(UAT_AZURE_SUBSCRIPTION) APP_NAME: $(UAT_WEB_APP_NAME) STAGE: "u" - RESOURCE_GROUP: 'pagopa-u-weu-shared-rg' + RESOURCE_GROUP: 'pagopa-u-weu-shared-txnm-rg' dockerRegistryServiceConnection: $(UAT_CONTAINER_REGISTRY_SERVICE_CONN) dockerNamespace: $(UAT_CONTAINER_NAMESPACE) ${{ if eq(parameters['ENV'], 'prod') }}: AZURE_SUBSCRIPTION: $(PROD_AZURE_SUBSCRIPTION) APP_NAME: $(PROD_WEB_APP_NAME) STAGE: "p" - RESOURCE_GROUP: 'pagopa-p-weu-shared-rg' + RESOURCE_GROUP: 'pagopa-p-weu-shared-txnm-rg' dockerRegistryServiceConnection: $(PROD_CONTAINER_REGISTRY_SERVICE_CONN) dockerNamespace: $(PROD_CONTAINER_NAMESPACE) @@ -155,7 +155,7 @@ stages: condition: in('${{ parameters.ENV }}', 'dev', 'uat') inputs: azureSubscription: $(AZURE_SUBSCRIPTION) - appName: "${{variables.APP_NAME}}-shared-taxonomy-fn" + appName: "${{variables.APP_NAME}}-shared-txnm-fn" imageName: "${{variables.dockerNamespace}}/${{ variables.imageRepository }}:latest" slotName: production resourceGroupName: $(RESOURCE_GROUP) @@ -164,7 +164,7 @@ stages: condition: eq('${{ parameters.ENV }}', 'prod') inputs: azureSubscription: $(AZURE_SUBSCRIPTION) - appName: "${{variables.APP_NAME}}-shared-taxonomy-fn" + appName: "${{variables.APP_NAME}}-shared-txnm-fn" imageName: "${{variables.dockerNamespace}}/${{ variables.imageRepository }}:latest" deployToSlotOrASE: true slotName: staging @@ -198,7 +198,7 @@ stages: displayName: Swapping App Service Deploy inputs: ConnectedServiceName: $(AZURE_SUBSCRIPTION) - WebAppName: "${{variables.APP_NAME}}-shared-taxonomy-fn" + WebAppName: "${{variables.APP_NAME}}-shared-txnm-fn" ResourceGroupName: $(RESOURCE_GROUP) SourceSlot: staging SwapWithProduction: true diff --git a/src/main/java/it/gov/pagopa/taxonomy/TaxonomyGetFunction.java b/src/main/java/it/gov/pagopa/taxonomy/TaxonomyGetFunction.java index a3a339d..322e849 100644 --- a/src/main/java/it/gov/pagopa/taxonomy/TaxonomyGetFunction.java +++ b/src/main/java/it/gov/pagopa/taxonomy/TaxonomyGetFunction.java @@ -27,19 +27,24 @@ public class TaxonomyGetFunction { private static final String storageConnString = System.getenv("STORAGE_ACCOUNT_CONN_STRING"); - private static final String blobContainerName = System.getenv("BLOB_CONTAINER_NAME"); - private static final String blobName = System.getenv("JSON_NAME"); - + private static final String blobContainerNameOutput = System.getenv("BLOB_CONTAINER_NAME_OUTPUT"); + private static final String jsonName = System.getenv("JSON_NAME"); private static ObjectMapper objectMapper = null; - private static BlobContainerClient blobContainerClient; + private static BlobContainerClient blobContainerClientOutput; + private static BlobServiceClient blobServiceClient; + private static BlobServiceClient getBlobServiceClient(){ + if(blobServiceClient == null){ + blobServiceClient = new BlobServiceClientBuilder().connectionString(storageConnString).buildClient(); + } + return blobServiceClient; + } - private static BlobContainerClient getBlobContainerClient(){ - if(blobContainerClient == null){ - BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(storageConnString).buildClient(); - blobContainerClient = blobServiceClient.createBlobContainerIfNotExists(blobContainerName); + private static BlobContainerClient getBlobContainerClientOutput(){ + if(blobContainerClientOutput == null){ + blobContainerClientOutput = getBlobServiceClient().createBlobContainerIfNotExists(blobContainerNameOutput); } - return blobContainerClient; + return blobContainerClientOutput; } private static ObjectMapper getObjectMapper(){ @@ -57,7 +62,7 @@ public HttpResponseMessage getTaxonomy( methods = {HttpMethod.GET}, route = "taxonomy", authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage> request, - final ExecutionContext context) throws JsonProcessingException { + final ExecutionContext context) { Logger logger = context.getLogger(); try { @@ -67,6 +72,7 @@ public HttpResponseMessage getTaxonomy( map.put(AppConstant.RESPONSE_HEADER_CREATED, taxonomyJson.getCreated().toString()); String payload = AppUtil.getPayload(getObjectMapper(), taxonomyJson.getTaxonomyList()); + logger.info("Taxonomy retrieved successfully"); return AppUtil.writeResponseWithHeaders(request, HttpStatus.OK, payload, @@ -100,11 +106,12 @@ private static TaxonomyJson getTaxonomy(Logger logger) { try { Instant now = Instant.now(); logger.info("Retrieving standard json from the blob storage at: [" + now + "]"); - String content = getBlobContainerClient().getBlobClient(blobName).downloadContent().toString(); - - logger.info("Versioning the json"); - return getObjectMapper().readValue(content, TaxonomyJson.class); + String content = getBlobContainerClientOutput().getBlobClient(jsonName).downloadContent().toString(); + TaxonomyJson taxonomyJson = getObjectMapper().readValue(content, TaxonomyJson.class); + logger.info("Versioning json id = [" + taxonomyJson.getUuid() + "] to the standard version"); + return taxonomyJson; } catch (JsonProcessingException parsingException) { + logger.info("An AppException has occurred"); throw new AppException(parsingException, AppErrorCodeMessageEnum.JSON_PARSING_ERROR); } } diff --git a/src/main/java/it/gov/pagopa/taxonomy/TaxonomyUpdateFunction.java b/src/main/java/it/gov/pagopa/taxonomy/TaxonomyUpdateFunction.java index 8115946..7bcbf8d 100644 --- a/src/main/java/it/gov/pagopa/taxonomy/TaxonomyUpdateFunction.java +++ b/src/main/java/it/gov/pagopa/taxonomy/TaxonomyUpdateFunction.java @@ -21,14 +21,7 @@ import it.gov.pagopa.taxonomy.model.json.TaxonomyJson; import it.gov.pagopa.taxonomy.util.AppUtil; import org.modelmapper.ModelMapper; - -import java.io.FileNotFoundException; -import java.io.IOException; import java.io.InputStreamReader; -import java.net.ConnectException; -import java.net.MalformedURLException; -import java.net.URL; -import java.nio.charset.StandardCharsets; import java.time.Instant; import java.util.List; import java.util.Optional; @@ -39,24 +32,39 @@ public class TaxonomyUpdateFunction { - private static final String csvUrl = System.getenv("CSV_URL"); private static final String storageConnString = System.getenv("STORAGE_ACCOUNT_CONN_STRING"); - private static final String blobContainerName = System.getenv("BLOB_CONTAINER_NAME"); - private static final String blobName = System.getenv("JSON_NAME"); - + private static final String blobContainerNameInput = System.getenv("BLOB_CONTAINER_NAME_INPUT"); + private static final String blobContainerNameOuput = System.getenv("BLOB_CONTAINER_NAME_OUTPUT"); + private static final String jsonName = System.getenv("JSON_NAME"); + private static final String csvName = System.getenv("CSV_NAME"); private static ObjectMapper objectMapper = null; private static ModelMapper modelMapper = null; - private static BlobContainerClient blobContainerClient; + private static BlobContainerClient blobContainerClientInput; + private static BlobContainerClient blobContainerClientOutput; + private static BlobServiceClient blobServiceClient; + private static BlobServiceClient getBlobServiceClient(){ + if(blobServiceClient == null){ + blobServiceClient = new BlobServiceClientBuilder().connectionString(storageConnString).buildClient(); + } + return blobServiceClient; + } - private static BlobContainerClient getBlobContainerClient(){ - if(blobContainerClient == null){ - BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(storageConnString).buildClient(); - blobContainerClient = blobServiceClient.createBlobContainerIfNotExists(blobContainerName); + private static BlobContainerClient getBlobContainerClientInput(){ + if(blobContainerClientInput == null){ + blobContainerClientInput = getBlobServiceClient().createBlobContainerIfNotExists(blobContainerNameInput); } - return blobContainerClient; + return blobContainerClientInput; } + private static BlobContainerClient getBlobContainerClientOutput(){ + if(blobContainerClientOutput == null){ + blobContainerClientOutput = getBlobServiceClient().createBlobContainerIfNotExists(blobContainerNameOuput); + } + return blobContainerClientOutput; + } + + private static ObjectMapper getObjectMapper(){ if(objectMapper == null){ objectMapper = new ObjectMapper(); @@ -85,6 +93,7 @@ public HttpResponseMessage updateTaxonomy( try { updateTaxonomy(logger); String payload = AppUtil.getPayload(getObjectMapper(), Message.builder().message("Taxonomy updated successfully").build()); + logger.info("Taxonomy updated successfully"); return AppUtil.writeResponse(request, HttpStatus.OK, payload @@ -117,10 +126,11 @@ public HttpResponseMessage updateTaxonomy( private static void updateTaxonomy(Logger logger) { try { - logger.info("Download csv [" + csvUrl + "]"); - InputStreamReader inputStreamReader = new InputStreamReader(new URL(csvUrl).openStream(), StandardCharsets.UTF_8); + logger.info("Download csv file [" + csvName + "] from blob at [" + Instant.now().toString() + "]"); + + InputStreamReader inputStreamReader = new InputStreamReader(getBlobContainerClientInput().getBlobClient(csvName).downloadContent().toStream()); - logger.info("Transform csv to json"); + logger.info("Converting [" + csvName + "] into [" + jsonName + "]"); List taxonomyCsvList = new CsvToBeanBuilder(inputStreamReader) .withSeparator(';') .withSkipLines(0) @@ -140,19 +150,12 @@ private static void updateTaxonomy(Logger logger) { byte[] jsonBytes = getObjectMapper().writeValueAsBytes(taxonomyJson); - logger.info("Upload json id=[" + id + "] created at : [" + now + "]"); - getBlobContainerClient().getBlobClient(blobName).upload(BinaryData.fromBytes(jsonBytes), true); + logger.info("Uploading json id = [" + id + "] created at: [" + now + "]"); + getBlobContainerClientOutput().getBlobClient(jsonName).upload(BinaryData.fromBytes(jsonBytes), true); - } catch (ConnectException connException) { - throw new AppException(connException, AppErrorCodeMessageEnum.CONNECTION_REFUSED, csvUrl); - } catch (FileNotFoundException fileNotFoundException) { - throw new AppException(fileNotFoundException, AppErrorCodeMessageEnum.FILE_DOES_NOT_EXIST); - } catch (MalformedURLException malformedURLException) { - throw new AppException(malformedURLException, AppErrorCodeMessageEnum.MALFORMED_URL); } catch (JsonProcessingException | IllegalStateException parsingException) { + logger.info("An AppException has occurred"); throw new AppException(parsingException, AppErrorCodeMessageEnum.CSV_PARSING_ERROR); - } catch (IOException ioException) { - throw new AppException(ioException, AppErrorCodeMessageEnum.ERROR_READING_WRITING); } } }