Skip to content

Commit 0f80094

Browse files
Merge branch 'typer' of github.com:cortexapps/cli into typer
2 parents 3d5535a + 99c5112 commit 0f80094

File tree

10 files changed

+106
-36
lines changed

10 files changed

+106
-36
lines changed

.github/workflows/validate-pr.yml

Lines changed: 0 additions & 30 deletions
This file was deleted.

HISTORY.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@ Release History
2121
- plugins `get-by-tag` subcommand changed to `get`
2222
- `resource-definitions` command changed to `entity-types`
2323

24+
0.27.0 (2025-01-04)
25+
------------------
26+
27+
**Improvements**
28+
- Add support for catalog patch command.
29+
2430
0.26.7 (2024-11-18)
2531
------------------
2632

2733
**Bugfixes**
2834
- Fix dryRun query parameter for catalog create command.
29-
>>>>>>> main
3035

3136
0.26.6 (2024-07-30)
3237
------------------

cortexapps_cli/cortex.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,8 @@ def debug_json(r, method):
597597
print(json_data, file=sys.stderr)
598598

599599
def exit(r, method, expected_rc=200, err=None):
600-
if r.status_code != expected_rc:
600+
# Quick fix for catalog patch; will fix with typer change.
601+
if r.status_code != expected_rc and r.status_code != 201:
601602
sys.stderr.write(r.reason + "\n")
602603
if r.status_code == 401:
603604
if config['config_file'] == "ENVIRONMENT":
@@ -689,6 +690,17 @@ def post(url, headers={}, payload="", expected_rc=200):
689690
err = e.response.text
690691
exit(r, 'POST', expected_rc, err)
691692

693+
def patch(url, headers={}, payload="", expected_rc=200):
694+
api_key(headers)
695+
696+
err = None
697+
try:
698+
r = requests.patch(config['url'] + url, headers=headers,data=payload)
699+
r.raise_for_status()
700+
except requests.exceptions.RequestException as e:
701+
err = e.response.text
702+
exit(r, 'PATCH', expected_rc, err)
703+
692704
# Generate HTTP API options. Everything in the Namespace argparse object is
693705
# added to the URL with the exception of those listed in the array below.
694706
def parse_opts(args, ignore_tags=[]):
@@ -972,6 +984,7 @@ def subparser_catalog_opts(subparsers):
972984
subparser_catalog_gitops_logs(sp)
973985
subparser_catalog_list(sp)
974986
subparser_catalog_list_descriptors(sp)
987+
subparser_catalog_patch(sp)
975988
subparser_catalog_scorecard_scores(sp)
976989
subparser_catalog_unarchive(sp)
977990

@@ -1148,6 +1161,52 @@ def subparser_catalog_details(subparser):
11481161
def catalog_details(args):
11491162
get("/api/v1/catalog/" + args.tag + parse_opts(args))
11501163

1164+
def subparser_catalog_patch(subparser):
1165+
sp = subparser.add_parser(
1166+
'patch',
1167+
help='Creates or updates an entity using OpenAPI. If the YAML refers to an entity that already exists (as referenced by the x-cortex-tag), this API will merge the specified changes into the existing entity.')
1168+
add_argument_file(sp, 'File containing openapi descriptor for entity')
1169+
sp.add_argument(
1170+
'-m',
1171+
'--delete-marker-value',
1172+
dest='deleteMarkerValue',
1173+
help='Delete keys with this value from the merged yaml, e.g. __delete__, if any values match this, they will not be included in merged YAML. For example my_value: __delete__ will remove my_value from the merged YAML.',
1174+
required=False,
1175+
default="__delete__",
1176+
metavar=''
1177+
)
1178+
sp.add_argument(
1179+
'-d',
1180+
'--dry-run',
1181+
dest="dryRun",
1182+
help='When true, this endpoint only validates the descriptor contents and returns any errors or warnings.',
1183+
action='store_true',
1184+
default='false'
1185+
)
1186+
sp.add_argument(
1187+
'-a',
1188+
'--append-arrays',
1189+
dest="appendArrays",
1190+
help='Default merge behavior is to replace arrays, set this to true to append arrays instead. For simple types, duplicate values will be removed from the merged array',
1191+
action='store_true',
1192+
default='false'
1193+
)
1194+
sp.add_argument(
1195+
'-n',
1196+
'--fail-if-entity-does-not-exist',
1197+
dest="failIfEntityDoesNotExist",
1198+
help='Default behavior is to upsert the entity, if set command will fail (404) if the entity specified in x-cortex-tag does not exist.',
1199+
action='store_true',
1200+
default='false'
1201+
)
1202+
sp.set_defaults(func=catalog_patch)
1203+
1204+
def catalog_patch(args):
1205+
patch(
1206+
"/api/v1/open-api" + parse_opts(args),
1207+
default_headers('application/openapi'), read_file(args)
1208+
)
1209+
11511210
def subparser_catalog_scorecard_scores(subparser):
11521211
sp = subparser.add_parser('scorecard-scores', help='Retrieve entity Scorecard scores')
11531212
add_argument_tag(sp)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Patch Entity
4+
description: Entity that will be created to test catalog patch entity
5+
x-cortex-tag: patch-entity
6+
x-cortex-type: component
7+
x-cortex-groups:
8+
- public-api-test
9+
x-cortex-definition: {}
10+
x-cortex-custom-metadata:
11+
owners:
12+
- owner-1

data/run-time/patch-entity.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
openapi: 3.0.0
2+
info:
3+
x-cortex-tag: patch-entity
4+
x-cortex-custom-metadata:
5+
owners:
6+
- owner-2

tests/test_audit_logs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
#@pytest.mark.skip(reason="Disabled until CET-15982 is resolved.")
44
def test():
55
result = cli(["audit-logs", "get"])
6-
assert (len(result['logs']) > 0)
6+
assert (len(result['logs']) > 0)

tests/test_audit_logs_page.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
#@pytest.mark.skip(reason="Disabled until CET-15982 is resolved.")
44
def test():
55
result = cli(["audit-logs", "get", "-p", "0"])
6-
assert (len(result['logs']) > 0)
6+
assert (len(result['logs']) > 0)

tests/test_audit_logs_size.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
#@pytest.mark.skip(reason="Disabled until CET-15982 is resolved.")
44
def test():
55
result = cli(["audit-logs", "get", "-p", "0", "-z", "1"])
6-
assert (len(result['logs']) == 1)
6+
assert (len(result['logs']) == 1)

tests/test_audit_logs_start_date.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
def test():
55
start_date = yesterday()
66
result = cli(["audit-logs", "get", "-s", start_date])
7-
assert (len(result['logs']) > 0)
7+
assert (len(result['logs']) > 0)

tests/test_catalog_patch_entity.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from common import *
2+
3+
def test(capsys):
4+
cli(["-q", "catalog", "patch", "-f", "data/run-time/create-patch-entity.yaml"])
5+
# Need to clear captured system output from the above commands to clear the way for the next one.
6+
capsys.readouterr()
7+
8+
response = cli_command(capsys, ["catalog", "descriptor", "-t", "patch-entity"])
9+
assert response['info']['x-cortex-tag'] == "patch-entity"
10+
11+
# Need to clear captured system output from the above commands to clear the way for the next one.
12+
capsys.readouterr()
13+
14+
cli(["-q", "catalog", "patch", "-a", "-f", "data/run-time/patch-entity.yaml"])
15+
capsys.readouterr()
16+
17+
response = cli_command(capsys, ["custom-data", "get", "-t", "patch-entity", "-k", "owners"])
18+
assert 'owner-2' in response['value'], "owner-2 should have been merged in owners array"

0 commit comments

Comments
 (0)