In the past, we have shown you how to leverage Skyline Insights API using Curl with shell scripting and Powershell. Here let’s look at the same task using Python. The formula is still the same. You will need to use your API token to get your Access Token. Then you can query Skyline for the desired results. The only difference here is syntax. Let’s look at this together.
For all python code you need the basic structure. Here it is.
1 2 3 4 5 6 7 8 9 10 11 12 |
#!/usr/bin/env python import os import sys import json import requests def main(): ADD CODE HERE if __name__ == '__main__': main() |
In our case, we need to add an additional section to suppress insecure request warning. It looks like this. We can drop these lines after the “import” section and before the “def main()” section.
1 2 |
from urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning) |
Now that we have the basic structure defined, let’s look at the actual Skyline Insights API tasks. First, get the Access Token using the API Token. In order to do so, we need to contact console.cloud.vmware.com using this:
1 2 3 4 5 6 7 8 9 10 11 |
myHeaders = { 'accept' : 'application/json', 'Content-Type' : 'application/x-www-form-urlencoded'} myData = { 'refresh_token' : 'YOUR-API-TOKEN-HERE' } skyline_url = "https://console.cloud.vmware.com/csp/gateway/am/api/auth/api-tokens/authorize?grant_type=refresh_token" response = requests.post(skyline_url, data=myData, headers=myHeaders, verify=False) response_json = response.json() access_token = response_json['access_token'] |
Now that you have your Access Token, we can start to get the Findings detail for your organization. We will now go directly to skyline.vmware.com to get the data. Your json file may vary based on what you want to display. I have added the “pretty” section so that the displayed results can be easily read.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
myHeaders['Authorization']= 'Bearer ' + access_token myHeaders['Content-Type']= 'application/json' i_url = "https://skyline.vmware.com/public/api/data" myData = {'query':"""{ activeFindings(limit: 200) { findings { findingId accountId products findingDisplayName severity findingDescription findingImpact recommendations kbLinkURLs recommendationsVCF kbLinkURLsVCF categoryName findingTypes firstObserved totalAffectedObjectsCount } totalRecords timeTaken } }""" } i_res = requests.post(i_url, data=json.dumps(myData), headers=myHeaders, verify=False) pretty = json.dumps(i_res.json(), indent=2) print (pretty) |
To put all of this together, the entire code will look like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#!/usr/bin/env python import os import sys import json import requests from urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning) def main(): myHeaders = { 'accept' : 'application/json', 'Content-Type' : 'application/x-www-form-urlencoded'} myData = { 'refresh_token' : 'a93hJph2RgnEwfxsAnLPfx5nCKDbYC007z62bV0tCQ6UwcQLDQpuePJV8rok7tSb' } skyline_url = "https://console.cloud.vmware.com/csp/gateway/am/api/auth/api-tokens/authorize?grant_type=refresh_token" response = requests.post(skyline_url, data=myData, headers=myHeaders, verify=False) response_json = response.json() access_token = response_json['access_token'] myHeaders['Authorization']= 'Bearer ' + access_token myHeaders['Content-Type']= 'application/json' i_url = "https://skyline.vmware.com/public/api/data" myData = {'query':"""{ activeFindings(limit: 200) { findings { findingId accountId products findingDisplayName severity findingDescription findingImpact recommendations kbLinkURLs recommendationsVCF kbLinkURLsVCF categoryName findingTypes firstObserved totalAffectedObjectsCount } totalRecords timeTaken } }""" } i_res = requests.post(i_url, data=json.dumps(myData), headers=myHeaders, verify=False) pretty = json.dumps(i_res.json(), indent=2) print (pretty) if __name__ == '__main__': main() |
I hope this helps you use the Skyline Insights API with Python. Please feel free to review other blog posts and convert them from Curl or Powershell over to Python. We will continue to post more blogs to help you do more with less.
Comments