Skip to content

Commit 682f11d

Browse files
committed
Synsation story runner
Problem: Convert synsation samples to YAML story runner. Solution: New functest test_runner_synsation uses new locations creator and types of assets. The story is creted via Jinja2 template.... Signed-off-by: Paul Hewlett <phewlett76@gmail.com>
1 parent 77236d3 commit 682f11d

54 files changed

Lines changed: 1375 additions & 169 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

archivist/assets.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,12 @@ def create_if_not_exists(
193193
arc_serial_number: das-j1-01
194194
arc_description: Electronic door entry system to Jitsuin France
195195
wavestone_asset_id: paris.france.jitsuin.das
196+
location:
197+
identity: locations/xxxxxxxxxxxxxxxxxxxxxxxxxx
196198
location:
197199
selector:
198200
- display_name
199-
display_name: Jitsuin Paris,
201+
display_name: Jitsuin Paris
200202
description: Sales and sales support for the French region
201203
latitude: 48.8339211,
202204
longitude: 2.371345,
@@ -213,6 +215,9 @@ def create_if_not_exists(
213215
If 'location' is specified then the 'selector' value is required and is used as a
214216
secondary key. Likewise the secondary key must exist in the attributes of the location.
215217
218+
Alternatively the identity of the location is specified - both
219+
are shown - choose one.
220+
216221
Returns:
217222
tuple of :class:`Asset` instance, Boolean is True if asset already existed
218223
@@ -239,10 +244,13 @@ def create_if_not_exists(
239244

240245
# is location present?
241246
if location is not None:
242-
loc, _ = self._archivist.locations.create_if_not_exists(
243-
location,
244-
)
245-
data["attributes"]["arc_home_location_identity"] = loc["identity"]
247+
if "identity" in location:
248+
data["attributes"]["arc_home_location_identity"] = location["identity"]
249+
else:
250+
loc, _ = self._archivist.locations.create_if_not_exists(
251+
location,
252+
)
253+
data["attributes"]["arc_home_location_identity"] = loc["identity"]
246254

247255
# any attachments ?
248256
if attachments is not None:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"""Archivist runner
2+
"""
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# pylint: disable=missing-docstring
2+
3+
4+
from .main import main
5+
6+
7+
if __name__ == "__main__":
8+
# execute only if run as a script
9+
main()

archivist/cmds/template/main.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# pylint: disable=missing-docstring
2+
3+
from logging import getLogger
4+
from sys import exit as sys_exit
5+
from sys import stdout as sys_stdout
6+
7+
from ...parser import common_parser, endpoint
8+
9+
from .run import run
10+
11+
LOGGER = getLogger(__name__)
12+
13+
14+
def main():
15+
parser = common_parser("Executes the archivist runner from a template file")
16+
17+
parser.add_argument(
18+
"values",
19+
help="the values file describing the data to be injected into the template",
20+
)
21+
parser.add_argument(
22+
"template", help="the template file describing the operations to conduct"
23+
)
24+
args = parser.parse_args()
25+
26+
arch = endpoint(args)
27+
28+
run(arch, args)
29+
30+
parser.print_help(sys_stdout)
31+
sys_exit(1)

archivist/cmds/template/run.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# pylint: disable=missing-docstring
2+
3+
from logging import getLogger
4+
from os import environ
5+
from pathlib import PurePath
6+
from sys import exit as sys_exit
7+
8+
from jinja2 import Environment, FileSystemLoader
9+
import yaml
10+
11+
from ... import about
12+
13+
# pylint:disable=unused-import # To prevent cyclical import errors forward referencing is used
14+
# pylint:disable=cyclic-import # but pylint doesn't understand this feature
15+
from ... import archivist as type_helper
16+
17+
18+
LOGGER = getLogger(__name__)
19+
20+
21+
def run(arch: "type_helper.Archivist", args):
22+
23+
LOGGER.info("Using version %s of jitsuin-archivist", about.__version__)
24+
LOGGER.info("Namespace %s", args.namespace)
25+
26+
path = PurePath(args.template)
27+
jinja = Environment(
28+
loader=FileSystemLoader(path.parent),
29+
trim_blocks=True,
30+
lstrip_blocks=True,
31+
)
32+
template = jinja.get_template(path.name)
33+
34+
# if namespace is specified on the commandline then override any environment
35+
# setting...
36+
if args.namespace:
37+
environ["ARCHIVIST_NAMESPACE"] = args.namespace
38+
39+
# environment is injected into the template
40+
with open(args.values, "r", encoding="utf-8") as fd:
41+
arch.runner(
42+
yaml.load(
43+
template.render(
44+
yaml.load(
45+
fd,
46+
Loader=yaml.SafeLoader,
47+
),
48+
env=environ,
49+
),
50+
Loader=yaml.SafeLoader,
51+
),
52+
)
53+
54+
sys_exit(0)

archivist/events.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def create_from_data(self, asset_id: str, data: Dict, *, confirm=False) -> Event
159159
Args:
160160
asset_id (str): asset identity e.g. assets/xxxxxxxxxxxxxxxxxxxxxxxxxx
161161
data (dict): request body of event.
162-
confirm (bool): if True wait for event to be confirmed on DLT.
162+
confirm (bool): if True wait for event to be confirmed.
163163
164164
Returns:
165165
:class:`Event` instance
@@ -171,10 +171,13 @@ def create_from_data(self, asset_id: str, data: Dict, *, confirm=False) -> Event
171171
# is location present?
172172
location = data.pop("location", None)
173173
if location is not None:
174-
loc, _ = self._archivist.locations.create_if_not_exists(
175-
location,
176-
)
177-
event_attributes["arc_location_identity"] = loc["identity"]
174+
if "identity" in location:
175+
data["event_attributes"]["arc_location_identity"] = location["identity"]
176+
else:
177+
loc, _ = self._archivist.locations.create_if_not_exists(
178+
location,
179+
)
180+
event_attributes["arc_location_identity"] = loc["identity"]
178181

179182
sbom = data.pop("sbom", None)
180183
if sbom is not None:

archivist/locations.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from .dictmerge import _deepmerge
3535
from .errors import ArchivistNotFoundError
3636
from .utils import selector_signature
37+
from .type_aliases import NoneOnError
3738

3839

3940
LOGGER = getLogger(__name__)
@@ -46,6 +47,17 @@ class Location(dict):
4647
4748
"""
4849

50+
@property
51+
def name(self) -> NoneOnError[str]:
52+
"""str: name of the location"""
53+
name = None
54+
try:
55+
name = self["display_name"]
56+
except KeyError:
57+
pass
58+
59+
return name
60+
4961

5062
class _LocationsClient:
5163
"""LocationsClient

archivist/parser.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
from warnings import filterwarnings
1313

1414
from .archivist import Archivist
15-
from .logger import set_logger
1615
from .dictmerge import _deepmerge
16+
from .logger import set_logger
1717
from .proof_mechanism import ProofMechanism
1818
from .utils import get_auth
1919

@@ -74,15 +74,15 @@ def common_parser(description: str):
7474
dest="url",
7575
action="store",
7676
default="https://rkvst.poc.jitsuin.io",
77-
help="location of Archivist service",
77+
help="url of Archivist service",
7878
)
7979
parser.add_argument(
8080
"-p",
8181
"--proof-mechanism",
8282
type=ProofMechanism,
8383
action=EnumAction,
8484
dest="proof_mechanism",
85-
default=ProofMechanism.SIMPLE_HASH,
85+
default=None,
8686
help="mechanism for proving the evidence for events on the Asset",
8787
)
8888
parser.add_argument(
@@ -132,11 +132,14 @@ def endpoint(args):
132132

133133
arch = None
134134
LOGGER.info("Initialising connection to Jitsuin Archivist...")
135-
fixtures = {
136-
"assets": {
137-
"proof_mechanism": args.proof_mechanism.name,
138-
},
139-
}
135+
fixtures = {}
136+
if args.proof_mechanism is not None:
137+
fixtures = {
138+
"assets": {
139+
"proof_mechanism": args.proof_mechanism.name,
140+
},
141+
}
142+
140143
if args.namespace is not None:
141144
fixtures = _deepmerge(
142145
fixtures,

0 commit comments

Comments
 (0)