X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=n2vc%2Futils.py;h=14ac5d1f63c1f95694d22809fe65c4ad218dcbc6;hb=950f8451deba524b3882b1c6cb09e48ab5e2fdbd;hp=990575d29bca6d9e4f101b0ce5cdf1c4ca8213ee;hpb=4fee80e46dff88732b7927e502007203fcd8a15c;p=osm%2FN2VC.git diff --git a/n2vc/utils.py b/n2vc/utils.py index 990575d..14ac5d1 100644 --- a/n2vc/utils.py +++ b/n2vc/utils.py @@ -17,6 +17,7 @@ from juju.machine import Machine from juju.application import Application from juju.action import Action from juju.unit import Unit +import yaml class N2VCDeploymentStatus(Enum): @@ -67,35 +68,24 @@ class EntityType(Enum): return cls.get_entity(v) -FinalStatus = Dict( - { - EntityType.MACHINE: Dict({"field": "agent_status", "status": ["started"]}), - EntityType.APPLICATION: Dict( - {"field": "status", "status": ["active", "blocked"]} - ), - EntityType.ACTION: Dict( - {"field": "status", "status": ["completed", "failed", "cancelled"]} - ), - } -) - JujuStatusToOSM = { - EntityType.MACHINE: { + "machine": { "pending": N2VCDeploymentStatus.PENDING, "started": N2VCDeploymentStatus.COMPLETED, }, - EntityType.APPLICATION: { + "application": { "waiting": N2VCDeploymentStatus.RUNNING, "maintenance": N2VCDeploymentStatus.RUNNING, "blocked": N2VCDeploymentStatus.RUNNING, "error": N2VCDeploymentStatus.FAILED, "active": N2VCDeploymentStatus.COMPLETED, }, - EntityType.ACTION: { + "action": { + "pending": N2VCDeploymentStatus.PENDING, "running": N2VCDeploymentStatus.RUNNING, "completed": N2VCDeploymentStatus.COMPLETED, }, - EntityType.UNIT: { + "unit": { "waiting": N2VCDeploymentStatus.RUNNING, "maintenance": N2VCDeploymentStatus.RUNNING, "blocked": N2VCDeploymentStatus.RUNNING, @@ -103,3 +93,41 @@ JujuStatusToOSM = { "active": N2VCDeploymentStatus.COMPLETED, }, } + +DB_DATA = Dict( + { + "api_endpoints": Dict( + {"table": "admin", "filter": {"_id": "juju"}, "key": "api_endpoints"} + ) + } +) + + +def obj_to_yaml(obj: object) -> str: + """ + Converts object to yaml format + :return: yaml data + """ + # dump to yaml + dump_text = yaml.dump(obj, default_flow_style=False, indent=2) + # split lines + lines = dump_text.splitlines() + # remove !!python/object tags + yaml_text = "" + for line in lines: + index = line.find("!!python/object") + if index >= 0: + line = line[:index] + yaml_text += line + "\n" + return yaml_text + + +def obj_to_dict(obj: object) -> dict: + """ + Converts object to dictionary format + :return: dict data + """ + # convert obj to yaml + yaml_text = obj_to_yaml(obj) + # parse to dict + return yaml.load(yaml_text, Loader=yaml.Loader)