-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e0f907
commit acfeaed
Showing
20 changed files
with
688 additions
and
6 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
cli/src/main/java/io/kestra/cli/commands/flows/namespaces/FlowNamespaceExtractCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.kestra.cli.commands.flows.namespaces; | ||
|
||
import io.kestra.cli.AbstractApiCommand; | ||
import io.kestra.cli.commands.flows.FlowValidateCommand; | ||
import io.micronaut.http.HttpRequest; | ||
import io.micronaut.http.HttpResponse; | ||
import io.micronaut.http.MediaType; | ||
import io.micronaut.http.MutableHttpRequest; | ||
import io.micronaut.http.client.exceptions.HttpClientResponseException; | ||
import io.micronaut.http.client.netty.DefaultHttpClient; | ||
import lombok.extern.slf4j.Slf4j; | ||
import picocli.CommandLine; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
@CommandLine.Command( | ||
name = "extract", | ||
description = "extract namespace flows", | ||
mixinStandardHelpOptions = true | ||
) | ||
@Slf4j | ||
public class FlowNamespaceExtractCommand extends AbstractApiCommand { | ||
private static final String DEFAULT_FILE_NAME = "flows.zip"; | ||
|
||
@CommandLine.Parameters(index = "0", description = "the namespace of templates to extract") | ||
public String namespace; | ||
|
||
@CommandLine.Parameters(index = "1", description = "the directory to extract the file to") | ||
public Path directory; | ||
|
||
@Override | ||
public Integer call() throws Exception { | ||
super.call(); | ||
|
||
try(DefaultHttpClient client = client()) { | ||
MutableHttpRequest<Object> request = HttpRequest | ||
.GET("/api/v1/flows/extract/by_query?namespace=" + namespace).accept(MediaType.APPLICATION_OCTET_STREAM); | ||
|
||
HttpResponse<byte[]> response = client.toBlocking().exchange(this.requestOptions(request), byte[].class); | ||
Path zipFile = Path.of(directory.toString(), DEFAULT_FILE_NAME); | ||
zipFile.toFile().createNewFile(); | ||
Files.write(zipFile, response.body()); | ||
|
||
stdOut("Extracted flow(s) for namespace '" + namespace + "' successfully done !"); | ||
} catch (HttpClientResponseException e) { | ||
FlowValidateCommand.handleHttpException(e, "flow"); | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
...ain/java/io/kestra/cli/commands/templates/namespaces/TemplateNamespaceExtractCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.kestra.cli.commands.templates.namespaces; | ||
|
||
import io.kestra.cli.AbstractApiCommand; | ||
import io.kestra.cli.commands.AbstractServiceNamespaceUpdateCommand; | ||
import io.kestra.cli.commands.flows.FlowValidateCommand; | ||
import io.kestra.cli.commands.templates.TemplateValidateCommand; | ||
import io.kestra.core.models.templates.Template; | ||
import io.kestra.core.serializers.YamlFlowParser; | ||
import io.micronaut.core.type.Argument; | ||
import io.micronaut.http.HttpRequest; | ||
import io.micronaut.http.HttpResponse; | ||
import io.micronaut.http.MediaType; | ||
import io.micronaut.http.MutableHttpRequest; | ||
import io.micronaut.http.client.exceptions.HttpClientResponseException; | ||
import io.micronaut.http.client.netty.DefaultHttpClient; | ||
import jakarta.inject.Inject; | ||
import lombok.extern.slf4j.Slf4j; | ||
import picocli.CommandLine; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import javax.validation.ConstraintViolationException; | ||
|
||
@CommandLine.Command( | ||
name = "extract", | ||
description = "extract namespace templates", | ||
mixinStandardHelpOptions = true | ||
) | ||
@Slf4j | ||
public class TemplateNamespaceExtractCommand extends AbstractApiCommand { | ||
private static final String DEFAULT_FILE_NAME = "templates.zip"; | ||
|
||
@CommandLine.Parameters(index = "0", description = "the namespace of templates to extract") | ||
public String namespace; | ||
|
||
@CommandLine.Parameters(index = "1", description = "the directory to extract the file to") | ||
public Path directory; | ||
|
||
@Override | ||
public Integer call() throws Exception { | ||
super.call(); | ||
|
||
try(DefaultHttpClient client = client()) { | ||
MutableHttpRequest<Object> request = HttpRequest | ||
.GET("/api/v1/templates/extract/by_query?namespace=" + namespace).accept(MediaType.APPLICATION_OCTET_STREAM); | ||
|
||
HttpResponse<byte[]> response = client.toBlocking().exchange(this.requestOptions(request), byte[].class); | ||
Path zipFile = Path.of(directory.toString(), DEFAULT_FILE_NAME); | ||
zipFile.toFile().createNewFile(); | ||
Files.write(zipFile, response.body()); | ||
|
||
stdOut("Extracted template(s) for namespace '" + namespace + "' successfully done !"); | ||
} catch (HttpClientResponseException e) { | ||
TemplateValidateCommand.handleHttpException(e, "template"); | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
...rc/test/java/io/kestra/cli/commands/flows/namespaces/FlowNamespaceExtractCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.kestra.cli.commands.flows.namespaces; | ||
|
||
import io.micronaut.configuration.picocli.PicocliRunner; | ||
import io.micronaut.context.ApplicationContext; | ||
import io.micronaut.context.env.Environment; | ||
import io.micronaut.runtime.server.EmbeddedServer; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import java.net.URL; | ||
import java.util.zip.ZipFile; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.core.Is.is; | ||
import static org.hamcrest.core.StringContains.containsString; | ||
|
||
class FlowNamespaceExtractCommandTest { | ||
@Test | ||
void run() throws IOException { | ||
URL directory = FlowNamespaceUpdateCommandTest.class.getClassLoader().getResource("flows"); | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(out)); | ||
|
||
try (ApplicationContext ctx = ApplicationContext.run(Environment.CLI, Environment.TEST)) { | ||
|
||
EmbeddedServer embeddedServer = ctx.getBean(EmbeddedServer.class); | ||
embeddedServer.start(); | ||
|
||
// we use the update command to add flows to extract | ||
String[] updateArgs = { | ||
"--server", | ||
embeddedServer.getURL().toString(), | ||
"--user", | ||
"myuser:pass:word", | ||
"io.kestra.cli", | ||
directory.getPath(), | ||
|
||
}; | ||
PicocliRunner.call(FlowNamespaceUpdateCommand.class, ctx, updateArgs); | ||
assertThat(out.toString(), containsString("3 flow(s)")); | ||
|
||
// then we extract them | ||
String[] extractArgs = { | ||
"--server", | ||
embeddedServer.getURL().toString(), | ||
"--user", | ||
"myuser:pass:word", | ||
"io.kestra.cli", | ||
"/tmp", | ||
}; | ||
PicocliRunner.call(FlowNamespaceExtractCommand.class, ctx, extractArgs); | ||
File file = new File("/tmp/flows.zip"); | ||
assertThat(file.exists(), is(true)); | ||
ZipFile zipFile = new ZipFile(file); | ||
assertThat(zipFile.stream().count(), is(3L)); | ||
|
||
file.delete(); | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...java/io/kestra/cli/commands/templates/namespaces/TemplateNamespaceExtractCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package io.kestra.cli.commands.templates.namespaces; | ||
|
||
import io.micronaut.configuration.picocli.PicocliRunner; | ||
import io.micronaut.context.ApplicationContext; | ||
import io.micronaut.context.env.Environment; | ||
import io.micronaut.runtime.server.EmbeddedServer; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import java.net.URL; | ||
import java.util.zip.ZipFile; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.core.StringContains.containsString; | ||
import static org.hamcrest.core.Is.is; | ||
|
||
class TemplateNamespaceExtractCommandTest { | ||
@Test | ||
void run() throws IOException { | ||
URL directory = TemplateNamespaceExtractCommandTest.class.getClassLoader().getResource("templates"); | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(out)); | ||
|
||
try (ApplicationContext ctx = ApplicationContext.run(Environment.CLI, Environment.TEST)) { | ||
|
||
EmbeddedServer embeddedServer = ctx.getBean(EmbeddedServer.class); | ||
embeddedServer.start(); | ||
|
||
// we use the update command to add templates to extract | ||
String[] args = { | ||
"--server", | ||
embeddedServer.getURL().toString(), | ||
"--user", | ||
"myuser:pass:word", | ||
"io.kestra.tests", | ||
directory.getPath(), | ||
|
||
}; | ||
PicocliRunner.call(TemplateNamespaceUpdateCommand.class, ctx, args); | ||
assertThat(out.toString(), containsString("3 template(s)")); | ||
|
||
// then we extract them | ||
String[] extractArgs = { | ||
"--server", | ||
embeddedServer.getURL().toString(), | ||
"--user", | ||
"myuser:pass:word", | ||
"io.kestra.tests", | ||
"/tmp", | ||
}; | ||
PicocliRunner.call(TemplateNamespaceExtractCommand.class, ctx, extractArgs); | ||
File file = new File("/tmp/templates.zip"); | ||
assertThat(file.exists(), is(true)); | ||
ZipFile zipFile = new ZipFile(file); | ||
assertThat(zipFile.stream().count(), is(3L)); | ||
|
||
file.delete(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.