-
Notifications
You must be signed in to change notification settings - Fork 16
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
C2-2432: implement create namespace (database) command #88
Merged
Merged
Changes from all commits
Commits
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
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
4 changes: 4 additions & 0 deletions
4
src/main/java/io/stargate/sgv2/jsonapi/api/model/command/CollectionCommand.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,4 @@ | ||
package io.stargate.sgv2.jsonapi.api.model.command; | ||
|
||
/** Interface for all commands executed against a collection in a namespace (database). */ | ||
public interface CollectionCommand extends Command {} |
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
4 changes: 4 additions & 0 deletions
4
src/main/java/io/stargate/sgv2/jsonapi/api/model/command/DatabaseCommand.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,4 @@ | ||
package io.stargate.sgv2.jsonapi.api.model.command; | ||
|
||
/** Interface for all commands executed against a namespace (database). */ | ||
public interface DatabaseCommand extends Command {} |
7 changes: 7 additions & 0 deletions
7
src/main/java/io/stargate/sgv2/jsonapi/api/model/command/GeneralCommand.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,7 @@ | ||
package io.stargate.sgv2.jsonapi.api.model.command; | ||
|
||
/** | ||
* Interface for all general commands, that are not executed against a namespace (database) nor a | ||
* collection . | ||
*/ | ||
public interface GeneralCommand extends Command {} |
2 changes: 1 addition & 1 deletion
2
src/main/java/io/stargate/sgv2/jsonapi/api/model/command/ModifyCommand.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package io.stargate.sgv2.jsonapi.api.model.command; | ||
|
||
/** Base for any commands that modify, such as insert, delete, update, etc. */ | ||
public interface ModifyCommand extends Command {} | ||
public interface ModifyCommand extends CollectionCommand {} |
2 changes: 1 addition & 1 deletion
2
src/main/java/io/stargate/sgv2/jsonapi/api/model/command/ReadCommand.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package io.stargate.sgv2.jsonapi.api.model.command; | ||
|
||
/** Basic interface for all read commands. */ | ||
public interface ReadCommand extends Command {} | ||
public interface ReadCommand extends CollectionCommand {} |
3 changes: 0 additions & 3 deletions
3
src/main/java/io/stargate/sgv2/jsonapi/api/model/command/SchemaChangeCommand.java
This file was deleted.
Oops, something went wrong.
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
58 changes: 58 additions & 0 deletions
58
src/main/java/io/stargate/sgv2/jsonapi/api/model/command/impl/CreateDatabaseCommand.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,58 @@ | ||
package io.stargate.sgv2.jsonapi.api.model.command.impl; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAnySetter; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonTypeName; | ||
import io.stargate.sgv2.jsonapi.api.model.command.GeneralCommand; | ||
import java.util.Map; | ||
import javax.annotation.Nullable; | ||
import javax.validation.Valid; | ||
import javax.validation.constraints.NotBlank; | ||
import javax.validation.constraints.NotNull; | ||
import javax.validation.constraints.Pattern; | ||
import javax.validation.constraints.Size; | ||
import org.eclipse.microprofile.openapi.annotations.media.Schema; | ||
|
||
@Schema(description = "Command that creates a namespace (database).") | ||
@JsonTypeName("createDatabase") | ||
public record CreateDatabaseCommand( | ||
@NotBlank @Size(min = 1, max = 48) @Schema(description = "Name of the database") String name, | ||
@Nullable @Valid CreateDatabaseCommand.Options options) | ||
implements GeneralCommand { | ||
|
||
@Schema( | ||
name = "CreateDatabaseCommand.Options", | ||
description = "Options for creating a new database.") | ||
public record Options(@Nullable @Valid Replication replication) {} | ||
|
||
/** | ||
* Replication options for the create namespace. | ||
* | ||
* @param strategy Cassandra keyspace strategy class name to use (SimpleStrategy or | ||
* NetworkTopologyStrategy). | ||
* @param strategyOptions Options for each strategy. For <code>SimpleStrategy</code>, | ||
* `replication_factor` is optional. For the <code>NetworkTopologyStrategy</code> each data | ||
* center with replication. | ||
*/ | ||
@Schema(description = "Cassandra based replication settings.") | ||
// no record due to the @JsonAnySetter, see | ||
// https://github.com/FasterXML/jackson-databind/issues/562 | ||
public static class Replication { | ||
@NotNull() | ||
@Pattern(regexp = "SimpleStrategy|NetworkTopologyStrategy") | ||
@JsonProperty("class") | ||
private String strategy; | ||
|
||
@JsonAnySetter | ||
@Schema(hidden = true) | ||
private Map<String, Integer> strategyOptions; | ||
|
||
public String strategy() { | ||
return strategy; | ||
} | ||
|
||
public Map<String, Integer> strategyOptions() { | ||
return strategyOptions; | ||
} | ||
} | ||
} |
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
77 changes: 77 additions & 0 deletions
77
src/main/java/io/stargate/sgv2/jsonapi/api/v1/GeneralResource.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,77 @@ | ||
package io.stargate.sgv2.jsonapi.api.v1; | ||
|
||
import io.smallrye.mutiny.Uni; | ||
import io.stargate.sgv2.jsonapi.api.model.command.CommandContext; | ||
import io.stargate.sgv2.jsonapi.api.model.command.CommandResult; | ||
import io.stargate.sgv2.jsonapi.api.model.command.GeneralCommand; | ||
import io.stargate.sgv2.jsonapi.api.model.command.impl.CreateDatabaseCommand; | ||
import io.stargate.sgv2.jsonapi.config.constants.OpenApiConstants; | ||
import io.stargate.sgv2.jsonapi.service.processor.CommandProcessor; | ||
import javax.inject.Inject; | ||
import javax.validation.Valid; | ||
import javax.validation.constraints.NotNull; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import org.eclipse.microprofile.openapi.annotations.Operation; | ||
import org.eclipse.microprofile.openapi.annotations.media.Content; | ||
import org.eclipse.microprofile.openapi.annotations.media.ExampleObject; | ||
import org.eclipse.microprofile.openapi.annotations.media.Schema; | ||
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody; | ||
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; | ||
import org.eclipse.microprofile.openapi.annotations.responses.APIResponses; | ||
import org.eclipse.microprofile.openapi.annotations.security.SecurityRequirement; | ||
import org.eclipse.microprofile.openapi.annotations.tags.Tag; | ||
import org.jboss.resteasy.reactive.RestResponse; | ||
|
||
@Path(GeneralResource.BASE_PATH) | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@SecurityRequirement(name = OpenApiConstants.SecuritySchemes.TOKEN) | ||
@Tag(ref = "General") | ||
public class GeneralResource { | ||
|
||
public static final String BASE_PATH = "/v1"; | ||
|
||
private final CommandProcessor commandProcessor; | ||
|
||
@Inject | ||
public GeneralResource(CommandProcessor commandProcessor) { | ||
this.commandProcessor = commandProcessor; | ||
} | ||
|
||
@Operation(summary = "Execute command", description = "Executes a single general command.") | ||
@RequestBody( | ||
content = | ||
@Content( | ||
mediaType = MediaType.APPLICATION_JSON, | ||
schema = @Schema(anyOf = {CreateDatabaseCommand.class}), | ||
examples = { | ||
@ExampleObject(ref = "createDatabase"), | ||
@ExampleObject(ref = "createDatabaseWithReplication"), | ||
})) | ||
@APIResponses( | ||
@APIResponse( | ||
responseCode = "200", | ||
description = | ||
"Call successful. Returns result of the command execution. Note that in case of errors, response code remains `HTTP 200`.", | ||
content = | ||
@Content( | ||
mediaType = MediaType.APPLICATION_JSON, | ||
schema = @Schema(implementation = CommandResult.class), | ||
examples = { | ||
@ExampleObject(ref = "resultCreate"), | ||
@ExampleObject(ref = "resultError"), | ||
}))) | ||
@POST | ||
public Uni<RestResponse<CommandResult>> postCommand(@NotNull @Valid GeneralCommand command) { | ||
|
||
// call processor | ||
return commandProcessor | ||
.processCommand(CommandContext.empty(), command) | ||
// map to 2xx always | ||
.map(RestResponse::ok); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...n/java/io/stargate/sgv2/jsonapi/service/operation/model/impl/CreateDatabaseOperation.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,39 @@ | ||
package io.stargate.sgv2.jsonapi.service.operation.model.impl; | ||
|
||
import io.smallrye.mutiny.Uni; | ||
import io.stargate.bridge.proto.QueryOuterClass; | ||
import io.stargate.sgv2.jsonapi.api.model.command.CommandResult; | ||
import io.stargate.sgv2.jsonapi.service.bridge.executor.QueryExecutor; | ||
import io.stargate.sgv2.jsonapi.service.operation.model.Operation; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Operation that creates a new Cassandra keyspace that serves as a namespace (database) for the | ||
* JSON API. | ||
* | ||
* @param name Name of the namespace to create. | ||
* @param replicationMap A replication json, see | ||
* https://docs.datastax.com/en/cql-oss/3.3/cql/cql_reference/cqlCreateKeyspace.html#Table2.Replicationstrategyclassandfactorsettings. | ||
*/ | ||
public record CreateDatabaseOperation(String name, String replicationMap) implements Operation { | ||
|
||
// simple pattern for the cql | ||
private static final String CREATE_KEYSPACE_CQL = | ||
"CREATE KEYSPACE IF NOT EXISTS \"%s\" WITH REPLICATION = %s;\n"; | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public Uni<Supplier<CommandResult>> execute(QueryExecutor queryExecutor) { | ||
QueryOuterClass.Query query = | ||
QueryOuterClass.Query.newBuilder() | ||
.setCql(String.format(CREATE_KEYSPACE_CQL, name, replicationMap)) | ||
.build(); | ||
|
||
// execute | ||
return queryExecutor | ||
.executeSchemaChange(query) | ||
|
||
// if we have a result always respond positively | ||
.map(any -> new SchemaChangeResult(true)); | ||
} | ||
} |
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.
Can we name this as NamespaceResource? General resource doesn't convey what it does.
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.
Does not fit, we already have a
DatabaseResource
I followed the logic:/v1
- GeneralResurce/v1/{database}
- DatabaseResource/v1/{database}/{collection}
- CollectionResourceThat would mean that we would have database resource and namespace resource, but they are both actually the same..
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.
it should be
/v1
- GeneralResource/v1/{namespace}
- NamespaceResource/v1/{namespace}/{collection}
- CollectionResourceThere 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.
OK but this
database
tonamespace
change is huge and needs a separate PR imo.. WIll create a ticket..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.
#93