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'
Script will use the REST Admin API to fetch Split treatments.
Environment:
- Python 2.7.15
- requests 2.18.1
How to use:
- Class wrapper for Admin API can be downloaded from the link below:
SplitAPI.py
- Use the code below, update the following variables:
sourceSplitName, sourceWorkspaceName, sourceEnvironmentName
targetYamlFile
Admin API Key
import SplitAPI
from yaml import dump
#############################################
sourceSplitNames=["clients", "sample_feature"]
sourceWorkspaceName="Default"
sourceEnvironmentName="Production"
targetYamlFile="splits_treatment"
#############################################
mySplit = SplitAPI.SplitAPI('ADMIN Api Key')
sourceWorkspaceId = mySplit.GetWorkspaceId(sourceWorkspaceName)
sourceEnvironmentId = mySplit.GetEnvironmentId(sourceWorkspaceId, sourceEnvironmentName)
pyarr=[[]]
splitCnt=1
arrCnt=1
for splitName in sourceSplitNames:
sourceSplitInfo = mySplit.GetSplitDefinition(sourceWorkspaceId, splitName, sourceEnvironmentId)
allTreatments=sourceSplitInfo["treatments"]
treatmentCnt=1
for treatment in allTreatments:
if treatmentCnt>arrCnt:
pyarr.append([])
arrCnt=arrCnt+1
if "configurations" in treatment:
config = treatment['configurations'].encode('utf-8')
pyarr[treatmentCnt-1].append({splitName: {"treatment": treatment['name'].encode('utf-8'), "config": config}})
else:
pyarr[treatmentCnt-1].append({splitName: {"treatment": treatment['name'].encode('utf-8')}})
treatmentCnt=treatmentCnt+1
splitCnt=splitCnt+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.