Example:
Basic code to use Python to copy feature flag fefinition (Individual targets, targeting rules, and treatments) for a given feature flag in workspace from one environment to another, and overwrite existing definitions in the target environment.
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
- Use the code below, update the following variables:
sourceSplitName, sourceWorkspaceName, sourceEnvironmentName, targetEnvironmentName
Admin API key
from splitapiclient.main import get_client
import json
#############################################
sourceSplitName="clients"
sourceWorkspaceName="Default"
sourceEnvironmentName="Production"
targetSplitName=sourceSplitName
targetWorkspaceName=sourceWorkspaceName
targetEnvironmentName="Staging"
#############################################
client = get_client({'apikey': 'ADMIN API KEY'})
sourceWs = client.workspaces.find(sourceWorkspaceName)
sourceEnv = client.environments.find(sourceEnvironmentName, sourceWs.id)
sourceSplitDef = client.split_definitions.find(sourceSplitName, sourceEnv.id, sourceWs.id)
targetWs = client.workspaces.find(targetWorkspaceName)
targetEnv = client.environments.find(targetEnvironmentName, targetWs.id)
targetSplitDef = client.split_definitions.find(targetSplitName, targetEnv.id, 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}
targetSplitDef.update_definition(splitDefinition)
Comments
0 comments
Please sign in to leave a comment.