Friday, June 17, 2016

Using Python and "requests" to access the Free IPA API

For a few days, not I have needed a dynamic way to create DNS entries, or Host on my DNS/IDM provider.

So I set out on a quest to see if I could use python and the Free IPA API, to add dynamically created hosts (in say a cloud environment or IaaS platform), and update DNS or Host Records.

In my search I found a good article by Alexander Bokovoy that gave me the information I needed, to get started, and complete my desired goal.

Below is a sample of what I needed. 
#!/bin/python

#import http-parser 
import requests
import json

ipaurl="https://idm.example.com/ipa/"

session = requests.Session() 

resp = session.post('{0}session/login_password'.format(ipaurl),
   params="", data = {'user':'certadmin','password':'redhat'}, verify=False, 
   headers={'Content-Type':'application/x-www-form-urlencoded', 'Accept':'applicaton/json'})

header={'referer': ipaurl, 'Content-Type':'application/json', 'Accept':'application/json'}

create_host = session.post('{0}session/json'.format(ipaurl), headers=header,
    data=json.dumps({'id': 0, 'method': 'host_add', 
        'params': [[event['object']['spec']['host']], 
            {'force': True, 'ip_address': 192.168.1.101}]}), verify=False) 

print "    Host Create Return Code: {0}".format(create_host.status_code) 
 
This should create a host, entry in your IPA server, and set its IP address. As such allowing you to query the IPA server for DNS and resolve the proper IP of the host name that is created.

No comments:

Post a Comment

Its the little things (like opening a web browser)

Sometimes as your developing a solution/script for a problem your faced with interesting challenges where the dumbest workaround (opening a ...