cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
19310
Views
30
Helpful
36
Replies

Prime Infrastructure ReST API Calls

arunav.12
Level 1
Level 1

Hi,

I have a query regarding the Cisco Prime Infrastructure API documentation hosted under URL: https://developer.cisco.com/site/prime-infrastructure/documents/api-reference/rest-api-v3-0/. I am using ReST apis mentioned in the above stated url to fetch inventory data from the Prime Infrastructure (PI) without using the GUI provided from the PI. Similarly I want to create VLAN on a switch using ReST api calls without using GUI.

My query is that can we create VLAN from ReST api calls? If so, is it through Cli templates?

According to the documentation mentioned above, it is said that we can download Cli templates for Configure vlan from the URL mentioned under "Get Cli Templates" section of the documentation and it is also said that we can deploy templates through jobs. Therefore, I followed the documentation and downloaded Cli template for Configure vlan and deployed it through job using the urls mentioned in the documentation, I am getting an error message saying "Invalid variable values" and the vlan is not being created.

Am I missing something here? Any advice for me ?

Thanks in advance

36 Replies 36

Hi Aradford,

I have solved the access issue. Now Job is not throwing any error, Job's run history says success, but how to check whether the vlan is created or not?. Following is the JSON body I am using.

Am I missing something here?


{

  "cliTemplateCommand": {

    "targetDevices": {

      "targetDevice": {

        "targetDeviceID": "074084",

        "variableValues": {

          "variableValue": [

            {

              "name": "encapsulation",

              "value": ""

            },

            {

              "name": "interfaceName",

              "value": ""

            },

            {

              "name": "mtu",

              "value": ""

            },

            {

              "name": "vlanid",

              "value": "244"

            },

            {

              "name": "vlanName",

              "value": "vlan199"

            },

            {

              "name": "mode",

              "value": ""

            },

            {

              "name": "option",

              "value": ""

            }

          ]

        }

      }

    },

    "templateName": "Configure VLAN"

  }

}

Hi Arun,

almost there..

This particular template is to apply an VLAN to an interface.  You need to specify an "name": "interfaceName", on the device.

For example "gig1/0/3" would apply vlan #244 to int gig1/0/3.

{ "name": "interfaceName",

"value" : "gig1/0/3"

}

If you just want to create a vlan on a device, there is another template for that.  You can get a list of templates using the tools in the github repo i mentioned earlier.

Here is the schema for "create_vlan"

$ python get_template.py -n Add_Vlan -s

{

  "cliTemplateCommand": {

    "targetDevices": {

      "targetDevice": {

        "targetDeviceID": "<DEVICEID>",

        "variableValues": {

          "variableValue": [

            {

              "name": "vlanId",

              "value": "required"

            },

            {

              "name": "vlanName",

              "value": "required"

            }

          ]

        }

      }

    },

    "templateName": "Add_Vlan"

  }

}

Hi Aradford,

I have tried the above specified JSON but I am sorry to say that "Add_Vlan" template does not exist in my instance of Cisco Prime.

Correct me If I am wrong. We can apply VLAN  to an interface, only if that VLAN is already created and exists isn't it ? . Will configure VLAN template creates VLAN and add it to the interface?

sorry, that may be the case. I am using PI 3.1

However, if you want to assign the vlan to a switch port and the VLAN does not exist, the switch will create it for you.

3650-dns(config-if)#switchport access vlan 222

% Access VLAN does not exist. Creating vlan 222

3650-dns#show vlan

VLAN Name                             Status    Ports

---- -------------------------------- --------- -------------------------------

222  VLAN0222                         active    Gi1/0/10

Adam

Hi Aradford,

I am using Cisco Prime Infrastructure 3.0 instance. Should we give any option like "Create" for the "options" variable in the template to create VLAN from ReST APIs.

Thanks

If you look at the definition of the template ( you can use $ python get_template.py -n "Configure VLAN")

Option can be create or delete.  You should set "Create"

{

              "name": "option",

              "value": "Create"

            }

{

                "displayLabel": "Operation",

                "description": "Create,Delete",

                "defaultValue": "Create,Delete",

                "required": false,

                "type": "Dropdown",

                "name": "option"

              }

Hi Aradford,

As suggested , following is the JSON I am using here. But still I am not able to see the VLAN being created. But I really wonder how job is complete and success?

{

  "cliTemplateCommand": {

    "targetDevices": {

      "targetDevice": {

        "targetDeviceID": "074084",

        "variableValues": {

          "variableValue": [

            {

              "name": "encapsulation",

              "value": ""

            },

            {

              "name": "interfaceName",

              "value": "GigabitEthernet0/21"

            },

            {

              "name": "mtu",

              "value": ""

            },

            {

              "name": "vlanid",

              "value": "249"

            },

            {

              "name": "vlanName",

              "value": "vlan249"

            },

            {

              "name": "mode",

              "value": "Access"

            },

            {

              "name": "option",

              "value": "Create"

            }

          ]

        }

      }

    },

    "templateName": "Configure VLAN"

  }

}

That looks ok.  If there is an issue, take a look at the detailed job history.

first you get the job name after you create job above..  I assume you have done the first part (got the job name)?

jobname = result.json()['mgmtResponse']['cliTemplateCommandJobResult']['jobName']


Then you monitor the job name until it completes

url = BASE + 'data/JobSummary.json?jobName=%s' %jobname
jobresult = requests.get(url,
verify=False)


status = jobresult['queryResponse']['entityId'][0]['@displayName'].split(",")[-1]

# Status will be != "SCHEDULED" when job completes


Then you need to get the job number.

jobnumber = jobresult['queryResponse']['entityId'][0]['$']


from the job number, you can get the full information about the job.

url = base + 'data/JobSummary/%s.json' %jobnumber

jobdeteailresult = requests.get(url, verify=False)

print jobdetailresult.json()

That should show you something like the following:

{

  "mgmtResponse": {

    "@responseType": "operation",

    "job": {

      "description": "Cli Template Deploy for IOS devices",

      "runInstances": {

        "runInstance": {

          "runStatus": "COMPLETED",

          "results": {

            "result": [

              {

                "property": "JobCliTemplateDeployIOSDevices08_40_09_490_PM_06_27_2016",

                "value": "1/1 template configurations successfully applied."

              },

              {

                "property": 610622,

                "value": "configlet:\n        interface gigabitethernet1/0/2\n                   description Fred1\n                            switchport mode access\n                            switchport ACCess vlan 8   \n                            \n               \n        \n        \n        \n        \n         \n        exit\n\n\n\n\n\n\n\n\n\n\n response:\n<cliChunks><cliChunk><cli>terminal width 0\nconfig t\ninterface gigabitethernet1/0/2\ndescription Fred1\nswitchport mode access\nswitchport ACCess vlan 8\nexit\n</cli><response><![CDATA[terminal width 0\n3650-dns#config t\nEnter configuration commands, one per line.  End with CNTL/Z.\n3650-dns(config)#interface gigabitethernet1/0/2\n3650-dns(config-if)#description Fred1\n3650-dns(config-if)#switchport mode access\n3650-dns(config-if)#switchport ACCess vlan 8\n3650-dns(config-if)#exit\n3650-dns(config)#]]></response></cliChunk></cliChunks>"

              }

            ]

          },

          "lastStartTime": "2016-06-27T20:40:39.565+10:00",

          "completionTime": "2016-06-27T20:40:40.588+10:00",

          "runId": 6365409,

          "resultStatus": "SUCCESS"

        }

      },

      "jobStatus": "COMPLETED",

      "jobName": "JobCliTemplateDeployIOSDevices08_40_09_490_PM_06_27_2016",

      "jobId": 560879,

      "jobType": "CliTemplateDeployIOSDevices"

    },

    "@rootUrl": "https://adam-pi/webacs/api/v1/op",

    "@requestUrl": "https://adam-pi/webacs/api/v1/op/jobService/runhistory?jobName=JobCliTemplateDeployIOSDevices08_40_09_490_PM_06_27_2016"

  }

}

Hi Aradford,

As suggested, using JobID generated I am querying both Job Summary as well as full Run History that can give me full run history of the job deployed. I will check in switch console and keep you posted on this.

Thanks

Arun

can you share the Run History similar to mine above please?

That will tell you the commands that were run and if they were successful.

Hi Aradford,

This is the run history I am getting right now. It says error in running some XDE procedure, is this a problem with the switch I am using?. Till yesterday my run history was telling me that job completed successfully. But now I don't know what has happened. And Also I am not able to see the vlan in the console also.

<job>

<description>Cli Template Deploy for IOS devices</description>

<jobId>49695729</jobId>

<jobName>

JobCliTemplateDeployIOSDevices10_28_17_099_AM_06_28_2016

</jobName>

<jobStatus>COMPLETED</jobStatus>

<jobType>CliTemplateDeployIOSDevices</jobType>

<runInstances>

<runInstance>

<completionTime>2016-06-28T10:28:56.224Z</completionTime>

<lastStartTime>2016-06-28T10:28:47.186Z</lastStartTime>

<resultStatus>FAILURE</resultStatus>

<results>

<result>

<property>1074084</property>

<value>

Error in running XDE Procedure, Error Message::Failed to establish ssh2 connection to device- Cause: Authentication failed on device. Update the Device credentials and retry the task.

</value>

</result>

</results>

<runId>60328470</runId>

<runStatus>COMPLETED</runStatus>

</runInstance>

</runInstances>

</job>

yes.  that indicates that PI was not able to log in to the device via ssh.

Can you check the status of the device?

GET https://pi-server/webacs/api/v1/data/Devices?.full=true&ipAddress=10.10.8.100    Where pi-server is your Prime Infrastructure.

You should see something like this?

{

  "queryResponse": {

    "@last": "0",

    "@first": "0",

    "@count": "1",

    "@type": "Devices",

    "@responseType": "listEntityInstances",

    "@requestUrl": "https://adam-pi/webacs/api/v1/data/Devices?.full=true&ipAddress=10.10.8.100",

    "@rootUrl": "https://adam-pi/webacs/api/v1/data",

    "entity": [

      {

        "@dtoType": "devicesDTO",

        "@type": "Devices",

        "@url": "https://adam-pi/webacs/api/v1/data/Devices/610622",

        "devicesDTO": {

          "@displayName": "610622",

          "@id": "610622",

          "clearedAlarms": 1,

          "collectionDetail": "<status><general code=\"SUCCESS\"/></status>",

          "collectionTime": "2016-06-28T22:00:35.708+10:00",

          "creationTime": "2016-05-15T17:27:46.354+10:00",

          "criticalAlarms": 0,

          "deviceId": 614650,

          "deviceName": "3650-dns",

          "deviceType": "Cisco Catalyst 36xx stack-able ethernet switch",

          "informationAlarms": 0,

          "ipAddress": "10.10.8.100",

          "majorAlarms": 0,

          "managementStatus": "MANAGED_AND_SYNCHRONIZED",

          "manufacturerPartNrs": {

            "manufacturerPartNr": "WS-C3650-48PD-L"

          },

          "minorAlarms": 0,

          "productFamily": "Switches and Hubs",

          "reachability": "Reachable",

          "softwareType": "IOS-XE",

          "softwareVersion": "03.07.04E",

          "warningAlarms": 0

        }

      }

    ]

  }

}

Hi Aradford,

Yes, there was a login issue. I sincerely appreciate your dedication to help me. With able help, I am able to resolve the issue I had. Thanks a lot for your help and time. Your expertise helped me a lot in resolving this issue.

Thanks again for guiding me in each and every step. 

Regards,

Arun

Absolute pleasure Arun.

I glad we got it resolved.  Thanks for your patience and dedication.

Regards,

Adam

PS, Thanks for marking the question as resolved.

Sure Aradford!! . Thanks a lot to you too.