Introduction
In the NetApp OnCommand Insight (OCI) Data Warehouse world, it was fairly standard to integrate with the MySQL OCI-DWH by running queries from an external client (like Splunk for example.) In the NetApp Cloud Insights (CI) world, over the REST API ODATA interface, there is another way of doing it; create custom SQL queries in CI, and the integration simply calls that custom query (one API call). So how do we do this?
0) Setup
Notes: 1) We're just running things directly in the Python Client, no creating scripts here. 2) Update the highlighted bits as per your CI tenant.
- import requests
- import urllib3
- urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
- apiKey = "YOUR_API_KEY"
- yourTenant = "aaXXXX.bYY-cc-Z"
- baseUrl = "https://" + yourTenant + ".cloudinsights.netapp.com/rest/v1/"
- headers = {'X-CloudInsights-ApiKey': '{key}'.format(key=apiKey)}
1) Seeing What Custom SQL Queries Are Already in the CI DWH (there may be none)
To see the Custom Queries that are currently in the CI DWH, run:
- api = "dwh-management/odata/dwh_custom/custom_queries"
- url = baseUrl + api
- jsondata = requests.get(url, headers=headers, verify = False).json()
- print(jsondata)
The json format isn't super human readable, but we can make it more readable with the following.
To print out the odata.context (the full API URL):
- print(jsondata['@odata.context'])
To print out the list of queries:
- i = 0
- for query in jsondata['value']:
- print(str(i) + ":" + query['queryName'])
- i += 1
- print(jsondata['value'][i]['querySql'])
2) How to We Upload a Custom Query to CI?
The method changes to POST, and the BODY needs two fields - queryName and querySql, like so:
{
"queryName": "test_query_1",
"querySql": "SELECT * FROM dwh_inventory.storage"
}
To create our very simple Custom Query in the CI DWH, run:
- api = "dwh-management/odata/dwh_custom/custom_queries"
- url = baseUrl + api
- body = {
- "queryName": "test_query_1",
- "querySql": "SELECT * FROM dwh_inventory.storage"
- }
- postdata = requests.post(url, headers=headers, json=body, verify = False)
3) How to We Run a Custom Query in CI?
To execute the custom query, it is simply (replace test_query_1 with the name of the query):
- api = "dwh-management/odata/dwh_custom/test_query_1"
- url = baseUrl + api
- jsondata = requests.get(url, headers=headers, verify = False).json()
And similar to the above you'll find the table output in 'value':
- i = 0
- for query in jsondata['value']:
- print(str(i) + ":" + str(query))
- i += 1
You will need to pass the data as per requirements.
4) How do We Delete a Custom Query in CI?
To execute the custom query, it is simply (replace test_query_1 with the name of the query):
- api = "dwh-management/odata/dwh_custom/custom_queries('test_query_1')"
- url = baseUrl + api
- delQuery = requests.delete(url, headers=headers, verify = False)
Response [200] = success!
Comments
Post a Comment