Skip to content

Commit

Permalink
Merge pull request #356 from aiven/kmichel-clickhouse-database-cld
Browse files Browse the repository at this point in the history
Add clickhouse database create/delete/list
  • Loading branch information
alanfranz authored Aug 4, 2023
2 parents a8bf45d + 5055d8e commit 464c304
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
71 changes: 71 additions & 0 deletions aiven/client/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2360,6 +2360,77 @@ def service__integration_list(self) -> None:
table_layout=layout,
)

@arg.project
@arg.service_name
@arg.json
@arg("database", help="Database name")
def service__clickhouse__database__create(self) -> None:
"""Create a ClickHouse database"""
project_name = self.get_project()
response = self.client.clickhouse_database_create(
project=project_name,
service=self.args.service_name,
database=self.args.database,
)
self.print_response(response)

@arg.project
@arg.service_name
@arg.json
@arg("database", help="Database name")
def service__clickhouse__database__delete(self) -> None:
"""Delete a ClickHouse database"""
project_name = self.get_project()
response = self.client.clickhouse_database_delete(
project=project_name,
service=self.args.service_name,
database=self.args.database,
)
self.print_response(response)

@arg.project
@arg.service_name
@arg.json
def service__clickhouse__database__list(self) -> None:
"""List ClickHouse databases"""
project_name = self.get_project()
layout = [
[
"name",
"engine",
"state",
]
]
self.print_response(
self.client.clickhouse_database_list(project=project_name, service=self.args.service_name),
json=self.args.json,
table_layout=layout,
)

@arg.project
@arg.service_name
@arg.json
@arg("database", help="Database name")
def service__clickhouse__table__list(self) -> None:
"""List ClickHouse database tables"""
project_name = self.get_project()
layout = [
[
"name",
"uuid",
"engine",
"total_rows",
"total_bytes",
"state",
]
]
databases = self.client.clickhouse_database_list(project=project_name, service=self.args.service_name)
for database in databases:
if database["name"] == self.args.database:
self.print_response(database["tables"], json=self.args.json, table_layout=layout)
return
raise argx.UserError(f"Could not find database named {self.args.database}.")

@arg.project
@arg.service_name
@arg.json
Expand Down
12 changes: 12 additions & 0 deletions aiven/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2166,6 +2166,18 @@ def delete_oauth2_client_secret(self, account_id: str, client_id: str, secret_id
self.build_path("account", account_id, "oauth_client", client_id, "secret", secret_id),
)

def clickhouse_database_create(self, project: str, service: str, database: str) -> Mapping:
path = self.build_path("project", project, "service", service, "clickhouse", "db")
return self.verify(self.post, path, body={"database": database})

def clickhouse_database_delete(self, project: str, service: str, database: str) -> Mapping:
path = self.build_path("project", project, "service", service, "clickhouse", "db", database)
return self.verify(self.delete, path)

def clickhouse_database_list(self, project: str, service: str) -> Mapping:
path = self.build_path("project", project, "service", service, "clickhouse", "db")
return self.verify(self.get, path, result_key="databases")

def flink_list_applications(
self,
*,
Expand Down
46 changes: 46 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,52 @@ def _create_redirect(account_id: str, client_id: str, uri: str) -> Mapping:
)


def test_clickhouse_database_create() -> None:
aiven_client = mock.Mock(spec_set=AivenClient)
aiven_client.clickhouse_database_create.return_value = {"message": "created"}
args = ["service", "clickhouse", "database", "create", "--project=myproj", "myservice", "mydatabase"]
build_aiven_cli(aiven_client).run(args=args)
aiven_client.clickhouse_database_create.assert_called_with(project="myproj", service="myservice", database="mydatabase")


def test_clickhouse_database_delete() -> None:
aiven_client = mock.Mock(spec_set=AivenClient)
aiven_client.clickhouse_database_delete.return_value = {"message": "deleting"}
args = ["service", "clickhouse", "database", "delete", "--project=myproj", "myservice", "mydatabase"]
build_aiven_cli(aiven_client).run(args=args)
aiven_client.clickhouse_database_delete.assert_called_with(project="myproj", service="myservice", database="mydatabase")


def test_clickhouse_database_list() -> None:
aiven_client = mock.Mock(spec_set=AivenClient)
aiven_client.clickhouse_database_list.return_value = [
{"name": "mydatabase", "engine": "Replicated", "state": "ok", "tables": []}
]
args = ["service", "clickhouse", "database", "list", "--project=myproj", "myservice"]
build_aiven_cli(aiven_client).run(args=args)
aiven_client.clickhouse_database_list.assert_called_with(project="myproj", service="myservice")


def test_clickhouse_table_list() -> None:
aiven_client = mock.Mock(spec_set=AivenClient)
tables = [
{
"name": "mytable",
"uuid": str(uuid.UUID(int=0)),
"engine": "ReplicatedMergeTree",
"total_rows": 10,
"total_bytes": 100,
"state": "ok",
}
]
aiven_client.clickhouse_database_list.return_value = [
{"name": "mydatabase", "engine": "Replicated", "state": "ok", "tables": tables}
]
args = ["service", "clickhouse", "table", "list", "--project=myproj", "myservice", "mydatabase"]
build_aiven_cli(aiven_client).run(args=args)
aiven_client.clickhouse_database_list.assert_called_with(project="myproj", service="myservice")


def test_static_ips_list(capsys: CaptureFixture[str]) -> None:
aiven_client = mock.Mock(spec_set=AivenClient)
aiven_client.list_static_ip_addresses.return_value = [
Expand Down

0 comments on commit 464c304

Please sign in to comment.