Skip to content

Commit 13c278d

Browse files
Merge pull request #106 from cortexapps/105-add-catalog-patch-sub-command
Add catalog patch #95
2 parents bafd3ce + 8a56a10 commit 13c278d

11 files changed

+108
-1
lines changed

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Release History
22
===============
33

4+
0.27.0 (2025-01-04)
5+
------------------
6+
7+
**Improvements**
8+
- Add support for catalog patch command.
9+
410
0.26.7 (2024-11-18)
511
------------------
612

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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from common import *
22

3+
@pytest.mark.skip(reason="Disabled until CET-15982 is resolved.")
34
def test(capsys):
45
response = cli_command(capsys, ["audit-logs", "get",])
56
assert (len(response['logs']) > 0)

tests/test_audit_logs_dates.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from common import *
22

3+
@pytest.mark.skip(reason="Disabled until CET-15982 is resolved.")
34
def test(capsys):
45
start_date = yesterday()
56
end_date = today()

tests/test_audit_logs_end_date.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from common import *
22

3+
@pytest.mark.skip(reason="Disabled until CET-15982 is resolved.")
34
def test(capsys):
45
end_date = today()
56
response = cli_command(capsys, ["audit-logs", "get", "-e", end_date])

tests/test_audit_logs_page.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from common import *
22

3+
@pytest.mark.skip(reason="Disabled until CET-15982 is resolved.")
34
def test(capsys):
45
response = cli_command(capsys, ["audit-logs", "get", "-p", "0",])
56
assert (len(response['logs']) > 0)

tests/test_audit_logs_size.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from common import *
22

3+
@pytest.mark.skip(reason="Disabled until CET-15982 is resolved.")
34
def test(capsys):
45
response = cli_command(capsys, ["audit-logs", "get", "-z", "1"])
56
assert (len(response['logs']) == 1)

tests/test_audit_logs_start_date.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from common import *
22

3+
@pytest.mark.skip(reason="Disabled until CET-15982 is resolved.")
34
def test(capsys):
45
start_date = yesterday()
56
response = cli_command(capsys, ["audit-logs", "get", "-s", start_date])

0 commit comments

Comments
 (0)