Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. from remoteobjects import RemoteObject, fields, ListObject
  2. import splunk.auth as au
  3. import splunk.entity as en
  4. import time
  5.  
  6.  
  7. #
  8. # base remoteable model class
  9. #
  10.  
  11. class SplunkdObject(RemoteObject):
  12. '''
  13. Defines base remotable splunkd EAI object
  14. '''
  15.  
  16. # set common fields
  17. name = fields.Field()
  18.  
  19. # internals
  20. _cachedEntity = None
  21.  
  22. def get_entity_class(self):
  23. '''
  24. Returns the entity class path for a splunkd object; same as the
  25. entityClass used in splunk.entity
  26. '''
  27. raise NotImplementedError
  28.  
  29.  
  30. @classmethod
  31. def get(cls, url, http=None, **kwargs):
  32. '''
  33. Returns an existing SplunkdObject
  34. '''
  35.  
  36. self = cls()
  37. self._cachedEntity = en.getEntity(
  38. self.get_entity_class(),
  39. url,
  40. namespace=kwargs.get('namespace'),
  41. owner=kwargs.get('owner'))
  42. self.update_from_entity(self._cachedEntity)
  43. return self
  44.  
  45.  
  46. def update_from_entity(self, entity):
  47. '''
  48. Populates the current object from a splunk.entity.Entity object
  49. '''
  50.  
  51. # grab all the iterable properties
  52. convertedDict = dict(entity)
  53.  
  54. # then insert the named properties
  55. for k in ['name']:
  56. convertedDict[k] = getattr(entity, k)
  57.  
  58. super(self.__class__, self).update_from_dict(convertedDict)
  59.  
  60.  
  61. def post(self, obj=None, http=None):
  62. '''
  63. Commits the current splunkd object
  64. '''
  65.  
  66. objParams = self.to_dict()
  67. self.update_to_entity(objParams)
  68. en.setEntity(self._cachedEntity)
  69.  
  70. return True
  71.  
  72.  
  73. def update_to_entity(self, d):
  74. '''
  75. Updates the linked entity object with current model properties
  76. This is the inverse of udpate_from_entity
  77. '''
  78.  
  79. # set iterable properties
  80. for k in d:
  81. # a bit of an issue: we can't have a 'name' key here, otherwise EAI will try to create new
  82. if k == 'name':
  83. continue
  84. self._cachedEntity[k] = d[k]
  85.  
  86. # set named properties
  87. self._cachedEntity.name = self.name
  88.  
  89.  
  90.  
  91. #
  92. # model classes
  93. #
  94.  
  95. class SavedSearch(SplunkdObject):
  96.  
  97. name = fields.Field()
  98. description = fields.Field()
  99. alertSchedule = fields.Field(api_name='cron_schedule')
  100. modifyDate = fields.Datetime(api_name='request.ui_dispatch_app')
  101.  
  102. def get_entity_class(self):
  103. return 'saved/searches'
  104.  
  105. class WorkflowAction(SplunkdObject):
  106.  
  107. displayLocation = fields.Field()
  108. fieldList = fields.Field(api_name='fields')
  109. label = fields.Field()
  110. linkMethod = fields.Field(api_name='link.method', default='get')
  111. linkTarget = fields.Field(api_name='link.target', default='blank')
  112. linkUri = fields.Field(api_name='link.uri')
  113. type = fields.Field()
  114.  
  115. def get_entity_class(self):
  116. return 'admin/workflow-actions'
  117.  
  118.  
  119.  
  120. #
  121. # examples
  122. #
  123.  
  124. def sample_get_saved_search():
  125. ss = SavedSearch.get('Errors in the last 24 hours', namespace='search')
  126. print 'name: %s' % ss.name
  127. print 'desc: %s' % ss.description
  128. print 'email: %s' % ss.alertSchedule
  129.  
  130. def sample_post_saved_search():
  131. ss = SavedSearch.get('Errors in the last 24 hours', namespace='search')
  132. print 'desc: %s' % ss.description
  133. ss.description = time.time()
  134. print 'desc (changed): %s' % ss.description
  135. ss.post()
  136.  
  137. def sample_bad_validation():
  138. ss = SavedSearch.get('Errors in the last 24 hours', namespace='search')
  139. ss.modifyDate = 'not a date'
  140. ss.post()
  141.  
  142. def sample_workflow():
  143. w = WorkflowAction.get('ifx', namespace='search')
  144. print 'fields: %s' % w.fieldList
  145. print 'label: %s' % w.label
  146. print 'link.method: %s' % w.linkMethod
  147.  
  148. if __name__ == '__main__':
  149. au.getSessionKey('admin','changeme')
  150. sample_workflow()
  151. sample_get_saved_search()
  152. sample_post_saved_search()
  153. sample_bad_validation()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement