cloud-setup: return structure for get_config() result instead of generic hash table
Returning a struct seems easier to understand, because then the result is typed. Also, we might return additional results, which are system wide and not per-interface.
This commit is contained in:
@@ -200,30 +200,30 @@ _nmc_get_device_by_hwaddr(NMClient *nmc, const char *hwaddr)
|
|||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
GMainLoop * main_loop;
|
GMainLoop * main_loop;
|
||||||
GHashTable *config_dict;
|
NMCSProviderGetConfigResult *result;
|
||||||
} GetConfigData;
|
} GetConfigData;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
_get_config_cb(GObject *source, GAsyncResult *result, gpointer user_data)
|
_get_config_cb(GObject *source, GAsyncResult *res, gpointer user_data)
|
||||||
{
|
{
|
||||||
GetConfigData * data = user_data;
|
GetConfigData * data = user_data;
|
||||||
gs_unref_hashtable GHashTable *config_dict = NULL;
|
nm_auto_free_nmcs_provider_get_config_result NMCSProviderGetConfigResult *result = NULL;
|
||||||
gs_free_error GError *error = NULL;
|
gs_free_error GError *error = NULL;
|
||||||
|
|
||||||
config_dict = nmcs_provider_get_config_finish(NMCS_PROVIDER(source), result, &error);
|
result = nmcs_provider_get_config_finish(NMCS_PROVIDER(source), res, &error);
|
||||||
|
|
||||||
if (!config_dict) {
|
if (!result) {
|
||||||
if (!nm_utils_error_is_cancelled(error))
|
if (!nm_utils_error_is_cancelled(error))
|
||||||
_LOGI("failure to get meta data: %s", error->message);
|
_LOGI("failure to get meta data: %s", error->message);
|
||||||
} else
|
} else
|
||||||
_LOGD("meta data received");
|
_LOGD("meta data received");
|
||||||
|
|
||||||
data->config_dict = g_steal_pointer(&config_dict);
|
data->result = g_steal_pointer(&result);
|
||||||
g_main_loop_quit(data->main_loop);
|
g_main_loop_quit(data->main_loop);
|
||||||
}
|
}
|
||||||
|
|
||||||
static GHashTable *
|
static NMCSProviderGetConfigResult *
|
||||||
_get_config(GCancellable *sigterm_cancellable, NMCSProvider *provider, NMClient *nmc)
|
_get_config(GCancellable *sigterm_cancellable, NMCSProvider *provider, NMClient *nmc)
|
||||||
{
|
{
|
||||||
nm_auto_unref_gmainloop GMainLoop *main_loop = g_main_loop_new(NULL, FALSE);
|
nm_auto_unref_gmainloop GMainLoop *main_loop = g_main_loop_new(NULL, FALSE);
|
||||||
@@ -243,7 +243,7 @@ _get_config(GCancellable *sigterm_cancellable, NMCSProvider *provider, NMClient
|
|||||||
|
|
||||||
g_main_loop_run(main_loop);
|
g_main_loop_run(main_loop);
|
||||||
|
|
||||||
return data.config_dict;
|
return data.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
@@ -396,13 +396,13 @@ _nmc_mangle_connection(NMDevice * device,
|
|||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
|
|
||||||
static guint
|
static guint
|
||||||
_config_data_get_num_valid(GHashTable *config_dict)
|
_config_data_get_num_valid(const NMCSProviderGetConfigResult *result)
|
||||||
{
|
{
|
||||||
const NMCSProviderGetConfigIfaceData *config_data;
|
const NMCSProviderGetConfigIfaceData *config_data;
|
||||||
GHashTableIter h_iter;
|
GHashTableIter h_iter;
|
||||||
guint n = 0;
|
guint n = 0;
|
||||||
|
|
||||||
g_hash_table_iter_init(&h_iter, config_dict);
|
g_hash_table_iter_init(&h_iter, result->iface_datas);
|
||||||
while (g_hash_table_iter_next(&h_iter, NULL, (gpointer *) &config_data)) {
|
while (g_hash_table_iter_next(&h_iter, NULL, (gpointer *) &config_data)) {
|
||||||
if (nmcs_provider_get_config_iface_data_is_valid(config_data))
|
if (nmcs_provider_get_config_iface_data_is_valid(config_data))
|
||||||
n++;
|
n++;
|
||||||
@@ -525,7 +525,9 @@ try_again:
|
|||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
_config_all(GCancellable *sigterm_cancellable, NMClient *nmc, GHashTable *config_dict)
|
_config_all(GCancellable * sigterm_cancellable,
|
||||||
|
NMClient * nmc,
|
||||||
|
const NMCSProviderGetConfigResult *result)
|
||||||
{
|
{
|
||||||
GHashTableIter h_iter;
|
GHashTableIter h_iter;
|
||||||
const NMCSProviderGetConfigIfaceData *c_config_data;
|
const NMCSProviderGetConfigIfaceData *c_config_data;
|
||||||
@@ -533,9 +535,9 @@ _config_all(GCancellable *sigterm_cancellable, NMClient *nmc, GHashTable *config
|
|||||||
gboolean is_single_nic;
|
gboolean is_single_nic;
|
||||||
gboolean any_changes = FALSE;
|
gboolean any_changes = FALSE;
|
||||||
|
|
||||||
is_single_nic = (_config_data_get_num_valid(config_dict) <= 1);
|
is_single_nic = (_config_data_get_num_valid(result) <= 1);
|
||||||
|
|
||||||
g_hash_table_iter_init(&h_iter, config_dict);
|
g_hash_table_iter_init(&h_iter, result->iface_datas);
|
||||||
while (g_hash_table_iter_next(&h_iter, (gpointer *) &c_hwaddr, (gpointer *) &c_config_data)) {
|
while (g_hash_table_iter_next(&h_iter, (gpointer *) &c_hwaddr, (gpointer *) &c_config_data)) {
|
||||||
if (_config_one(sigterm_cancellable, nmc, is_single_nic, c_hwaddr, c_config_data))
|
if (_config_one(sigterm_cancellable, nmc, is_single_nic, c_hwaddr, c_config_data))
|
||||||
any_changes = TRUE;
|
any_changes = TRUE;
|
||||||
@@ -564,12 +566,12 @@ sigterm_handler(gpointer user_data)
|
|||||||
int
|
int
|
||||||
main(int argc, const char *const *argv)
|
main(int argc, const char *const *argv)
|
||||||
{
|
{
|
||||||
gs_unref_object GCancellable * sigterm_cancellable = NULL;
|
gs_unref_object GCancellable * sigterm_cancellable = NULL;
|
||||||
nm_auto_destroy_and_unref_gsource GSource *sigterm_source = NULL;
|
nm_auto_destroy_and_unref_gsource GSource *sigterm_source = NULL;
|
||||||
gs_unref_object NMCSProvider *provider = NULL;
|
gs_unref_object NMCSProvider *provider = NULL;
|
||||||
gs_unref_object NMClient *nmc = NULL;
|
gs_unref_object NMClient * nmc = NULL;
|
||||||
gs_unref_hashtable GHashTable *config_dict = NULL;
|
nm_auto_free_nmcs_provider_get_config_result NMCSProviderGetConfigResult *result = NULL;
|
||||||
gs_free_error GError *error = NULL;
|
gs_free_error GError *error = NULL;
|
||||||
|
|
||||||
_nm_logging_enabled_init(g_getenv(NMCS_ENV_VARIABLE("NM_CLOUD_SETUP_LOG")));
|
_nm_logging_enabled_init(g_getenv(NMCS_ENV_VARIABLE("NM_CLOUD_SETUP_LOG")));
|
||||||
|
|
||||||
@@ -614,17 +616,17 @@ main(int argc, const char *const *argv)
|
|||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
config_dict = _get_config(sigterm_cancellable, provider, nmc);
|
result = _get_config(sigterm_cancellable, provider, nmc);
|
||||||
if (!config_dict)
|
if (!result)
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
if (_config_all(sigterm_cancellable, nmc, config_dict))
|
if (_config_all(sigterm_cancellable, nmc, result))
|
||||||
_LOGI("some changes were applied for provider %s", nmcs_provider_get_name(provider));
|
_LOGI("some changes were applied for provider %s", nmcs_provider_get_name(provider));
|
||||||
else
|
else
|
||||||
_LOGD("no changes were applied for provider %s", nmcs_provider_get_name(provider));
|
_LOGD("no changes were applied for provider %s", nmcs_provider_get_name(provider));
|
||||||
|
|
||||||
done:
|
done:
|
||||||
nm_clear_pointer(&config_dict, g_hash_table_unref);
|
nm_clear_pointer(&result, nmcs_provider_get_config_result_free);
|
||||||
g_clear_object(&nmc);
|
g_clear_object(&nmc);
|
||||||
g_clear_object(&provider);
|
g_clear_object(&provider);
|
||||||
|
|
||||||
|
@@ -49,6 +49,28 @@ nmcs_provider_get_main_context(NMCSProvider *self)
|
|||||||
|
|
||||||
return nm_http_client_get_main_context(NMCS_PROVIDER_GET_PRIVATE(self)->http_client);
|
return nm_http_client_get_main_context(NMCS_PROVIDER_GET_PRIVATE(self)->http_client);
|
||||||
}
|
}
|
||||||
|
/*****************************************************************************/
|
||||||
|
|
||||||
|
static NMCSProviderGetConfigResult *
|
||||||
|
nmcs_provider_get_config_result_new(GHashTable *iface_datas)
|
||||||
|
{
|
||||||
|
NMCSProviderGetConfigResult *result;
|
||||||
|
|
||||||
|
result = g_new(NMCSProviderGetConfigResult, 1);
|
||||||
|
*result = (NMCSProviderGetConfigResult){
|
||||||
|
.iface_datas = g_hash_table_ref(iface_datas),
|
||||||
|
};
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
nmcs_provider_get_config_result_free(NMCSProviderGetConfigResult *result)
|
||||||
|
{
|
||||||
|
if (result) {
|
||||||
|
nm_g_hash_table_unref(result->iface_datas);
|
||||||
|
g_free(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
|
|
||||||
@@ -137,8 +159,8 @@ _get_config_task_maybe_return(NMCSProviderGetConfigTaskData *get_config_data, GE
|
|||||||
} else {
|
} else {
|
||||||
_LOGD("get-config: success");
|
_LOGD("get-config: success");
|
||||||
g_task_return_pointer(get_config_data->task,
|
g_task_return_pointer(get_config_data->task,
|
||||||
g_hash_table_ref(get_config_data->result_dict),
|
nmcs_provider_get_config_result_new(get_config_data->result_dict),
|
||||||
(GDestroyNotify) g_hash_table_unref);
|
(GDestroyNotify) nmcs_provider_get_config_result_free);
|
||||||
}
|
}
|
||||||
|
|
||||||
nm_clear_g_signal_handler(g_task_get_cancellable(get_config_data->task),
|
nm_clear_g_signal_handler(g_task_get_cancellable(get_config_data->task),
|
||||||
@@ -217,7 +239,7 @@ nmcs_provider_get_config(NMCSProvider * self,
|
|||||||
NMCS_PROVIDER_GET_CLASS(self)->get_config(self, get_config_data);
|
NMCS_PROVIDER_GET_CLASS(self)->get_config(self, get_config_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
GHashTable *
|
NMCSProviderGetConfigResult *
|
||||||
nmcs_provider_get_config_finish(NMCSProvider *self, GAsyncResult *result, GError **error)
|
nmcs_provider_get_config_finish(NMCSProvider *self, GAsyncResult *result, GError **error)
|
||||||
{
|
{
|
||||||
g_return_val_if_fail(NMCS_IS_PROVIDER(self), FALSE);
|
g_return_val_if_fail(NMCS_IS_PROVIDER(self), FALSE);
|
||||||
|
@@ -43,6 +43,24 @@ nmcs_provider_get_config_iface_data_is_valid(const NMCSProviderGetConfigIfaceDat
|
|||||||
|
|
||||||
NMCSProviderGetConfigIfaceData *nmcs_provider_get_config_iface_data_new(gboolean was_requested);
|
NMCSProviderGetConfigIfaceData *nmcs_provider_get_config_iface_data_new(gboolean was_requested);
|
||||||
|
|
||||||
|
/*****************************************************************************/
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
/* A dictionary of (const char *) -> (NMCSProviderGetConfigIfaceData *).
|
||||||
|
* This is the per-interface result of get_config(). */
|
||||||
|
GHashTable *iface_datas;
|
||||||
|
} NMCSProviderGetConfigResult;
|
||||||
|
|
||||||
|
void nmcs_provider_get_config_result_free(NMCSProviderGetConfigResult *result);
|
||||||
|
|
||||||
|
NM_AUTO_DEFINE_FCN0(NMCSProviderGetConfigResult *,
|
||||||
|
_nm_auto_free_nmcs_provider_get_config_result,
|
||||||
|
nmcs_provider_get_config_result_free);
|
||||||
|
#define nm_auto_free_nmcs_provider_get_config_result \
|
||||||
|
nm_auto(_nm_auto_free_nmcs_provider_get_config_result)
|
||||||
|
|
||||||
|
/*****************************************************************************/
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
GTask *task;
|
GTask *task;
|
||||||
|
|
||||||
@@ -124,7 +142,7 @@ void nmcs_provider_get_config(NMCSProvider * provider,
|
|||||||
GAsyncReadyCallback callback,
|
GAsyncReadyCallback callback,
|
||||||
gpointer user_data);
|
gpointer user_data);
|
||||||
|
|
||||||
GHashTable *
|
NMCSProviderGetConfigResult *
|
||||||
nmcs_provider_get_config_finish(NMCSProvider *provider, GAsyncResult *result, GError **error);
|
nmcs_provider_get_config_finish(NMCSProvider *provider, GAsyncResult *result, GError **error);
|
||||||
|
|
||||||
#endif /* __NMCS_PROVIDER_H__ */
|
#endif /* __NMCS_PROVIDER_H__ */
|
||||||
|
Reference in New Issue
Block a user