Skip to content

Commit d401213

Browse files
authored
add initial version of TypeDB extension (#108)
1 parent f42eba6 commit d401213

12 files changed

Lines changed: 648 additions & 4 deletions

File tree

.github/workflows/typedb.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: LocalStack TypeDB Extension Tests
2+
3+
on:
4+
pull_request:
5+
workflow_dispatch:
6+
7+
env:
8+
LOCALSTACK_DISABLE_EVENTS: "1"
9+
LOCALSTACK_AUTH_TOKEN: ${{ secrets.LOCALSTACK_AUTH_TOKEN }}
10+
11+
jobs:
12+
integration-tests:
13+
name: Run Integration Tests
14+
runs-on: ubuntu-latest
15+
timeout-minutes: 10
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v4
19+
20+
- name: Setup LocalStack and extension
21+
run: |
22+
cd typedb
23+
24+
docker pull localstack/localstack-pro &
25+
docker pull typedb/typedb &
26+
pip install localstack
27+
28+
make install
29+
make dist
30+
localstack extensions -v install file://$(ls ./dist/localstack_extension_typedb-*.tar.gz)
31+
32+
DEBUG=1 localstack start -d
33+
localstack wait
34+
35+
- name: Run integration tests
36+
run: |
37+
cd typedb
38+
make test
39+
40+
- name: Print logs
41+
if: always()
42+
run: |
43+
localstack logs
44+
localstack stop

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ $ localstack extensions install "git+https://github.com/localstack/localstack-ex
6363
## Official LocalStack Extensions
6464

6565
Here is the current list of extensions developed by the LocalStack team and their support status.
66-
You can install the respective extension by calling `localstack install <Install name>`.
66+
You can install the respective extension by calling `localstack extensions install <Install name>`.
6767

6868
| Extension | Install name | Version | Support status |
6969
|----------------------------------------------------------------------------------------------------| ------------ |---------| -------------- |
@@ -75,6 +75,7 @@ You can install the respective extension by calling `localstack install <Install
7575
| [Miniflare](https://github.com/localstack/localstack-extensions/tree/main/miniflare) | localstack-extension-miniflare | 0.1.0 | Experimental |
7676
| [Stripe](https://github.com/localstack/localstack-extensions/tree/main/stripe) | localstack-extension-stripe | 0.2.0 | Stable |
7777
| [Terraform Init](https://github.com/localstack/localstack-extensions/tree/main/terraform-init) | localstack-extension-terraform-init | 0.2.0 | Experimental |
78+
| [TypeDB](https://github.com/localstack/localstack-extensions/tree/main/typedb) | localstack-extension-typedb | 0.1.0 | Experimental |
7879

7980

8081
## Developing Extensions
@@ -168,7 +169,7 @@ class ReadyAnnouncerExtension(Extension):
168169
name = "my_ready_announcer"
169170

170171
def on_platform_ready(self):
171-
LOG.info("my plugin is loaded and localstack is ready to roll!")
172+
LOG.info("my plugin is loaded and localstack is ready to roll!")
172173
```
173174

174175
### Package your Extension
@@ -227,9 +228,9 @@ Commands:
227228
new Create a new LocalStack extension from the official extension...
228229
```
229230

230-
#### Creating a new extensions
231+
#### Creating a new extension
231232

232-
First, create a new extensions from a template:
233+
First, create a new extension from a template:
233234

234235
```console
235236
% localstack extensions dev new

typedb/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.venv
2+
dist
3+
build
4+
**/*.egg-info
5+
.eggs

typedb/Makefile

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
VENV_BIN = python3 -m venv
2+
VENV_DIR ?= .venv
3+
VENV_ACTIVATE = $(VENV_DIR)/bin/activate
4+
VENV_RUN = . $(VENV_ACTIVATE)
5+
6+
usage: ## Shows usage for this Makefile
7+
@cat Makefile | grep -E '^[a-zA-Z_-]+:.*?## .*$$' | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}'
8+
9+
venv: $(VENV_ACTIVATE)
10+
11+
$(VENV_ACTIVATE): pyproject.toml
12+
test -d .venv || $(VENV_BIN) .venv
13+
$(VENV_RUN); pip install --upgrade pip setuptools plux
14+
$(VENV_RUN); pip install -e .[dev]
15+
touch $(VENV_DIR)/bin/activate
16+
17+
clean:
18+
rm -rf .venv/
19+
rm -rf build/
20+
rm -rf .eggs/
21+
rm -rf *.egg-info/
22+
23+
install: venv ## Install dependencies
24+
$(VENV_RUN); python -m plux entrypoints
25+
26+
dist: venv ## Create distribution
27+
$(VENV_RUN); python -m build
28+
29+
publish: clean-dist venv dist ## Publish extension to pypi
30+
$(VENV_RUN); pip install --upgrade twine; twine upload dist/*
31+
32+
entrypoints: venv ## Generate plugin entrypoints for Python package
33+
$(VENV_RUN); python -m plux entrypoints
34+
35+
format: ## Run ruff to format the codebase
36+
$(VENV_RUN); python -m ruff format .; python -m ruff check --output-format=full --fix .
37+
38+
test: ## Run integration tests (requires LocalStack running with the Extension installed)
39+
$(VENV_RUN); pytest tests $(PYTEST_ARGS)
40+
41+
clean-dist: clean
42+
rm -rf dist/
43+
44+
.PHONY: clean clean-dist dist install publish usage venv format test

typedb/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
TypeDB on LocalStack
2+
=====================
3+
4+
This repo contains a [LocalStack Extension](https://github.com/localstack/localstack-extensions) that facilitates developing [TypeDB](https://typedb.com)-based applications locally.
5+
6+
After installing the extension, a TypeDB server instance will become available under `typedb.localhost.localstack.cloud:4566`, allowing you to create and manage TypeDB databases directly from your AWS applications running in LocalStack.
7+
8+
For example, you could create a microservice backed by a Lambda function that connects to a TypeDB database upon invocation. See [here](https://github.com/typedb-osi/typedb-localstack-demo) for a simple example application that makes use of this extension.
9+
10+
## Configuration
11+
12+
The following environment variables can be passed to the LocalStack container (make sure to prefix them with `LOCALSTACK_...` when using the `localstack start` CLI), to steer the behavior of the extension:
13+
14+
* `TYPEDB_FLAGS`: Additional user-defined command args to pass to the TypeDB container.
15+
* `TYPEDB_HTTP2_PROXY`: Flag to enable/disable HTTP2 proxy for gRPC traffic (use this if you experience network issues, and use the HTTP variant of the TypeDB driver).
16+
17+
## Prerequisites
18+
19+
* Docker
20+
* LocalStack Pro (free trial available)
21+
* `localstack` CLI
22+
* `make`
23+
24+
## Install from GitHub repository
25+
26+
This extension can be installed directly from this Github repo via:
27+
28+
```bash
29+
localstack extensions install "git+https://github.com/localstack/localstack-extensions.git#egg=typedb&subdirectory=typedb"
30+
```
31+
32+
## Install local development version
33+
34+
Please refer to the docs [here](https://github.com/localstack/localstack-extensions?tab=readme-ov-file#start-localstack-with-the-extension) for instructions on how to start the extension in developer mode.
35+
36+
## License
37+
38+
The code in this repo is available under the Apache 2.0 license.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
name = "localstack_typedb"
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import os
2+
import shlex
3+
4+
from localstack.config import is_env_not_false
5+
from localstack.utils.docker_utils import DOCKER_CLIENT
6+
from localstack_typedb.utils.docker import ProxiedDockerContainerExtension
7+
from rolo import Request
8+
from werkzeug.datastructures import Headers
9+
10+
# environment variable for user-defined command args to pass to TypeDB
11+
ENV_CMD_FLAGS = "TYPEDB_FLAGS"
12+
# environment variable for flag to enable/disable HTTP2 proxy for gRPC traffic
13+
ENV_HTTP2_PROXY = "TYPEDB_HTTP2_PROXY"
14+
15+
16+
class TypeDbExtension(ProxiedDockerContainerExtension):
17+
name = "typedb"
18+
19+
# pattern of the hostname under which the extension is accessible
20+
HOST = "typedb.<domain>"
21+
# name of the Docker image to spin up
22+
DOCKER_IMAGE = "typedb/typedb"
23+
# default command args to pass to TypeDB
24+
DEFAULT_CMD_FLAGS = ["--diagnostics.reporting.metrics=false"]
25+
# default port for TypeDB HTTP2/gRPC endpoint
26+
TYPEDB_PORT = 1729
27+
28+
def __init__(self):
29+
command_flags = (os.environ.get(ENV_CMD_FLAGS) or "").strip()
30+
command_flags = self.DEFAULT_CMD_FLAGS + shlex.split(command_flags)
31+
http2_ports = [self.TYPEDB_PORT] if is_env_not_false(ENV_HTTP2_PROXY) else []
32+
super().__init__(
33+
image_name=self.DOCKER_IMAGE,
34+
container_ports=[8000, 1729],
35+
host=self.HOST,
36+
request_to_port_router=self.request_to_port_router,
37+
command=command_flags,
38+
http2_ports=http2_ports,
39+
)
40+
41+
def should_proxy_request(self, headers: Headers) -> bool:
42+
# determine if this is a gRPC request targeting TypeDB
43+
content_type = headers.get("content-type") or ""
44+
req_path = headers.get(":path") or ""
45+
is_typedb_grpc_request = (
46+
"grpc" in content_type and "/typedb.protocol.TypeDB" in req_path
47+
)
48+
return is_typedb_grpc_request
49+
50+
def request_to_port_router(self, request: Request) -> int:
51+
# TODO add REST API / gRPC routing based on request
52+
return 1729

typedb/localstack_typedb/utils/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)