X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FN2VC.git;a=blobdiff_plain;f=n2vc%2Futils.py;h=0dbd71ef1d7c8db567f56e5fbd633abbcf414537;hp=e8cf64dac123207f6023ca18ac035ebe9f1840e0;hb=4395cfa6c7d0d80980c00d9f078440e0333fd826;hpb=c38a696d168531e3c067451044262ef4d78ef11f diff --git a/n2vc/utils.py b/n2vc/utils.py index e8cf64d..0dbd71e 100644 --- a/n2vc/utils.py +++ b/n2vc/utils.py @@ -12,11 +12,36 @@ # See the License for the specific language governing permissions and # limitations under the License. +import base64 +import re +import binascii +import yaml from enum import Enum from juju.machine import Machine from juju.application import Application from juju.action import Action from juju.unit import Unit +from n2vc.exceptions import N2VCInvalidCertificate + + +def base64_to_cacert(b64string): + """Convert the base64-encoded string containing the VCA CACERT. + + The input string.... + + """ + try: + cacert = base64.b64decode(b64string).decode("utf-8") + + cacert = re.sub( + r"\\n", + r"\n", + cacert, + ) + except binascii.Error as e: + raise N2VCInvalidCertificate(message="Invalid CA Certificate: {}".format(e)) + + return cacert class N2VCDeploymentStatus(Enum): @@ -93,10 +118,32 @@ JujuStatusToOSM = { }, } -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)