Use python to update policy objects on MC via API
search cancel

Use python to update policy objects on MC via API

book

Article ID: 171608

calendar_today

Updated On:

Products

Management Center

Issue/Introduction

The Management Center (MC) uses Application Programmable Interface (API) which you can access via curl TECH250273 or python. This article was written for MC 1.11.x.x. The syntax in future 2.x release may change. If you encounter any problems please contact the support.

 

Resolution

On the client, where you are going to run the python script, make sure you install the "requests" and "import" modules via the "easy_install.exe" included in the folder where you installed python. Failing to do this will result in an error on execution of the script. For reference on how to do this follow this guide

Below is a sample script you can use as a reference

import requests
import json
headers = { 'X-Auth-Token': 'Your_API_Token'
           }

url='https://Your_MC:8082/api/policies/Your_Policy_Object_UUID/content'
jsonContent = {
  "content" : {
    "sections" : [ {
      "name" : "Untitled",
      "purpose" : "CustomSolution",
      "defaultPolicy" : "I used python with API token to edit the policy. This is where the CPL code goes",
    } ],
    "editMode" : "SECTIONED"
  },
  "schemaVersion" : "1.0",
  "contentType" : "cpl",
    "changeDescription" : "changed p2olicy"
   }
r = requests.post(url, headers=headers , json=jsonContent, verify=False)
print(r.status_code, "\n", r.json())



Save this file with a ".py" extension and run it. In Windows environment this means just browsing to the location of the .py script and type in the command prompt "Your_script.py" In Linux environment you have to type "./Your_script.py". If everything went OK you get a 200 HTTP response code from MC along with the entire contents of the json file:

(200, '\n', {u'content': {u'referenceDeviceUuid': None, u'sections': [{u'default
Policy': u'used python with API token', u'overridePolicy': None, u'mandatoryPoli
cy': None, u'name': u'Untitled', u'purpose': u'CustomSolution'}], u'editMode': u
'SECTIONED'}, u'schemaVersion': u'1.0', u'revisionInfo': {u'revisionDescription'
: u'changed p2olicy', u'revisionDate': u'2018-04-27T14:06:56', u'revisionNumber'
: u'1.35', u'author': u'admin'}})