Copy management zones between Dynatrace environments

This use case shows you how to use Management zones API to copy management zones between Dynatrace environments.

The execution of REST calls is up to you—you can use any REST client or write a script, like the one, provided below.

You can also use Dynatrace API explorer to familiarize yourself with endpoints and execute all the required requests.

Generate a new access token for the Dynatrace API. Make sure to assign Read configuration and Write configuration permissions to it.

First, you need to obtain the configuration of management zones from the source environment. To do so:

  1. Execute the GET all management zones request.
    The response contains the list of short representations of management zones—just IDs and names, like in example below.
    {
        "values": [        
            {
               "id": "-8706723556235787125",
               "name": "All"
            },
            {
                "id": "1856893735401542875",
                "name": "easyTravel"
            }
        ]
    }
    
  2. Use the ID from the response to execute the GET a management zone requests, to obtain configuration of each management zone. The response contains parameters of a management zone. The example below shows parameters of the easyTravel management zone.
    {
      "metadata": {
        "clusterVersion": "1.163.0.20190128-084301",
        "configurationVersions": [
          0
        ]
      },
      "id": "1856893735401542875",
      "name": "easyTravel",
      "rules": [
        {
          "type": "WEB_APPLICATION",
          "enabled": true,
          "propagationTypes": [],
          "conditions": [
            {
              "key": {
                "attribute": "WEB_APPLICATION_TAGS"
              },
              "comparisonInfo": {
                "type": "TAG",
                "operator": "TAG_KEY_EQUALS",
                "value": {
                  "context": "CONTEXTLESS",
                  "key": "easyTravel"
                },
                "negate": false
              }
            }
          ]
        },
        {
          "type": "SERVICE",
          "enabled": true,
          "propagationTypes": [
            "SERVICE_TO_PROCESS_GROUP_LIKE",
            "SERVICE_TO_HOST_LIKE"
          ],
          "conditions": [
            {
              "key": {
                "attribute": "SERVICE_TAGS"
              },
              "comparisonInfo": {
                "type": "TAG",
                "operator": "TAG_KEY_EQUALS",
                "value": {
                  "context": "CONTEXTLESS",
                  "key": "easyTravel"
                },
                "negate": false
              }
            }
          ]
        }
      ]
    }
    
  3. Save the JSON payload as a file on your computer and/or in a version control system.
    By storing the configuration in a version control system, you can get the delta of recent changes and you can use tagging mechanisms to decide which configuration you want to distribute into your Dynatrace environments.
  4. Repeat the sub-steps 2 and 3 for all management zones you want to copy.

Now let's upload the management zones to other Dynatrace environment. Execute the PUT a management zone request with the JSON from the previous steps as a payload. Make sure to use the id of the management zone in the URL—it will ensure the same IDs across all environments.

Repeat the step 3 for all management zones you want to copy.

Script example

This Python script saves configuration of all management zones of your environment to your computer. The folder structure reflects the API path. You can later save these configurations in a version control system and/or upload them to another Dynatrace environment.

You need Python version 3.5 or higher to use this script.

Make the following adjustments to the script:

  1. Replace <YOUR_ENVIRONMENT> with the URL of your Dynatrace environment:
    • Managed https://{your-domain}/e/{your-environment-id}
    • SaaS https://{your-environment-id}.live.dynatrace.com
  2. Replace <YOUR_API_TOKEN> with your API token which has the Read configuration permission assigned.
api-examples/config/management-zones/save.py
Download
"""
Example script for fetching given Dynatrace config list items and store them on disk.
"""
import requests, ssl, os, sys

ENV = '<YOUR_ENVIRONMENT>'
TOKEN = '<YOUR_API_TOKEN>'
HEADERS = {'Authorization': 'Api-Token ' + TOKEN}
PATH = os.getcwd()

def save(path, file, content):
	if not os.path.isdir(PATH + path):
		os.makedirs(PATH + path)
	with open(PATH + path + "/" + file, "w", encoding='utf8') as text_file:
		text_file.write("%s" % content)

def saveList(list_type):
	try:
		r = requests.get(ENV + '/api/config/v1/' + list_type, headers=HEADERS)
		print("%s response code: %d" % (list_type, r.status_code))
		res = r.json()
		for entry in res['values']:
			print(entry['id'])
			tr = requests.get(ENV + '/api/config/v1/' + list_type + '/' + entry['id'], headers=HEADERS)
			save('/api/config/v1/' + list_type + '/', entry['id'], tr.json())
	except ssl.SSLError:
		print("SSL Error")


def main():
	saveList('managementZones')

if __name__ == '__main__':
	main()