Merge pull request 'Feat(clan.service): enable opt-in json-schema restriction for clan.service interface modules' (#3415) from hsjobeki/clan-core:new-json-schemas into main
Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3415
This commit is contained in:
35
lib/inventory/distributed-service/api-feature.nix
Normal file
35
lib/inventory/distributed-service/api-feature.nix
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
# This module enables itself if
|
||||||
|
# manifest.features.API = true
|
||||||
|
# It converts the roles.interface to a json-schema
|
||||||
|
{ clanLib }:
|
||||||
|
let
|
||||||
|
converter = clanLib.jsonschema {
|
||||||
|
includeDefaults = true;
|
||||||
|
};
|
||||||
|
in
|
||||||
|
{ lib, config, ... }:
|
||||||
|
{
|
||||||
|
options.result.api = lib.mkOption {
|
||||||
|
default = { };
|
||||||
|
type = lib.types.submodule (
|
||||||
|
lib.optionalAttrs config.manifest.features.API {
|
||||||
|
options.schema = lib.mkOption {
|
||||||
|
description = ''
|
||||||
|
The API schema for configuring the service.
|
||||||
|
|
||||||
|
Each 'role.<name>.interface' is converted to a json-schema.
|
||||||
|
This can be used to generate and type check the API relevant objects.
|
||||||
|
'';
|
||||||
|
defaultText = lib.literalExpression ''
|
||||||
|
{
|
||||||
|
peer = { $schema" = "http://json-schema.org/draft-07/schema#"; ... }
|
||||||
|
commuter = { $schema" = "http://json-schema.org/draft-07/schema#"; ... }
|
||||||
|
distributor = { $schema" = "http://json-schema.org/draft-07/schema#"; ... }
|
||||||
|
}
|
||||||
|
'';
|
||||||
|
default = lib.mapAttrs (_roleName: v: converter.parseModule v.interface) config.roles;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -126,6 +126,9 @@ let
|
|||||||
./service-module.nix
|
./service-module.nix
|
||||||
# Import the resolved module.
|
# Import the resolved module.
|
||||||
(builtins.head instances).instance.resolvedModule
|
(builtins.head instances).instance.resolvedModule
|
||||||
|
|
||||||
|
# feature modules
|
||||||
|
(lib.modules.importApply ./api-feature.nix { inherit clanLib; })
|
||||||
]
|
]
|
||||||
# Include all the instances that correlate to the resolved module
|
# Include all the instances that correlate to the resolved module
|
||||||
++ (builtins.map (v: {
|
++ (builtins.map (v: {
|
||||||
|
|||||||
@@ -243,6 +243,28 @@ in
|
|||||||
'';
|
'';
|
||||||
type = types.str;
|
type = types.str;
|
||||||
};
|
};
|
||||||
|
features = mkOption {
|
||||||
|
description = ''
|
||||||
|
Enable built-in features for the module
|
||||||
|
|
||||||
|
See the documentation for each feature:
|
||||||
|
- API
|
||||||
|
'';
|
||||||
|
type = types.submoduleWith {
|
||||||
|
modules = [
|
||||||
|
{
|
||||||
|
options.API = mkOption {
|
||||||
|
type = types.bool;
|
||||||
|
default = false;
|
||||||
|
description = ''
|
||||||
|
Enables automatic API schema conversion for the interface of this module.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
default = { };
|
||||||
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -12,7 +12,6 @@
|
|||||||
./nix-settings.nix
|
./nix-settings.nix
|
||||||
./options.nix
|
./options.nix
|
||||||
./outputs.nix
|
./outputs.nix
|
||||||
./schema.nix
|
|
||||||
./sops.nix
|
./sops.nix
|
||||||
./vars
|
./vars
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,18 +1,5 @@
|
|||||||
{ lib, ... }:
|
{ lib, ... }:
|
||||||
let
|
let
|
||||||
# {
|
|
||||||
# roles = {
|
|
||||||
# client = {
|
|
||||||
# machines = [
|
|
||||||
# "camina_machine"
|
|
||||||
# "vi_machine"
|
|
||||||
# ];
|
|
||||||
# };
|
|
||||||
# server = {
|
|
||||||
# machines = [ "vyr_machine" ];
|
|
||||||
# };
|
|
||||||
# };
|
|
||||||
# }
|
|
||||||
instanceOptions = lib.types.submodule {
|
instanceOptions = lib.types.submodule {
|
||||||
options.roles = lib.mkOption {
|
options.roles = lib.mkOption {
|
||||||
description = ''
|
description = ''
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
{ options, lib, ... }:
|
|
||||||
let
|
|
||||||
jsonschema = import ../../lib/jsonschema { inherit lib; } { };
|
|
||||||
in
|
|
||||||
{
|
|
||||||
options.clanSchema = lib.mkOption {
|
|
||||||
type = lib.types.attrs;
|
|
||||||
description = "The json schema for the .clan options namespace";
|
|
||||||
default = jsonschema.parseOptions options.clan { };
|
|
||||||
defaultText = lib.literalExpression "jsonschema.schemaToJSON options.clan";
|
|
||||||
};
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user