def server_token(context: Context, client_types: "string[]" = ["agent"]) -> "string": token = Config.get("compiler_rest_transport", "token", "") if token == "": return "" # Request a new token for this agent token = "" try: client = context.get_client() env = Config.get("config", "environment", None) if env is None: raise Exception( "The environment of this model should be configured in config>environment" ) def call(): return client.create_token(tid=env, client_types=list(client_types), idempotent=True) result = context.run_sync(call) if result.code == 200: token = result.result["token"] else: logging.getLogger(__name__).warning("Unable to get a new token") raise Exception("Unable to get a valid token") except ConnectionRefusedError: logging.getLogger(__name__).exception("Unable to get a new token") raise Exception("Unable to get a valid token") return token
def environment_server(ctx: Context) -> "string": """ Return the address of the management server """ client = ctx.get_client() server_url = client._transport_instance._get_client_config() match = re.search("^http[s]?://([^:]+):", server_url) if match is not None: return match.group(1) return Unknown(source=server_url)
def getfact(context: Context, resource: "any", fact_name: "string", default_value: "any"=None) -> "any": """ Retrieve a fact of the given resource """ resource_id = resources.to_id(resource) if resource_id is None: raise Exception("Facts can only be retreived from resources.") # Special case for unit testing and mocking if hasattr(context.compiler, "refs") and "facts" in context.compiler.refs: if resource_id in context.compiler.refs["facts"] and fact_name in context.compiler.refs["facts"][resource_id]: return context.compiler.refs["facts"][resource_id][fact_name] fact_value = Unknown(source=resource) unknown_parameters.append({"resource": resource_id, "parameter": fact_name, "source": "fact"}) if default_value is not None: return default_value return fact_value # End special case fact_value = None try: client = context.get_client() env = Config.get("config", "environment", None) if env is None: raise Exception("The environment of this model should be configured in config>environment") def call(): return client.get_param(tid=env, id=fact_name, resource_id=resource_id) result = context.run_sync(call) if result.code == 200: fact_value = result.result["parameter"]["value"] else: logging.getLogger(__name__).info("Param %s of resource %s is unknown", fact_name, resource_id) fact_value = Unknown(source=resource) unknown_parameters.append({"resource": resource_id, "parameter": fact_name, "source": "fact"}) except ConnectionRefusedError: logging.getLogger(__name__).warning("Param %s of resource %s is unknown because connection to server was refused", fact_name, resource_id) fact_value = Unknown(source=resource) unknown_parameters.append({"resource": resource_id, "parameter": fact_name, "source": "fact"}) if isinstance(fact_value, Unknown) and default_value is not None: return default_value return fact_value