From afe62877d3a87b8a93e65463002b140052aa1e45 Mon Sep 17 00:00:00 2001 From: MercierCorentin Date: Mon, 29 Mar 2021 14:46:03 +0200 Subject: [PATCH] Domain settings: transform cli to be like app settings --- data/actionsmap/yunohost.yml | 67 ++++++----------------- src/yunohost/domain.py | 103 ++++++++++++++--------------------- 2 files changed, 56 insertions(+), 114 deletions(-) diff --git a/data/actionsmap/yunohost.yml b/data/actionsmap/yunohost.yml index c84e2c4b0..b363a218f 100644 --- a/data/actionsmap/yunohost.yml +++ b/data/actionsmap/yunohost.yml @@ -551,57 +551,22 @@ domain: pattern: *pattern_domain path: help: The path to check (e.g. /coffee) - - subcategories: - config: - subcategory_help: Domains DNS settings - actions: - # domain_config_list - list: - action_help: Get settings for all domains - api: GET /domains/list - - # domain_config_show - show: - action_help: Get settings for all domains - api: GET /domains//show - arguments: - domain: - help: Target domain - extra: - pattern: *pattern_domain - - # domain_config_get - get: - action_help: Get specific setting of a domain - api: GET /domains// - arguments: - domain: - help: Target domain - extra: - pattern: *pattern_domain - key: - help: Setting requested. One of ttl, xmpp, mail, owned_dns_zone - extra: - pattern: &pattern_domain_key - - !!str ^(ttl)|(xmpp)|(mail)|(owned_dns_zone)|$ - - "pattern_domain_key" - - # domain_config_set - set: - action_help: Set a setting of a domain - api: POST /domains// - arguments: - domain: - help: Target domain - extra: - pattern: *pattern_domain - key: - help: Setting requested. One of ttl (Time To Live of this domain's DNS records), xmpp (Configure XMPP in this domain's DNS records?), mail (Configure mail in this domain's DNS records?), owned_dns_zone (Is it a full domain, i.e not a subdomain?) - extra: - pattern: *pattern_domain_key - value: - help: Value of the setting. Must be a positive integer number for "ttl", or one of ("True", "False", "true", "false", "1", "0") for other settings + ### domain_setting() + setting: + action_help: Set or get an app setting value + api: GET /domains//settings + arguments: + domain: + help: Domaine name + key: + help: Key to get/set + -v: + full: --value + help: Value to set + -d: + full: --delete + help: Delete the key + action: store_true ### domain_info() diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index d88eb6b60..e799f52b2 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -722,38 +722,48 @@ def _load_domain_settings(): return new_domains - -def domain_config_list(): +def domain_setting(domain, key, value=None, delete=False): """ - Show settings of all domains + Set or get an app setting value + + Keyword argument: + value -- Value to set + app -- App ID + key -- Key to get/set + delete -- Delete the key - Keyword arguments: - domain -- The domain name """ - return _load_domain_settings() + domains = _load_domain_settings() + if not domain in domains.keys(): + # TODO add locales + raise YunohostError("domain_name_unknown", domain=domain) + + domain_settings = _get_domain_settings(domain, False) or {} -def domain_config_show(domain): - """ - Show settings of a domain + # GET + if value is None and not delete: + return domain_settings.get(key, None) - Keyword arguments: - domain -- The domain name - """ - return _get_domain_settings(domain, False) + # DELETE + if delete: + if key in domain_settings: + del domain_settings[key] + # SET + else: + + if "ttl" == key: + try: + ttl = int(value) + except: + # TODO add locales + raise YunohostError("bad_value_type", value_type=type(ttl)) -def domain_config_get(domain, key): - """ - Show a setting of a domain - - Keyword arguments: - domain -- The domain name - key -- ttl, xmpp, mail, owned_dns_zone - """ - settings = _get_domain_settings(domain, False) - return settings[domain][key] - + if ttl < 0: + # TODO add locales + raise YunohostError("must_be_positive", value_type=type(ttl)) + domain_settings[key] = value def _get_domain_settings(domain, subdomains): """ @@ -780,55 +790,22 @@ def _get_domain_settings(domain, subdomains): return only_wanted_domains -def domain_config_set(domain, key, value): - #(domain, ttl=None, xmpp=None, mail=None, owned_dns_zone=None): +def _set_domain_settings(domain, domain_settings): """ - Set some settings of a domain, for DNS generation. + Set settings of a domain Keyword arguments: domain -- The domain name - key must be one of this strings: - ttl -- the Time To Live for this domains DNS record - xmpp -- configure XMPP DNS records for this domain - mail -- configure mail DNS records for this domain - owned_dns_zone -- is this domain DNS zone owned? (is it a full domain or a subdomain?) - value must be set according to the key + settings -- Dict with doamin settings + """ domains = _load_domain_settings() - if not domain in domains.keys(): - # TODO add locales raise YunohostError("domain_name_unknown", domain=domain) - if "ttl" == key: - try: - ttl = int(value) - except: - # TODO add locales - raise YunohostError("bad_value_type", value_type=type(ttl)) - - if ttl < 0: - # TODO add locales - raise YunohostError("must_be_positive", value_type=type(ttl)) - - domains[domain]["ttl"] = ttl - - elif "xmpp" == key: - domains[domain]["xmpp"] = value in ["True", "true", "1"] - - elif "mail" == key: - domains[domain]["mail"] = value in ["True", "true", "1"] - - elif "owned_dns_zone" == key: - domains[domain]["owned_dns_zone"] = value in ["True", "true", "1"] - - else: - # TODO add locales - raise YunohostError("no_setting_given") + domains[domain] = domain_settings # Save the settings to the .yaml file with open(DOMAIN_SETTINGS_PATH, 'w') as file: - yaml.dump(domains, file) - - return domains[domain] + yaml.dump(domains, file, default_flow_style=False)