Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Schema for tagging and merge system tags for create call #60

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion aws-qbusiness-application/.rpdk-config
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@
"protocolVersion": "2.0.0"
},
"logProcessorEnabled": "true",
"executableEntrypoint": "software.amazon.qbusiness.application.HandlerWrapperExecutable"
"executableEntrypoint": "software.amazon.qbusiness.application.HandlerWrapperExecutable",
"contractSettings": {
"MaxConcurrentTestCase": 10
},
"canarySettings": {}
}
11 changes: 10 additions & 1 deletion aws-qbusiness-application/aws-qbusiness-application.json
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,16 @@
}
},
"tagging": {
"taggable": true
"taggable": true,
"tagUpdatable": true,
"tagOnCreate": true,
"cloudFormationSystemTags": true,
"tagProperty": "/properties/Tags",
"permissions": [
"qbusiness:UntagResource",
"qbusiness:TagResource",
"qbusiness:ListTagsForResource"
]
},
"additionalProperties": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected ProgressEvent<ResourceModel, CallbackContext> handleRequest(
return ProgressEvent.progress(request.getDesiredResourceState(), callbackContext)
.then(progress ->
proxy.initiate("AWS-QBusiness-Application::Create", proxyClient, progress.getResourceModel(), progress.getCallbackContext())
.translateToServiceRequest(model -> Translator.translateToCreateRequest(request.getClientRequestToken(), model))
.translateToServiceRequest(model -> Translator.translateToCreateRequest(request.getClientRequestToken(), model, request.getSystemTags()))
.backoffDelay(backOffStrategy)
.makeServiceCall((awsRequest, clientProxyClient) -> callCreateApplication(awsRequest, clientProxyClient, progress.getResourceModel()))
.stabilize((awsReq, response, clientProxyClient, model, context) -> isStabilized(clientProxyClient, model, logger))
Expand All @@ -73,9 +73,9 @@ protected ProgressEvent<ResourceModel, CallbackContext> handleRequest(
// Immediately update the application to add auto-subscribe configuration to it.
// TODO: Remove after AutoSubscribeConfiguration is added to the CreateApplication API.
return proxy.initiate("AWS-QBusiness-Application::Update", proxyClient, progress.getResourceModel(), progress.getCallbackContext())
.translateToServiceRequest(model -> Translator.translateToPostCreateUpdateRequest(model))
.translateToServiceRequest(Translator::translateToPostCreateUpdateRequest)
.backoffDelay(backOffStrategy)
.makeServiceCall((awsRequest, clientProxyClient) -> callUpdateApplication(awsRequest, clientProxyClient))
.makeServiceCall(this::callUpdateApplication)
.stabilize((awsReq, response, clientProxyClient, model, context) -> isStabilized(clientProxyClient, model, logger))
.handleError((updateReq, error, client, model, context) ->
handleError(updateReq, model, error, context, logger, API_UPDATE_APPLICATION))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package software.amazon.qbusiness.application;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -74,17 +75,35 @@ public static List<software.amazon.qbusiness.application.Tag> cfnTagsFromService
}

public static List<Tag> serviceTagsFromCfnTags(
Collection<software.amazon.qbusiness.application.Tag> modelTags
Collection<software.amazon.qbusiness.application.Tag> modelTags,
Map<String, String> systemTags
) {
if (modelTags == null) {
if (modelTags == null && systemTags == null) {
return null;
}
return modelTags.stream()
.map(tag -> Tag.builder()
.key(tag.getKey())
.value(tag.getValue())
.build()
).toList();

var tags = new ArrayList<Tag>();
if (modelTags != null) {
for (software.amazon.qbusiness.application.Tag modelTag : modelTags) {
tags.add(
Tag.builder()
.key(modelTag.getKey())
.value(modelTag.getValue())
.build()
);
}
}

if (systemTags != null) {
for (Map.Entry<String, String> systemTag: systemTags.entrySet()) {
tags.add(Tag.builder()
.key(systemTag.getKey())
.value(systemTag.getValue()).build()
);
}
}

return tags;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public class Translator {
* @param model resource model
* @return awsRequest the aws service request to create a resource
*/
static CreateApplicationRequest translateToCreateRequest(final String idempotentToken, final ResourceModel model) {
static CreateApplicationRequest translateToCreateRequest(
final String idempotentToken,
final ResourceModel model,
final Map<String, String> systemTags) {
return CreateApplicationRequest.builder()
.clientToken(idempotentToken)
.displayName(model.getDisplayName())
Expand All @@ -50,7 +53,7 @@ static CreateApplicationRequest translateToCreateRequest(final String idempotent
.description(model.getDescription())
.encryptionConfiguration(toServiceEncryptionConfig(model.getEncryptionConfiguration()))
.attachmentsConfiguration(toServiceAttachmentConfiguration(model.getAttachmentsConfiguration()))
.tags(TagHelper.serviceTagsFromCfnTags(model.getTags()))
.tags(TagHelper.serviceTagsFromCfnTags(model.getTags(), systemTags))
.qAppsConfiguration(toServiceQAppsConfiguration(model.getQAppsConfiguration()))
.personalizationConfiguration(toServicePersonalizationConfiguration(model.getPersonalizationConfiguration()))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
import static org.mockito.Mockito.when;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.jupiter.api.AfterEach;
Expand All @@ -22,6 +23,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
Expand Down Expand Up @@ -102,6 +104,9 @@ public void setup() {
.build())
.iamIdentityProviderArn("arn:aws:iam::123456:oidc-provider/trial-123456.okta.com")
.clientIdsForOIDC(List.of("0oaglq4vdnaWau7hW697"))
.tags(List.of(
Tag.builder().key("TagA").value("ValueA").build()
))
.build();

testRequest = ResourceHandlerRequest.<ResourceModel>builder()
Expand All @@ -110,6 +115,10 @@ public void setup() {
.awsPartition("aws")
.region("us-east-1")
.stackId("Stack1")
.systemTags(Map.of(
"aws:cloudformation:logical-id", "AiChatApp",
"aws:cloudformation:stack-name", "StackedStack"
))
.build();

when(sdkClient.createApplication(any(CreateApplicationRequest.class)))
Expand All @@ -130,7 +139,15 @@ public void tear_down() throws Exception {
public void handleRequest_SimpleSuccess() {
// set up scenario
when(sdkClient.listTagsForResource(any(ListTagsForResourceRequest.class))).thenReturn(ListTagsForResourceResponse.builder()
.tags(List.of())
.tags(List.of(
software.amazon.awssdk.services.qbusiness.model.Tag.builder()
.key("aws:cloudformation:logical-id")
.value("AiChatApp").build(),
software.amazon.awssdk.services.qbusiness.model.Tag.builder()
.key("aws:cloudformation:stack-name")
.value("StackedStack").build(),
software.amazon.awssdk.services.qbusiness.model.Tag.builder().key("TagA").value("ValueA").build()
))
.build());
when(sdkClient.getApplication(any(GetApplicationRequest.class)))
.thenReturn(GetApplicationResponse.builder()
Expand All @@ -155,7 +172,22 @@ proxy, testRequest, new CallbackContext(), proxyClient, logger
assertThat(model.getDescription()).isEqualTo(createModel.getDescription());
assertThat(model.getStatus()).isEqualTo(ApplicationStatus.ACTIVE.toString());

verify(sdkClient).createApplication(any(CreateApplicationRequest.class));
ArgumentCaptor<CreateApplicationRequest> createAppReqCaptor = ArgumentCaptor.forClass(CreateApplicationRequest.class);
verify(sdkClient).createApplication(createAppReqCaptor.capture());

var sdkRequest = createAppReqCaptor.getValue();
assertThat(sdkRequest.displayName()).isEqualTo("TheMeta");

var expectedTagsAsMap = Map.of(
"aws:cloudformation:logical-id", "AiChatApp",
"aws:cloudformation:stack-name", "StackedStack",
"TagA", "ValueA"
);
Map<String, String> requestTags = sdkRequest.tags().stream().collect(Collectors.toMap(
software.amazon.awssdk.services.qbusiness.model.Tag::key,
software.amazon.awssdk.services.qbusiness.model.Tag::value));
assertThat(requestTags).isEqualTo(expectedTagsAsMap);

verify(sdkClient, times(3)).getApplication(
argThat((ArgumentMatcher<GetApplicationRequest>) t -> t.applicationId().equals(APP_ID))
);
Expand Down
4 changes: 2 additions & 2 deletions aws-qbusiness-application/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ Resources:
Properties:
Handler: software.amazon.qbusiness.application.HandlerWrapper::handleRequest
Runtime: java17
CodeUri: ./target/aws-qbusiness-application-handler-1.0-SNAPSHOT.jar
CodeUri: ./target/aws-qbusiness-application-1.0.jar

TestEntrypoint:
Type: AWS::Serverless::Function
Properties:
Handler: software.amazon.qbusiness.application.HandlerWrapper::testEntrypoint
Runtime: java17
CodeUri: ./target/aws-qbusiness-application-handler-1.0-SNAPSHOT.jar
CodeUri: ./target/aws-qbusiness-application-1.0.jar