Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from remoteobjects import RemoteObject, fields, ListObject
- import splunk.auth as au
- import splunk.entity as en
- import time
- #
- # base remoteable model class
- #
- class SplunkdObject(RemoteObject):
- '''
- Defines base remotable splunkd EAI object
- '''
- # set common fields
- name = fields.Field()
- # internals
- _cachedEntity = None
- def get_entity_class(self):
- '''
- Returns the entity class path for a splunkd object; same as the
- entityClass used in splunk.entity
- '''
- raise NotImplementedError
- @classmethod
- def get(cls, url, http=None, **kwargs):
- '''
- Returns an existing SplunkdObject
- '''
- self = cls()
- self._cachedEntity = en.getEntity(
- self.get_entity_class(),
- url,
- namespace=kwargs.get('namespace'),
- owner=kwargs.get('owner'))
- self.update_from_entity(self._cachedEntity)
- return self
- def update_from_entity(self, entity):
- '''
- Populates the current object from a splunk.entity.Entity object
- '''
- # grab all the iterable properties
- convertedDict = dict(entity)
- # then insert the named properties
- for k in ['name']:
- convertedDict[k] = getattr(entity, k)
- super(self.__class__, self).update_from_dict(convertedDict)
- def post(self, obj=None, http=None):
- '''
- Commits the current splunkd object
- '''
- objParams = self.to_dict()
- self.update_to_entity(objParams)
- en.setEntity(self._cachedEntity)
- return True
- def update_to_entity(self, d):
- '''
- Updates the linked entity object with current model properties
- This is the inverse of udpate_from_entity
- '''
- # set iterable properties
- for k in d:
- # a bit of an issue: we can't have a 'name' key here, otherwise EAI will try to create new
- if k == 'name':
- continue
- self._cachedEntity[k] = d[k]
- # set named properties
- self._cachedEntity.name = self.name
- #
- # model classes
- #
- class SavedSearch(SplunkdObject):
- name = fields.Field()
- description = fields.Field()
- alertSchedule = fields.Field(api_name='cron_schedule')
- modifyDate = fields.Datetime(api_name='request.ui_dispatch_app')
- def get_entity_class(self):
- return 'saved/searches'
- class WorkflowAction(SplunkdObject):
- displayLocation = fields.Field()
- fieldList = fields.Field(api_name='fields')
- label = fields.Field()
- linkMethod = fields.Field(api_name='link.method', default='get')
- linkTarget = fields.Field(api_name='link.target', default='blank')
- linkUri = fields.Field(api_name='link.uri')
- type = fields.Field()
- def get_entity_class(self):
- return 'admin/workflow-actions'
- #
- # examples
- #
- def sample_get_saved_search():
- ss = SavedSearch.get('Errors in the last 24 hours', namespace='search')
- print 'name: %s' % ss.name
- print 'desc: %s' % ss.description
- print 'email: %s' % ss.alertSchedule
- def sample_post_saved_search():
- ss = SavedSearch.get('Errors in the last 24 hours', namespace='search')
- print 'desc: %s' % ss.description
- ss.description = time.time()
- print 'desc (changed): %s' % ss.description
- ss.post()
- def sample_bad_validation():
- ss = SavedSearch.get('Errors in the last 24 hours', namespace='search')
- ss.modifyDate = 'not a date'
- ss.post()
- def sample_workflow():
- w = WorkflowAction.get('ifx', namespace='search')
- print 'fields: %s' % w.fieldList
- print 'label: %s' % w.label
- print 'link.method: %s' % w.linkMethod
- if __name__ == '__main__':
- au.getSessionKey('admin','changeme')
- sample_workflow()
- sample_get_saved_search()
- sample_post_saved_search()
- sample_bad_validation()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement