def do_get(self, **conditions): ''' Get one device information from RS Params: conditions: The conditions for device selecting. Return: One dict with device information. Exception message ''' if self.m_udid: # search by udid resource = DEVRES_TEMPLATE_STR % (self.m_ver, self.m_udid) method = 'GET' post_data = None else: # without udid if conditions: resource = DEVSEARCH_TEMPLATE_STR % self.m_ver method = 'POST' post_data = '{"base":"ou=devices", "filter":"(&(objectClass=mobileDevice)' for cond in conditions: temp_str = '(%s=%s)' % (str(cond), str(conditions[cond])) post_data = post_data + temp_str post_data = ''.join([post_data,')"}']) else: raise Exception('Require device UDID or conditions for searching!') logger.debug('Get one device info - resource:%s' % (resource)) wrest = HostedAccess() devinfo = wrest.do_access(resource, method, data=post_data, headers=None) del wrest return devinfo
def do_insert(self, devinfo=None): ''' Insert one new device information to RS Params: devinfo: Detail information of the device. Return: Instance object of class RestResult; Exception message ''' if not self.m_udid: raise Exception('Require device UDID!') resource = DEVRES_TEMPLATE_STR % (self.m_ver, self.m_udid) if isinstance(devinfo, dict): temp = devinfo elif isinstance(devinfo, basestring): temp = json.loads(devinfo) else: raise Exception('Invalid input!') temp = {'attributes':temp} data = json.dumps(temp) wrest = HostedAccess() logger.debug('Insert new device info - resource:%s, data:%s' % (resource, data)) retval = wrest.do_access(resource, 'PUT', data=data, headers=None) del wrest return retval
def do_update(self, udid=None, attributes=None): ''' Update one device information to RS Params: attributes: Attributes of the devicd information. Return: Instance object of class RestResult; Exception message ''' if udid: udid_str = str(udid) else: udid_str = self.m_udid if not udid_str: raise Exception('Require device UDID!') resource = DEVRES_TEMPLATE_STR % (self.m_ver, udid_str) if isinstance(attributes, list): attr = json.dumps(attributes) elif isinstance(attributes, basestring): attr = attributes else: raise Exception('Invalid input!') content = { 'method':'modify', 'params':attributes, } data = json.dumps(content) wrest = HostedAccess() logger.debug('Update device info - resource:%s, data:%s' % (resource, data)) retval = wrest.do_access(resource, 'POST', data=data, headers=None) del wrest return retval
def do_get_many(self, idx, max, sortby, **conditions): ''' Get devices' information from RS Params: idx: Page index number. (0 ~ ) max: Page size. sortby: Sort condition string. conditions: The conditions for device selecting. Return: One dict with devices information. Exception message ''' offset_val = idx * max + 1 after_val = max - 1 vlv_str = '{"sort":["%s"],"vlv":{"before":0,"after":%d,"offset":%d},' % (sortby, after_val, offset_val) resource = DEVSEARCH_TEMPLATE_STR % self.m_ver method = 'POST' post_data = ''.join([vlv_str, '"base":"ou=devices", "filter":"(&(objectClass=mobileDevice)']) if conditions: for k,v in conditions.iteritems(): cond_str = '(%s=%s)' % (k, str(v)) post_data = ''.join([post_data, cond_str]) post_data = ''.join([post_data, ')"}']) logger.debug('Get %d devices info - resource:%s, data:%s' % (max, resource, repr(post_data))) wrest = HostedAccess() devinfo = wrest.do_access(resource, method, data=post_data, headers=None) del wrest return devinfo
def do_bulk_op(self, content): ''' Bulk add or/and update device information into RS Params: content: data in a specific format, generated by format_bulk_data Return: instance object of class RestResult; exception message ''' if isinstance(content, list): data = json.dumps(content) elif isinstance(attributes, basestring): data = content else: raise Exception('Invalid content!') resource = BULK_OP_URL % self.m_ver wrest = HostedAccess() logger.debug('bulk operation info - resource:%s, data:%s' % (resource, data)) retval = wrest.do_access(resource, 'POST', data=data, headers=None) del wrest return retval
def get_devices_by_owner(self, deviceowner): ''' get devices by device owner ''' if deviceowner: resource = DEVSEARCH_TEMPLATE_STR % self.m_ver method = 'POST' post_data = '{"base":"ou=devices", "filter":"(&(objectClass=mobileDevice)(deviceOwner=%s))"}' % deviceowner else: raise Exception('Require device owner for searching!') logger.debug('Get devices by device owner - resource:%s' % (resource)) wrest = HostedAccess() devinfo = wrest.do_access(resource, method, data=post_data, headers=None) del wrest return devinfo
class HostedOS(object): """ this class implements to access hosted OS """ def __init__(self): self.m_wrest = HostedAccess() pass def get_object(self, objName=None, namespace='mobile', account=1, version=1): """ get an object from OS Params: objName: Name of the object namespace: Name of the namespace account: account ID version: OS version Return: Instatnce of RestResult """ try: if objName: resource = HOSTED_OS_OBJECT % (version, account, namespace, objName) logger.debug('Get object from OS - resource:%s' % resource) return self.m_wrest.do_access(resource, 'GET', data=None, headers=None) else: raise Exception('Invalid parameters.') except Exception, e: logger.error('access hosted OS error: %s' % repr(e)) raise e
class HostedAPP(object): """ this class implements to access hosted app service """ def __init__(self): self.m_wrest = HostedAccess() pass def get_appinfo(self, app_id, account=1, version=1): """ get an object from app service Params: account: account ID version: app service version Return: Instatnce of RestResult """ try: if app_id: resource = "/appservice/v-%d/app/%s" % (version, app_id) logger.debug('Get app info from app service - resource:%s' % resource) return self.m_wrest.do_access(resource, 'GET', data=None, headers=None) else: raise Exception('Invalid parameters.') except Exception, e: logger.error('access app service error error: %s' % repr(e)) raise e
class Hosted_category_id_name(object): def __init__(self): self.m_wrest = HostedAccess() pass def get_category_ID(self, category_id, category_dirname='Category', account=1, version=1): ''' Get an object from category server Params: Category: category ID Return: Instatnce of RestResult ''' try: if category_id: resource = "/ug/v-%d/%s?a=[%s]" % (version, category_dirname, category_id) logger.debug('Get category info from category service - resource: %s' % resource) return self.m_wrest.do_access(resource, 'GET', data=None, headers=None) else: raise Exception("Invalid parameters") except Exception as e: logger.error("Access category service error, error: %s", repr(e)) raise e """
class HostedAPP(object): """ this class implements to access hosted app service """ def __init__(self): self.m_wrest = HostedAccess() pass def get_appinfo(self, app_id, account=1, version=1): """ get an object from app service Params: account: account ID version: app service version Return: Instatnce of RestResult """ try: if app_id: resource = "/appservice/v-%d/app/%s" % (version, app_id) logger.debug('Get app info from app service - resource:%s' % resource) return self.m_wrest.do_access(resource, 'GET', data=None, headers=None) else: raise Exception('Invalid parameters.') except Exception, e: logger.error('access app service error error: %s' % repr(e)) raise e
class Hosted_category_user_name(object): def __init__(self): self.m_wrest = HostedAccess() pass def get_category_user(self, category_id, hosted_user='******', account=1, version=1): ''' Get an object from category service Params: category: category ID version: category service version Return: Instatnce of RestResult ''' try: if category_id: resource = '/ug/v-%d/%s?a=[%s]' % (version, hosted_user, category_id) logger.debug('Get category info from category service - resource: %s' % resource) return self.m_wrest.do_access(resource, 'GET', data=None, headers=None) else: raise Exception('Invalid parameters') except Exception as e: logger.error('Access category service error, error: %s' % repr(e)) raise e """
class HostedVpnInfo(object): def __init__(self): self.m_wrest = HostedAccess() pass def get_vpninfo(self, account_id, version=1, name='LocalNetworkAccessKey+LocalNetworkAccessValue'): ''' Get an object from VPN server Params: account_id: account id version: VPN server version Return: Instance of Restresult ''' try: if account_id: resource = '/rs/v-%d/account-%s?attributes=%s' % (version, account_id, name) logger.debug("Get vpn info from service - resource: %s" % resource) result = self.m_wrest.do_access(resource, "GET", data=None, headers=None) return result else: raise Exception("Invalid parameters") except Exception as e: logger.info("access vpn info error: %s" % repr(e)) return [] def search_vpninfo(self, search_param, version=1): ''' Get an section from VPN server Params: search_param: dict, query parameters version: VPN server version Return: instance of RestResult ''' try: resource = "/rs/v-%d/search" % version data = json.dumps(search_param) logger.debug("Search vpn info from service - resource: %s:" % resource) return self.m_wrest.do_access(resource, "POST", data=data, headers=None) except Exception as e: logger.error("Access vpn service error: %s" % repr(e)) raise e
def hosted_get_customer_account(account=1): ''' Get Websense customer account from hosted RS. Params: account: Account ID. Return: Websense customer account info Exception message ''' access_obj = HostedAccess() filter_rule = '{"base" : "account=%d", "filter" : "(objectclass=websenseCustomerAccount)"}' % int(account) resource = '/rs/v-1/search' result = access_obj.do_access(resource, 'POST', filter_rule) if result.code == 200: return result.content else: return ''
def hosted_get_customer_account(account=1): ''' Get Websense customer account from hosted RS. Params: account: Account ID. Return: Websense customer account info Exception message ''' access_obj = HostedAccess() filter_rule = '{"base" : "account=%d", "filter" : "(objectclass=websenseCustomerAccount)"}' % int( account) resource = '/rs/v-1/search' result = access_obj.do_access(resource, 'POST', filter_rule) if result.code == 200: return result.content else: return ''
def hosted_replace_rs_attr(key=None, value=None, account=1): ''' Replace RS attribute. Params: key: attribute name value: attribute value account: Account ID. Return: Websense customer account info Exception message ''' access_obj = HostedAccess() filter_rule = '{"method":"modify","params":[{"attribute":"%s", "type":"replace", "values":["%s"]}]}' % (key, value) resource = '/rs/v-1/account-%d' % int(account) result = access_obj.do_access(resource, 'POST', filter_rule) if result.code == 200: return True else: return False
def hosted_get_pacfileurl_by_email(account, email): ''' Get Pac file URL from hosted RS. Params: account: Account ID. email: user email. Return: Pac file URL Exception message ''' access_obj = HostedAccess() filter_rule = '{"base":"account=%d","filter":"(&(objectClass=hostedUser)(mail=%s))"}' % (int(account), email) resource = '/rs/v-1/search' result = access_obj.do_access(resource, 'POST', filter_rule) if result.code == 200: return result.content else: return '' pass
def hosted_get_pacfileurl(account=1, version=1): ''' Get Pac file URL from hosted RS. Params: account: Account ID. version: Version of the resource, default value 1. Return: Pac file URL Exception message ''' access_obj = HostedAccess() filter_rule = {"filter" : "objectclass=hostedwebpolicy"} resource = '/rs/v-%d/account-%d/search' % (int(version), int(account)) result = access_obj.do_access(resource, 'GET', filter_rule) if result.code == 200: return result.content else: return '' pass
def hosted_get_pacfileurl(account=1, version=1): ''' Get Pac file URL from hosted RS. Params: account: Account ID. version: Version of the resource, default value 1. Return: Pac file URL Exception message ''' access_obj = HostedAccess() filter_rule = {"filter": "objectclass=hostedwebpolicy"} resource = '/rs/v-%d/account-%d/search' % (int(version), int(account)) result = access_obj.do_access(resource, 'GET', filter_rule) if result.code == 200: return result.content else: return '' pass
def hosted_get_pacfileurl_by_email(account, email): ''' Get Pac file URL from hosted RS. Params: account: Account ID. email: user email. Return: Pac file URL Exception message ''' access_obj = HostedAccess() filter_rule = '{"base":"account=%d","filter":"(&(objectClass=hostedUser)(mail=%s))"}' % ( int(account), email) resource = '/rs/v-1/search' result = access_obj.do_access(resource, 'POST', filter_rule) if result.code == 200: return result.content else: return '' pass
def hosted_replace_rs_attr(key=None, value=None, account=1): ''' Replace RS attribute. Params: key: attribute name value: attribute value account: Account ID. Return: Websense customer account info Exception message ''' access_obj = HostedAccess() filter_rule = '{"method":"modify","params":[{"attribute":"%s", "type":"replace", "values":["%s"]}]}' % ( key, value) resource = '/rs/v-1/account-%d' % int(account) result = access_obj.do_access(resource, 'POST', filter_rule) if result.code == 200: return True else: return False
class HostedOS(object): """ this class implements to access hosted OS """ def __init__(self): self.m_wrest = HostedAccess() pass def get_object(self, objName=None, namespace='mobile', account=1, version=1): """ get an object from OS Params: objName: Name of the object namespace: Name of the namespace account: account ID version: OS version Return: Instatnce of RestResult """ try: if objName: resource = HOSTED_OS_OBJECT % (version, account, namespace, objName) logger.debug('Get object from OS - resource:%s' % resource) return self.m_wrest.do_access(resource, 'GET', data=None, headers=None) else: raise Exception('Invalid parameters.') except Exception, e: logger.error('access hosted OS error: %s' % repr(e)) raise e
class Hosted_category_user_name(object): def __init__(self): self.m_wrest = HostedAccess() pass def get_category_user(self, category_id, hosted_user='******', account=1, version=1): ''' Get an object from category service Params: category: category ID version: category service version Return: Instatnce of RestResult ''' try: if category_id: resource = '/ug/v-%d/%s?a=[%s]' % (version, hosted_user, category_id) logger.debug( 'Get category info from category service - resource: %s' % resource) return self.m_wrest.do_access(resource, 'GET', data=None, headers=None) else: raise Exception('Invalid parameters') except Exception as e: logger.error('Access category service error, error: %s' % repr(e)) raise e """
def __init__(self): self.m_wrest = HostedAccess() pass
def __init__(self): self.m_wrest = HostedAccess() pass
class HostedTaskQueue(object): ''' class HostedTaskQueue This class implements the access to TaskQueue Attributes: m_wrest: Instance object of class HostedAccess ''' def __init__(self): self.m_wrest = HostedAccess() pass def do_add(self, tqname=None, namespace='mobile', account=1, version=1, payload=None, tags=None, priority='low', delay=0): ''' Insert one task into the specific task queue. Params: tqname: Name of task queue. namespace: Namespace of the task queue. account: Account ID. version: Version of the resource, default value 1. payload: Base64 encode string. tags: Tags in the task. priority: Priority of the task. delay: If delay is set, the lease request will not see the added task in [delay] seconds. Return: Instance object of class RestResult; Exception message ''' assert(tqname) resource = TQRES_TEMPLATE_STR % (version, account, namespace, tqname) resource = '/'.join([resource, 'addtask']) if delay > 0: resource += '?delay=%s' % delay data = json.dumps({'payload_base64' : payload, 'tags' : tags, 'priority' : priority}, encoding="utf-8") logger.debug('Add task - resource:%s, data:%s' % (resource, data)) return self.m_wrest.do_access(resource, 'POST', data=data, headers=None) def do_bulk_add(self, tqname=None, namespace='mobile', account=1, version=1, taskslist=[]): ''' Insert tasks into the specific task queue. Params: tqname: Name of task queue. namespace: Namespace of the task queue. account: Account ID. version: Version of the resource, default value 1. taskslist: task structures in list. For example: [ { "payload_base64": "TXkgbGl0dGxlIHRhc2sK", "tags": ["state=red", "watchthis", "code=41"], "priority" : "high" }, ... ] Return: Instance object of class RestResult; Exception message ''' #check data is available assert(tqname) assert(isinstance(taskslist, list)) for task in taskslist: assert(isinstance(task, dict)) resource = TQRES_TEMPLATE_STR % (version, account, namespace, tqname) resource = '/'.join([resource, 'addtasks']) data = json.dumps(taskslist, encoding="utf-8") logger.debug('Add %d tasks - resource:%s, data:%s' % (len(taskslist), resource, data)) return self.m_wrest.do_access(resource, 'POST', data=data, headers=None) def do_delete(self, tqname=None, namespace='mobile', account=1, version=1, taskname=0): ''' Delete task from specific task queue. Params: tqname: Name of task queue. namespace: Namespace of the task queue. account: Account ID. version: Version of the resource, default value 1. taskname: Name of the specific task. Return: Instance object of class RestResult; Exception message ''' assert(tqname) resource = TQRES_TEMPLATE_STR % (version, account, namespace, tqname) temp = 'task-%s' % taskname resource = '/'.join([resource, temp]) logger.debug('Delete task - resource:%s' % (resource)) return self.m_wrest.do_access(resource, 'DELETE', data=None, headers=None) def do_bulk_delete(self, tqname=None, namespace='mobile', account=1, version=1, numslist=[]): ''' Delete tasks by bulk mode from specific task queue. Params: tqname: Name of task queue. namespace: Namespace of the task queue. account: Account ID. version: Version of the resource, default value 1. numslist: List of task ids Return: Instance object of class RestResult; Exception message ''' assert(tqname) assert(isinstance(numslist, list)) resource = TQRES_TEMPLATE_STR % (version, account, namespace, tqname) resource = '/'.join([resource, 'deletetasks']) data = json.dumps(numslist, encoding="utf-8") logger.debug('Delete %d tasks - resource:%s, data:%s' % (len(numslist), resource, data)) return self.m_wrest.do_access(resource, 'POST', data=data, headers=None) def do_update(self, tqname=None, namespace='mobile', account=1, version=1, taskname=0, payload=None, tags=None, priority='low', enqueuetime=''): ''' Modify seciton value of one task. Params: tqname: Name of task queue. namespace: Namespace of the task queue. account: Account ID. version: Version of the resource, default value 1. taskname: Name of the specific task. payload: Base64 encode string. tags: Tags in the task. priority: Priority of the task. enqueuetime: Last modify time. Return: Instance object of class RestResult; Exception message ''' assert(tqname) assert(payload and tags) resource = TQRES_TEMPLATE_STR % (version, account, namespace, tqname) temp = 'task-%s' % taskname resource = '/'.join([resource, temp]) data = json.dumps({'payload_base64' : payload, 'tags' : tags, 'priority' : priority}, encoding="utf-8") header = self.m_wrest.generate_default_header() header.update({'If-Unmodified-Since' : enqueuetime}) logger.debug('Update task - resource:%s, header:%s, data:%s' % (resource, header, data)) return self.m_wrest.do_access(resource, 'PUT', data=data, headers=header) def do_get(self, tqname=None, namespace='mobile', account=1, version=1, taskname=0): ''' Get one task information from the specific task queue. Params: tqname: Name of task queue. namespace: Namespace of the task queue. account: Account ID. version: Version of the resource, default value 1. taskname: Name of the specific task. Return: Instance object of class RestResult; Exception message ''' assert(tqname) resource = TQRES_TEMPLATE_STR % (version, account, namespace, tqname) temp = 'task-%s' % taskname resource = '/'.join([resource, temp]) logger.debug('Get task - resource:%s' % (resource)) return self.m_wrest.do_access(resource, 'GET', data=None, headers=None) def do_get_many(self, tqname=None, namespace='mobile', account=1, version=1, tasknum=0, tags=None): ''' Get a bulk of tasks' information from the specific task queue. Params: tqname: Name of task queue. namespace: Namespace of the task queue. account: Account ID. version: Version of the resource, default value 1. tasknum: Number of the tasks that expected. tags: Search conditions. Return: Instance object of class RestResult; Exception message ''' assert(tqname) resource = TQRES_TEMPLATE_STR % (version, account, namespace, tqname) resource = '/'.join([resource, 'search?numTasks=%s']) resource = resource % tasknum resource = '&'.join([resource, 'orderby=priority']) if tags: data = json.dumps({'tags' : tags}, encoding="utf-8") else: data = tags logger.debug('Get tasks - resource:%s, data:%s' % (resource, data)) return self.m_wrest.do_access(resource, 'POST', data=data, headers=None) def do_lease(self, tqname=None, namespace='mobile', account=1, version=1, tasknum=0, tags=None, leasesec=10): ''' Acquire a lease on the topmost {numTask} unowned tasks in the specified queue. Params: tqname: Name of task queue. namespace: Namespace of the task queue. account: Account ID. version: Version of the resource, default value 1. tasknum: Number of the tasks that expected. tags: Search conditions. Return: Instance object of class RestResult; Exception message ''' assert(tqname) resource = TQRES_TEMPLATE_STR % (version, account, namespace, tqname) resource = '/'.join([resource, 'lease?numTasks=%s']) resource = resource % tasknum resource = '&'.join([resource, 'leaseSecs=%s']) resource = resource % leasesec if tags: data = json.dumps({'tags' : tags}, encoding="utf-8") else: data = tags logger.debug('Lease tasks - resource:%s, data:%s' % (resource, data)) return self.m_wrest.do_access(resource, 'POST', data=data, headers=None) def do_get_taskqueue(self, tqname=None, namespace='mobile', account=1, version=1): ''' Get the specific taskqueue. If the task queue exists, return True, else return False. Params: tqname: Name of task queue. namespace: Namespace of the task queue. account: Account ID. version: Version of the resource, default value 1. Return: True: get task queue success. False: get task queue failed. ''' assert(tqname) resource = TQRES_TEMPLATE_STR % (version, account, namespace, tqname) logger.debug('Get one taskqueue - name:%s, resource:%s' % (tqname, resource)) try: result = self.m_wrest.do_access(resource, 'GET', data=None, headers=None) if result.code >= 200 and result.code < 300: logger.debug('Task Queue exists!') return True except MDMiHttpError, e: logger.debug('Task Queue NOT exists! %s' % str(e)) return False