Skip to content

Commit

Permalink
Merge pull request #2 from ace-winters/master
Browse files Browse the repository at this point in the history
Adding JSON Builder Playbook App.
  • Loading branch information
paulh-tc authored Feb 1, 2018
2 parents 4ec0f8e + 6acb651 commit 98a40ed
Show file tree
Hide file tree
Showing 12 changed files with 577 additions and 0 deletions.
47 changes: 47 additions & 0 deletions apps/TCPB_-_JSON_Builder/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#=================================================
# Python App Template
# Version: 1.0.0
# Last Updated: 2017/10/04
#=================================================

#-------------------------------------------------
# Language Exclusions
#-------------------------------------------------
__pycache__
*.pyc

#-------------------------------------------------
# App Specific Exclusions
#-------------------------------------------------
*.log
log
sample-*.json

#-------------------------------------------------
# Build Exclusions
#-------------------------------------------------
build
lib_*
target
temp

#-------------------------------------------------
# IDE Exclusions
#-------------------------------------------------
.c9
.idea
.project
.python-version
.settings
.vscode
nbproject

#-------------------------------------------------
# Other Nonsense
#-------------------------------------------------
bcs*

#*************************************************
# Custom Exclusion (per App exclusions)
#*************************************************
sample.json
35 changes: 35 additions & 0 deletions apps/TCPB_-_JSON_Builder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Release Notes
## 1.0.0
* Initial Release

---

# Summary
This playbook App will take a JSON formatted String with embedded variables and output a JSON String.

> Note: String value must be wrapped in double quotes. All other values should **not** have double quotes.
# Dependencies
* tcex>=0.7,<0.8

# Input Definitions
* JSON Data - The JSON string to resolve embedded variables.

# Output Definitions
* json.data - The JSON String with all embedded variables resolved.

# Building

```
pip install tcex
tclib
tcpackage
```

# Local Testing

All the environment variables in `tcex.d/profiles/json_builder.json` file must be set on the local system.

```
tcrun --group qa-build
```
73 changes: 73 additions & 0 deletions apps/TCPB_-_JSON_Builder/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""Set lib directory for current version of Python"""
import os
import subprocess
import sys

__version__ = '1.0.1'


def main():
"""Main"""
lib_directory = None

# All Python Version that will be searched
lib_major_version = 'lib_{}'.format(sys.version_info.major)
lib_minor_version = '{}.{}'.format(lib_major_version, sys.version_info.minor)
lib_micro_version = '{}.{}'.format(lib_minor_version, sys.version_info.micro)

# Get all "lib" directories
app_path = os.getcwd()
contents = os.listdir(app_path)
lib_directories = []
for c in contents:
# ensure content starts with lib, is directory, and is readable
if c.startswith('lib') and os.path.isdir(c) and (os.access(c, os.R_OK)):
lib_directories.append(c)
# reverse sort directories
lib_directories.sort(reverse=True)

# Find most appropriate FULL version
lib_directory = None
if lib_micro_version in lib_directories:
lib_directory = lib_micro_version
elif lib_minor_version in lib_directories:
lib_directory = lib_minor_version
elif lib_major_version in lib_directories:
lib_directory = lib_major_version
else:
for lv in [lib_micro_version, lib_minor_version, lib_major_version]:
for ld in lib_directories:
if lv in ld:
lib_directory = ld
break
else:
continue
break

# No reason to continue if no valid lib directory found
if lib_directory is None:
print('Failed to find lib directory ({}).'.format(lib_directories))
sys.exit(1)

# Use this if you want to include modules from a subfolder
# lib_path = os.path.realpath(
# os.path.abspath(
# os.path.join(
# os.path.split(inspect.getfile(inspect.currentframe()))[0], lib_directory)))
lib_path = os.path.join(app_path, lib_directory)
if 'PYTHONPATH' in os.environ:
os.environ['PYTHONPATH'] = '{}{}{}'.format(lib_path, os.pathsep, os.environ['PYTHONPATH'])
else:
os.environ['PYTHONPATH'] = '{}'.format(lib_path)

# Update system arguments
sys.argv[0] = sys.executable
sys.argv[1] = '{}.py'.format(sys.argv[1])

# Make sure to exit with the return value from the subprocess call
ret = subprocess.call(sys.argv)
sys.exit(ret)


if __name__ == '__main__':
main()
Binary file added apps/TCPB_-_JSON_Builder/docs/screenshot_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions apps/TCPB_-_JSON_Builder/install.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"allowOnDemand": true,
"displayName": "JSON Builder",
"languageVersion": "3.6",
"listDelimiter": "|",
"note": "Take data of multiple types and output a JSON String.",
"params": [{
"label": "JSON Data",
"name": "json_data",
"note": "The JSON data including variables types. String variables must be manually wrapped in double quotes. All other Types should not have double quotes. Binary/BinaryArray values are not supported.",
"playbookDataType": [
"Any"
],
"required": true,
"sequence": 1,
"type": "String",
"validValues": ["${TEXT}"],
"viewRows": 20
}],
"playbook": {
"outputVariables": [{
"name": "json.data",
"type": "String"
}],
"type": "Utility"
},
"programLanguage": "PYTHON",
"programMain": "json_builder",
"programVersion": "1.0.0",
"runtimeLevel": "Playbook"
}
49 changes: 49 additions & 0 deletions apps/TCPB_-_JSON_Builder/json_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
""" JSON Pretty Playbook App """
import codecs
import json
import traceback
import sys

from tcex import TcEx

# Python 2 unicode
if sys.version_info[0] == 2:
reload(sys)
sys.setdefaultencoding('utf-8')

tcex = TcEx()

# App args
tcex.parser.add_argument('--json_data', required=True)
args = tcex.args


def main():
"""Main App Logic"""
json_data = tcex.playbook.read(args.json_data)

try:
json.loads(json_data)
except Exception as e:
err = 'JSON data was not properly formatted ({}).'.format(e)
tcex.log.error(err)
tcex.message_tc(err)
tcex.playbook.exit(1)

# create output
tcex.log.info('JSON data: {}'.format(json_data))
tcex.playbook.create_output('json.data', json_data)

tcex.message_tc('JSON data has been created.')
tcex.exit()


if __name__ == '__main__':
try:
main()
except Exception as e:
main_err = 'Generic Error. See logs for more details ({}).'.format(e)
tcex.log.error(traceback.format_exc())
tcex.message_tc(main_err)
tcex.playbook.exit(1)
1 change: 1 addition & 0 deletions apps/TCPB_-_JSON_Builder/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tcex>=0.7,<0.8
7 changes: 7 additions & 0 deletions apps/TCPB_-_JSON_Builder/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[flake8]
max-line-length = 100
ignore = E402

[pep8]
max-line-length = 100
ignore = E402
29 changes: 29 additions & 0 deletions apps/TCPB_-_JSON_Builder/tcex.d/data/json_builder.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[{
"data": "\"one\"",
"variable": "#App:0001:name!String"
},
{
"data": ["#App:0001:name!String", "two", "three"],
"variable": "#App:0001:counts!StringArray"
},
{
"data": {
"key": "numbers",
"value": "#App:0001:counts!StringArray"
},
"variable": "#App:0002:numbers!KeyValue"
},
{
"data": [{
"key": "alias",
"value": "ace"
}, {
"key": "alias",
"value": "#App:0001:name!String"
}, {
"key": "counts",
"value": "#App:0001:counts!StringArray"
}],
"variable": "#App:0002:aliases!KeyValueArray"
}
]
50 changes: 50 additions & 0 deletions apps/TCPB_-_JSON_Builder/tcex.d/profiles/json_builder.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"args": {
"api_default_org": "$env.API_DEFAULT_ORG",
"api_access_id": "$env.API_ACCESS_ID",
"api_secret_key": "$envs.API_SECRET_KEY",
"tc_api_path": "$env.TC_API_PATH",
"tc_log_level": "debug",
"tc_log_path": "log",
"tc_log_to_api": false,
"tc_out_path": "log",
"tc_proxy_external": false,
"tc_proxy_tc": false,
"tc_temp_path": "log",
"tc_playbook_db_type": "Redis",
"tc_playbook_db_context": "686be118-ff9a-4d05-bb14-4c1c6a0c9b87",
"tc_playbook_db_path": "$env.DB_PATH",
"tc_playbook_db_port": "$env.DB_PORT",
"tc_playbook_out_variables": "#App:3681:json.data!String",
"json_data": "{\"name\": \"#App:0001:name!String\", \"counts\": #App:0001:counts!StringArray, \"numbers\": #App:0002:numbers!KeyValue, \"aliases\": #App:0002:aliases!KeyValueArray}"
},
"data_files": [
"tcex.d/data/json_builder.json"
],
"description": "Pass test of JSON Builder playbook App.",
"exit_codes": [
0
],
"groups": [
"local"
],
"install_json": "install.json",
"profile_name": "json-builder",
"quiet": false,
"validations": [
{
"data": null,
"data_type": "redis",
"operator": "ne",
"variable": "#App:3681:json.data!String"
},
{
"data": "string",
"data_type": "redis",
"operator": "it",
"variable": "#App:3681:json.data!String"
}
]
}
]
15 changes: 15 additions & 0 deletions apps/TCPB_-_JSON_Builder/tcex.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"package": {
"app_name": "TCPB_-_JSON_Create",
"bundle": false,
"excludes": [
"requirements.txt",
"tcex.d"
],
"outdir": "target"
},
"profiles": [],
"profile_include_dirs": [
"tcex.d/profiles"
]
}
Loading

0 comments on commit 98a40ed

Please sign in to comment.