Why can't Node-RED parse the VCAP_SERVICES environment in Bluemix? - ibm-cloud

I am using Node-RED and want to parse in Bluemix VCAP_SERVICES but I am getting an error. My code is:
var services = context.global.VCAP_SERVICES;
var env_cloudint = services['CloudIntegration'][0].credentials;
but I get this error :
TypeError: Cannot read property 'CloudIntegration' of undefined
I do have CloudIntegration in my VCAP_SERVICES. Do I need anything extra in my code to exploit VCAP_SERVICES?

By default, environment variables are not added to the Function global context object. To access the Bluemix VCAP_SERVICES environment variable from a Node-RED flow, you will need to add it to the Function node's global context.
Edit bluemix-settings.js and add an entry to the functionGlobalContext property:
functionGlobalContext: { VCAP_SERVICES: JSON.parse(process.env.VCAP_SERVICES)}
Then redeploy your app. When redeployed, you can then access VCAP_SERVICES in a Function node as the context.global.VCAP_SERVICES variable.

Related

How Environment variable names reflect the structure of an appsettings.json

I am using ASP.NET Core 5.0 and I have a Web API app deployed to internal cloud where few settings like DB are controlled via environment variables on the host cloud. In my Startup.cs I have the below code
string projectDbConnection = Configuration.GetSection("ProjectDatabaseSettings").GetValue<string>("PROJECT_DB_CONNECTION");
string projectDbName = Configuration.GetSection("ProjectDatabaseSettings").GetValue<string>("PROJECT_DB_NAME");
Here as I understand, when running locally in IIS Express it looks for appsettings.<Environment>.json and they take precedence over appsettings.json values.
But this app is always connecting to the wrong DB when I deployed to Cloud where I mentioned the PROJECT_DB_CONNECTION & PROJECT_DB_NAME as Environment variables for the app.
To make the app read from the Environment variables I had to change the above Code in Startup.cs as
string projectDbConnection = Configuration.GetValue<string>("PROJECT_DB_CONNECTION");
string projectDbName = Configuration.GetValue<string>("PROJECT_DB_NAME");
I am unable to understand the difference between the GetSection.GetValue and just GetValue and why I should use Configuration.GetValue() to direct app to read from Env variables.
what am I missing and when should we use what?
Naming of environment variables
There is kind of a naming convention in the environment variables for nested appsettings to env vars, see naming of environment variables.
Each element in the hierarchy is separated by a double underscore.
In your case it would work if you name the env variable: ProjectDatabaseSettings__PROJECT_DB_CONNECTION.
Config Order
Regarding to Microsoft Documentation there is a order in which the config sources are checked.
ChainedConfigurationProvider : Adds an existing IConfiguration as a source. In the default configuration case, adds the host configuration and setting it as the first source for the app configuration.
appsettings.json using the JSON configuration provider.
appsettings.Environment.json using the JSON configuration provider. For example, appsettings.Production.json and appsettings.Development.json.
App secrets when the app runs in the Development environment.
Environment variables using the Environment Variables configuration provider.
Command-line arguments using the Command-line configuration provider.
The usecase
This is useful when you are developing local using appsettings.json, but run in a cluster or cloud in production where it is more convenient to use environment variables (f.e.: in kubernetes environment variables are set via config maps).

How to get an instance by it's name using googleapiclient

I am using the googleapiclient.discovery as client to connect to GCP. Ideally, I would like to retrieve a virtual machine by it's
zone
project
name
I am having a difficult time finding code samples that do this. I am initializing the client like so
client = googleapiclient.discovery.build('compute', 'v1')
I've exported the environment variable GOOGLE_APPLICATION_CREDENTIALS and I am able to successfully connect to GCP. However, I am unable to fetch an instance by it's name. I am looking for a method like
instance = client.compute.instances().get("project","zone","instance_name")
Any help with this would be greatly appreciated.
Just need to set up a client with discovery like so
compute = discovery.build('compute', 'v1', credentials=credential)
getinstance = compute.instances().get(project=project_id, zone=region, instance=instance_id).execute()

Failed to read environment Variables in Scala using $ symbol

Adding Property in Scala Environment Properties
val sysProps = System.getProperties
sysProps.setProperty("current.date.time", LocalDateTime.now().toString())
i'm able to save this property.
I tried accessing this property(current.date.time) in log4j.properties like below
log4j.appender.file.File=C:/Users/vsami/Desktop/Demo_${current.date.time}.log
log4j.appender.file.File=C:/Users/vsami/Desktop/Demo_${env:current.date.time}.log
Log file is getting generated in above location like Demo_.log, Expected :- Demo_2019/11/27T13:21:00.log
Above implementation is not helping me in accessing variable from environment properties and generate log file with expected naming convention.
JVM has properties that can be passed via -D parameter at VM boot. -Dprop=value.
These properties can be read via System.getProperties API call. See docs for more info.
Environment variables are not specified on JVM boot and managed independently from VM by your boot environment (it can be shell, bash etc). You cannot change environment variables in already running VM. These variables can be read via System.getenv()
$ is a look up operator in log4j and can be used to resolve env variables with env: prefix or Main Arguments Lookup with prefix main:.
You could use main:current.date.time and initialise your value as following
MainMapLookup.setMainArguments(Array("--current.date.time", LocalDateTime.now().toString()));
Make sure that MainMapLookup is called before logging is initialised.

Function app binding issue - Value cannot be null. Parameter name: hostAccount

When I try to upload the zip file to an azure function app using kudu REST API, it throws an error while I try to view the c# code in Function App editor in the browser. The error is:
"Error:
Function ($Source) Error: Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.Source'. Microsoft.Azure.WebJobs.Host: Value cannot be null.
Parameter name: hostAccount.
Session Id: xxxxxxxxxxx
Timestamp: 2016-12-02T18:35:00.083Z"
Please note that I have automated end to end of Application Insights starting from creation of a resource group till exporting the multi-setep web test results to our Splunk - All using Powershell.
In this process of automation, I am forming a storage connection string and setting it to the app settings of the function app and then providing that key in my function.json binding.
But still I get this error.
Here is the issue I created in the Azure Function App - Git: https://github.com/Azure/azure-webjobs-sdk-script/issues/1020
The error points to missing host configuration (e.g. the host storage account).
If you're automating the creation process, you need to make sure the required environment variables are properly set. The recommended way would be to use an ARM template manage that for you.
I have some details on how you can get the ARM template for a resource (which you could use to look at the required settings for your Function App) here.
We also have some sample templates you can use linked here
I hope this helps!

How to get User-Defined variables in node-red app in Bluemix

In my node-red app in Bluemix, I added a User-Defined environment variable.
Not sure how to get that variable from a function in my node-red application.
Thanks.
You will need to edit the bluemix-settings.js file to include the "process" built-n or other variables in functionGlobalContext.
functionGlobalContext: {
process: process
}
Once redeployed you can access the process in a function node as...
context.global.process
https://developer.ibm.com/answers/questions/170246/how-do-i-get-at-my-vcap-variables-from-node-red.html
To get User Defined Variable from Bluemix you must use something like this:
var services = context.global.process.env['USR_DEFINED_VAR'];
after configuring functionGlobalContext: { process: process } in bluemix-settings.js
To change the value is just like that:
context.global.process.env['USR_DEFINED_VAR'] = value;