|
| 1 | +# ------------------------------------------------------------------------- |
| 2 | +# Copyright (c), PTC Inc. and/or all its affiliates. All rights reserved. |
| 3 | +# See License.txt in the project root for |
| 4 | +# license information. |
| 5 | +# -------------------------------------------------------------------------- |
| 6 | + |
| 7 | +r"""`lls` exposes an API to allow modifications to Local License Server parameters in |
| 8 | +the Kepware Administration through the Kepware Configuration API |
| 9 | +""" |
| 10 | +from typing import Union |
| 11 | +from ..error import KepHTTPError, KepError |
| 12 | + |
| 13 | + |
| 14 | +LLS_ROOT = '/admin' |
| 15 | +LICENSING_SERVER_PORT = "libadminsettings.LICENSING_SERVER_PORT" |
| 16 | +LICENSING_SERVER_NAME = "libadminsettings.LICENSING_SERVER_NAME" |
| 17 | +LICENSING_SERVER_ENABLE = "libadminsettings.LICENSING_SERVER_ENABLE" |
| 18 | +LICENSING_CHECK_PERIOD_MINS = "libadminsettings.LICENSING_CHECK_PERIOD_MINS" |
| 19 | +LICENSING_SERVER_SSL_PORT = "libadminsettings.LICENSING_SERVER_SSL_PORT" |
| 20 | +LICENSING_SERVER_ALLOW_INSECURE_COMMS = "libadminsettings.LICENSING_SERVER_ALLOW_INSECURE_COMMS" |
| 21 | +LICENSING_SERVER_ALLOW_SELF_SIGNED_CERTS = "libadminsettings.LICENSING_SERVER_ALLOW_SELF_SIGNED_CERTS" |
| 22 | +LICENSING_CLIENT_ALIAS = "libadminsettings.LICENSING_CLIENT_ALIAS" |
| 23 | + |
| 24 | +class lls_config: |
| 25 | + '''A class to represent a admin properties for the Local License Server connection from an instance of Kepware. |
| 26 | + This object is used to easily manage the LLS parameters for a Kepware instance. |
| 27 | +
|
| 28 | + Properties: |
| 29 | +
|
| 30 | + "server_name" - Host name or IP address of the LLS server |
| 31 | + "server_port" - HTTP/non-SSL port to target for the LLS server |
| 32 | + "check_period" - Period that Kepware checks licensing status |
| 33 | + "server_port_SSL" - HTTPS/SSL port to target for the LLS server |
| 34 | + "allow_insecure_comms" - When True, use HTTP/non-SSL connection to LLS |
| 35 | + "allow_self_signed_certs" - Allow for self signed certificates to be used during HTTPS/SSL connections to the LLS |
| 36 | + "instance_alias_name" - Alias name for LLS to use as reference to this Kepware instance |
| 37 | + ''' |
| 38 | + |
| 39 | + def __init__(self, config = {}): |
| 40 | + self.server_name = config[LICENSING_SERVER_NAME] if LICENSING_SERVER_NAME in config else '' |
| 41 | + self.server_port = config[LICENSING_SERVER_PORT] if LICENSING_SERVER_PORT in config else 7070 |
| 42 | + self.check_period = config[LICENSING_CHECK_PERIOD_MINS] if LICENSING_CHECK_PERIOD_MINS in config else 5 |
| 43 | + self.server_port_SSL = config[LICENSING_SERVER_SSL_PORT] if LICENSING_SERVER_SSL_PORT in config else 1883 |
| 44 | + self.allow_insecure_comms = config[LICENSING_SERVER_ALLOW_INSECURE_COMMS] if LICENSING_SERVER_ALLOW_INSECURE_COMMS in config else False |
| 45 | + self.allow_self_signed_certs = config[LICENSING_SERVER_ALLOW_SELF_SIGNED_CERTS] if LICENSING_SERVER_ALLOW_SELF_SIGNED_CERTS in config else False |
| 46 | + self.instance_alias_name = config[LICENSING_CLIENT_ALIAS] if LICENSING_CLIENT_ALIAS in config else '' |
| 47 | + |
| 48 | + def _get_dict(self): |
| 49 | + return { |
| 50 | + LICENSING_SERVER_PORT: self.server_port, |
| 51 | + LICENSING_SERVER_NAME: self.server_name, |
| 52 | + LICENSING_CHECK_PERIOD_MINS: self.check_period, |
| 53 | + LICENSING_SERVER_SSL_PORT: self.server_port_SSL, |
| 54 | + LICENSING_SERVER_ALLOW_INSECURE_COMMS: self.allow_insecure_comms, |
| 55 | + LICENSING_SERVER_ALLOW_SELF_SIGNED_CERTS: self.allow_self_signed_certs, |
| 56 | + LICENSING_CLIENT_ALIAS: self.instance_alias_name |
| 57 | + } |
| 58 | + |
| 59 | + def __str__(self) -> str: |
| 60 | + return "{}".format(self._get_dict()) |
| 61 | + |
| 62 | +def get_lls_config(server) -> lls_config: |
| 63 | + '''Returns the properties of the Local License server properties. Returned object is lls_config class object. |
| 64 | + |
| 65 | + INPUTS: |
| 66 | + "server" - instance of the "server" class |
| 67 | + |
| 68 | + RETURNS: |
| 69 | + lls_config - class object with lls configuration |
| 70 | +
|
| 71 | + EXCEPTIONS: |
| 72 | + KepHTTPError - If urllib provides an HTTPError |
| 73 | + KepURLError - If urllib provides an URLError |
| 74 | + ''' |
| 75 | + |
| 76 | + r = server._config_get(server.url + LLS_ROOT) |
| 77 | + return lls_config(r.payload) |
| 78 | + |
| 79 | +def update_lls_config(server, config: lls_config) -> bool: |
| 80 | + '''Updates the Local License Server admin properties for Kepware. |
| 81 | + |
| 82 | + INPUTS: |
| 83 | + "server" - instance of the "server" class |
| 84 | + "config" - lls_config class object |
| 85 | + |
| 86 | + RETURNS: |
| 87 | + True - If a "HTTP 200 - OK" is received from Kepware |
| 88 | +
|
| 89 | + EXCEPTIONS: |
| 90 | + KepHTTPError - If urllib provides an HTTPError |
| 91 | + KepURLError - If urllib provides an URLError |
| 92 | + ''' |
| 93 | + |
| 94 | + DATA = config._get_dict() |
| 95 | + r = server._config_update(server.url + LLS_ROOT, DATA) |
| 96 | + if r.code == 200: return True |
| 97 | + else: raise KepHTTPError(r.url, r.code, r.msg, r.hdrs, r.payload) |
| 98 | + |
| 99 | +def enable_lls(server) -> bool: |
| 100 | + '''Enables the Local License Server connection for Kepware. |
| 101 | + |
| 102 | + INPUTS: |
| 103 | + "server" - instance of the "server" class |
| 104 | + |
| 105 | + RETURNS: |
| 106 | + True - If a "HTTP 200 - OK" is received from Kepware |
| 107 | +
|
| 108 | + EXCEPTIONS: |
| 109 | + KepHTTPError - If urllib provides an HTTPError |
| 110 | + KepURLError - If urllib provides an URLError |
| 111 | + ''' |
| 112 | + |
| 113 | + r = server._config_update(server.url + LLS_ROOT, {LICENSING_SERVER_ENABLE: True}) |
| 114 | + if r.code == 200: return True |
| 115 | + else: raise KepHTTPError(r.url, r.code, r.msg, r.hdrs, r.payload) |
| 116 | + |
| 117 | +def disable_lls(server) -> bool: |
| 118 | + '''Disables the Local License Server connection for Kepware. |
| 119 | + |
| 120 | + INPUTS: |
| 121 | + "server" - instance of the "server" class |
| 122 | + |
| 123 | + RETURNS: |
| 124 | + True - If a "HTTP 200 - OK" is received from Kepware |
| 125 | +
|
| 126 | + EXCEPTIONS: |
| 127 | + KepHTTPError - If urllib provides an HTTPError |
| 128 | + KepURLError - If urllib provides an URLError |
| 129 | + ''' |
| 130 | + |
| 131 | + r = server._config_update(server.url + LLS_ROOT, {LICENSING_SERVER_ENABLE: False}) |
| 132 | + if r.code == 200: return True |
| 133 | + else: raise KepHTTPError(r.url, r.code, r.msg, r.hdrs, r.payload) |
0 commit comments