kepconfig.ua_gateway.common
1from ..utils import _url_parse_object 2from ..connection import server 3from ..error import KepHTTPError 4from enum import Enum 5 6r"""`common` contains common internal functions and constants used by the 7`ua_gateway` module. 8""" 9 10CERT_TRUST_KEY = 'ua_gateway.UA_CERTIFICATE_TRUST_STATUS' 11 12# URL Constants for UA Gateway Module 13 14UA_GATEWAY_ROOT = '/project/_ua_gateway' 15CERT_ROOT = f'{UA_GATEWAY_ROOT}/certificates' 16CLIENT_ROOT = f'{UA_GATEWAY_ROOT}/ua_client_interfaces/Client Interface' 17CONN_ROOT = f'{CLIENT_ROOT}/ua_client_connections' 18CLIENT_CERT_ROOT = f'{CLIENT_ROOT}/certificates' 19CLIENT_INST_CERT_ROOT = f'{CLIENT_ROOT}/client_instance_certificates' 20SERVER_ROOT = f'{UA_GATEWAY_ROOT}/ua_server_interfaces/Server Interface' 21ENDPOINT_ROOT = f'{SERVER_ROOT}/ua_server_endpoints' 22SERVER_CERT_ROOT = f'{SERVER_ROOT}/certificates' 23SERVER_INST_CERT_ROOT = f'{SERVER_ROOT}/server_instance_certificates' 24 25 26class _INTER_TYPE(Enum): 27 SERVER = 0 28 CLIENT = 1 29 CERTS = 2 30 31# TODO: DEPRECATED: This constant is deprecated and will be removed in a future release. 32INSTANCE_CERTIFICATE = "Instance Certificate" 33 34def _create_url_cert(interface, certificate = None): 35 '''Creates url object for the "certificate" branch of Kepware's UA Gateway. Used 36 to build a part of Kepware Configuration API URL structure 37 ''' 38 if interface == _INTER_TYPE.SERVER: 39 if certificate == None: 40 return SERVER_CERT_ROOT 41 else: 42 return f'{SERVER_CERT_ROOT}/{_url_parse_object(certificate)}' 43 elif interface == _INTER_TYPE.CLIENT: 44 if certificate == None: 45 return CLIENT_CERT_ROOT 46 else: 47 return f'{CLIENT_CERT_ROOT}/{_url_parse_object(certificate)}' 48 # TODO: DEPRECATED: This interface type is deprecated and will be removed in a future release. 49 else: 50 if certificate == None: 51 return CERT_ROOT 52 else: 53 return '{}/{}'.format(CERT_ROOT,_url_parse_object(certificate)) 54 55def _create_url_inst_cert(interface, certificate = None): 56 '''Creates url object for the "instance certificate" branch of Kepware's UA Gateway interfaces. Used 57 to build a part of Kepware Configuration API URL structure 58 ''' 59 if interface == _INTER_TYPE.SERVER: 60 if certificate == None: 61 return SERVER_INST_CERT_ROOT 62 else: 63 return f'{SERVER_INST_CERT_ROOT}/{_url_parse_object(certificate)}' 64 elif interface == _INTER_TYPE.CLIENT: 65 if certificate == None: 66 return CLIENT_INST_CERT_ROOT 67 else: 68 return f'{CLIENT_INST_CERT_ROOT}/{_url_parse_object(certificate)}' 69 else: 70 if certificate == None: 71 return CERT_ROOT 72 else: 73 return '{}/{}'.format(CERT_ROOT,_url_parse_object(certificate)) 74 75def _change_cert_trust(server: server, inter_type, certificate: str, trust: bool): 76 DATA = { 77 CERT_TRUST_KEY: int(trust) 78 } 79 80 cert_data = server._force_update_check(True, DATA) 81 r = server._config_update(server.url + _create_url_cert(inter_type, certificate), cert_data) 82 if r.code == 200: return True 83 else: 84 raise KepHTTPError(r.url, r.code, r.msg, r.hdrs, r.payload) 85 86def _delete_cert_truststore(server: server, inter_type, certificate: str): 87 r = server._config_del(server.url + _create_url_cert(inter_type, certificate)) 88 if r.code == 200: return True 89 else: 90 raise KepHTTPError(r.url, r.code, r.msg, r.hdrs, r.payload) 91 92def _create_url_server(ua_server_endpoint = None): 93 '''Creates url object for the "ua_server_endpoints" branch of Kepware's UA Gateway. Used 94 to build a part of Kepware Configuration API URL structure 95 96 Returns the UA Gateway client connections specific url when a value is passed as the ua client interface name. 97 ''' 98 if ua_server_endpoint == None: 99 return ENDPOINT_ROOT 100 else: 101 return f'{ENDPOINT_ROOT}/{_url_parse_object(ua_server_endpoint)}' 102 103def _create_url_client(ua_client_connection = None): 104 '''Creates url object for the "ua_client_connections" branch of Kepware's UA Gateway. Used 105 to build a part of Kepware Configuration API URL structure 106 107 Returns the UA Gateway client connections specific url when a value is passed as the ua client interface name. 108 ''' 109 if ua_client_connection == None: 110 return CONN_ROOT 111 else: 112 return f'{CONN_ROOT}/{_url_parse_object(ua_client_connection)}'
CERT_TRUST_KEY =
'ua_gateway.UA_CERTIFICATE_TRUST_STATUS'
UA_GATEWAY_ROOT =
'/project/_ua_gateway'
CERT_ROOT =
'/project/_ua_gateway/certificates'
CLIENT_ROOT =
'/project/_ua_gateway/ua_client_interfaces/Client Interface'
CONN_ROOT =
'/project/_ua_gateway/ua_client_interfaces/Client Interface/ua_client_connections'
CLIENT_CERT_ROOT =
'/project/_ua_gateway/ua_client_interfaces/Client Interface/certificates'
CLIENT_INST_CERT_ROOT =
'/project/_ua_gateway/ua_client_interfaces/Client Interface/client_instance_certificates'
SERVER_ROOT =
'/project/_ua_gateway/ua_server_interfaces/Server Interface'
ENDPOINT_ROOT =
'/project/_ua_gateway/ua_server_interfaces/Server Interface/ua_server_endpoints'
SERVER_CERT_ROOT =
'/project/_ua_gateway/ua_server_interfaces/Server Interface/certificates'
SERVER_INST_CERT_ROOT =
'/project/_ua_gateway/ua_server_interfaces/Server Interface/server_instance_certificates'
INSTANCE_CERTIFICATE =
'Instance Certificate'