[Python][CI-DWH][ODATA] Experimenting with Inserting and Deleting Data into the CI DWH Custom

In this post we try out inserting and deleting data in NetApp Cloud Insights dwh_custom tables. The INSERT/DELETE APIs are the same APIs used for creating and deleting custom queries.

The APIs

  • INSERT
    • POST ​/dwh-management​/odata​/{schema}​/**
    • Write data and create custom queries in Data Warehouse database through ODATA protocol.
  • DELETE
    • DELETE ​/dwh-management​/odata​/{schema}​/**
    • Delete data and delete custom queries in Data Warehouse database through ODATA protocol

Example

1) The Basic Connection Stuff

import requests
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
 apiKey = "YOUR_API_KEY"
yourTenant = "https://YOUR_TENANT.cloudinsights.netapp.com"
headers = {'X-CloudInsights-ApiKey': '{key}'.format(key=apiKey)}

2) GETting a dwh_custom table

api = "/rest/v1/dwh-management/odata/dwh_custom/country_codes"
j = requests.get(yourTenant + api,headers=headers,verify=False).json()
j['value'][0] # Get the first row (value)
for o in j['value']: print(o['country']) # Get the country for each row

3) Inserting a new row in the table

api = '/rest/v1/dwh-management/odata/dwh_custom/country_codes'
body = {
  'country': 'BARBADOS',
  'two': 'BB',
  'three': 'BRB'
}
p = requests.post(yourTenant+api, headers=headers, json=body, verify = False)
if p.status_code != 201: print("Failed to insert row!")

4) Verifying the new row is there

api = "/rest/v1/dwh-management/odata/dwh_custom/country_codes?&$filter=country eq 'BARBADOS'"
j = requests.get(yourTenant + api,headers=headers,verify=False).json()
j['value']

Note: How you use double and single quotes is important!

5) Deleting the new row

api = "/rest/v1/dwh-management/odata/dwh_custom/country_codes('BARBADOS')"
d = requests.delete(yourTenant + api, headers=headers, verify = False)
if d.status_code != 200: print("Failed to delete row! ",end = "\r\n\n")
else: print("Deleted row!")

6) Verify the row has been deleted

api = "/rest/v1/dwh-management/odata/dwh_custom/country_codes?&$filter=country eq 'BARBADOS'"
j = requests.get(yourTenant + api,headers=headers,verify=False).json()
j['value']

The output will be [] if BARBADOS has been deleted.

Note: We could also have use len() or $count to see if the row count goes up/down as we add/remove items. I think searching for the entry you’ve added/removed is the best checking method.


Comments