Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,6 @@ venv.bak/
.vscode

cdk-deployment/lambda/stac_validator/
cdk-deployment/lambda/libraries.zip
cdk-deployment/build-libraries/libraries.zip

Pipfile*
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ build: ## Build a Docker container
build-libraries: # Build the libraries for layers. Used internally
docker build -f "cdk-deployment/build-libraries/Dockerfile-libraries" -t lambdalayer:latest .
docker run -d -it --name lambdalayer lambdalayer:latest
docker cp lambdalayer:code/libraries.zip ./cdk-deployment/lambda
docker cp lambdalayer:code/libraries.zip ./cdk-deployment/build-libraries
docker stop lambdalayer
docker rm lambdalayer
docker rmi lambdalayer
cp -r stac_validator cdk-deployment/lambda

build-cdk: ## Build the libraries in preperation for CDK deployment
make build-libraries
Expand Down
4 changes: 2 additions & 2 deletions cdk-deployment/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
# with examples from the CDK Developer's Guide, which are in the process of
# being updated to use `cdk`. You may delete this import if you don't need it.
from aws_cdk import core
from validator_cdk.validator_cdk_stack import ValidatorCdkStack
from validator_cdk.validator_cdk_stack import FastAPICdkStack

app = core.App()
ValidatorCdkStack(app, "ValidatorCdkStack")
FastAPICdkStack(app, "FastAPICdkStack")

app.synth()
28 changes: 28 additions & 0 deletions cdk-deployment/build-libraries/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
__pycache__
*.pyc
*.pyo
*.pyd
.Python
env
pip-log.txt
pip-delete-this-directory.txt
.tox
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
*.log
.git
*.swp
package-lock.json
__pycache__
.pytest_cache
.env
.venv
*.egg-info

# CDK asset staging directory
.cdk.staging
cdk.out
2 changes: 1 addition & 1 deletion cdk-deployment/build-libraries/Dockerfile-libraries
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ COPY . /code/
RUN apt-get -y update && \
apt-get -y install zip unzip && \
python -m pip install --upgrade pip && \
pip install . requests>=2.19.1 pytest==6.2.2 pystac==0.5.6 jsonschema==3.2.0 click==7.1.2 -t ./python && \
pip install . mangum uvicorn fastapi[all] requests>=2.19.1 pystac==0.5.6 jsonschema==3.2.0 click==7.1.2 -t ./python && \
zip -r libraries.zip ./python/
76 changes: 46 additions & 30 deletions cdk-deployment/lambda/lambda.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,55 @@
import json
import tempfile

from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from mangum import Mangum

from stac_validator import stac_validator

app = FastAPI(title="STAC Validator", version=2.0, root_path="/prod/")

app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

def handler(event, context):
body = json.loads(event["body"])
if body.get("stac_url"):
body = body["stac_url"]
else:
# TODO: Look at alains devops template. store in mem
temp_stac_file = tempfile.NamedTemporaryFile(
delete=False,
mode="w+",
)
json.dump(body, temp_stac_file)
temp_stac_file.flush()
temp_stac_file.close()
body = temp_stac_file.name
stac = stac_validator.StacValidate(body)
stac.run()

def validate(stac_object):
stac = stac_validator.StacValidate(stac_object)
stac.run()
output = stac.message[0]
return output


@app.get("/url")
async def validate_url_get_request(stac_url):
output = validate(str(stac_url))
return {"body": output}


@app.post("/url")
async def validate_url_post_request(stac_url):
output = validate(str(stac_url))
return {"body": output}


@app.post("/json")
async def validate_json(request: Request):
stac_file = await request.json()
temp_stac_file = tempfile.NamedTemporaryFile(
delete=False,
mode="w+",
)
json.dump(stac_file, temp_stac_file)
temp_stac_file.flush()
temp_stac_file.close()
body = temp_stac_file.name
output = validate(body)
return {"body": output}


if "validation_method" in output:
output.pop("validation_method")

return {
"statusCode": 200,
"isBase64Encoded": False,
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET",
},
"body": json.dumps(output),
}
handler = Mangum(app)
10 changes: 5 additions & 5 deletions cdk-deployment/validator_cdk/validator_cdk_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ class ValidatorCdkStack(cdk.Stack):
def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)

stac_lib = _lambda.LayerVersion(
val_lib = _lambda.LayerVersion(
self,
"stac-lib-layer",
code=_lambda.AssetCode("lambda/libraries.zip"),
"validator-library-layer",
code=_lambda.AssetCode("build-libraries/libraries.zip"),
compatible_runtimes=[_lambda.Runtime.PYTHON_3_8],
)

# Defines an AWS Lambda resource
validator_lambda = _lambda.Function(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave this as validator lambda.

self,
"STACHandler",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

STACValidator

"STACValidator",
runtime=_lambda.Runtime.PYTHON_3_8,
code=_lambda.Code.asset("lambda"),
handler="lambda.handler",
timeout=cdk.Duration.seconds(30),
layers=[stac_lib],
layers=[val_lib],
)

cors = apigw.CorsOptions(allow_origins=["*"])
Expand Down
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[build-system]
requires = [
"requests",
"jsonschema",
"pystac",
"click",
]
build-backend = "setuptools.build_meta"
3 changes: 3 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"pytest"
"pytest-mypy"
"pre-commit"
2 changes: 0 additions & 2 deletions setup.cfg

This file was deleted.

11 changes: 3 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#!/usr/bin/env python

try:
from setuptools import setup
except ImportError:
from distutils.core import setup
from setuptools import setup

__version__ = "2.0.0"

Expand All @@ -17,7 +14,7 @@
setup(
name="stac_validator",
version=__version__,
author="James Banting, Darren Wiens, Jonathan Healy",
author="James Banting, Jonathan Healy",
author_email="jhealy@sparkgeo.com",
description="A package to validate STAC files",
license="MIT",
Expand All @@ -38,13 +35,11 @@
"jsonschema==3.2.0",
"pystac==0.5.6",
"click==7.1.2",
"pytest",
"pytest-mypy",
"pre-commit",
],
packages=["stac_validator"],
entry_points={
"console_scripts": ["stac_validator = stac_validator.stac_validator:main"]
},
python_requires=">=3.6",
tests_require=["pytest"],
)