mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-01-06 19:00:16 +01:00
cloud-setup: move implementation of getters for base URL to macro
It's the same code, four times. And once it was messed up. Move the definition of the function to a macro.
This commit is contained in:
parent
2cc54b2bb1
commit
4ef944d504
5 changed files with 29 additions and 84 deletions
|
|
@ -91,6 +91,31 @@ const char *nmcs_utils_parse_get_full_line(GBytes *mem, const char *needle);
|
|||
|
||||
/*****************************************************************************/
|
||||
|
||||
#define NMCS_DEFINE_HOST_BASE(base_fcn, nmcs_env_host, default_host) \
|
||||
static const char *base_fcn(void) \
|
||||
{ \
|
||||
static const char *base_cached = NULL; \
|
||||
const char *base; \
|
||||
\
|
||||
again: \
|
||||
base = g_atomic_pointer_get(&base_cached); \
|
||||
if (G_UNLIKELY(!base)) { \
|
||||
/* The base URI can be set via environment variable. \
|
||||
* This is mainly for testing, it's not usually supposed to be configured. \
|
||||
* Consider this private API! */ \
|
||||
base = g_getenv("" nmcs_env_host ""); \
|
||||
base = nmcs_utils_uri_complete_interned(base) ?: ("" default_host ""); \
|
||||
\
|
||||
if (!g_atomic_pointer_compare_and_exchange(&base_cached, NULL, base)) \
|
||||
goto again; \
|
||||
} \
|
||||
\
|
||||
return base; \
|
||||
} \
|
||||
_NM_DUMMY_STRUCT_FOR_TRAILING_SEMICOLON
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
char *nmcs_utils_uri_build_concat_v(const char *base, const char **components, gsize n_components);
|
||||
|
||||
#define nmcs_utils_uri_build_concat(base, ...) \
|
||||
|
|
|
|||
|
|
@ -18,27 +18,7 @@
|
|||
#define NM_ALIYUN_METADATA_URL_BASE /* $NM_ALIYUN_BASE/$NM_ALIYUN_API_VERSION */ \
|
||||
"/meta-data/network/interfaces/macs/"
|
||||
|
||||
static const char *
|
||||
_aliyun_base(void)
|
||||
{
|
||||
static const char *base_cached = NULL;
|
||||
const char *base;
|
||||
|
||||
again:
|
||||
base = g_atomic_pointer_get(&base_cached);
|
||||
if (G_UNLIKELY(!base)) {
|
||||
/* The base URI can be set via environment variable.
|
||||
* This is mainly for testing, it's not usually supposed to be configured.
|
||||
* Consider this private API! */
|
||||
base = g_getenv(NMCS_ENV_NM_CLOUD_SETUP_ALIYUN_HOST);
|
||||
base = nmcs_utils_uri_complete_interned(base) ?: ("" NM_ALIYUN_HOST);
|
||||
|
||||
if (!g_atomic_pointer_compare_and_exchange(&base_cached, NULL, base))
|
||||
goto again;
|
||||
}
|
||||
|
||||
return base;
|
||||
}
|
||||
NMCS_DEFINE_HOST_BASE(_aliyun_base, NMCS_ENV_NM_CLOUD_SETUP_ALIYUN_HOST, NM_ALIYUN_HOST);
|
||||
|
||||
#define _aliyun_uri_concat(...) nmcs_utils_uri_build_concat(_aliyun_base(), __VA_ARGS__)
|
||||
#define _aliyun_uri_interfaces(...) \
|
||||
|
|
|
|||
|
|
@ -17,27 +17,7 @@
|
|||
#define NM_AZURE_METADATA_URL_BASE /* $NM_AZURE_BASE/$NM_AZURE_API_VERSION */ \
|
||||
"/metadata/instance/network/interface/"
|
||||
|
||||
static const char *
|
||||
_azure_base(void)
|
||||
{
|
||||
static const char *base_cached = NULL;
|
||||
const char *base;
|
||||
|
||||
again:
|
||||
base = g_atomic_pointer_get(&base_cached);
|
||||
if (G_UNLIKELY(!base)) {
|
||||
/* The base URI can be set via environment variable.
|
||||
* This is mainly for testing, it's not usually supposed to be configured.
|
||||
* Consider this private API! */
|
||||
base = g_getenv(NMCS_ENV_NM_CLOUD_SETUP_AZURE_HOST);
|
||||
base = nmcs_utils_uri_complete_interned(base) ?: ("" NM_AZURE_BASE);
|
||||
|
||||
if (!g_atomic_pointer_compare_and_exchange(&base_cached, NULL, base))
|
||||
goto again;
|
||||
}
|
||||
|
||||
return base;
|
||||
}
|
||||
NMCS_DEFINE_HOST_BASE(_azure_base, NMCS_ENV_NM_CLOUD_SETUP_AZURE_HOST, NM_AZURE_BASE);
|
||||
|
||||
#define _azure_uri_concat(...) \
|
||||
nmcs_utils_uri_build_concat(_azure_base(), __VA_ARGS__, NM_AZURE_API_VERSION)
|
||||
|
|
|
|||
|
|
@ -21,27 +21,7 @@
|
|||
#define NM_EC2_TOKEN_TTL_HEADER "X-aws-ec2-metadata-token-ttl-seconds: 180"
|
||||
#define NM_EC2_TOKEN_HEADER "X-aws-ec2-metadata-token: "
|
||||
|
||||
static const char *
|
||||
_ec2_base(void)
|
||||
{
|
||||
static const char *base_cached = NULL;
|
||||
const char *base;
|
||||
|
||||
again:
|
||||
base = g_atomic_pointer_get(&base_cached);
|
||||
if (G_UNLIKELY(!base)) {
|
||||
/* The base URI can be set via environment variable.
|
||||
* This is mainly for testing, it's not usually supposed to be configured.
|
||||
* Consider this private API! */
|
||||
base = g_getenv(NMCS_ENV_NM_CLOUD_SETUP_EC2_HOST);
|
||||
base = nmcs_utils_uri_complete_interned(base) ?: ("" NM_EC2_BASE);
|
||||
|
||||
if (!g_atomic_pointer_compare_and_exchange(&base_cached, NULL, base))
|
||||
goto again;
|
||||
}
|
||||
|
||||
return base;
|
||||
}
|
||||
NMCS_DEFINE_HOST_BASE(_ec2_base, NMCS_ENV_NM_CLOUD_SETUP_EC2_HOST, NM_EC2_BASE);
|
||||
|
||||
#define _ec2_uri_concat(...) nmcs_utils_uri_build_concat(_ec2_base(), __VA_ARGS__)
|
||||
#define _ec2_uri_interfaces(...) \
|
||||
|
|
|
|||
|
|
@ -20,27 +20,7 @@
|
|||
|
||||
#define NM_GCP_METADATA_HEADER "Metadata-Flavor: Google"
|
||||
|
||||
static const char *
|
||||
_gcp_base(void)
|
||||
{
|
||||
static const char *base_cached = NULL;
|
||||
const char *base;
|
||||
|
||||
again:
|
||||
base = g_atomic_pointer_get(&base_cached);
|
||||
if (G_UNLIKELY(!base)) {
|
||||
/* The base URI can be set via environment variable.
|
||||
* This is mainly for testing, it's not usually supposed to be configured.
|
||||
* Consider this private API! */
|
||||
base = g_getenv(NMCS_ENV_NM_CLOUD_SETUP_GCP_HOST);
|
||||
base = nmcs_utils_uri_complete_interned(base) ?: ("" NM_GCP_BASE);
|
||||
|
||||
if (!g_atomic_pointer_compare_and_exchange(&base_cached, NULL, base))
|
||||
goto again;
|
||||
}
|
||||
|
||||
return base;
|
||||
}
|
||||
NMCS_DEFINE_HOST_BASE(_gcp_base, NMCS_ENV_NM_CLOUD_SETUP_GCP_HOST, NM_GCP_BASE);
|
||||
|
||||
#define _gcp_uri_concat(...) \
|
||||
nmcs_utils_uri_build_concat(_gcp_base(), \
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue