Create CNAME Resource Record in Zone Runner

Problem this snippet solves:

This pyControl v2 script will create a CNAME Record in Zone Runner

How to use this snippet:

This pyControl v2 script will create a CNAME Record in Zone Runner

Usage:

Create-CNAME-Record.py

Code :

#!/bin/env python

'''
----------------------------------------------------------------------------
The contents of this file are subject to the "END USER LICENSE AGREEMENT FOR F5
Software Development Kit for iControl"; you may not use this file except in
compliance with the License. The License is included in the iControl
Software Development Kit.

Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
the License for the specific language governing rights and limitations
under the License.

The Original Code is iControl Code and related documentation
distributed by F5.

The Initial Developer of the Original Code is F5 Networks,
Inc. Seattle, WA, USA. Portions created by F5 are Copyright (C) 1996-2004 F5 Networks,
Inc. All Rights Reserved.  iControl (TM) is a registered trademark of F5 Networks, Inc.

Alternatively, the contents of this file may be used under the terms
of the GNU General Public License (the "GPL"), in which case the
provisions of GPL are applicable instead of those above.  If you wish
to allow use of your version of this file only under the terms of the
GPL and not to allow others to use your version of this file under the
License, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the GPL.
If you do not delete the provisions above, a recipient may use your
version of this file under either the License or the GPL.
----------------------------------------------------------------------------
'''
import sys
import pycontrol.pycontrol as pc
import time

#######################################################################################
# Example of how to create a Resource Record in Zone Runner (ex. in this case a CNAME) 
# This example passes in the  'fromurl' keyword as True (default is False), which tells pycontrol
# to fetch the WSDL from the remote BigIP.
# For more information:
# https://devcentral.f5.com/s/wiki/iControl.Management__ResourceRecord__add_cname.ashx
#######################################################################################


if pc.__version__.startswith('2.0'):
    pass
else:
    print "Requires pycontrol version 2.x!"
    sys.exit()

if len(sys.argv) < 4:
    print "Usage %s ip_address username password" % sys.argv[0]
    sys.exit()

a = sys.argv[1:]

b = pc.BIGIP(
        hostname = a[0],
        username = a[1],
        password = a[2],
        fromurl = True,
        wsdls = ['Management.ResourceRecord','Management.Zone', 'Management.View'])

# create short cut
r = b.Management.ResourceRecord

# Variables for USER to fill in
view = "external"
zone_name = "gslb.f5lab.com."
domain_name = "www.glslb.f5lab.com"
cname = "www.gslb2.f5lab.com"
ttl = 60

#Start creating required objects
viewzone_obj = r.typefactory.create('Management.ViewZone')
viewzone_obj.view_name = view
viewzone_obj.zone_name = zone_name

view_zone_seq = r.typefactory.create('Management.ViewZoneSequence')
view_zone_seq.item = viewzone_obj 


cname_object = r.typefactory.create('Management.CNAMERecord')
cname_object.domain_name = domain_name
cname_object.cname = cname
cname_object.ttl = ttl

cname_object_seq = r.typefactory.create('Management.CNAMERecordSequence')
cname_object_seq.item = cname_object
cname_object_seq_seq = r.typefactory.create('Management.CNAMERecordSequenceSequence')
cname_object_seq_seq.item = cname_object_seq


print "Creating CNAME RECORD: " 
print "     \"" + domain_name + "     " + str(ttl) + "     IN      " + cname + "\"\n"

try:
    r.add_cname( 
                view_zones = view_zone_seq, 
                cname_records = cname_object_seq_seq 
                 )
except Exception, e:
    print "Error adding Record %s" % cname_object
    print e



print "Retrieving Resource Records for zone \"" + zone_name + "\":\n"
get_rrs_output = []

try:
    get_rrs_output = r.get_rrs(
                        view_zones = view_zone_seq
                        )


except Exception, e:
    print "Error retrieving resource records %s" % zone_name
    print e

for element in get_rrs_output:
    for resource_record in element:
        print resource_record
Updated Jun 06, 2023
Version 2.0

Was this article helpful?

1 Comment

  • Doesn't seem to work. Just exits without an error and no CNAME shows up in the zone.

     

    UPDATE: actually, i had the entries reversed. If you reverse the entries it very silently fails.