Reply
Highlighted

Can this be modified to delete instead of post

[ Edited ]
mrthingfish
Not applicable
Posts: 0
1141     0

I'm looking at an example file called a-record-create.py and am wondering if it could be modified to delete an A record instead of post/create. The section I'm looking at is:

r = requests.post(url + 'record:a',
                  data=json.dumps(req_data),
                  auth=(id, pw),
                  verify=valid_cert)

Could I just change the requests.post to requests.delete? ie

r = requests.delete(url + 'record:a',
                  data=json.dumps(req_data),
                  auth=(id, pw),
                  verify=valid_cert)

 

Below is the entire example code. Obviously I would need to change the "if r.status_code != requests.codes.created:" section.

#!/usr/local/bin/python

"""
Create A record at a specific address and next available address.

To use this script change the 'url' variable to contain the domain
name or IP address of a grid member supporting WAPI modify access
(grid master or cloud platform appliance), and change the 'id'
variable to contain a userid with WAPI access and the needed
permissions. (The script will prompt for the password when run.)

If the WAPI grid member uses a self-signed TLS/SSL certificate then
set the variable 'valid_cert' to False.

This script should work for NIOS 6.8.2 and later (WAPI 1.2.1 and
later).
"""

# Import the required Python modules.
import requests
import json
import getpass
import sys

# Set parameters to access the NIOS WAPI.
url = 'https://gm.example.com/wapi/v2.1/'
id = 'api'
valid_cert = False  # True if grid member has valid certificate

# Prompt for the API user password.
pw = getpass.getpass('Password for user ' + id + ': ')

# Create an A record at a specific IP address.
fqdn = 'test1.example.com'
dns_view = 'internal'
address = '192.168.0.15'
req_data = {'name': fqdn,
            'ipv4addr': address,
            'view': dns_view}
r = requests.post(url + 'record:a',
                  data=json.dumps(req_data),
                  auth=(id, pw),
                  verify=valid_cert)
if r.status_code != requests.codes.created:
    print r.text
    exit_msg = 'Error {} creating A record: {}'
    sys.exit(exit_msg.format(r.status_code, r.reason))
results = r.json()
print 'Results: ', results
print 'Created A record "' + fqdn + \
      '" pointing to "' + address + '"'

# Create an A record at the next available address, return address.
# Note 1: This requires WAPI 1.2.1 or later (NIOS 6.8.2 or later).
# Note 2: We reauthenticate using id/pw, but could have used cookie.
fqdn = 'test2.example.com'
dns_view = 'internal'
address = 'func:nextavailableip:192.168.0.0/24'
req_data = {'name': fqdn,
            'ipv4addr': address,
            'view': dns_view}
req_params = {'_return_fields': 'ipv4addr'}
r = requests.post(url + 'record:a',
                  params = req_params,
                  data=json.dumps(req_data),
                  auth=(id, pw),
                  verify=valid_cert)
if r.status_code != requests.codes.created:
    print r.text
    exit_msg = 'Error {} creating A record at available address: {}'
    sys.exit(exit_msg.format(r.status_code, r.reason))
results = r.json()
print 'Results: ', results
print 'Created A record "' + fqdn + \
      '" pointing to "' + results['ipv4addr'] + '"'

 

 

 

 

#!/usr/local/bin/python

"""
Create A record at a specific address and next available address.

To use this script change the 'url' variable to contain the domain
name or IP address of a grid member supporting WAPI modify access
(grid master or cloud platform appliance), and change the 'id'
variable to contain a userid with WAPI access and the needed
permissions. (The script will prompt for the password when run.)

If the WAPI grid member uses a self-signed TLS/SSL certificate then
set the variable 'valid_cert' to False.

This script should work for NIOS 6.8.2 and later (WAPI 1.2.1 and
later).
"""

# Import the required Python modules.
import requests
import json
import getpass
import sys

# Set parameters to access the NIOS WAPI.
url = 'https://gm.example.com/wapi/v2.1/'
id = 'api'
valid_cert = False  # True if grid member has valid certificate

# Prompt for the API user password.
pw = getpass.getpass('Password for user ' + id + ': ')

# Create an A record at a specific IP address.
fqdn = 'test1.example.com'
dns_view = 'internal'
address = '192.168.0.15'
req_data = {'name': fqdn,
            'ipv4addr': address,
            'view': dns_view}
r = requests.post(url + 'record:a',
                  data=json.dumps(req_data),
                  auth=(id, pw),
                  verify=valid_cert)
if r.status_code != requests.codes.created:
    print r.text
    exit_msg = 'Error {} creating A record: {}'
    sys.exit(exit_msg.format(r.status_code, r.reason))
results = r.json()
print 'Results: ', results
print 'Created A record "' + fqdn + \
      '" pointing to "' + address + '"'

# Create an A record at the next available address, return address.
# Note 1: This requires WAPI 1.2.1 or later (NIOS 6.8.2 or later).
# Note 2: We reauthenticate using id/pw, but could have used cookie.
fqdn = 'test2.example.com'
dns_view = 'internal'
address = 'func:nextavailableip:192.168.0.0/24'
req_data = {'name': fqdn,
            'ipv4addr': address,
            'view': dns_view}
req_params = {'_return_fields': 'ipv4addr'}
r = requests.post(url + 'record:a',
                  params = req_params,
                  data=json.dumps(req_data),
                  auth=(id, pw),
                  verify=valid_cert)
if r.status_code != requests.codes.created:
    print r.text
    exit_msg = 'Error {} creating A record at available address: {}'
    sys.exit(exit_msg.format(r.status_code, r.reason))
results = r.json()
print 'Results: ', results
print 'Created A record "' + fqdn + \
      '" pointing to "' + results['ipv4addr'] + '"'

 

Re: Can this be modified to delete instead of post

TTiscareno Community Manager
Community Manager
Posts: 340
1142     0

It looks like this was posted under the wrong forums and should have been submitted under API Integrations. Within the API Integrations forurm, you will find a thread titled "The definitive list of REST examples" (https://community.infoblox.com/t5/API-Integration/The-definitive-list-of-REST-examples/m-p/1214#M2). This thread includes examples for how to delete objects using the API and should help you with updating your script to do what you are looking for here.

Regards,
Tony

Re: Can this be modified to delete instead of post

spenumaka Community Manager
Community Manager
Posts: 60
1142     0

Thanks, Tony. I moved to the API discussion forum.

Srinivas 

Community Manager

--------------------------------------
Check out our new Tech docs website for latest documentation on Infoblox products.

Re: Can this be modified to delete instead of post

Adviser
Posts: 84
1142     0

Hi,

 

For DELETE operations, you would need to add the object reference ID to the WAPI call.

 

Here is a sample python script, where I have highlighted the reference ID.

import requests
requests.packages.urllib3.disable_warnings()  # Disable SSL warnings in requests #
url = "https://127.0.0.1/wapi/v2.7/record:a/ZG5zLmhvc3QkLl9kZWZhdWx0LmNvbS5pbmZvLmhvc3Qx:server.info.com/default?_return_as_object=1"
headers = {'content-type': "application/json"}
response = requests.request("DELETE", url, auth=('admin', 'Infoblox'), headers=headers, verify=False)
print(response.text)

I've modified the sample code you have posted to first query the A record, fetch the object reference, and then delete it. Hope this helps.

"""
Delete A record with a specific name.

To use this script change the 'url' variable to contain the domain
name or IP address of a grid member supporting WAPI modify access
(grid master or cloud platform appliance), and change the 'id'
variable to contain a userid with WAPI access and the needed
permissions. (The script will prompt for the password when run.)

If the WAPI grid member uses a self-signed TLS/SSL certificate then
set the variable 'valid_cert' to False.

This script should work for NIOS 6.8.2 and later (WAPI 1.2.1 and
later).
"""

# Import the required Python modules.
import requests
import json
import getpass
import sys

# Set parameters to access the NIOS WAPI.
url = 'https://grid-master/wapi/v2.1/'
id = 'api'
valid_cert = False  # True if grid member has valid certificate

# Prompt for the API user password.
pw = getpass.getpass('Password for user ' + id + ': ')

# Get an A record by name to fetch the object reference ID.
a_record = 'a1.demo.com'
r = requests.get(url + 'record:a?name=' + a_record,
                  auth=(id, pw),
                  verify=valid_cert)
if r.status_code != requests.codes.ok:
    print r.text
    exit_msg = 'Error {} fetching A record: {}'
    sys.exit(exit_msg.format(r.status_code, r.reason))
results = r.json()
print 'Results: ', results

# Delete the A record with the reference ID
ref_id = results[0]['_ref']
print 'Object_ID: ',ref_id
r = requests.delete(url + ref_id,
                  auth=(id, pw),
                  verify=valid_cert)
if r.status_code != requests.codes.ok:
    print r.text
    exit_msg = 'Error {} deleting A record: {}'
    sys.exit(exit_msg.format(r.status_code, r.reason))
results = r.json()
print 'Successfully deleted: ', results
Showing results for 
Search instead for 
Do you mean 

Recommended for You