-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
feat: extract flows and templates #984
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c22fe91
feat: extract flows and templates
loicmathieu abe57c3
fix: feedback from code review
loicmathieu 5e470b9
fix(webserver): extract by-ids based on a lit of IdWithNamespace
loicmathieu 81c737d
chore(webserver): @ApiResponses is not needed as @ApiResponse is repe…
loicmathieu 48ff49d
fix(webserver): remove un-needed annotation value
loicmathieu 0f08b12
feat(ui): Implement extract flows endpoints
Skraye a70fb3e
fix: rename extract to export
loicmathieu cf5bc7a
cleanup
tchiotludo b690831
select all don't work
tchiotludo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
54 changes: 54 additions & 0 deletions
54
cli/src/main/java/io/kestra/cli/commands/flows/FlowExportCommand.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; | ||
|
||
import io.kestra.cli.AbstractApiCommand; | ||
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 = "export", | ||
description = "export flows to a zip file", | ||
mixinStandardHelpOptions = true | ||
) | ||
@Slf4j | ||
public class FlowExportCommand extends AbstractApiCommand { | ||
private static final String DEFAULT_FILE_NAME = "flows.zip"; | ||
|
||
@CommandLine.Option(names = {"--namespace"}, description = "the namespace of flows to export") | ||
public String namespace; | ||
|
||
@CommandLine.Parameters(index = "0", description = "the directory to export 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/export/by-query" + (namespace != null ? "?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("Exporting flow(s) for namespace '" + namespace + "' successfully done !"); | ||
} catch (HttpClientResponseException e) { | ||
FlowValidateCommand.handleHttpException(e, "flow"); | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
} |
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
53 changes: 53 additions & 0 deletions
53
cli/src/main/java/io/kestra/cli/commands/templates/TemplateExportCommand.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,53 @@ | ||
package io.kestra.cli.commands.templates; | ||
|
||
import io.kestra.cli.AbstractApiCommand; | ||
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 = "export", | ||
description = "export templates to a zip file", | ||
mixinStandardHelpOptions = true | ||
) | ||
@Slf4j | ||
public class TemplateExportCommand extends AbstractApiCommand { | ||
private static final String DEFAULT_FILE_NAME = "templates.zip"; | ||
|
||
@CommandLine.Option(names = {"--namespace"}, description = "the namespace of templates to export") | ||
public String namespace; | ||
|
||
@CommandLine.Parameters(index = "0", description = "the directory to export 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/export/by-query" + (namespace != null ? "?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("Exporting 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
cli/src/test/java/io/kestra/cli/commands/flows/FlowExportCommandTest.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; | ||
|
||
import io.kestra.cli.commands.flows.namespaces.FlowNamespaceUpdateCommand; | ||
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 FlowExportCommandTest { | ||
@Test | ||
void run() throws IOException { | ||
URL directory = FlowExportCommandTest.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 export them | ||
String[] exportArgs = { | ||
"--server", | ||
embeddedServer.getURL().toString(), | ||
"--user", | ||
"myuser:pass:word", | ||
"--namespace", | ||
"io.kestra.cli", | ||
"/tmp", | ||
}; | ||
PicocliRunner.call(FlowExportCommand.class, ctx, exportArgs); | ||
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(); | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
cli/src/test/java/io/kestra/cli/commands/templates/TemplateExportCommandTest.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,66 @@ | ||
package io.kestra.cli.commands.templates; | ||
|
||
import io.kestra.cli.commands.templates.namespaces.TemplateNamespaceUpdateCommand; | ||
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 TemplateExportCommandTest { | ||
@Test | ||
void run() throws IOException { | ||
URL directory = TemplateExportCommandTest.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 export them | ||
String[] exportArgs = { | ||
"--server", | ||
embeddedServer.getURL().toString(), | ||
"--user", | ||
"myuser:pass:word", | ||
"--namespace", | ||
"io.kestra.tests", | ||
"/tmp", | ||
}; | ||
PicocliRunner.call(TemplateExportCommand.class, ctx, exportArgs); | ||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this method is not necessary, a simple mapper will generate the source on templates
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I setup the object mapper once for all and use it directly.
By the way, I think the same can be done on the flow class, as the
Flow.generateSource()
method is now only used in tests this should be fine to change it the same way I did for templates.