nmcli: convert everywhere dhcp-send-hostname <-> dhcp-send-hostname-v2

Since introducing the setting ipv[46].dhcp-send-hostname-v2 internally
in NM for supporting global default behavior of DHCP send hostname,
confusion arises for setting the dhcp-send-hostname-v2 and old
dhcp-send-hostname in nmcli. To avoid any confusion from user
configuring dhcp-send-hostname-v2 and old dhcp-send-hostname using
nmcli, introduce the mapping from nmcli argument dhcp-send-hostname to
internal dhcp-send-hostname-v2 property and the mapping from nmcli
argument dhcp-send-hostname-deprecated to internal old dhcp-send-hostname
property.

The change in split_required_fields_for_con_show makes that properties
specified with -g or -f are converted to the libnm's "internal" names.

The change in _print_fill makes that the names are converted to the
"external" user facing names.
This commit is contained in:
Wen Liang
2024-09-25 17:19:15 -04:00
committed by Wen Liang
parent 51ea910cc2
commit add4dad505
4 changed files with 84 additions and 12 deletions

View File

@@ -1883,8 +1883,23 @@ split_required_fields_for_con_show(const char *input,
for (i = 0; i < _NM_META_SETTING_TYPE_NUM; i++) { for (i = 0; i < _NM_META_SETTING_TYPE_NUM; i++) {
if (is_all || is_common if (is_all || is_common
|| !g_ascii_strcasecmp(s_mutable, nm_meta_setting_infos[i].setting_name)) { || !g_ascii_strcasecmp(s_mutable, nm_meta_setting_infos[i].setting_name)) {
if (dot) gs_free char *to_free = NULL;
if (dot) {
/* If there was a dot we have 'setting.property'. Some properties has different
* name for the user than internally in libnm and D-Bus. Make the conversion
* from user names to libnm names.
*/
const char *prop_user = dot + 1;
const char *prop_libnm =
nmc_setting_propname_user_to_libnm(s_mutable, prop_user);
if (prop_user != prop_libnm) {
to_free = g_strdup_printf("%s.%s", s_mutable, prop_libnm);
s_mutable = to_free;
}
*dot = '.'; *dot = '.';
}
g_string_append(str1, s_mutable); g_string_append(str1, s_mutable);
g_string_append_c(str1, ','); g_string_append_c(str1, ',');
found = TRUE; found = TRUE;
@@ -4392,7 +4407,7 @@ set_property(NMClient *client,
} }
/* Don't ask for this property in interactive mode. */ /* Don't ask for this property in interactive mode. */
disable_options(setting_name, property_name); disable_options(setting_name, nmc_setting_propname_user_to_libnm(setting_name, property_name));
return TRUE; return TRUE;
} }

View File

@@ -525,6 +525,8 @@ get_property_val(NMSetting *setting,
NM_IN_SET(get_type, NM_META_ACCESSOR_GET_TYPE_PARSABLE, NM_META_ACCESSOR_GET_TYPE_PRETTY), NM_IN_SET(get_type, NM_META_ACCESSOR_GET_TYPE_PARSABLE, NM_META_ACCESSOR_GET_TYPE_PRETTY),
NULL); NULL);
prop = nmc_setting_propname_user_to_libnm(nm_setting_get_name(setting), prop);
if ((property_info = nm_meta_property_info_find_by_setting(setting, prop))) { if ((property_info = nm_meta_property_info_find_by_setting(setting, prop))) {
if (property_info->property_type->get_fcn) { if (property_info->property_type->get_fcn) {
NMMetaAccessorGetOutFlags out_flags = NM_META_ACCESSOR_GET_OUT_FLAGS_NONE; NMMetaAccessorGetOutFlags out_flags = NM_META_ACCESSOR_GET_OUT_FLAGS_NONE;
@@ -593,8 +595,11 @@ nmc_setting_set_property(NMClient *client,
NM_META_ACCESSOR_MODIFIER_ADD), NM_META_ACCESSOR_MODIFIER_ADD),
FALSE); FALSE);
prop = nmc_setting_propname_user_to_libnm(nm_setting_get_name(setting), prop);
if (!(property_info = nm_meta_property_info_find_by_setting(setting, prop))) if (!(property_info = nm_meta_property_info_find_by_setting(setting, prop)))
goto out_fail_read_only; goto out_fail_read_only;
if (!property_info->property_type->set_fcn) if (!property_info->property_type->set_fcn)
goto out_fail_read_only; goto out_fail_read_only;
@@ -661,8 +666,12 @@ nmc_setting_get_valid_properties(NMSetting *setting)
num = setting_info ? setting_info->properties_num : 0; num = setting_info ? setting_info->properties_num : 0;
valid_props = g_new(char *, num + 1); valid_props = g_new(char *, num + 1);
for (i = 0; i < num; i++) for (i = 0; i < num; i++) {
valid_props[i] = g_strdup(setting_info->properties[i]->property_name); const char *prop =
nmc_setting_propname_libnm_to_user(setting_info->general->setting_name,
setting_info->properties[i]->property_name);
valid_props[i] = g_strdup(prop);
}
valid_props[num] = NULL; valid_props[num] = NULL;
return valid_props; return valid_props;
@@ -678,6 +687,8 @@ nmc_setting_get_property_allowed_values(NMSetting *setting, const char *prop, ch
*out_to_free = NULL; *out_to_free = NULL;
prop = nmc_setting_propname_user_to_libnm(nm_setting_get_name(setting), prop);
if ((property_info = nm_meta_property_info_find_by_setting(setting, prop))) { if ((property_info = nm_meta_property_info_find_by_setting(setting, prop))) {
if (property_info->property_type->values_fcn) { if (property_info->property_type->values_fcn) {
return property_info->property_type->values_fcn(property_info, out_to_free); return property_info->property_type->values_fcn(property_info, out_to_free);
@@ -711,6 +722,8 @@ nmc_setting_get_property_desc(NMSetting *setting, const char *prop)
g_return_val_if_fail(NM_IS_SETTING(setting), FALSE); g_return_val_if_fail(NM_IS_SETTING(setting), FALSE);
prop = nmc_setting_propname_user_to_libnm(nm_setting_get_name(setting), prop);
property_info = nm_meta_property_info_find_by_setting(setting, prop); property_info = nm_meta_property_info_find_by_setting(setting, prop);
if (!property_info) if (!property_info)
return NULL; return NULL;
@@ -774,3 +787,33 @@ setting_details(const NmcConfig *nmc_config, NMSetting *setting, const char *one
return TRUE; return TRUE;
} }
const char *
nmc_setting_propname_user_to_libnm(const char *setting_name, const char *prop)
{
if (NM_IN_STRSET(setting_name,
NM_SETTING_IP4_CONFIG_SETTING_NAME,
NM_SETTING_IP6_CONFIG_SETTING_NAME)) {
if (nm_streq0(prop, "dhcp-send-hostname"))
return NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME_V2;
else if (nm_streq0(prop, "dhcp-send-hostname-deprecated"))
return NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME;
}
return prop;
}
const char *
nmc_setting_propname_libnm_to_user(const char *setting_name, const char *prop)
{
if (NM_IN_STRSET(setting_name,
NM_SETTING_IP4_CONFIG_SETTING_NAME,
NM_SETTING_IP6_CONFIG_SETTING_NAME)) {
if (nm_streq0(prop, NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME_V2))
return "dhcp-send-hostname";
else if (nm_streq0(prop, NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME))
return "dhcp-send-hostname-deprecated";
}
return prop;
}

View File

@@ -34,4 +34,7 @@ gboolean nmc_setting_set_property(NMClient *client,
gboolean setting_details(const NmcConfig *nmc_config, NMSetting *setting, const char *one_prop); gboolean setting_details(const NmcConfig *nmc_config, NMSetting *setting, const char *one_prop);
const char *nmc_setting_propname_user_to_libnm(const char *setting_name, const char *prop);
const char *nmc_setting_propname_libnm_to_user(const char *setting_name, const char *prop);
#endif /* NMC_SETTINGS_H */ #endif /* NMC_SETTINGS_H */

View File

@@ -1016,6 +1016,8 @@ _print_fill(const NmcConfig *nmc_config,
PrintDataHeaderCell *header_cell; PrintDataHeaderCell *header_cell;
guint col_idx; guint col_idx;
const NMMetaAbstractInfo *info; const NMMetaAbstractInfo *info;
const char *setting_name;
gboolean is_prop;
col = &cols[i_col]; col = &cols[i_col];
if (!col->is_leaf) if (!col->is_leaf)
@@ -1036,14 +1038,23 @@ _print_fill(const NmcConfig *nmc_config,
header_cell->to_print = FALSE; header_cell->to_print = FALSE;
header_cell->title = nm_meta_abstract_info_get_name(info, TRUE); header_cell->title = nm_meta_abstract_info_get_name(info, TRUE);
if (nmc_config->multiline_output && col->parent_col
&& NM_IN_SET(info->meta_type, is_prop =
&nm_meta_type_property_info, col->parent_col
&nmc_meta_type_generic_info)) { && NM_IN_SET(info->meta_type, &nm_meta_type_property_info, &nmc_meta_type_generic_info);
header_cell->title = g_strdup_printf(
"%s.%s", if (is_prop) {
nm_meta_abstract_info_get_name(col->parent_col->selection_item->info, FALSE), /* Some properties has different name for the user than internally in
header_cell->title); * libnm and D-Bus. Make the conversion from libnm names to user names.
*/
setting_name =
nm_meta_abstract_info_get_name(col->parent_col->selection_item->info, FALSE);
header_cell->title =
nmc_setting_propname_libnm_to_user(setting_name, header_cell->title);
}
if (nmc_config->multiline_output && is_prop) {
header_cell->title = g_strdup_printf("%s.%s", setting_name, header_cell->title);
header_cell->title_to_free = TRUE; header_cell->title_to_free = TRUE;
} }
} }