Example:
Example to use Python to export Split names and Treatments to YAML files. The YAML files can be used with test automation to test all possible treatments from each Split.
The tool accept a list of Split names, will fetch the first treatment of each split and create a yaml file named "splits_treatment1.yaml".
Then for each subsequent treatments, a yaml file will be created for each treatment, for example:
For example, splits_treatment1.yaml:
- split1:
treatment: 'on'
config: '{"name":"Staging config with spaces","value":"99"}'
- split2:
treatment: 'on'
- split3:
treatment: 'on'
splits_treatment2.yaml:
- split1:
treatment: 'off'
- split2:
treatment: 'off'
- split3:
treatment: 'off'
splits_treatment3.yaml:
- split1:
treatment: 'unallocated'
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
targetYamlFile
Admin API Key
from splitapiclient.main import get_client
from yaml import dump
#############################################
sourceSplitNames=["clients", "sample_feature"]
sourceWorkspaceName="Default"
sourceEnvironmentName="Production"
targetYamlFile="splits_treatment"
#############################################
client = get_client({'apikey': 'ADMIN API KEY'})
ws = client.workspaces.find(workspaceName)
env = client.environments.find(environmentName, ws.id)
pyarr=[[]]
arrCnt=1
for splitName in splitNames:
splitInfo = client.split_definitions.find(splitName, env.id, ws.id)
allTreatments=splitInfo._treatments
treatmentCnt=1
for treatment in allTreatments:
if treatmentCnt>arrCnt:
pyarr.append([])
arrCnt=arrCnt+1
if treatment._configurations != "":
config = treatment._configurations
pyarr[treatmentCnt-1].append({splitName: {"treatment": treatment._name, "config": config}})
else:
pyarr[treatmentCnt-1].append({splitName: {"treatment": treatment._name}})
treatmentCnt=treatmentCnt+1
cnt=1
for curarr in pyarr:
stream = file("temp.yaml", 'w')
print dump(curarr)
stream.write(dump(curarr))
stream.close()
fin = open("temp.yaml")
fout = file(targetYamlFile+str(cnt)+".yaml", 'w')
for line in fin:
line = line.replace("!!python/str", "")
fout.write(line)
fin.close()
fout.close()
cnt=cnt+1
Comments
0 comments
Please sign in to leave a comment.