// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System; using System.Collections.Generic; using System.Text.Json; using Regorus.Internal; #nullable enable namespace Regorus { /// /// Provides static methods for managing the global target registry. /// Targets define resource types and their associated schemas for Azure Policy evaluation. /// public static unsafe class TargetRegistry { /// /// Register a target from JSON definition. /// The target JSON should follow the target schema format. /// Once registered, the target can be referenced in Rego policies using `__target__` rules. /// /// JSON encoded target definition /// Thrown when target registration fails public static void RegisterFromJson(string targetJson) { Utf8Marshaller.WithUtf8(targetJson, targetPtr => { unsafe { ResultHelpers.GetStringResult(Internal.API.regorus_register_target_from_json((byte*)targetPtr)); } }); } /// /// Check if a target is registered. /// /// Name of the target to check /// True if the target is registered, false otherwise /// Thrown when the operation fails public static bool Contains(string name) { return Utf8Marshaller.WithUtf8(name, namePtr => { unsafe { var result = Internal.API.regorus_target_registry_contains((byte*)namePtr); return ResultHelpers.GetBoolResult(result); } }); } /// /// Get a list of all registered target names. /// /// JSON array of target names /// Thrown when the operation fails public static string ListNames() { return ResultHelpers.GetStringResult(Internal.API.regorus_target_registry_list_names()) ?? "[]"; } /// /// Get a list of all registered target names as managed strings. /// public static IReadOnlyList GetNames() { var json = ListNames(); return JsonSerializer.Deserialize(json) ?? Array.Empty(); } /// /// Remove a target from the registry by name. /// /// The target name to remove /// True if the target was removed, false if it wasn't found /// Thrown when the operation fails public static bool Remove(string name) { return Utf8Marshaller.WithUtf8(name, namePtr => { unsafe { var result = Internal.API.regorus_target_registry_remove((byte*)namePtr); return ResultHelpers.GetBoolResult(result); } }); } /// /// Clear all targets from the registry. /// /// Thrown when the operation fails public static void Clear() { ResultHelpers.GetStringResult(Internal.API.regorus_target_registry_clear()); } /// /// Get the number of registered targets. /// /// The number of registered targets /// Thrown when the operation fails public static long Count { get { var result = Internal.API.regorus_target_registry_len(); return ResultHelpers.GetIntResult(result); } } /// /// Check if the target registry is empty. /// /// True if the registry is empty, false otherwise /// Thrown when the operation fails public static bool IsEmpty { get { var result = Internal.API.regorus_target_registry_is_empty(); return ResultHelpers.GetBoolResult(result); } } } }