UCSM Python SDK and UCS Platform Emulator 3.1

Video created by dsoper on May 24, 2016

    Here's an overview of setting up a basic server environment on the UCS Platform Emulator using the Python SDK.  Portions of the script used in the video are provided below.  For more information on the emulator and the Python SDK please see the following:

     

    Also, if you're headed to Cisco Live 2016 in Las Vegas, be sure to check out one of several sessions covering UCS Programmability and the Python SDK.  Here's a post with additional info: UCS Programmability at Cisco Live!

     

    Script used in the video:

     

    import csv

    from ucsmsdk import ucshandle

    from server import service_profile

    from ucsmsdk.mometa.ls.LsRequirement import LsRequirement

     

    if __name__ == "__main__":

        try:

            <snip>

            handle = ucshandle.UcsHandle(settings.ip, settings.user, settings.pw, secure=settings.secure)

            handle.login()

            '''

            apply settings from each row in the csv file

            '''

            csvFilename = sys.argv[1]

            csvFile = open(csvFilename, "r")

            if not csvFile:

                print "Error: could not open %s" % csvFilename

                sys.exit(1)

            csvReader = csv.DictReader(csvFile)

            rowNum = 1

            for row in csvReader:

                rowNum += 1

                if not row['template']:

                    print "Error on row %d: no template name found" % rowNum

                    continue

                template = row['template']

                profileName = row['profile']

                if not profileName:

                    profileName = "%s-instance-" % template

                numInstances = int(row['instances'])

                if not numInstances:

                    numInstances = 1

                print "Creating %s service profiles with name prefix %s" % (numInstances, profileName)

                parentDN = 'org-root'

                service_profile.sp_template_create(handle, name=template, type="initial-template",

                                                   ident_pool_name=row['UUID'],

                                                   parent_dn=parentDN)

                service_profile.sp_create_from_template(handle,

                                                        naming_prefix=profileName,

                                                        name_suffix_starting_number="1",

                                                        number_of_instance=numInstances,

                                                        sp_template_name=template,

                                                        in_error_on_existing="true",

                                                        parent_dn=parentDN)

                serverPool = row['Server-Pool'] if (row['Server-Pool']) else 'default'

                for instance in range(1, numInstances + 1):

                    dn = "%s/ls-%s%d" % (parentDN, profileName, instance)

                    mo = LsRequirement(parent_mo_or_dn=dn, restrict_migration="no",

                                       name=serverPool, qualifier="")

                    handle.add_mo(mo, True)

                    handle.commit()

            handle.logout()

     

     

        except Exception, err:

            print "Exception:", str(err)

            import traceback, sys

            print '-'*60

            traceback.print_exc(file=sys.stdout)

            print '-'*60