Skip to content

Commit cde617f

Browse files
author
Alan Christie
committed
feat: Now offers get_environment_assets()
1 parent 93d9419 commit cde617f

File tree

5 files changed

+73
-5
lines changed

5 files changed

+73
-5
lines changed

decoder/decoder.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,10 @@ def get_asset_names(job_definition: Dict[str, Any]) -> Set[str]:
251251
def get_file_assets(job_definition: Dict[str, Any]) -> List[Dict[str, str]]:
252252
"""Given a Job definition this function returns the list of all the
253253
file-based assets declared. What's returned is the asset 'name' and the
254-
'image file' the asset is expected to mapped to.
254+
'image file' the asset is expected to be mapped to.
255+
256+
In order to get the asset the caller will need to interact with the
257+
Account Server (AS), what is returned here is the asset name, not the asset.
255258
"""
256259
file_assets: List[Dict[str, str]] = []
257260

@@ -269,6 +272,32 @@ def get_file_assets(job_definition: Dict[str, Any]) -> List[Dict[str, str]]:
269272
return file_assets
270273

271274

275+
def get_environment_assets(job_definition: Dict[str, Any]) -> List[Dict[str, str]]:
276+
"""Given a Job definition this function returns the list of all the
277+
environment-based assets declared. What's returned is the asset 'name' and the
278+
environment 'variable' the asset is expected to be mapped to.
279+
280+
In order to get the asset the caller will need to interact with the
281+
Account Server (AS), what is returned here is the asset name, not the asset.
282+
"""
283+
env_assets: List[Dict[str, str]] = []
284+
285+
# Iterate through the environment block...
286+
environment: List[Dict[str, Any]] = job_definition.get("image", {}).get(
287+
"environment", []
288+
)
289+
for item in environment:
290+
if "account-server-asset" in item["value-from"]:
291+
env_assets.append(
292+
{
293+
"asset": item["value-from"]["account-server-asset"]["name"],
294+
"variable": item["name"],
295+
}
296+
)
297+
298+
return env_assets
299+
300+
272301
def decode(
273302
template_text: str,
274303
variable_map: Optional[Dict[str, str]],

tests/test_get_asset_names.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Tests for the decoder's get_job_doc_url() function.
1+
# Tests for the decoder's get_asset_names() function.
22
from typing import Dict
33

44
import pytest
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_environment_assets() 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_environment_assets():
12+
# Arrange
13+
job_definition: Dict = {
14+
"image": {
15+
"environment": [
16+
{
17+
"name": "BLOB",
18+
"value-from": {"account-server-asset": {"name": "asset-c"}},
19+
}
20+
],
21+
"file": [
22+
{
23+
"name": "/tmp/blob-1.txt",
24+
"content-from": {"account-server-asset": {"name": "asset-a"}},
25+
},
26+
{
27+
"name": "/tmp/blob-2.txt",
28+
"content-from": {"account-server-asset": {"name": "asset-b"}},
29+
},
30+
],
31+
},
32+
}
33+
34+
# Act
35+
env_assets = decoder.get_environment_assets(job_definition)
36+
37+
# Assert
38+
assert len(env_assets) == 1
39+
assert env_assets[0]["asset"] == "asset-c"
40+
assert env_assets[0]["variable"] == "BLOB"

tests/test_get_file_assets.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Tests for the decoder's get_job_doc_url() function.
1+
# Tests for the decoder's get_file_assets() function.
22
from typing import Dict
33

44
import pytest
@@ -36,7 +36,6 @@ def test_get_file_assets():
3636

3737
# Assert
3838
assert len(file_assets) == 2
39-
print(file_assets)
4039
for file_asset in file_assets:
4140
if file_asset["image-file"] == "/tmp/blob-1.txt":
4241
assert file_asset["asset"] == "asset-a"

tests/test_get_pull_secret_names.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Tests for the decoder's get_job_doc_url() function.
1+
# Tests for the decoder's get_pull_secret_names() function.
22
from typing import Dict
33

44
import pytest

0 commit comments

Comments
 (0)