Reputation: 301
I would like the features to be based on the environment. For example a feature is being worked or tested so I could have it on in DEV , but it's not ready for the public, so it's turned off in PROD.
Do I need to implement a custom strategy or can I use one of the existing strategies in a creative way?
If there any concise example that would be most helpful.
Upvotes: 0
Views: 1185
Reputation: 9
The easiest way I’ve found to implement feature flags at the environment level is to use a third-party hosted management system. Many feature flag services allow you to control which environment a flag will be enabled in. In the example below, I used DevCycle’s Python SDK, referencing feature flags and environments I had created in the DevCycle dashboard.
First, I installed the SDK from the command line:
$ pip install devcycle-python-server-sdk
Then I initialized the SDK it with the SDK key that corresponded to my desired environment. In this case, I used the key for my Dev environment. DevCycle provides SDK keys for each environment you set up.
from __future__ import print_function
from devcycle_python_sdk import Configuration, DVCClient
from devcycle_python_sdk.rest import ApiException
configuration = Configuration()
# Set up authorization
configuration.api_key['Authorization'] = 'SDK_KEY_FOR_DEV_ENV'
# Create an instance of the API class
dvc = DVCClient(configuration)
# Create user object. All functions require user data to be an instance of the UserData class
user = UserData(
user_id='test'
)
key = 'enable-feature-flag' # feature flag key created in the DevCycle Dashboard
try:
# Fetch variable values using the identifier key, with a default value and user object
# The default value can be of type string, boolean, number, or JSON
flag = dvc.variable(user, key, False)
# Use received value of feature flag.
if flag.value:
# Put feature code here, or launch feature from here
else:
# Put existing functionality here
except ApiException as e:
print("Exception when calling DVCClient->variable: %s" %e)
By passing in the SDK key for my Dev environment, 'SDK_KEY_FOR_DEV_ENV'
, it gives my program access to only the features enabled in Dev. You can choose which environment(s) a feature is enabled in directly from the DevCycle dashboard. So if 'enable-feature-flag'
was set to true
for your Dev environment, you would see your feature. Likewise, you could set 'enable-feature-flag'
to false
in your Prod environment, and replace 'SDK_KEY_FOR_DEV_ENV'
with the key for your Prod environment. This would disable the new functionality from Prod.
Full disclosure: My name is Sandrine and I am a Developer Advocate for DevCycle. I hope this answer helps you get started on environment-specific feature flags.
Upvotes: 0