-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp_spec_yml.py
487 lines (420 loc) · 18.3 KB
/
app_spec_yml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
"""TcEx Framework Module"""
# standard library
import json
import logging
import os
from functools import cached_property
from pathlib import Path
# third-party
import yaml
try:
# third-party
from yaml import CDumper as Dumper
from yaml import CLoader as Loader
except ImportError:
from yaml import Dumper, Loader
from .install_json import InstallJson
from .model.app_spec_yml_model import AppSpecYmlModel
from .tcex_json import TcexJson
# get logger
_logger = logging.getLogger(__name__.split('.', maxsplit=1)[0])
class AppSpecYml:
"""Class object for app_spec.yml configuration file"""
def __init__(
self,
filename: str | None = None,
path: str | None = None,
logger: logging.Logger | None = None,
):
"""Initialize instance properties."""
filename = filename or 'app_spec.yml'
path = path or os.getcwd()
self.log = logger or _logger
# properties
self.fqfn = Path(os.path.join(path, filename))
self.ij = InstallJson(logger=self.log)
self.tj = TcexJson(logger=self.log)
@property
def _feature_data_advanced_request_inputs(self):
"""Return all inputs for advanced request."""
return [
{
'display': '''tc_action in ('Advanced Request')''',
'label': 'API Endpoint/Path',
'name': 'tc_adv_req_path',
'note': 'The API Path request.',
'playbookDataType': ['String'],
'required': True,
'type': 'String',
'validValues': ['${TEXT}'],
},
{
'display': '''tc_action in ('Advanced Request')''',
'default': 'GET',
'label': 'HTTP Method',
'name': 'tc_adv_req_http_method',
'note': 'HTTP method to use.',
'required': True,
'type': 'Choice',
'validValues': ['GET', 'POST', 'DELETE', 'PUT', 'HEAD', 'PATCH', 'OPTIONS'],
},
{
'display': '''tc_action in ('Advanced Request')''',
'label': 'Query Parameters',
'name': 'tc_adv_req_params',
'note': (
'Query parameters to append to the URL. For sensitive information like API '
'keys, using variables is recommended to ensure that the Playbook will not '
'export sensitive data.'
),
'playbookDataType': ['String', 'StringArray'],
'required': False,
'type': 'KeyValueList',
'validValues': ['${KEYCHAIN}', '${TEXT}'],
},
{
'display': '''tc_action in ('Advanced Request')''',
'label': 'Exclude Empty/Null Parameters',
'name': 'tc_adv_req_exclude_null_params',
'note': (
'''Some API endpoint don't handle null/empty query parameters properly '''
'''(e.g., ?name=&type=String). If selected this options will exclude any '''
'''query parameters that has a null/empty value.'''
),
'type': 'Boolean',
},
{
'display': '''tc_action in ('Advanced Request')''',
'label': 'Headers',
'name': 'tc_adv_req_headers',
'note': (
'Headers to include in the request. When using Multi-part Form/File data, '
'do **not** add a **Content-Type** header. For sensitive information like '
'API keys, using variables is recommended to ensure that the Playbook will '
'not export sensitive data.'
),
'playbookDataType': ['String'],
'required': False,
'type': 'KeyValueList',
'validValues': ['${KEYCHAIN}', '${TEXT}'],
},
{
'display': (
'''tc_action in ('Advanced Request') AND tc_adv_req_http_method '''
'''in ('POST', 'PUT', 'DELETE', 'PATCH')'''
),
'label': 'Body',
'name': 'tc_adv_req_body',
'note': 'Content of the HTTP request.',
'playbookDataType': ['String', 'Binary'],
'required': False,
'type': 'String',
'validValues': ['${KEYCHAIN}', '${TEXT}'],
'viewRows': 4,
},
{
'display': (
'''tc_action in ('Advanced Request') AND tc_adv_req_http_method '''
'''in ('POST', 'PUT', 'DELETE', 'PATCH')'''
),
'label': 'URL Encode JSON Body',
'name': 'tc_adv_req_urlencode_body',
'note': (
'''URL encode a JSON-formatted body. Typically used for'''
''' 'x-www-form-urlencoded' data, where the data can be configured in the '''
'''body as a JSON string.'''
),
'type': 'Boolean',
},
{
'display': '''tc_action in ('Advanced Request')''',
'default': True,
'label': 'Fail for Status',
'name': 'tc_adv_req_fail_on_error',
'note': 'Fail if the response status code is 4XX - 5XX.',
'type': 'Boolean',
},
]
@staticmethod
def _feature_data_advanced_request_outputs(prefix: str) -> dict:
"""Return all outputs for advanced request."""
return {
'display': 'tc_action in (\'Advanced Request\')',
'outputVariables': [
{
'name': f'{prefix}.request.content',
},
{
'name': f'{prefix}.request.content.binary',
'type': 'Binary',
},
{
'name': f'{prefix}.request.headers',
},
{
'name': f'{prefix}.request.ok',
},
{
'name': f'{prefix}.request.reason',
},
{
'name': f'{prefix}.request.status_code',
},
{
'name': f'{prefix}.request.url',
},
],
}
def _migrate_schema_100_to_110(self, contents: dict):
"""Migrate 1.0.0 schema to 1.1.0 schema."""
# moved app.* to top level
self._migrate_schema_100_to_110_app(contents)
# migrate app.feeds to app.organization.feeds
self._migrate_schema_100_to_110_organization_feeds(contents)
# migrate app.feeds to app.organization.repeating_minutes
self._migrate_schema_100_to_110_organization_repeating_minutes(contents)
# migrate app.feeds to app.organization.publish_out_files
self._migrate_schema_100_to_110_organization_publish_out_files(contents)
# migrate app.inputGroup to new schema
self._migrate_schema_100_to_110_input_groups(contents)
# migrate app.note to app.notePerAction with new schema
self._migrate_schema_100_to_110_notes_per_action(contents)
# migrate app.outputGroups to outputData with new schema
self._migrate_schema_100_to_110_output_groups(contents)
# migrate app.playbookType to category
contents['category'] = contents.pop('playbookType', '')
# migrate app.jira to internalNotes schema
self._migrate_schema_100_to_110_jira_notes(contents)
# migrate app.releaseNotes to new schema
self._migrate_schema_100_to_110_release_notes(contents)
# migrate app.retry to playbook.retry
self._migrate_schema_100_to_110_retry(contents)
# update the schema version
contents['schemaVersion'] = contents.get('schemaVersion') or '1.1.0'
@staticmethod
def _migrate_schema_100_to_110_app(contents: dict):
"""Migrate 1.0.0 schema to 1.1.0 schema."""
# remove "app" top level
for k, v in dict(contents).get('app', {}).items():
contents[k] = v
# assure minServerVersion exists
if contents.get('minServerVersion') is None:
contents['minServerVersion'] = '6.0.0'
# remove "app" from "app_spec"
del contents['app']
@staticmethod
def _migrate_schema_100_to_110_organization_feeds(contents: dict):
"""Migrate 1.0.0 schema to 1.1.0 schema."""
if contents.get('feeds') is not None and contents['runtimeLevel'].lower() == 'organization':
contents.setdefault('organization', {})
contents['organization']['feeds'] = contents.pop('feeds', [])
@staticmethod
def _migrate_schema_100_to_110_organization_repeating_minutes(contents: dict):
"""Migrate 1.0.0 schema to 1.1.0 schema."""
if (
contents.get('repeatingMinutes') is not None
and contents['runtimeLevel'].lower() == 'organization'
):
contents.setdefault('organization', {})
contents['organization']['repeatingMinutes'] = contents.pop('repeatingMinutes', [])
@staticmethod
def _migrate_schema_100_to_110_organization_publish_out_files(contents: dict):
"""Migrate 1.0.0 schema to 1.1.0 schema."""
if (
contents.get('publishOutFiles') is not None
and contents['runtimeLevel'].lower() == 'organization'
):
contents.setdefault('organization', {})
contents['organization']['publishOutFiles'] = contents.pop('publishOutFiles', [])
@staticmethod
def _migrate_schema_100_to_110_input_groups(contents: dict):
"""Migrate 1.0.0 schema to 1.1.0 schema."""
contents['sections'] = contents.pop('inputGroups', {})
for section in contents.get('sections') or []:
section['sectionName'] = section.pop('group')
section['params'] = section.pop('inputs')
# add missing name
for param in section['params']:
if param.get('name') is None:
param['name'] = param.get('label')
if 'sequence' in param:
del param['sequence']
if param.get('type') is None:
param['type'] = 'String'
@staticmethod
def _migrate_schema_100_to_110_notes_per_action(contents: dict):
"""Migrate 1.0.0 schema to 1.1.0 schema."""
contents['notePerAction'] = contents.pop('notes', {})
note_per_action = []
for action, note in contents['notePerAction'].items():
note_per_action.append({'action': action, 'note': note})
contents['notePerAction'] = note_per_action
@staticmethod
def _migrate_schema_100_to_110_retry(contents: dict):
"""Migrate 1.0.0 schema to 1.1.0 schema."""
if contents['runtimeLevel'].lower() == 'playbook':
contents.setdefault('playbook', {})
if contents.get('playbook', {}).get('retry'):
contents['playbook']['retry'] = contents.pop('retry', {})
@staticmethod
def _migrate_schema_100_to_110_output_groups(contents: dict):
"""Migrate 1.0.0 schema to 1.1.0 schema."""
outputs = []
contents['outputData'] = contents.pop('outputGroups', {})
for display, group in contents.get('outputData', {}).items():
output_data = {'display': display, 'outputVariables': []}
# fix schema when output type is assumed
if isinstance(group, list):
group = {'String': group}
for variable_type, variables in group.items():
for name in variables:
disabled = False
if name.startswith('~'):
name = name.replace('~', '')
disabled = True
output_data['outputVariables'].append(
{
'disabled': disabled,
'encrypt': False,
'intelTypes': [],
'name': name,
'note': None,
'type': variable_type,
}
)
outputs.append(output_data)
contents['outputData'] = outputs
@staticmethod
def _migrate_schema_100_to_110_jira_notes(contents: dict):
"""Migrate 1.0.0 schema to 1.1.0 schema."""
jira_notes = []
for k, v in contents.get('jira', {}).items():
# look for the trailer to find our items
if k == '_TRAILER_':
for item in v:
jira_notes.append(item)
contents['internalNotes'] = jira_notes
@staticmethod
def _migrate_schema_100_to_110_release_notes(contents: dict):
"""Migrate 1.0.0 schema to 1.1.0 schema."""
release_notes = []
# need to see if this exist, older apps it might not
if contents.get('releaseNotes'):
for k, v in contents.get('releaseNotes', {}).items():
release_notes.append({'version': k, 'notes': v})
contents['releaseNotes'] = release_notes
@cached_property
def contents(self) -> dict:
"""Return install.json file contents."""
def _load_contents() -> dict:
"""Load contents from file."""
contents = {}
if self.fqfn.is_file():
try:
with self.fqfn.open(encoding='utf-8') as fh:
contents = yaml.load(fh, Loader=Loader) # nosec
except (OSError, ValueError): # pragma: no cover
self.log.error(
f'feature=app-spec-yml, exception=failed-reading-file, filename={self.fqfn}'
)
else: # pragma: no cover
self.log.error(
f'feature=app-spec-yml, exception=file-not-found, filename={self.fqfn}'
)
return contents
contents = _load_contents()
# migrate schema from 1.0.0 to 1.1.0
if contents.get('schemaVersion', '1.0.0') == '1.0.0':
self._migrate_schema_100_to_110(contents)
# reformat file
self.rewrite_contents(contents)
# migrate schema
return _load_contents()
@staticmethod
def dict_to_yaml(data: dict) -> str:
"""Convert dict to yaml."""
return yaml.dump(
data,
Dumper=Dumper,
default_flow_style=False,
sort_keys=False,
)
@property
def has_spec(self):
"""Return True if App has app_spec.yml file."""
return self.fqfn.is_file()
@cached_property
def model(self) -> AppSpecYmlModel:
"""Return the Install JSON model.
If writing app_spec.yml file after the method then the model will include
advancedRequest inputs/outputs, etc.
"""
_contents = self.contents
# special case for dynamic handling of advancedRequest feature
if 'advancedRequest' in _contents.get('features', []):
# look for a Configure section which required for Advanced Request
if 'Configure' not in [
section.get('sectionName') for section in _contents.get('sections', [])
]:
raise RuntimeError('The advancedRequest feature requires a Configure section.')
# Add "Advanced Request" action to Valid Values
# when "advancedRequest" feature is enabled
for section in _contents.get('sections', []):
for param in section.get('params', []):
if param.get('name') == 'tc_action' and 'Advanced Request' not in param.get(
'validValues', []
):
param['validValues'].append('Advanced Request')
if section.get('sectionName') == 'Configure':
section['params'].extend(self._feature_data_advanced_request_inputs)
# add outputs
prefix = _contents.get('outputPrefix', '')
_contents['outputData'].append(self._feature_data_advanced_request_outputs(prefix))
return AppSpecYmlModel(**self.contents)
def fix_contents(self, contents: dict):
"""Fix missing data"""
# fix for null appId value
if 'appId' in contents and contents.get('appId') is None:
del contents['appId']
# update features
contents['features'] = AppSpecYmlModel(**contents).updated_features
# fix missing packageName
if contents.get('packageName') is None:
contents['packageName'] = self.tj.model.package.app_name
# fix programMain to always be run.py
if contents.get('programMain') in [None, 'run']:
contents['programMain'] = 'run.py'
# fix missing outputPrefix
if contents.get('outputPrefix') is None and 'advancedRequest' in contents.get(
'features', []
):
if self.ij.model.playbook is not None:
contents['outputPrefix'] = self.ij.model.playbook.output_prefix
# ensure displayPath is set for API Service Apps
if contents.get('displayPath') is None and contents['runtimeLevel'].lower() in [
'apiservice',
'feedapiservice',
]:
contents['displayPath'] = contents['displayName'].replace(' ', '-').lower()
def rewrite_contents(self, contents: dict):
"""Rewrite app_spec.yml file."""
self.fix_contents(contents)
# exclude_defaults - if False then all unused fields are added in - not good.
# exclude_none - this should be safe to leave as True.
# exclude_unset - this should be False to ensure that all fields are included.
contents = json.loads(
AppSpecYmlModel(**contents).json(
by_alias=True,
exclude_defaults=True,
exclude_none=True,
exclude_unset=False,
sort_keys=True,
)
)
# write the new contents to the file
self.write(contents)
return contents
def write(self, contents: dict):
"""Write yaml to file."""
with self.fqfn.open(mode='w', encoding='utf-8') as fh:
fh.write(self.dict_to_yaml(contents))