Expand my Community achievements bar.

SOLVED

Using Environment Variables in OSGi configurations as part of String

Avatar

Level 4

Hey all,

 

I am trying to use environment variables to setup certain configurations -

 

{
    "getAPIEndPoint": "$[env:ENDPOINT_Sys;default=/public/dev4/v1/store/store]",
}

 

 

This I know if allowed but is it possible to reference the environment variable directly in any OSGi configuration as shown below -
Here the api_env is an environment variable but not tied to any OSGi configuration - Is it allowed to use this environment variable directly on the fly?

 

{
    "getAPIEndPoint": "/public/$[env:api_env;default=dev4]/v1/category/api/v1/categories",
}

 

As per https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager-forms/how-to-access-enviro... we need to establish an OSGi configuration if we want to use an environment variable as part of another java osgi service.

 

thanks,

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@NageshRaja - It will work as part of the string too.

{
    "getAPIEndPoint": "/public/$[env:api_env;default=dev4]/v1/category/api/v1/categories",
}

Assuming your api_env has value 123 set in environment variable it will set the config as -  /public/123/v1/category/api/v1/categories else it will be DEFAULT -  /public/dev4/v1/category/api/v1/categories

 

Hope this helps!

View solution in original post

8 Replies

Avatar

Level 9

Hi @NageshRaja,

I believe you can use Environment variables directly in your code without having them included in the OSGi configuration. I haven't tried it myself, but the Java API: System.getenv("YOUR_ENV_VARIABLE") should work normally.

 

Good luck,

Daniel

Avatar

Level 4

yeah I don't want to actually change any code if possible was hoping this too work directly! thanks for the response!

Avatar

Correct answer by
Community Advisor

@NageshRaja - It will work as part of the string too.

{
    "getAPIEndPoint": "/public/$[env:api_env;default=dev4]/v1/category/api/v1/categories",
}

Assuming your api_env has value 123 set in environment variable it will set the config as -  /public/123/v1/category/api/v1/categories else it will be DEFAULT -  /public/dev4/v1/category/api/v1/categories

 

Hope this helps!

Avatar

Level 4

thanks arun i did check but was not sure whether it would work as part of string too!

Avatar

Level 8

hi @NageshRaja 

I believe that Cloud evaluates your configs and wherever it finds placeholders like $[secret:SECRET_VAR_NAME] or $[env:ENV_VAR_NAME] it replaces them with the var values (if there are any), or replaces them with the default values (if any were provided) or it replaces them with nothing (I believe you'll have an empty string).

 

But this is really simple to test out. Just add some configs and see how things behave (learning through empirical)

Avatar

Level 4

hey @Tethich, we still don't have access to the cloud so just preparing the code migration changes which will come up hence the reason for asking!