Implement ara_record data types
Data types allows a user to specify a type when recording data. Depending on the type, the display behavior or format will be different in the web interface. The first implemented types are as follows: - text (default): straight text in <pre> - url: value becomes a hyperlink - json: value is json pretty-printed Other example types that could be implemented eventually would be markdown or rst for rich formatting. Change-Id: I28b78a2b5899ece3b0abc4648bd8d0a2678c80ee
This commit is contained in:
@@ -60,6 +60,7 @@ EXAMPLES = '''
|
||||
with_items:
|
||||
- foo.key
|
||||
- foo.value
|
||||
- foo.type
|
||||
- foo.playbook_id
|
||||
'''
|
||||
|
||||
@@ -122,12 +123,14 @@ class ActionModule(ActionBase):
|
||||
if data:
|
||||
result['key'] = data.key
|
||||
result['value'] = data.value
|
||||
result['type'] = data.type
|
||||
result['playbook_id'] = data.playbook_id
|
||||
msg = "Sucessfully read data for the key {0}".format(data.key)
|
||||
result['msg'] = msg
|
||||
except Exception as e:
|
||||
result['key'] = None
|
||||
result['value'] = None
|
||||
result['type'] = None
|
||||
result['playbook_id'] = None
|
||||
result['failed'] = True
|
||||
msg = "Could not read data for key {0}: {1}".format(key, str(e))
|
||||
|
||||
@@ -42,6 +42,11 @@ options:
|
||||
description:
|
||||
- Value of the key written to
|
||||
required: true
|
||||
type:
|
||||
description:
|
||||
- Type of the key
|
||||
choices: [text, url, json]
|
||||
default: text
|
||||
|
||||
requirements:
|
||||
- "python >= 2.6"
|
||||
@@ -62,6 +67,17 @@ EXAMPLES = '''
|
||||
- ara_record:
|
||||
key: "git_version"
|
||||
value: "{{ git_version.stdout }}"
|
||||
|
||||
# Write data with a type (otherwise defaults to "text")
|
||||
# This changes the behavior on how the value is presented in the web interface
|
||||
- ara_record:
|
||||
key: "{{ item.key }}"
|
||||
value: "{{ item.value }}"
|
||||
type: "{{ item.type }}"
|
||||
with_items:
|
||||
- { key: "log", value: "error", type: "text" }
|
||||
- { key: "website", value: "http://domain.tld", type: "url" }
|
||||
- { key: "data", value: "{ 'key': 'value' }", type: "json" }
|
||||
'''
|
||||
|
||||
|
||||
@@ -69,19 +85,22 @@ class ActionModule(ActionBase):
|
||||
''' Record persistent data as key/value pairs in ARA '''
|
||||
|
||||
TRANSFERS_FILES = False
|
||||
VALID_ARGS = frozenset(('key', 'value'))
|
||||
VALID_ARGS = frozenset(('key', 'value', 'type'))
|
||||
VALID_TYPES = ['text', 'url', 'json']
|
||||
|
||||
def create_or_update_key(self, playbook_id, key, value):
|
||||
def create_or_update_key(self, playbook_id, key, value, type):
|
||||
try:
|
||||
data = (models.Data.query
|
||||
.filter_by(key=key)
|
||||
.filter_by(playbook_id=playbook_id)
|
||||
.one())
|
||||
data.value = value
|
||||
data.type = type
|
||||
except models.NoResultFound:
|
||||
data = models.Data(playbook_id=playbook_id,
|
||||
key=key,
|
||||
value=value)
|
||||
value=value,
|
||||
type=type)
|
||||
db.session.add(data)
|
||||
db.session.commit()
|
||||
|
||||
@@ -110,14 +129,24 @@ class ActionModule(ActionBase):
|
||||
|
||||
key = self._task.args.get('key', None)
|
||||
value = self._task.args.get('value', None)
|
||||
type = self._task.args.get('type', 'text')
|
||||
|
||||
required = ['key', 'value']
|
||||
for parameter in required:
|
||||
if not self._task.args.get(parameter):
|
||||
result['failed'] = True
|
||||
result['msg'] = "{} parameter is required".format(parameter)
|
||||
result['msg'] = "Parameter '{0}' is required".format(parameter)
|
||||
return result
|
||||
|
||||
if type not in self.VALID_TYPES:
|
||||
result['failed'] = True
|
||||
msg = "Type '{0}' is not supported, choose one of: {1}".format(
|
||||
type,
|
||||
", ".join(self.VALID_TYPES)
|
||||
)
|
||||
result['msg'] = msg
|
||||
return result
|
||||
|
||||
# Retrieve the persisted playbook_id from tmpfile
|
||||
tmpfile = os.path.join(app.config['ARA_TMP_DIR'], 'ara.json')
|
||||
with open(tmpfile) as file:
|
||||
@@ -125,7 +154,7 @@ class ActionModule(ActionBase):
|
||||
playbook_id = data['playbook']['id']
|
||||
|
||||
try:
|
||||
self.create_or_update_key(playbook_id, key, value)
|
||||
self.create_or_update_key(playbook_id, key, value, type)
|
||||
result['msg'] = "Data recorded in ARA for this playbook."
|
||||
except Exception as e:
|
||||
result['failed'] = True
|
||||
|
||||
Reference in New Issue
Block a user