-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc84b77
commit b08d90e
Showing
28 changed files
with
513 additions
and
292 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
name: "ndc-python-lambda connector" | ||
on: | ||
pull_request: | ||
branches: | ||
- main | ||
- test-ci/** | ||
push: | ||
branches: | ||
- 'main' | ||
- test-ci/** | ||
tags: | ||
- v** | ||
|
||
env: | ||
DOCKER_REGISTRY: ghcr.io | ||
DOCKER_IMAGE_NAME: hasura/ndc-python-lambda | ||
|
||
jobs: | ||
build-and-test: | ||
name: Build and test ndc-lambda-sdk | ||
defaults: | ||
run: | ||
working-directory: ./ndc-lambda-sdk | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.9' | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r requirements.txt | ||
- name: Run tests | ||
run: pytest | ||
|
||
docker: | ||
name: Build base docker image | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: docker/login-action@v3 | ||
with: | ||
registry: ${{ env.DOCKER_REGISTRY }} | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
- name: Extract metadata (tags, labels) for Docker | ||
id: docker-metadata | ||
uses: docker/metadata-action@v5 | ||
with: | ||
images: ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_IMAGE_NAME }} | ||
- uses: docker/build-push-action@v5 | ||
with: | ||
context: . | ||
push: ${{ startsWith(github.ref, 'refs/tags/v') }} | ||
tags: ${{ steps.docker-metadata.outputs.tags }} | ||
labels: ${{ steps.docker-metadata.outputs.labels }} | ||
|
||
release-connector: | ||
name: Release connector | ||
defaults: | ||
run: | ||
working-directory: ./connector-definition | ||
runs-on: ubuntu-latest | ||
needs: | ||
- build-and-test | ||
- docker | ||
if: ${{ startsWith(github.ref, 'refs/tags/v') }} | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.9' | ||
- name: Build connector definition | ||
run: make build | ||
- uses: actions/upload-artifact@v4 | ||
with: | ||
name: connector-definition.tgz | ||
path: ./connector-definition/dist/connector-definition.tgz | ||
compression-level: 0 # Already compressed | ||
- name: Get version from tag | ||
id: get-version | ||
run: | | ||
echo "tagged_version=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT | ||
shell: bash | ||
- uses: mindsers/changelog-reader-action@v2 | ||
id: changelog-reader | ||
with: | ||
version: ${{ steps.get-version.outputs.tagged_version }} | ||
path: ./CHANGELOG.md | ||
- uses: softprops/action-gh-release@v1 | ||
with: | ||
draft: false | ||
tag_name: v${{ steps.get-version.outputs.tagged_version }} | ||
body: ${{ steps.changelog-reader.outputs.changes }} | ||
files: | | ||
./connector-definition/dist/connector-definition.tgz | ||
fail_on_unmatched_files: true |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.hasura-connector/ | ||
*.hml | ||
__pycache__ |
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,9 @@ | ||
FROM ghcr.io/hasura/ndc-python-lambda:v{{VERSION}} | ||
|
||
COPY requirements.txt /functions/ | ||
|
||
WORKDIR /functions | ||
RUN --mount=type=cache,target=/root/.cache/pip \ | ||
pip install -r requirements.txt | ||
|
||
COPY ./ /functions |
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,60 @@ | ||
# Set the default target to 'build' | ||
.DEFAULT_GOAL := build | ||
|
||
# Use bash as the shell for executing commands | ||
SHELL = /usr/bin/env bash | ||
|
||
# Extract the version from the latest git tag | ||
RELEASE_VERSION := $(shell git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0") | ||
|
||
# Define 'build' and 'clean' as phony targets (not associated with files) | ||
.PHONY: build | ||
.PHONY: clean | ||
|
||
# The 'build' target depends on creating the connector-definition.tgz file | ||
build: dist/connector-definition.tgz | ||
|
||
# The 'clean' target removes the dist directory | ||
clean: | ||
rm -rf dist | ||
|
||
# Create the dist and dist/.hasura-connector directories | ||
dist: | ||
mkdir -p dist | ||
mkdir -p dist/.hasura-connector | ||
|
||
# Copy connector-metadata.yaml to dist/.hasura-connector | ||
dist/.hasura-connector/connector-metadata.yaml: connector-metadata.yaml dist | ||
cp -f connector-metadata.yaml dist/.hasura-connector | ||
|
||
# Copy Dockerfile to dist/.hasura-connector and replace the version | ||
dist/.hasura-connector/Dockerfile: Dockerfile dist | ||
cp -f Dockerfile dist/.hasura-connector/ | ||
sed -i '' 's/{{VERSION}}/$(subst v,,$(RELEASE_VERSION))/g' dist/.hasura-connector/Dockerfile | ||
# sed -i 's/{{VERSION}}/$(subst v,,$(RELEASE_VERSION))/g' dist/.hasura-connector/Dockerfile | ||
|
||
# Copy .dockerignore to dist/.hasura-connector | ||
dist/.hasura-connector/.dockerignore: .dockerignore dist | ||
cp -f .dockerignore dist/.hasura-connector/ | ||
|
||
# Find all files in the template directory | ||
template_files := $(wildcard template/*) | ||
# Create corresponding paths for template files in the dist directory | ||
dist_template_files := $(patsubst template/%,dist/%,$(template_files)) | ||
|
||
# Copy all template files to the dist directory | ||
$(dist_template_files): $(template_files) | ||
cp -f $(template_files) dist/ | ||
|
||
# Find all files in the scripts directory | ||
script_files := $(wildcard scripts/*) | ||
# Create corresponding paths for script files in dist/.hasura-connector | ||
dist_script_files := $(patsubst scripts/%,dist/.hasura-connector/%,$(script_files)) | ||
|
||
# Copy all script files to dist/.hasura-connector | ||
$(dist_script_files): $(script_files) | ||
cp -f $(script_files) dist/.hasura-connector/ | ||
|
||
# Create the final connector-definition.tgz file | ||
dist/connector-definition.tgz: dist/.hasura-connector/connector-metadata.yaml dist/.hasura-connector/Dockerfile dist/.hasura-connector/.dockerignore $(dist_template_files) $(dist_script_files) | ||
cd dist && tar -czvf connector-definition.tgz * |
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,23 @@ | ||
packagingDefinition: | ||
type: ManagedDockerBuild | ||
nativeToolchainDefinition: | ||
commands: | ||
start: | ||
type: ShellScript | ||
bash: ./start.sh | ||
powershell: ./start.ps1 | ||
watch: | ||
type: ShellScript | ||
bash: ./watch.sh | ||
powershell: ./start.ps1 | ||
supportedEnvironmentVariables: [] | ||
commands: {} | ||
dockerComposeWatch: | ||
# Rebuild the container if a new package restore is required because requirements.txt changed | ||
- path: requirements.txt | ||
target: /functions/requirements.txt | ||
action: rebuild | ||
# For any other file change, simply copy it into the existing container and restart it | ||
- path: ./ | ||
target: /functions | ||
action: sync+restart |
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,3 @@ | ||
.hasura-connector/ | ||
*.hml | ||
__pycache__ |
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,9 @@ | ||
FROM ghcr.io/hasura/ndc-python-lambda:v0.0.0 | ||
|
||
COPY requirements.txt /functions/ | ||
|
||
WORKDIR /functions | ||
RUN --mount=type=cache,target=/root/.cache/pip \ | ||
pip install -r requirements.txt | ||
|
||
COPY ./ /functions |
23 changes: 23 additions & 0 deletions
23
connector-definition/dist/.hasura-connector/connector-metadata.yaml
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,23 @@ | ||
packagingDefinition: | ||
type: ManagedDockerBuild | ||
nativeToolchainDefinition: | ||
commands: | ||
start: | ||
type: ShellScript | ||
bash: ./start.sh | ||
powershell: ./start.ps1 | ||
watch: | ||
type: ShellScript | ||
bash: ./watch.sh | ||
powershell: ./start.ps1 | ||
supportedEnvironmentVariables: [] | ||
commands: {} | ||
dockerComposeWatch: | ||
# Rebuild the container if a new package restore is required because requirements.txt changed | ||
- path: requirements.txt | ||
target: /functions/requirements.txt | ||
action: rebuild | ||
# For any other file change, simply copy it into the existing container and restart it | ||
- path: ./ | ||
target: /functions | ||
action: sync+restart |
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,18 @@ | ||
$ErrorActionPreference = "Stop" | ||
|
||
$scriptDir = Get-Location | ||
|
||
& ./check-reqs.ps1 | ||
|
||
Push-Location $env:HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH | ||
try { | ||
# Activate virtual environment if it exists | ||
if (Test-Path "venv\Scripts\Activate.ps1") { | ||
.\venv\Scripts\Activate.ps1 | ||
} | ||
|
||
# Run the Python script with the serve command | ||
python3 connector-definition/template/functions.py serve --configuration ./ | ||
} finally { | ||
Pop-Location | ||
} |
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,16 @@ | ||
#!/usr/bin/env bash | ||
set -eu -o pipefail | ||
|
||
script_dir=$(pwd) | ||
|
||
./check-reqs.sh | ||
|
||
cd $HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH | ||
|
||
# Activate virtual environment if it exists | ||
if [ -f "venv/bin/activate" ]; then | ||
source venv/bin/activate | ||
fi | ||
|
||
# Run the Python script with the serve command | ||
python3 connector-definition/template/functions.py serve --configuration ./ |
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,18 @@ | ||
$ErrorActionPreference = "Stop" | ||
|
||
$scriptDir = Get-Location | ||
|
||
& ./check-reqs.ps1 | ||
|
||
Push-Location $env:HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH | ||
try { | ||
# Activate virtual environment if it exists | ||
if (Test-Path "venv\Scripts\Activate.ps1") { | ||
.\venv\Scripts\Activate.ps1 | ||
} | ||
|
||
# Run watchdog to watch for file changes and restart the Python script | ||
watchmedo auto-restart --pattern="*.py" --recursive -- python3 connector-definition/template/functions.py serve --configuration ./ | ||
} finally { | ||
Pop-Location | ||
} |
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,16 @@ | ||
#!/usr/bin/env bash | ||
set -eu -o pipefail | ||
|
||
script_dir=$(pwd) | ||
|
||
./check-reqs.sh | ||
|
||
cd $HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH | ||
|
||
# Activate virtual environment if it exists | ||
if [ -f "venv/bin/activate" ]; then | ||
source venv/bin/activate | ||
fi | ||
|
||
# Run watchdog to watch for file changes and restart the Python script | ||
watchmedo auto-restart --pattern="*.py" --recursive -- python3 connector-definition/template/functions.py serve --configuration ./ |
Binary file not shown.
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,52 @@ | ||
from hasura_ndc import start | ||
from hasura_ndc.instrumentation import with_active_span | ||
from opentelemetry.trace import get_tracer | ||
from hasura_ndc.function_connector import FunctionConnector | ||
from pydantic import BaseModel | ||
|
||
connector = FunctionConnector() | ||
tracer = get_tracer("ndc-sdk-python.server") | ||
|
||
@connector.register_query | ||
def do_the_thing(x: int): | ||
return f"Hello World {x}" | ||
|
||
|
||
@connector.register_mutation | ||
def some_mutation_function(arg1: str, | ||
arg2: int): | ||
return f"Hey {arg1} {arg2}" | ||
|
||
@connector.register_query | ||
async def my_query(x: str) -> str: | ||
return await with_active_span( | ||
tracer, | ||
"My Span", | ||
lambda span: f"My string is {x}", | ||
{"attr": "value"} | ||
) | ||
|
||
@connector.register_query | ||
async def my_query2(x: str): | ||
async def f(span): | ||
# return f"My string is {x}" | ||
return { | ||
"hey": "x", | ||
"var": x, | ||
10.1: 10.1, | ||
"dict": { | ||
1.0: 10 | ||
}, | ||
"floatables": [1.234, 10, "yep"] | ||
} | ||
|
||
return await with_active_span( | ||
tracer, | ||
"My Span", | ||
f, | ||
{"attr": "value"} | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
start(connector) |
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.