This is an example of a simple python script I wrote when we copied our production environment to our test environment. The goal was to make a copy of our extensions and extension settings from the test environment, and upload them again once the test environment was up and running after the database copy from production. We didn't want to manually configure them since this can be a tedious and lengthy process.
Disclaimer 1: You will need some experience with code to make sure you are aware of what you are doing.
Disclaimer 2: I am in no way a programmer, just a curious customer who understands simple code.
All of this was done using "Google Colab".
If you decide to use google colab for this, these are the steps to use the code below.
Before inRiver copies production data to test:
- Copy the below code block to a new Google Colab code block.
- Make a new code block in Google Colab and type in extensionsToJson() and extensionSettingsToJson()
- Run the code block by clicking the "Play" button on the left side of the code block. multiple new json files should appear in the folder in google colab. Save these files locally on your computer as a backup.
- Once inRiver has finished you need to upload the zipped file of your extensions.
- Then upload all the json files again, make a new code block and type: uploadAllExtensionsAndSettings()
- Run the code block and all your extensions should be added with their previous settings.
- But note! Channel ID's are probably different so these needs to be changed if they are used in your settings. This goes for all extensions that use entity IDs.
Link to the google colab document: https://colab.research.google.com/drive/1Uy3wIsSBDPPhHaqcjIMoEZwT1uBwIser?usp=sharing
from requests.auth import HTTPBasicAuth
from google.colab import files
import requests
import json
# Connect to inRiver API
#API details
baseUrl = 'https://apieuw.productmarketingcloud.com/api/v1.0.0/'
getAllExtensionsUrl = 'extensions'
headers = {'Accept': 'application/json'}
auth = HTTPBasicAuth('apikey', 'your rest api key here')
# generates extension(s) URL
def generateExtensionSettingsUrl(id):
return 'extensions/' + id + '/settings'
def generateExtensionUrl(id):
return 'extensions/' + id
# loop through all extensions
def getAllExtensions(baseUrl, getAllExtensionsUrl, headers, auth):
response = requests.get(baseUrl + getAllExtensionsUrl, headers=headers, auth=auth)
return response.json()
def getExtension(baseUrl, getAllExtensionsUrl, headers, auth):
response = requests.get(baseUrl + getAllExtensionsUrl, headers=headers, auth=auth)
print(response)
return response.json()
def getExtensionSettings(baseUrl, extensionSettingsUrl, headers, auth):
response = requests.get(baseUrl + extensionSettingsUrl, headers=headers, auth=auth)
return response.json()
def getAllExtensionsAndSettings():
allExtensionsJson = getAllExtensions(baseUrl, getAllExtensionsUrl, headers, auth)
allSettingsJson = []
for extension in allExtensionsJson:
# print(allExtensionsJson)
settingsJson = getExtensionSettings(baseUrl, generateExtensionSettingsUrl(extension['extensionId']), headers=headers, auth=auth)
jsonString = json.dumps(settingsJson)
with open(extension['extensionId'] + '.json', 'w') as outfile:
outfile.write(jsonString)
return allSettingsJson
def extensionsToJson():
allExtensions = getAllExtensions(baseUrl, getAllExtensionsUrl, headers, auth)
jsonString = json.dumps(allExtensions)
print(jsonString)
with open('allExtensions.json', 'w') as outfile:
outfile.write(jsonString)
def extensionSettingsToJson():
allExtensionSettings = getAllExtensionSettings()
jsonString = json.dumps(allExtensionSettings)
with open('allExtensionSettings.json', 'w') as outfile:
outfile.write(jsonString)
def postExtension(baseUrl, getAllExtensionsUrl, headers, auth, data):
print(baseUrl + getAllExtensionsUrl)
response = requests.post(baseUrl + getAllExtensionsUrl, headers=headers, auth=auth, data=generateExtensionDataStructure(data))
print(json.dumps(response.json(), indent=2))
return response.json()
def postExtensionSettings(baseUrl, headers, auth, extensionId, data):
for setting in data:
response = requests.put(baseUrl + generateExtensionSettingsUrl(extensionId), headers=headers, auth=auth, data=setting)
def uploadAllExtensionsAndSettings():
with open('allExtensions.json') as json_file:
allExtensions = json.load(json_file)
for extension in allExtensions:
extensionId = extension['extensionId']
print('Processing ' + extensionId)
postExtension(baseUrl, getAllExtensionsUrl, headers, auth, extension)
print(extensionId + ' Added')
with open(extensionId + '.json') as json_file_settings:
settings = json.load(json_file_settings)
print('Posting ' + extensionId + ' settings')
postExtensionSettings(baseUrl, headers, auth, extensionId, settings)
print(extensionId + ' settings added')
def generateExtensionDataStructure(extensionData):
return {
"assemblyName": extensionData['assemblyName'],
"assemblyType": extensionData['assemblyType'],
"extensionId": extensionData['extensionId'],
"extensionType": extensionData['extensionType'],
"packageId": extensionData['package']['id']
}
-
This is amazing Stig Hansen, thank you for sharing your tips & tricks with the community! 🤩
2 -
I'm no expert in this but I think that there are indentation faults in this that prevent it from running.
1 -
Hi Mark Stuart-Walker - You are absolutely correct. It seems the markdown formatting messed it up. I added a link to a shared google colab document that can be copied and runned.
0 -
Final problem: I had the change getAllExtensionSettings to getAllExtensionsAndSettings
I now have a JSON file dump of all my extensions. Thank you!
1
Please sign in to leave a comment.
Comments
4 comments