UpdateHandler extends BaseHandler<CallbackContext> { @Override public ProgressEvent<ResourceModel, CallbackContext> handleRequest( final AmazonWebServicesClientProxy proxy, final ResourceHandlerRequest<ResourceModel> request, final CallbackContext callbackContext, final Logger logger) { this.clientProxy = proxy; this.logger = logger; final ResourceModel currentModel = request.getDesiredResourceState(); final ResourceModel previousModel = request.getPreviousResourceState(); final String arn = previousModel.getArn(); UpdateInfrastructureConfigurationResponse response; try { response = proxy.injectCredentialsAndInvokeV2(RequestUtil.generateUpdateInfrastructureConfigurationRequest(arn, currentModel), ClientBuilder.getImageBuilderClient()::updateInfrastructureConfiguration); Map<String, String> previousTagMap = previousModel.getTags() == null ? new HashMap<>() : previousModel.getTags(); Map<String, String> currentTagMap = currentModel.getTags() == null ? new HashMap<>() : currentModel.getTags(); if (previousTagMap.isEmpty() && currentTagMap.isEmpty()) { } else if (previousTagMap.isEmpty()) { proxy.injectCredentialsAndInvokeV2(RequestUtil.generateTagDistributionConfigurationRequest( arn, currentTagMap), ClientBuilder.getImageBuilderClient()::tagResource); } else if (currentTagMap.isEmpty()) { List<String> keyList = new LinkedList<>(); for (Map.Entry<String, String> previousTagMapEntry : previousTagMap.entrySet()) { keyList.add(previousTagMapEntry.getKey()); } proxy.injectCredentialsAndInvokeV2(RequestUtil.generateUntagDistributionConfigurationRequest(arn, keyList), ClientBuilder.getImageBuilderClient()::untagResource); } else { List<String> untagKeyList = new LinkedList<>(); for (Map.Entry<String,String> previousTagMapEntry : previousTagMap.entrySet()) { if (! currentTagMap.containsKey(previousTagMapEntry.getKey())) { untagKeyList.add(previousTagMapEntry.getKey()); } } if (!untagKeyList.isEmpty()) { proxy.injectCredentialsAndInvokeV2(RequestUtil.generateUntagDistributionConfigurationRequest(arn, untagKeyList), ClientBuilder.getImageBuilderClient()::untagResource); } Map<String, String> tagKeyMap = new HashMap<>(); for (Map.Entry<String,String> currentTagMapEntry : currentTagMap.entrySet()) { if (! previousTagMap.containsKey(currentTagMapEntry.getKey())) { tagKeyMap.put(currentTagMapEntry.getKey(), currentTagMapEntry.getValue()); } } if (!tagKeyMap.isEmpty()) { proxy.injectCredentialsAndInvokeV2(RequestUtil.generateTagDistributionConfigurationRequest(arn, tagKeyMap), ClientBuilder.getImageBuilderClient()::tagResource); } Map<String, String> updateKeyMap = new HashMap<>(); for (Map.Entry<String,String> currentTagMapEntry : currentTagMap.entrySet()) { String currentTagEntryKey = currentTagMapEntry.getKey(); String currentTagEntryValue = currentTagMapEntry.getValue(); if (previousTagMap.containsKey(currentTagEntryKey) && ! previousTagMap.get(currentTagEntryKey).equals(currentTagEntryValue)) { updateKeyMap.put(currentTagMapEntry.getKey(), currentTagMapEntry.getValue()); } } if (!updateKeyMap.isEmpty()) { proxy.injectCredentialsAndInvokeV2(RequestUtil.generateTagDistributionConfigurationRequest(arn, updateKeyMap), ClientBuilder.getImageBuilderClient()::tagResource); } } currentModel.setArn(arn); } catch (ResourceNotFoundException e) { throw new CfnNotFoundException(ResourceModel.TYPE_NAME, arn); } return ProgressEvent.<ResourceModel, CallbackContext>builder() .resourceModel(currentModel) .status(OperationStatus.SUCCESS) .build(); }  @Override ProgressEvent<ResourceModel, CallbackContext> handleRequest(
        final AmazonWebServicesClientProxy proxy,
        final ResourceHandlerRequest<ResourceModel> request,
        final CallbackContext callbackContext,
        final Logger logger);  }
@Test public void handleRequest_Success() { final UpdateInfrastructureConfigurationResponse updateInfrastructureConfigurationResponse = UpdateInfrastructureConfigurationResponse.builder() .infrastructureConfigurationArn("Infrastructure-configuration-arn-test") .build(); doReturn(updateInfrastructureConfigurationResponse) .when(proxy) .injectCredentialsAndInvokeV2(any(), any()); final ProgressEvent<ResourceModel, CallbackContext> response = handler.handleRequest(proxy, request, null, logger); assertThat(response).isNotNull(); assertThat(response.getStatus()).isEqualTo(OperationStatus.SUCCESS); assertThat(response.getCallbackContext()).isNull(); assertThat(response.getCallbackDelaySeconds()).isEqualTo(0); assertThat(response.getResourceModel()).isEqualTo(request.getDesiredResourceState()); assertThat(response.getResourceModels()).isNull(); assertThat(response.getMessage()).isNull(); assertThat(response.getErrorCode()).isNull(); }
@Test public void handleRequest_ResourceNotFound_Failure() { doThrow(ResourceNotFoundException.class) .when(proxy) .injectCredentialsAndInvokeV2(any(), any()); assertThrows(CfnNotFoundException.class, () -> handler.handleRequest(proxy, request, null, logger)); }
@Test public void handleRequest_NoTagsInBothPreviousAndCurrentModel() { final ResourceHandlerRequest<ResourceModel> tagTestRequest = ResourceHandlerRequest.<ResourceModel>builder() .desiredResourceState(noTagsCurrentModel) .previousResourceState(noTagsPreviousModel) .build(); final UpdateInfrastructureConfigurationResponse updateInfrastructureConfigurationResponse = UpdateInfrastructureConfigurationResponse.builder() .infrastructureConfigurationArn("Infrastructure-configuration-arn-test") .build(); doReturn(updateInfrastructureConfigurationResponse) .when(proxy) .injectCredentialsAndInvokeV2(any(), any()); final ProgressEvent<ResourceModel, CallbackContext> response = handler.handleRequest(proxy, tagTestRequest, null, logger); assertThat(response).isNotNull(); assertThat(response.getStatus()).isEqualTo(OperationStatus.SUCCESS); assertThat(response.getCallbackContext()).isNull(); assertThat(response.getCallbackDelaySeconds()).isEqualTo(0); assertThat(response.getResourceModel()).isEqualTo(tagTestRequest.getDesiredResourceState()); assertThat(response.getResourceModels()).isNull(); assertThat(response.getMessage()).isNull(); assertThat(response.getErrorCode()).isNull(); }
@Test public void handleRequest_NoTagsInPreviousModel() { final ResourceHandlerRequest<ResourceModel> tagTestRequest = ResourceHandlerRequest.<ResourceModel>builder() .desiredResourceState(currentModel) .previousResourceState(noTagsPreviousModel) .build(); final UpdateInfrastructureConfigurationResponse updateInfrastructureConfigurationResponse = UpdateInfrastructureConfigurationResponse.builder() .infrastructureConfigurationArn("Infrastructure-configuration-arn-test") .build(); doReturn(updateInfrastructureConfigurationResponse) .when(proxy) .injectCredentialsAndInvokeV2(any(), any()); final ProgressEvent<ResourceModel, CallbackContext> response = handler.handleRequest(proxy, tagTestRequest, null, logger); assertThat(response).isNotNull(); assertThat(response.getStatus()).isEqualTo(OperationStatus.SUCCESS); assertThat(response.getCallbackContext()).isNull(); assertThat(response.getCallbackDelaySeconds()).isEqualTo(0); assertThat(response.getResourceModel()).isEqualTo(tagTestRequest.getDesiredResourceState()); assertThat(response.getResourceModels()).isNull(); assertThat(response.getMessage()).isNull(); assertThat(response.getErrorCode()).isNull(); }
@Test public void handleRequest_NoTagsInCurrentModel() { final ResourceHandlerRequest<ResourceModel> tagTestRequest = ResourceHandlerRequest.<ResourceModel>builder() .desiredResourceState(noTagsCurrentModel) .previousResourceState(previousModel) .build(); final UpdateInfrastructureConfigurationResponse updateInfrastructureConfigurationResponse = UpdateInfrastructureConfigurationResponse.builder() .infrastructureConfigurationArn("Infrastructure-configuration-arn-test") .build(); doReturn(updateInfrastructureConfigurationResponse) .when(proxy) .injectCredentialsAndInvokeV2(any(), any()); final ProgressEvent<ResourceModel, CallbackContext> response = handler.handleRequest(proxy, tagTestRequest, null, logger); assertThat(response).isNotNull(); assertThat(response.getStatus()).isEqualTo(OperationStatus.SUCCESS); assertThat(response.getCallbackContext()).isNull(); assertThat(response.getCallbackDelaySeconds()).isEqualTo(0); assertThat(response.getResourceModel()).isEqualTo(tagTestRequest.getDesiredResourceState()); assertThat(response.getResourceModels()).isNull(); assertThat(response.getMessage()).isNull(); assertThat(response.getErrorCode()).isNull(); }