global variable for poor man's dependency injection in parse cloud code - parse-platform

I would like a variable to be shared among the various modules that I use for my cloud code.
For example, I was hoping I would be able to do the following:
In main.js, I would have the following:
Env = 'prod';
var Foo = require('cloud/foo.js').Foo;
Then in foo.js, I'd want to be able to access the value of Env
console.log("environment is: " + Env);
This does not work when deployed on Parse, but it does work if I run this in node.js.
Essentially, what I am looking for is a poor man's way to do dependency injection to allow me to easily test my cloud code in a local environment using node.js.
In the case above, Env would store the information that differs whether the cloud code executes in production (as a cloud function in Parse) or in a test (in node.js run locally).
[In the simple example above, I set Env to prod in main.js, and I'd set it to 'test' in my test script.]
Thanks for any insight.

Related

Connect to Google Cloud Datastore using dev_appserver.py & google.golang.org/appengine#v1.6.6

As the title says. We have a legacy Go 1.11 AppEngine API that requires the dev_appserver.py to run. Simply, I want appengine.Main() & appengine.NewContext(r) to allow my application to point to my Cloud Datastore using my project-id, rather than the local emulator's storage. I have set GOOGLE_APPLICATION_CREDENTIALS to no avail.
This would be so I can locally run the server, while accessing a shared, Cloud DB.
I am using google.golang.org/appengine#v1.6.6 w/ dev_appserver.py --enable_console --port=8081 --support_datastore_emulator=true --go_debugging=true app.yaml
Is this possible? Or am I stuck on a local emulator when using the old Go libraries?
Moving from comments to answer
Take a look at remote_api for Go 1.11 https://cloud.google.com/appengine/docs/legacy/standard/go111/tools/remoteapi
The logic for using it would be something along the lines of -
If running on local environment, use remote_api else stick to the default behavior (i.e. since remote_api isn't enabled, it will either use the emulator in local environments or use production data directly in production)
To keep things simple, you could try using same variable name i.e.
if this is local environment
ctx, err := remote_api.NewRemoteContext(host, hc)
else
ctx := appengine.NewContext(r)
Then you use 'ctx' in the rest of your queries/calls to datastore
Note: I'm not familiar with 'Go' so take the above as pseudo-code and not working code
You might also want to consider not running the above changes with the --support_datastore_emulator=true flag

How to read .runsettings test parameter in xUnit fixture

I'm writing xUnit unit test cases for a dotnet core application which uses DocumentDB (CosmosDB) as storage. The unit test are written to execute against the local cosmos db emulator. On the Azure DevOps build environment, I've setup the Azure Cosmos DB CI/CD task which internally creates a container to install the emulator. However, I'm not able to figure out that how the endpoint of emulator can be passed to xUnit fixture?
Is there any way through which xUnit fixture can read the .runsettings test parameters or parameters can be passed via other source?
Update
Currently, I implemented the scenario using Environment Variable but still not happy to define the connection string as a environment variable using powershell in build task and read it in through code during unit test execution. I was thinking if there could be another way of achieving it..
Below snapshot shows how the build tasks are configured currently as workaround to achieve the desired:
And code to read the value as
var serviceEndpoint = Environment.GetEnvironmentVariable("CosmosDbEmulatorEndpointEnvironmentVariable");
Since, UnitTest task provides the option to pass .runsettings/.testsettings with option to override the test run parameters so was thinking it something can be achieved using those options.
This is not supported in xUnit.
See SO answers here and here, and this github issue indicating that it is not something that will be supported in xUnit.
Currently, I implemented the scenario using Environment Variable but still not happy to define the connection string as a environment variable using powershell in build task and read it in through code during unit test execution. I was thinking if there could be another way of achieving it..
Below snapshot shows how the build tasks are configured currently as workaround to achieve the desired:
And code to read the value as
var serviceEndpoint = Environment.GetEnvironmentVariable("CosmosDbEmulatorEndpointEnvironmentVariable");
Since, UnitTest task provides the option to pass .runsettings/.testsettings with option to override the test run parameters so was thinking it something can be achieved using those options.

Serverless Detect Running Locally

I am running a command like the following.
serverless invoke local --function twilio_incoming_call
When I run locally in my code I plan to detect this and instead of looking for POST variables look for a MOCK file I'll be giving it.
I don't know how to detect if I'm running serverless with this local command however.
How do you do this?
I looked around on the serverless website and could find lots of info about running in local but not detecting if you were in local.
I found out the answer. process.env.IS_LOCAL will detect if you are running locally. Missed this on their website somehow...
If you're using AWS Lambda, it has some built-in environment variables. In the absence of those variables, then you can conclude that your function is running locally.
https://docs.aws.amazon.com/lambda/latest/dg/lambda-environment-variables.html
const isRunningLocally = !process.env.AWS_EXECUTION_ENV
This method works regardless of the framework you use whether you are using serverless, Apex UP, AWS SAM, etc.
You can also check what is in process.argv:
process.argv[1] will equal '/usr/local/bin/sls'
process.argv[2] will equal 'invoke'
process.argv[3] will equal 'local'

Config variables not visible as environment variables in Heroku app

I've set a few custom config variables. I can see them in my application's settings->config variables. I can also see the values with the heroku config command. But when I start my application the environment variables are not there. I use (System/getenv "MY_VARIABLE_NAME") in Clojure to fetch them.
Is it because I try to retrieve them at boot time? Are they only available later? Or is there some twitch which I can get rid of by doing some trick? I've used config variables in Heroku before and they've worked, I don't know what's the problem here...
I was trying to retrieve client ID and secret for oauth authentication with Google from a config variable with System/getenv. I use a library called Friend to do this. Problem is, the set up for oauth parameters in that library is done via macros. And macro expansion happens compile-time. Heroku config variables are not available as environment variables during compilation (for good reasons). They are, however available via filesystem which was my solution to the problem. So instead of:
(System/getenv "MY_APP_GOOGLE_CLIENT_ID")
I'm using this:
(slurp (str (System/getenv "ENV_DIR") "/" "MY_APP_GOOGLE_CLIENT_ID"))
And it works!

Is it Possible to Alias Config Variables on Heroku?

I'm currently writing a tutorial on setting up Wordpress on Heroku. Right now I'm using the ClearDB add-on which sets a CLEARDB_DATABASE_URL ENV variable automatically. Is it possible to alias the ENV variable through Heroku as DATABASE_URL?
It's not possible to alias an config var or refer to one from another. I asked a similar question and this is what they said:
I'm afraid that Config Variables can't refer to each other in this
way, as they are simple a collection of Names and Values, with no
interpolation or calculation available to the values.
You might like try a
profile
file...
I've had a similar issue - where I'm trying to use an app in a pipeline connected to 2 different heroku DB's - in order to keep all he environments consistent in code, I did the following:
Heroku Configs:
DATABASE_URL=XXXXXXXX - this was the first DB that heroku attached
HEROKU_POSTGRESQL_JADE_URL=XXXX - this was the second DB that heroku attached (the key name changes in each environment)
SECOND_DB_KEY_NAME=HEROKU_POSTGRESQL_JADE_URL
(ie. after each environment was set up - I added a reference to the new key)
This second DB key name, does not change if the DB credentials refresh.
In code, I then did the following at start up:
const databaseUrlKey = process.env.SECOND_DB_KEY_NAME
process.env['SECOND_DATABASE_URL'] = process.env[databaseUrlKey]
Maybe I'll just say something stupid, but why not just do this:
heroku config:set DATABASE_URL=CLEARDB_DATABASE_URL
In code:
ENV[ENV['DATABASE_URL']]
I'm not sure this information will be helpful for anyone but just in case:
This question involves an eroneous assertion. The ClearDB add-on does not set a CLEARDB_DATABASE_URL ENV variable. The ClearDB add-on creates a CLEARDB_DATABASE_URL config var. When the app is started an ENV variable is created from the config var. These two variables are different and could even have different values if you changed the ENV variable in your code base.
Of course, within your code base, you can do whatever you want with the ENV variables.
As to whether config vars can reference other config vars, or other ENV variables, or vica versa - I don't know. But surely this would be something pretty hacky, and contrary to intended use, and proper coding practice, and socially responsible behavior.

Resources