diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..61ab63d --- /dev/null +++ b/.gitignore @@ -0,0 +1,135 @@ +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# dotenv +.env + +# virtualenv +.venv +venv/ +ENV/ + +# Spyder project settings +.spyderproject + +# Rope project settings +.ropeproject \ No newline at end of file diff --git a/opt/resource/check b/opt/resource/check new file mode 100755 index 0000000..057e9bd --- /dev/null +++ b/opt/resource/check @@ -0,0 +1,3 @@ +#!/bin/sh + +python3 /opt/resource/check.py $@ \ No newline at end of file diff --git a/opt/resource/check.py b/opt/resource/check.py new file mode 100755 index 0000000..2ba2b91 --- /dev/null +++ b/opt/resource/check.py @@ -0,0 +1,31 @@ +#! /usr/bin/env python3 +from jsonschema import Draft4Validator + +import schemas +from concourse_common import common + + +def execute(): + + payload = common.get_payload() + + validation_result = validate(payload) + if validation_result != 0: + return validation_result + + return 0 + + +def validate(payload): + v = Draft4Validator(schemas.checkSchema) + valid = True + + for error in sorted(v.iter_errors(payload), key=str): + common.log(error.message) + valid = False + + return 0 if valid else -1 + + +if __name__ == '__main__': + exit(execute()) \ No newline at end of file diff --git a/opt/resource/concourse_common/__init__.py b/opt/resource/concourse_common/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/opt/resource/concourse_common/common.py b/opt/resource/concourse_common/common.py new file mode 100644 index 0000000..1176b8b --- /dev/null +++ b/opt/resource/concourse_common/common.py @@ -0,0 +1,16 @@ +import json +import sys +import tempfile + + +def get_payload(): + payload = json.load(sys.stdin) + _, fname = tempfile.mkstemp() + log("Logging payload to {}".format(fname)) + with open(fname, 'w') as fp: + fp.write(json.dumps(payload)) + log(payload) + return payload + +def log(*args, **kwargs): + print(*args, file=sys.stderr, **kwargs) \ No newline at end of file diff --git a/opt/resource/concourse_common/common_tests.py b/opt/resource/concourse_common/common_tests.py new file mode 100644 index 0000000..1ebb56e --- /dev/null +++ b/opt/resource/concourse_common/common_tests.py @@ -0,0 +1,32 @@ +import sys +import unittest +from io import StringIO + +from concourse_common import common + + +class CommonTest(unittest.TestCase): + check_payload = ('{"source":{' + '"bucket":"bucketName",' + '"access_key":"apiKey123",' + '"secret_access_key":"secretKey321",' + '"region_name":"eu-west-1",' + '"regexp":"directory_on_s3/release-(.*).tar.gz' + '"},' + '"version":{"version":"version-v1-dev"}}') + + def test_getPayload(self): + put_stdin(self.check_payload) + result = common.get_payload() + self.assertEqual(result['source']['access_key'], "apiKey123") + self.assertEqual(result['source']['secret_access_key'], "secretKey321") + self.assertEqual(result['source']['region_name'], "eu-west-1") + self.assertEqual(result['source']['regexp'], "directory_on_s3/release-(.*).tar.gz") + self.assertEqual(result['version']['version'], "version-v1-dev") + + +def put_stdin(content): + sys.stdin = StringIO(content) + +if __name__ == '__main__': + unittest.main() diff --git a/opt/resource/in b/opt/resource/in new file mode 100755 index 0000000..c0a24ae --- /dev/null +++ b/opt/resource/in @@ -0,0 +1,3 @@ +#!/bin/sh + +python3 /opt/resource/input.py $@ \ No newline at end of file diff --git a/opt/resource/out b/opt/resource/out new file mode 100755 index 0000000..c9c6d16 --- /dev/null +++ b/opt/resource/out @@ -0,0 +1,3 @@ +#!/bin/sh + +python3 /opt/resource/out.py $@ \ No newline at end of file diff --git a/opt/resource/schemas.py b/opt/resource/schemas.py new file mode 100644 index 0000000..ab0c7f6 --- /dev/null +++ b/opt/resource/schemas.py @@ -0,0 +1,70 @@ +checkSchema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "properties": { + "source": { + "type": "object", + "properties": { + "preFix": { + "type": "string" + }, + + }, + "required": [ + "preFix" + ] + }, + "version": { + "type": "object", + "properties": { + "version": { + "type": "string" + } + }, + "required": [ + "version" + ] + } + }, + "required": [ + "source" + ] +} + +outSchema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "properties": { + "source": { + "type": "object", + "properties": { + "preFix": { + "type": "string" + }, + + }, + "required": [ + "preFix" + ] + }, + "names": { + "type": "object", + "properties": { + "default": { + "type": "string" + }, + "heroku": { + "type": "string" + } + }, + "required": [ + "default", + "heroku" + ] + } + }, + "required": [ + "source", + "names" + ] +} \ No newline at end of file