summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2023-05-16 14:01:12 +0200
committerThomas Haller <thaller@redhat.com>2023-05-17 20:27:56 +0200
commit4525f0d3334800f3fb1980a7a69413549218396f (patch)
tree05cba1337999d84ca738586b67f9dbf10cb61fb3
parent1757e78c7faa386cc7dfb710599aa29ab6984a2e (diff)
downloadNetworkManager-4525f0d3334800f3fb1980a7a69413549218396f.tar.gz
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.
-rw-r--r--src/nm-cloud-setup/nm-cloud-setup-utils.h25
-rw-r--r--src/nm-cloud-setup/nmcs-provider-aliyun.c22
-rw-r--r--src/nm-cloud-setup/nmcs-provider-azure.c22
-rw-r--r--src/nm-cloud-setup/nmcs-provider-ec2.c22
-rw-r--r--src/nm-cloud-setup/nmcs-provider-gcp.c22
5 files changed, 29 insertions, 84 deletions
diff --git a/src/nm-cloud-setup/nm-cloud-setup-utils.h b/src/nm-cloud-setup/nm-cloud-setup-utils.h
index 5b34f49d4d..f4e274226a 100644
--- a/src/nm-cloud-setup/nm-cloud-setup-utils.h
+++ b/src/nm-cloud-setup/nm-cloud-setup-utils.h
@@ -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, ...) \
diff --git a/src/nm-cloud-setup/nmcs-provider-aliyun.c b/src/nm-cloud-setup/nmcs-provider-aliyun.c
index aa98ca7115..20010a2c8f 100644
--- a/src/nm-cloud-setup/nmcs-provider-aliyun.c
+++ b/src/nm-cloud-setup/nmcs-provider-aliyun.c
@@ -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(...) \
diff --git a/src/nm-cloud-setup/nmcs-provider-azure.c b/src/nm-cloud-setup/nmcs-provider-azure.c
index 89013aa4ee..771c43d9ad 100644
--- a/src/nm-cloud-setup/nmcs-provider-azure.c
+++ b/src/nm-cloud-setup/nmcs-provider-azure.c
@@ -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)
diff --git a/src/nm-cloud-setup/nmcs-provider-ec2.c b/src/nm-cloud-setup/nmcs-provider-ec2.c
index 9975a4fe47..9cdc7a0cec 100644
--- a/src/nm-cloud-setup/nmcs-provider-ec2.c
+++ b/src/nm-cloud-setup/nmcs-provider-ec2.c
@@ -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(...) \
diff --git a/src/nm-cloud-setup/nmcs-provider-gcp.c b/src/nm-cloud-setup/nmcs-provider-gcp.c
index 6f037282aa..4d9ef96501 100644
--- a/src/nm-cloud-setup/nmcs-provider-gcp.c
+++ b/src/nm-cloud-setup/nmcs-provider-gcp.c
@@ -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(), \