Skip to content

Commit

Permalink
Update the OCI Java SDK version (#745)
Browse files Browse the repository at this point in the history
* Update the OCI Java SDK version

The version removes the bmc-graalvm-addon module and all
the SdkClientsMetadata classes. As a result the sdk
processor is broken. This is fixed by browsing the oci
sdk repository and finding the client files explicitly.

* Improve object storage spec
  • Loading branch information
andriy-dmytruk authored Nov 17, 2023
1 parent 08927a2 commit 22ea299
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@ static Collection<String> parseOciBomArtifacts(String version) {
return ociBom.dependencyManagement.dependencies.dependency.artifactId*.text()
}

static Collection<File> parseOciSdkModules() {
var modules = new File("checkouts/oci-java-sdk").listFiles().findAll {it.name.startsWith("bmc-")}
var secondLevelModules = modules.collectMany {
it.listFiles().findAll{ it.name.startsWith("bmc-") }
}
return (modules + secondLevelModules)
.findAll {new File(it.path + "/src/main/java").exists() }
}

static Collection<String> parseOciSdkClientClassNames() {
return parseOciSdkModules()
.collect{ new File(it.path + "/src/main/java/com/oracle/bmc") }
.findAll{ it.exists() }
// Get all the packages com/oracle/bmc/*
.collectMany{List.of(it.listFiles()) }
// Get all the files com/oracle/bmc/package/*Client.java
.collectMany {
var packageName = "com.oracle.bmc.$it.name."
it.list()
.findAll(name -> name.endsWith("Client.java"))
.collect(name -> packageName + (name - ".java"))
}
}

if (!extraPropertiesExtension.has('ociArtifacts')) {
String ociVersion = libs.oci.bom.get().version
def ociArtifacts = parseOciBomArtifacts(ociVersion)
Expand All @@ -27,3 +51,9 @@ if (!extraPropertiesExtension.has('ociArtifacts')) {
extraPropertiesExtension.set('ociArtifacts', commonOciArtifacts)
extraPropertiesExtension.set('ociVersion', ociVersion)
}

if (!extraPropertiesExtension.has('ociSdkClientClasses')) {
extraPropertiesExtension.set('ociSdkClientClasses', parseOciSdkClientClassNames().join(","))
}


2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ commons-lang3 = '3.12.0'
shadow = '8.0.0'
groovy = "4.0.10"
spock = "2.3-groovy-4.0"
oci = "3.25.2"
oci = "3.29.0"
ojdbc = "21.10.0.0"
protobuf = '0.9.4'
netty-http3 = "0.0.22.Final"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package io.micronaut.oraclecloud.client

import com.oracle.bmc.auth.AuthenticationDetailsProvider
import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider
import com.oracle.bmc.objectstorage.ObjectStorageClient
import com.oracle.bmc.objectstorage.model.Bucket.AutoTiering
import com.oracle.bmc.objectstorage.model.CommitMultipartUploadDetails
import com.oracle.bmc.objectstorage.model.CommitMultipartUploadPartDetails
import com.oracle.bmc.objectstorage.model.CreateBucketDetails
import com.oracle.bmc.objectstorage.model.CreateMultipartUploadDetails
import com.oracle.bmc.objectstorage.requests.*
import com.oracle.bmc.objectstorage.requests.GetBucketRequest.Fields
import io.micronaut.context.annotation.Property
import io.micronaut.context.annotation.Requires
import io.micronaut.core.annotation.NonNull
Expand Down Expand Up @@ -83,14 +84,20 @@ class OciObjectStorageSpec extends Specification {

void "test get bucket"() {
when:
var request = GetBucketRequest.builder().namespaceName(namespace).bucketName(bucketName).build()
var request = GetBucketRequest.builder()
.namespaceName(namespace)
.fields([Fields.ApproximateSize, Fields.AutoTiering])
.bucketName(bucketName).build()
var response = client.getBucket(request)

then:
response.bucket.compartmentId == compartmentId
response.bucket.name == bucketName
response.bucket.isReadOnly == false
response.bucket.id != null
response.bucket.approximateSize != null
response.bucket.autoTiering != AutoTiering.UnknownEnumValue
response.bucket.approximateCount == null
}

void "test create object"() {
Expand Down
3 changes: 0 additions & 3 deletions oraclecloud-sdk-processor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ plugins {
dependencies {
implementation libs.javapoet
implementation mn.micronaut.inject.java
implementation(libs.oci.graalvm) {
transitive = false
}
implementation(libs.oci.common) {
transitive = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package io.micronaut.oraclecloud.clients.processor;

import com.oracle.bmc.SdkClients;
import com.oracle.bmc.graalvm.SdkClientPackages;
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
Expand All @@ -39,7 +37,6 @@
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.convert.value.MutableConvertibleValues;
import io.micronaut.core.naming.NameUtils;
import io.micronaut.core.reflect.ClassUtils;
import io.micronaut.inject.visitor.TypeElementVisitor;
import jakarta.inject.Singleton;

Expand Down Expand Up @@ -69,10 +66,8 @@
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
Expand All @@ -90,7 +85,15 @@
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class OracleCloudSdkProcessor extends AbstractProcessor {

/**
* The package to which client factories will be written.
*/
public static final String CLIENT_PACKAGE = "io.micronaut.oraclecloud.clients";

/**
* Processing environment option used to specify the client classes to process.
*/
public static final String OCI_SDK_CLIENT_CLASSES_OPTION = "ociSdkClientClasses";
private Filer filer;
private Messager messager;
private Elements elements;
Expand Down Expand Up @@ -152,6 +155,14 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
return false;
}

private List<String> resolveClientNames(Element e) {
String ociClientClasses = processingEnv.getOptions().get(OCI_SDK_CLIENT_CLASSES_OPTION);
if (ociClientClasses == null) {
return List.of();
}
return List.of(ociClientClasses.split(","));
}

private void writeRxJava2Clients(Element e, String packageName, String simpleName) {
if (simpleName.endsWith("AsyncClient")) {

Expand Down Expand Up @@ -371,13 +382,7 @@ private String writeClientFactory(Element e, String packageName, String simpleNa
final String factoryPackageName = packageName.replace("com.oracle.bmc", CLIENT_PACKAGE);
final TypeSpec.Builder builder = defineSuperclass(packageName, simpleName, factoryName);
final MethodSpec.Builder constructor = buildConstructor(simpleName, builder);
ClassName builderType = ClassName.get(packageName, simpleName + ".Builder");
if (Stream.of(
"KmsCrypto",
"KmsManagement"
).anyMatch(simpleName::startsWith)) {
builderType = ClassName.get(packageName, simpleName + "Builder");
}
ClassName builderType = getBuilderClassNameForClient(packageName, simpleName);
builder.addField(FieldSpec.builder(builderType, "builder", Modifier.PRIVATE).build());
builder.addAnnotation(Factory.class);
final ClassName authProviderType = ClassName.get("com.oracle.bmc.auth", "AbstractAuthenticationDetailsProvider");
Expand Down Expand Up @@ -438,13 +443,7 @@ private String writeClientFactory(Element e, String packageName, String simpleNa

private TypeSpec.Builder defineSuperclass(String packageName, String simpleName, String factoryName) {
final TypeSpec.Builder builder = TypeSpec.classBuilder(factoryName);
ClassName builderClassName = ClassName.get(packageName, simpleName + ".Builder");
if (Stream.of(
"KmsCrypto",
"KmsManagement"
).anyMatch(simpleName::startsWith)) {
builderClassName = ClassName.get(packageName, simpleName + "Builder");
}
ClassName builderClassName = getBuilderClassNameForClient(packageName, simpleName);
builder.superclass(ParameterizedTypeName.get(
ClassName.get("io.micronaut.oraclecloud.core.sdk", "AbstractSdkClientFactory"),
builderClassName,
Expand All @@ -468,37 +467,16 @@ private MethodSpec.Builder buildConstructor(String simpleName, TypeSpec.Builder
return constructor;
}

private List<String> resolveClientNames(Element e) {
return resolveOraceCloudClientNamesFromGraalVmAddons();
}

private List<String> resolveOraceCloudClientNamesFromGraalVmAddons() {
List<String> results = new ArrayList<>();
Class<?> metadataClass = ClassUtils.forName("com.oracle.bmc.graalvm.SdkAutomaticFeatureMetadata", getClass().getClassLoader()).orElse(null);
if (metadataClass != null) {
SdkClientPackages allSdkClientPackages =
metadataClass.getAnnotation(SdkClientPackages.class);
// oci sdk bug: this contains duplicate entries
List<String> allSdkClientPackagesStrings = new ArrayList<>(new LinkedHashSet<>(Arrays.asList(allSdkClientPackages.value())));
if (!allSdkClientPackagesStrings.contains("com.oracle.bmc.identity.SdkClientsMetadata")) {
allSdkClientPackagesStrings.add("com.oracle.bmc.identity.SdkClientsMetadata");
}
for (String sdkClientsMetadataPath : allSdkClientPackagesStrings) {
Class<?> sdkClientsMetadataClass = ClassUtils.forName(sdkClientsMetadataPath, getClass().getClassLoader()).orElse(null);
if (sdkClientsMetadataClass != null) {
SdkClients declaredAnnotation = sdkClientsMetadataClass.getDeclaredAnnotation(SdkClients.class);
if (declaredAnnotation != null) {
Class<?>[] allSdkClients = declaredAnnotation.value();
for (Class<?> sdkClient : allSdkClients) {
if (!isSdkInternal(sdkClient.getCanonicalName())) {
results.add(sdkClient.getName());
}
}
}
}
}
private ClassName getBuilderClassNameForClient(String packageName, String clientSimpleName) {
if (Stream.of(
"KmsCrypto",
"KmsManagement",
"StreamClient",
"StreamAsyncClient"
).anyMatch(clientSimpleName::startsWith)) {
return ClassName.get(packageName, clientSimpleName + "Builder");
}
return results;
return ClassName.get(packageName, clientSimpleName + ".Builder");
}

private static boolean isSdkInternal(String key) {
Expand Down
1 change: 1 addition & 0 deletions oraclecloud-sdk-reactor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies {
}

compileJava.options.fork = true
compileJava.options.compilerArgs.add("-AociSdkClientClasses=$gradle.ociSdkClientClasses")

tasks.withType(me.champeau.gradle.japicmp.JapicmpTask) {
// Ignore changes in the generated factories for the new OCI Java SDK
Expand Down
1 change: 1 addition & 0 deletions oraclecloud-sdk-rxjava2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies {
}

compileJava.options.fork = true
compileJava.options.compilerArgs.add("-AociSdkClientClasses=$gradle.ociSdkClientClasses")

tasks.withType(me.champeau.gradle.japicmp.JapicmpTask) {
// Ignore changes in the generated factories for the new OCI Java SDK
Expand Down
1 change: 1 addition & 0 deletions oraclecloud-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {

spotlessJavaMiscCheck.enabled = false
compileJava.options.fork = true
compileJava.options.compilerArgs.add("-AociSdkClientClasses=$gradle.ociSdkClientClasses")

tasks.withType(me.champeau.gradle.japicmp.JapicmpTask) {
// Ignore changes in the generated factories for the new OCI Java SDK
Expand Down

0 comments on commit 22ea299

Please sign in to comment.