Skip to content

Extension detected by customization flag #3067

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

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
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ public class CustomizationConfig {

private RetryMode defaultRetryMode;

/**
* The name of the interface that the client interface should extend.
*/
private String extensionInterface;


private CustomizationConfig() {
Expand Down Expand Up @@ -502,4 +506,12 @@ public ServiceConfig getServiceConfig() {
public void setServiceConfig(ServiceConfig serviceConfig) {
this.serviceConfig = serviceConfig;
}

public String getExtensionInterface() {
return extensionInterface;
}

public void setExtensionInterface(String extensionInterface) {
this.extensionInterface = extensionInterface;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package software.amazon.awssdk.codegen.poet;

import static software.amazon.awssdk.utils.StringUtils.isEmpty;
import static software.amazon.awssdk.utils.StringUtils.isNotBlank;

import com.squareup.javapoet.AnnotationSpec;
Expand All @@ -29,13 +30,17 @@
import java.util.Optional;
import java.util.function.Consumer;
import javax.lang.model.element.Modifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.annotations.Generated;
import software.amazon.awssdk.codegen.model.config.customization.CustomizationConfig;
import software.amazon.awssdk.codegen.model.intermediate.DocumentationModel;
import software.amazon.awssdk.codegen.model.intermediate.HasDeprecation;
import software.amazon.awssdk.core.internal.util.ClassLoaderHelper;

public final class PoetUtils {

private static final Logger log = LoggerFactory.getLogger(PoetUtils.class);

private static final AnnotationSpec GENERATED = AnnotationSpec.builder(Generated.class)
.addMember("value", "$S", "software.amazon.awssdk:codegen")
.build();
Expand Down Expand Up @@ -112,18 +117,21 @@ public static JavaFile buildJavaFile(ClassSpec spec) {
return builder.build();
}

public static Optional<Class<?>> findExtensionInterface(ClassName clientInterfaceName, Class<?> classLoaderClass) {
String extensionClassName = String.format("%sExtensionMethods", clientInterfaceName.canonicalName());
try {
Class<?> extensionClass = ClassLoaderHelper.loadClass(extensionClassName, classLoaderClass);
if (extensionClass.isInterface()) {
return Optional.of(extensionClass);
public static Optional<ClassName> findExtensionInterface(ClassName clientInterfaceName,
CustomizationConfig customizationConfig) {
String extensionInterfaceFqcn = customizationConfig.getExtensionInterface();

if (!isEmpty(extensionInterfaceFqcn)) {
ClassName interfaceName = classNameFromFqcn(extensionInterfaceFqcn);
log.info(String.format("Found client extension interface %s.", interfaceName));
if (!interfaceName.packageName().contains(clientInterfaceName.packageName())) {
throw new IllegalArgumentException(
String.format("Extension interface package is not part of service client package. "
+ "Client package: %s. Extension interface package: %s",
clientInterfaceName.packageName(), interfaceName.packageName()));
}
throw new IllegalStateException("Loaded class, but it is not an interface: " + extensionClassName + ".");
} catch (ClassNotFoundException e) {
return Optional.empty();
} catch (NoClassDefFoundError e) {
throw new IllegalStateException("Failed to load extension " + extensionClassName + ".", e);
return Optional.of(interfaceName);
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public TypeSpec poetSpec() {
ServiceMetadataProvider.class)
.build());

Optional<Class<?>> extensionClass = PoetUtils.findExtensionInterface(className, getClass());
Optional<ClassName> extensionClass = PoetUtils.findExtensionInterface(className, model.getCustomizationConfig());
extensionClass.ifPresent(result::addSuperinterface);

PoetUtils.addJavadoc(result::addJavadoc, getJavadoc());
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.squareup.javapoet.ClassName;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.codegen.model.config.customization.CustomizationConfig;
import software.amazon.awssdk.codegen.poet.PoetUtils;
import software.amazon.awssdk.codegen.poet.client.SyncClientInterface;

Expand All @@ -31,24 +32,29 @@ public class PoetUtilsTest {

@Test
public void findExtensionInterface_whenNotExists_returnEmpty() {
ClassName cl = ClassName.get("software.amazon.awssdk.codegen.poet.utils", "NoClient");
Optional<Class<?>> extensionClass = PoetUtils.findExtensionInterface(cl, SyncClientInterface.class);
ClassName cl = ClassName.get("software.amazon.awssdk.sdkservice", "SdkServiceClient");
CustomizationConfig customizationConfig = CustomizationConfig.create();
Optional<ClassName> extensionClass = PoetUtils.findExtensionInterface(cl, customizationConfig);
assertThat(extensionClass).isEmpty();
}

@Test
public void findExtensionInterface_whenExists_returnClass() {
ClassName cl = ClassName.get("software.amazon.awssdk.codegen.poet.utils","CorrectClient");
Optional<Class<?>> extensionClass = PoetUtils.findExtensionInterface(cl, getClass());
ClassName cl = ClassName.get("software.amazon.awssdk.sdkservice","SdkServiceClient");
CustomizationConfig customizationConfig = CustomizationConfig.create();
customizationConfig.setExtensionInterface("software.amazon.awssdk.sdkservice.SdkServiceClientExtensionMethods");
Optional<ClassName> extensionClass = PoetUtils.findExtensionInterface(cl, customizationConfig);
assertThat(extensionClass).isPresent();
assertThat(extensionClass.get().getSimpleName()).isEqualTo("CorrectClientExtensionMethods");
assertThat(extensionClass.get().canonicalName()).isEqualTo("software.amazon.awssdk.sdkservice.SdkServiceClientExtensionMethods");
}

@Test
public void findExtensionInterface_whenIncorrectType_throwException() {
ClassName cl = ClassName.get("software.amazon.awssdk.codegen.poet.utils","IncorrectClient");
assertThatThrownBy(() -> PoetUtils.findExtensionInterface(cl, getClass()))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("interface");
public void findExtensionInterface_differentPackages_throwsException() {
ClassName cl = ClassName.get("software.amazon.awssdk.sdkservice","SdkServiceClient");
CustomizationConfig customizationConfig = CustomizationConfig.create();
customizationConfig.setExtensionInterface("software.amazon.awssdk.someotherpackage.SdkServiceClientExtensionMethods");
assertThatThrownBy(() -> PoetUtils.findExtensionInterface(cl, customizationConfig))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("package");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"customServiceMetadata": {"contentType" : "application/json"}
"extensionInterface": "software.amazon.awssdk.services.sdkextensions.extensions.SdkExtensionsClientExtensionMethods"
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import software.amazon.awssdk.core.SdkClient;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.regions.ServiceMetadata;
import software.amazon.awssdk.services.sdkextensions.extensions.SdkExtensionsClientExtensionMethods;
import software.amazon.awssdk.services.sdkextensions.model.OneOperationRequest;
import software.amazon.awssdk.services.sdkextensions.model.OneOperationResponse;
import software.amazon.awssdk.services.sdkextensions.model.SdkExtensionsException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.services.s3.extensions;

import static org.assertj.core.api.Assertions.assertThat;
import static software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName;

import java.io.IOException;
import java.util.Random;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import software.amazon.awssdk.services.s3.S3IntegrationTestBase;

public class BucketExistsIntegrationTest extends S3IntegrationTestBase {

private static final String BUCKET = temporaryBucketName(BucketExistsIntegrationTest.class);

@BeforeClass
public static void setupFixture() throws IOException {
createBucket(BUCKET);
}

@AfterClass
public static void tearDownFixture() {
deleteBucketAndAllContents(BUCKET);
}

@Test
public void bucketExists() {
assertThat(s3.doesBucketExist(BUCKET)).isTrue();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v1 users rejoice!

It would be nice to test error conditions here as well. E.g., bucket exists but not owned by you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is primitive for sure!

}

@Test
public void bucketDoesNotExist() {
assertThat(s3.doesBucketExist(temporaryBucketName("noexist"))).isFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,6 @@
"createMethodParams": [
"clientConfiguration"
]
}
},
"extensionInterface": "software.amazon.awssdk.services.s3.extensions.S3ClientExtensionMethods"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we eventually support async extensions as well, do you foresee having 2 fields here? Would it maybe be simpler to just have the package prefix, and we can infer the rest? Or even a simple boolean if we can infer all of it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a name is better, at least a package name; leads to less inference. When I added this, I had in mind that we just slap Async on, but that's quite ugly. We could either infer both names or add a more complex structure.

}