Skip to content
Snippets Groups Projects
Commit 92de2589 authored by vlorentz's avatar vlorentz
Browse files

Add create-keyspace CLI endpoint

parent e22fc9f0
No related branches found
No related tags found
No related merge requests found
......@@ -172,6 +172,7 @@ def create_keyspace(
)
session.execute('USE "%s"' % keyspace)
for query in CREATE_TABLES_QUERIES:
logger.debug("Running:\n%s", query)
session.execute(query)
......
......@@ -65,6 +65,32 @@ def storage(ctx, config_file, check_config):
ctx.obj["check_config"] = check_config
@storage.command(name="create-keyspace")
@click.pass_context
def create_keyspace(ctx):
"""Creates a Cassandra keyspace with table definitions suitable for use
by swh-storage's Cassandra backend"""
from swh.storage.cassandra import create_keyspace
config = ctx.obj["config"]["storage"]
for key in ("cls", "hosts", "keyspace", "auth_provider"):
if key not in config:
ctx.fail(f"Missing {key} key in config file.")
if config["cls"] != "cassandra":
ctx.fail(f"cls must be 'cassandra', not '{config['cls']}'")
create_keyspace(
hosts=config["hosts"],
port=config.get("port", 9042),
keyspace=config["keyspace"],
auth_provider=config["auth_provider"],
)
print("Done.")
@storage.command(name="rpc-serve")
@click.option(
"--host",
......
......@@ -5,6 +5,7 @@
import copy
import logging
import os
import re
import tempfile
from unittest.mock import patch
......@@ -15,7 +16,7 @@ import pytest
import yaml
from swh.journal.serializers import key_to_kafka, value_to_kafka
from swh.model.model import Snapshot, SnapshotBranch, TargetType
from swh.model.model import Origin, Snapshot, SnapshotBranch, TargetType
from swh.storage import get_storage
from swh.storage.cli import storage as cli
from swh.storage.replay import OBJECT_CONVERTERS
......@@ -67,6 +68,34 @@ def invoke(*args, env=None, journal_config=None, local_config=None):
return ret
def test_create_keyspace(
swh_storage_cassandra_cluster,
cassandra_auth_provider_config,
):
(hosts, port) = swh_storage_cassandra_cluster
keyspace = "test" + os.urandom(10).hex()
storage_config = dict(
cls="cassandra",
hosts=hosts,
port=port,
keyspace=keyspace,
journal_writer={"cls": "memory"},
objstorage={"cls": "memory"},
auth_provider=cassandra_auth_provider_config,
)
result = invoke("create-keyspace", local_config={"storage": storage_config})
assert result.exit_code == 0, result.output
assert result.output == "Done.\n"
# Check we can write and read to it
storage = get_storage(**storage_config)
origin = Origin(url="http://example.org")
storage.origin_add([origin])
assert storage.origin_get([origin.url]) == [origin]
def test_replay(
swh_storage,
kafka_prefix: str,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment