Skip to content

Commit 6e1582e

Browse files
author
Alan Christie
committed
fix: Add get_asset_names function
§
1 parent b6fc21b commit 6e1582e

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

decoder/decoder.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import enum
88
import os
99
import re
10-
from typing import Any, Dict, List, Optional, Pattern, Tuple
10+
from typing import Any, Dict, List, Optional, Pattern, Set, Tuple
1111

1212
import jsonschema
1313
import yaml
@@ -211,6 +211,29 @@ def get_job_doc_url(
211211
return doc_url
212212

213213

214+
def get_asset_names(job_definition: Dict[str, Any]) -> Set[str]:
215+
""" "Given a Job definition this function returns the unique list of all the
216+
asset names declared. Asset names can be used in image environment
217+
variables and files.
218+
"""
219+
asset_names: Set[str] = set()
220+
221+
# Check the environment block...
222+
environment: List[Dict[str, Any]] = job_definition.get("image", {}).get(
223+
"environment", []
224+
)
225+
for env in environment:
226+
if "account-server-asset" in env["value-from"]:
227+
asset_names.add(env["value-from"]["account-server-asset"]["name"])
228+
# Check the file block...
229+
file_block: List[Dict[str, Any]] = job_definition.get("image", {}).get("file", [])
230+
for item in file_block:
231+
if "account-server-asset" in item["content-from"]:
232+
asset_names.add(item["content-from"]["account-server-asset"]["name"])
233+
234+
return asset_names
235+
236+
214237
def decode(
215238
template_text: str,
216239
variable_map: Optional[Dict[str, str]],

tests/test_get_asset_names.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Tests for the decoder's get_job_doc_url() function.
2+
from typing import Dict
3+
4+
import pytest
5+
6+
pytestmark = pytest.mark.unit
7+
8+
from decoder import decoder
9+
10+
11+
def test_get_asset_names():
12+
# Arrange
13+
job_definition: Dict = {
14+
"image": {
15+
"environment": [
16+
{
17+
"name": "BLOB",
18+
"value-from": {"account-server-asset": {"name": "asset-a"}},
19+
}
20+
],
21+
"file": [
22+
{
23+
"name": "/tmp/blob.txt",
24+
"content-from": {"account-server-asset": {"name": "asset-a"}},
25+
},
26+
{
27+
"name": "/tmp/blob.txt",
28+
"content-from": {"account-server-asset": {"name": "asset-b"}},
29+
},
30+
],
31+
},
32+
}
33+
34+
# Act
35+
asset_names = decoder.get_asset_names(job_definition)
36+
37+
# Assert
38+
assert len(asset_names) == 2
39+
assert "asset-a" in asset_names
40+
assert "asset-b" in asset_names

0 commit comments

Comments
 (0)