{unity connection api: schedules}

Recently, I’ve had the notion to create some automation tasks for Unity Connection (9.x) using the provided API (apparently it (the api) now goes back to 7.x) that uses REST..I’ve blogged about this before even..in Resetting a User’s VM PIN. Why am I spending time creating automation you might ask? The tasks that must be performed on a week by week basis (for the next 49 weeks) includes: Create a Class of Service, Directory Handler, Schedule (and Schedule Detail of course), GDMs, 2 Call Handlers, a User Template and a partridge in a pair tree..MULTIPLIED by 4..I’ve been doing all this for the past 6 weeks..all by hand..Today I’m going to briefly discuss automating the task of creating Schedules..(the actual Holiday Schedules are already configured..but and this doesn’t show you how to create a Holiday Schedule..but if you follow..then you might get the notion and/or no-how to do this 🙂

Before moving on with this, a few disclaimers must be added..this post is really about getting you started with creating Unity Schedules in a bulk fashion..I’m not sure there is any “built-in” methods to do this within Unity itself..unless you use COBRAS (and modify the CSV file?). My other intent is to share some code that might spawn ideas on how to do other interesting things using the API..

Diving right in, the first thing you have to do to create a schedule is get the Unity Location ObjectId; if you are “writing to” the Connect APIs you will need to get used to ObjectIds which is a Globally Unique ID (guid).. used for every object on the system..and you will have to parse it out of “documents” or returns from API calls routinely..To create a schedule here is a list of the items you need to do:

  1. Get the Unity Location Object
  2. Create the ScheduleSet
  3. Create the Schedule
  4. Apply the Schedule/Holiday as a ScheduleSet Members (one at a time)
  5. Create the Schedule Detail for the Schedule

If you are trying to learn Python, most of the blogs I’ve written (besides the contact center express ones) have demonstrated Python code..and I can’t express this enough..you really should get used to the Requests library as this is the one Python library that is most popular amoungst those perform Web Service calls..Below is the first step in the process of creating a schedule.

# Get the LocationObject
# GET /vmrest/locations/connectionlocations
objid = json.loads(req.content)['ConnectionLocation']['ObjectId']
#We Want JSON
headers = {'Accept' : 'application/json' }
#The Request Code:
req = requests.get(url,verify=False,headers=headers,auth=(user,pwd))

Create the ScheduleSet and the Schedule (these are essentially performed the same..the url is different..)..if there is a drawback of the API..this might be listed as one..unless I sat down with the Web Service development team and discussed the design decisions that were made..Why do I say this?..I will show you..and what I will show you..are Artifacts in the DB that are essentially abandoned..

Whenever a Schedule is created from the GUI, 2 Objects are actually created. The Schedule itself and a ScheduleSet let me demonstrate (however that works). So I created: GUI_Created_Schedule and you can check that Object out in the Database (OS Administration) twice..in the Schedule Table and the ScheduleSet Table.

admin:run cuc dbquery unitydirdb select objectid from tbl_Schedule WHERE DisplayName='GUI_Created_Schedule'

The return:

objectid
------------------------------------
abbd0501-6121-4cf2-ace7-1c44a84db169

If you want to get the ObjectId of ScheduleSet Object then just change the tbl to ScheduleSet(the ID is unique of course). Now..to the issue..if your code blows up..Let’s say I delete the ScheduleSet Object from above:

admin:run cuc dbquery unitydirdb delete from tbl_ScheduleSet WHERE DisplayName='GUI_Created_Schedule'
Rows: 1
#Indicates that it deleted 1 Row from the appropriate Table..

So as I mentioned..When a Schedule is created from the GUI, two objects are created..but I just removed one of them using a Query DB statement (I could have shown you an api way to do it too: request.delete). So the schedule is gone right?

AbandonedSchedule

As you can see above, the Schedule above has been officially abandoned and cannot be deleted from the GUI..Try it yourself 🙂 . Obviously you cannot delete the ScheduleSet from the GUI..it’s not really there. There are a few ways to fix the issue from above..you could create another ScheduleSet and then add the Schedule as a Member..For the rest of this post I’m going to piece together the code needed to “Complete The Schedule.”

Let’s get back to it..as I squirreled myself a few paragraph ago..when the abandonment of objects in the Unity Connection DB. First..we need to add to the Headers..which is a Python Dictionary Type (a “JSON” object). We also have to address the result of the Headers..when we get response data back from the server (cuc) we need to parse the JSON (which is converted to DICT type). And whenever data is “posted” (or “put”) on the server the DICT type has to be converted to JSON. Other points of interest when working with this API is how the ObjectId’s are passed for creating/updating objects and how they are returned. For the sake of Schedules I decided to Use Regular Expressions to parse the “content” of some of the data. (All these examples are below).

headers = {
           'Content-type' : 'application/json',
           'Accept'       : 'application/json'
          }
cucloc_json = json.loads(cuc_req.content)
sset_obj = {'ScheduleSetObjectId' : 'objid'}
data = json.dumps(sset_obj)
import re
parsestr = '/vmrest/users/f82b301d-1ec8-44c6-a3ee-0012269439bf'
regx = '/\w+/\w+/(\S+)'
objid = re.search(r''+regx+'',parsestr).group(1)
objid
'f82b301d-1ec8-44c6-a3ee-0012269439bf'

For the code above utilizing reguarlar expressions..you can input those lines into the python interpreter (>>>) to see how it works..I’m going to terminate this post..by leaving you with the code 🙂

cuc_schedule.py

return sam;

One response to “{unity connection api: schedules}

  1. Pingback: {unity connection api: schedules} | Dinesh Ram Kali.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s