Skip to content

Commit

Permalink
Merge pull request #502 from rkpattnaik780/new-sdk
Browse files Browse the repository at this point in the history
fix: use kiota generated Java SDK
  • Loading branch information
henryZrncik authored Mar 27, 2023
2 parents ff4cf93 + 7305c67 commit e94a4cc
Show file tree
Hide file tree
Showing 57 changed files with 1,144 additions and 1,070 deletions.
84 changes: 52 additions & 32 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,69 @@
<maven.checkstyle.version>3.2.1</maven.checkstyle.version>
<maven.failsafe.version>3.0.0-M7</maven.failsafe.version>
<download.maven.plugin.version>1.6.8</download.maven.plugin.version>
<fabric8.version>6.4.0</fabric8.version>
<fabric8.version>6.4.1</fabric8.version>
<sundrio.version>0.93.2</sundrio.version>
<!-- See 'Update the rhoas-model dependency' in the README.md on how to build and update the rhoas-model -->
<rhoas-model.version>0.9.7</rhoas-model.version>
<sdk.version>1.0.1</sdk.version>
</properties>

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.github.andreaTP.rh-java-sdk</groupId>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>6.2.3.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-multipart-provider</artifactId>
<version>6.2.3.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>6.2.3.Final</version>
</dependency>
<dependency>
<groupId>com.microsoft.kiota</groupId>
<artifactId>microsoft-kiota-http-okHttp</artifactId>
<version>0.3.2</version>
</dependency>

<dependency>
<groupId>com.redhat.cloud</groupId>
<artifactId>account-management-sdk</artifactId>
<version>0.0.5-SNAPSHOT</version>
<version>${sdk.version}</version>
</dependency>
<dependency>
<groupId>com.redhat.cloud</groupId>
<artifactId>kafka-instance-sdk</artifactId>
<version>${sdk.version}</version>
</dependency>
<dependency>
<groupId>com.redhat.cloud</groupId>
<artifactId>kafka-management-sdk</artifactId>
<version>${sdk.version}</version>
</dependency>
<dependency>
<groupId>com.redhat.cloud</groupId>
<artifactId>registry-instance-sdk</artifactId>
<version>${sdk.version}</version>
</dependency>
<dependency>
<groupId>com.github.andreaTP.kiota-utils</groupId>
<groupId>com.redhat.cloud</groupId>
<artifactId>registry-management-sdk</artifactId>
<version>${sdk.version}</version>
</dependency>
<dependency>
<groupId>com.redhat.cloud</groupId>
<artifactId>service-accounts-sdk</artifactId>
<version>${sdk.version}</version>
</dependency>
<dependency>
<groupId>com.redhat.cloud</groupId>
<artifactId>kiota-rh-auth</artifactId>
<version>0.1.5</version>
<version>0.0.3</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
Expand Down Expand Up @@ -147,31 +187,11 @@
<artifactId>joda-time</artifactId>
<version>2.12.2</version>
</dependency>
<dependency>
<groupId>com.redhat.cloud</groupId>
<artifactId>kafka-management-sdk</artifactId>
<version>0.31.0</version>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
<version>0.2.5</version>
</dependency>
<dependency>
<groupId>com.redhat.cloud</groupId>
<artifactId>registry-management-sdk</artifactId>
<version>0.31.0</version>
</dependency>
<dependency>
<groupId>com.redhat.cloud</groupId>
<artifactId>kafka-instance-sdk</artifactId>
<version>0.31.0</version>
</dependency>
<dependency>
<groupId>com.redhat.cloud</groupId>
<artifactId>service-accounts-sdk</artifactId>
<version>0.31.0</version>
</dependency>
<dependency>
<groupId>io.apicurio</groupId>
<artifactId>apicurio-registry-client</artifactId>
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/io/managed/services/test/cli/AsyncProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.microsoft.kiota.serialization.JsonParseNodeFactory;
import com.microsoft.kiota.serialization.ParseNode;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.input.TeeInputStream;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.openapitools.jackson.nullable.JsonNullableModule;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
Expand Down Expand Up @@ -167,6 +168,15 @@ public <T> T asJson(Class<T> c) {
}
}

public ParseNode parseNodeFromProcessOutput() {
InputStream targetStream = new java.io.ByteArrayInputStream(this.stdoutAsString().getBytes());
log.debug("process stdout: ");
log.debug(this.stdoutAsString());

var factory = new JsonParseNodeFactory();
return factory.getParseNode("application/json", targetStream);
}

public void readAll() {
readAll(liveStdout);
readAll(liveStderr);
Expand Down
67 changes: 40 additions & 27 deletions src/main/java/io/managed/services/test/cli/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import lombok.extern.log4j.Log4j2;
import org.openapitools.jackson.nullable.JsonNullableModule;
import org.testng.Assert;


import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
Expand All @@ -36,7 +34,6 @@
import java.util.Locale;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

import static java.time.Duration.ofMinutes;
import static lombok.Lombok.sneakyThrow;

Expand Down Expand Up @@ -134,7 +131,8 @@ public String help() {

public KafkaRequest createKafka(String name) throws CliGenericException {
return retryKafkaCreation(() -> exec("kafka", "create", "--bypass-checks", "--name", name, "--provider", Environment.CLOUD_PROVIDER, "--region", Environment.DEFAULT_KAFKA_REGION))
.asJson(KafkaRequest.class);
.parseNodeFromProcessOutput()
.getObjectValue(KafkaRequest::createFromDiscriminatorValue);
}

public void deleteKafka(String id) throws CliGenericException {
Expand All @@ -143,12 +141,14 @@ public void deleteKafka(String id) throws CliGenericException {

public KafkaRequest describeKafkaById(String id) throws CliGenericException {
return retry(() -> exec("kafka", "describe", "--id", id))
.asJson(KafkaRequest.class);
.parseNodeFromProcessOutput()
.getObjectValue(KafkaRequest::createFromDiscriminatorValue);
}

public KafkaRequest describeKafkaByName(String name) throws CliGenericException {
return retry(() -> exec("kafka", "describe", "--name", name))
.asJson(KafkaRequest.class);
.parseNodeFromProcessOutput()
.getObjectValue(KafkaRequest::createFromDiscriminatorValue);
}

public void useKafka(String id) throws CliGenericException {
Expand All @@ -157,25 +157,26 @@ public void useKafka(String id) throws CliGenericException {

public KafkaRequestList listKafka() throws CliGenericException {
return retry(() -> exec("kafka", "list", "-o", "json"))
.asJson(KafkaRequestList.class);
.parseNodeFromProcessOutput()
.getObjectValue(KafkaRequestList::createFromDiscriminatorValue);
}

public KafkaRequestList searchKafkaByName(String name) throws CliGenericException {
var p = retry(() -> exec("kafka", "list", "--search", name, "-o", "json"));
if (p.stderrAsString().contains("No Kafka instances were found")) {
return new KafkaRequestList();
}
return p.asJson(KafkaRequestList.class);
return retry(() -> exec("kafka", "list", "--search", name, "-o", "json"))
.parseNodeFromProcessOutput()
.getObjectValue(KafkaRequestList::createFromDiscriminatorValue);
}

public ServiceAccountData describeServiceAccount(String id) throws CliGenericException {
return retry(() -> exec("service-account", "describe", "--id", id))
.asJson(ServiceAccountData.class);
.parseNodeFromProcessOutput()
.getObjectValue(ServiceAccountData::createFromDiscriminatorValue);
}

public ServiceAccountData[] listServiceAccount() throws CliGenericException {
public List<ServiceAccountData> listServiceAccount() throws CliGenericException {
return retry(() -> exec("service-account", "list", "-o", "json"))
.asJson(ServiceAccountData[].class);
.parseNodeFromProcessOutput()
.getCollectionOfObjectValues(ServiceAccountData::createFromDiscriminatorValue);
}

public void deleteServiceAccount(String id) throws CliGenericException {
Expand All @@ -188,12 +189,14 @@ public void createServiceAccount(String name, Path path) throws CliGenericExcept

public Topic createTopic(String topicName) throws CliGenericException {
return retry(() -> exec("kafka", "topic", "create", "--name", topicName, "-o", "json"))
.asJson(Topic.class);
.parseNodeFromProcessOutput()
.getObjectValue(Topic::createFromDiscriminatorValue);
}

public Topic createTopic(String topicName, int partitions) throws CliGenericException {
return retry(() -> exec("kafka", "topic", "create", "--name", topicName, "--partitions", String.valueOf(partitions), "-o", "json"))
.asJson(Topic.class);
.parseNodeFromProcessOutput()
.getObjectValue(Topic::createFromDiscriminatorValue);
}

public void deleteTopic(String topicName) throws CliGenericException {
Expand All @@ -202,12 +205,14 @@ public void deleteTopic(String topicName) throws CliGenericException {

public TopicsList listTopics() throws CliGenericException {
return retry(() -> exec("kafka", "topic", "list", "-o", "json"))
.asJson(TopicsList.class);
.parseNodeFromProcessOutput()
.getObjectValue(TopicsList::createFromDiscriminatorValue);
}

public Topic describeTopic(String topicName) throws CliGenericException {
return retry(() -> exec("kafka", "topic", "describe", "--name", topicName, "-o", "json"))
.asJson(Topic.class);
.parseNodeFromProcessOutput()
.getObjectValue(Topic::createFromDiscriminatorValue);
}

public void updateTopic(String topicName, String retentionTime) throws CliGenericException {
Expand All @@ -216,7 +221,8 @@ public void updateTopic(String topicName, String retentionTime) throws CliGeneri

public ConsumerGroupList listConsumerGroups() throws CliGenericException {
return retry(() -> exec("kafka", "consumer-group", "list", "-o", "json"))
.asJson(ConsumerGroupList.class);
.parseNodeFromProcessOutput()
.getObjectValue(ConsumerGroupList::createFromDiscriminatorValue);
}

public void deleteConsumerGroup(String id) throws CliGenericException {
Expand All @@ -225,7 +231,8 @@ public void deleteConsumerGroup(String id) throws CliGenericException {

public ConsumerGroup describeConsumerGroup(String name) throws CliGenericException {
return retry(() -> exec("kafka", "consumer-group", "describe", "--id", name, "-o", "json"))
.asJson(ConsumerGroup.class);
.parseNodeFromProcessOutput()
.getObjectValue(ConsumerGroup::createFromDiscriminatorValue);
}

public void connectCluster(String token, String kubeconfig, String serviceType) throws CliGenericException {
Expand Down Expand Up @@ -253,12 +260,14 @@ public void createAcl(ACLEntityType aclEntityType, String entityIdentificator, A
//// kafka acl list
public AclBindingListPage listACLs() throws CliGenericException {
return retry(() -> exec("kafka", "acl", "list", "-o", "json"))
.asJson(AclBindingListPage.class);
.parseNodeFromProcessOutput()
.getObjectValue(AclBindingListPage::createFromDiscriminatorValue);
}

public AclBindingListPage listACLs(ACLEntityType aclEntityType, String entityIdentificator) throws CliGenericException {
return retry(() -> exec("kafka", "acl", "list", aclEntityType.flag, entityIdentificator, "-o", "json"))
.asJson(AclBindingListPage.class);
.parseNodeFromProcessOutput()
.getObjectValue(AclBindingListPage::createFromDiscriminatorValue);
}

//// kafka acl delete
Expand All @@ -285,22 +294,26 @@ public void grantAdminAcl(ACLEntityType aclEntityType, String entityIdentificato
*/
public Registry createServiceRegistry(String name) throws CliGenericException {
return retry(() -> exec("service-registry", "create", "--name", name))
.asJson(Registry.class);
.parseNodeFromProcessOutput()
.getObjectValue(Registry::createFromDiscriminatorValue);
}

public Registry describeServiceRegistry(String id) throws CliGenericException {
return retry(() -> exec("service-registry", "describe", "--id", id))
.asJson(Registry.class);
.parseNodeFromProcessOutput()
.getObjectValue(Registry::createFromDiscriminatorValue);
}

public Registry describeServiceRegistry() throws CliGenericException {
return retry(() -> exec("service-registry", "describe"))
.asJson(Registry.class);
.parseNodeFromProcessOutput()
.getObjectValue(Registry::createFromDiscriminatorValue);
}

public RegistryList listServiceRegistry() throws CliGenericException {
return retry(() -> exec("service-registry", "list", "-o", "json"))
.asJson(RegistryList.class);
.parseNodeFromProcessOutput()
.getObjectValue(RegistryList::createFromDiscriminatorValue);
}

public void useServiceRegistry(String id) throws CliGenericException {
Expand Down
31 changes: 28 additions & 3 deletions src/main/java/io/managed/services/test/cli/CLIUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import java.nio.file.Paths;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -181,8 +180,28 @@ public static void waitUntilRegistryIsDeleted(CLI cli, String registryId)
});
}

public static KafkaRequest applyKafkaInstance(CLI cli, String kafkaInstanceName) throws CliGenericException, KafkaUnknownHostsException, KafkaNotReadyException, InterruptedException {
Optional<KafkaRequest> optionalKafka = cli.listKafka().getItems().stream().filter(e -> kafkaInstanceName.equals(e.getName())).findAny();
KafkaRequest kafka = null;
if (optionalKafka.isPresent()) {
LOGGER.info("kafka instance with name {} is present", kafkaInstanceName);
kafka = optionalKafka.get();
} else {
LOGGER.info("kafka instance with name {} is not present, it will be created now", kafkaInstanceName);
kafka = cli.createKafka(kafkaInstanceName);
LOGGER.debug(kafka);

// updated version of kafka after it is ready needs to be passed
LOGGER.info("wait for kafka instance: {}", kafka.getId());
kafka = CLIUtils.waitUntilKafkaIsReady(cli, kafka.getId());
}

cli.useKafka(kafka.getId());
return kafka;
}

public static Optional<ServiceAccountData> getServiceAccountByName(CLI cli, String name) throws CliGenericException {
return Arrays.stream(cli.listServiceAccount()).filter(sa -> name.equals(sa.getName())).findAny();
return cli.listServiceAccount().stream().filter(sa -> name.equals(sa.getName())).findAny();
}

public static ServiceAccountSecret createServiceAccount(CLI cli, String name) throws CliGenericException {
Expand All @@ -204,7 +223,13 @@ public static ConsumerGroup waitForConsumerGroup(CLI cli, String name) {
public static Registry waitUntilServiceRegistryIsReady(CLI cli, String id)
throws InterruptedException, CliGenericException, RegistryNotReadyException {

return RegistryMgmtApiUtils.waitUntilRegistryIsReady(() -> cli.describeServiceRegistry(id));
return RegistryMgmtApiUtils.waitUntilRegistryIsReady(() -> {
try {
return cli.describeServiceRegistry(id);
} catch (CliGenericException e) {
throw new RuntimeException(e);
}
});
}

public static Topic applyTopic(CLI cli, String topicName, int partitions) throws CliGenericException {
Expand Down
Loading

0 comments on commit e94a4cc

Please sign in to comment.