Example:
Basic code to use Python to copy a feature flag definition (Individual targeting, targeting rules, and treatments) from source project/environment to a target Project/Environment. The script will create the target feature flag in the target Project, then add the feature flag definition.
If the target feature flag already exist, comment out the line that creates the target feature flag.
Script will use the REST Admin API to perform the actions.
How to use:
- Class wrapper for Admin API, installation instructions in this link: Python Admin API Library Wrapper
- Update the following variables in the code below:
sourceSplitName, sourceProjectName, sourceEnvironmentName
targetSplitName, targetProjectName, targetEnvironmentName, targetSplitDescription, targetTrafficTypeName
Admin API key
Note:
Please note each feature flag has a unique hash key used to calculate treatments when percentage distribution rule is used, i.e. users bucketing will be different in each feature flag, and their experience might change from the old to the new feature flag.
from splitapiclient.main import get_client
#############################################
sourceSplitName="clients"
sourceProjectName="Default"
sourceEnvironmentName="Production"
targetSplitName = "clients-new"
targetProjectName = "myApp"
targetEnvironmentName = "Prod-myApp"
targetSplitDescription = "target description"
targetTrafficTypeName = "user"
#############################################
client = get_client({'apikey': 'ADMIN API KEY'})
sourceWs = client.workspaces.find(sourceProjectName)
sourceEnv = client.environments.find(sourceEnvironmentName, sourceWs.id)
sourceSplitDef = client.split_definitions.find(sourceSplitName, sourceEnv.id, sourceWs.id)
targetWs = client.workspaces.find(targetProjectName)
targetEnv = client.environments.find(targetEnvironmentName, targetWs.id)
trs = []
for tr in sourceSplitDef._treatments:
trs.append(tr.export_dict())
rls = []
for rl in sourceSplitDef._rules:
rls.append(rl.export_dict())
drls = []
for drl in sourceSplitDef._default_rule:
drls.append(drl.export_dict())
splitDefinition = {"treatments": trs,"defaultTreatment": sourceSplitDef._default_treatment, "rules": rls, "defaultRule": drls}
split = targetWs.add_split({'name':targetSplitName, 'description':targetSplitDescription}, targetTrafficTypeName)
splitDef = split.add_to_environment(targetEnv.id, splitDefinition)
Comments
0 comments
Please sign in to leave a comment.